因为实在没有充裕时间,本文的内容只能点到即止。
在ecipse中,一个任务的运行可能有几种方式可供选择,最常用的就是”Java Application”,”Junit”,”Eclipse Application” 等。运用eclipse的插件机制,可以在其中定义自己的任务类型。
<
extension
id
="org.talend.designer.runprocess.debug"
name
="TalendJobDebugger"
point
="org.eclipse.debug.core.launchConfigurationTypes"
>
<
launchConfigurationType
name
="TalendJob"
delegate
="org.talend.designer.core.debug.JobLaunchConfigurationDelegate"
modes
="run"
public
="true"
id
="org.talend.designer.runprocess.jobLaunchConfiguration"
>
</
launchConfigurationType
>
</
extension
>
<
extension
point
="org.eclipse.debug.ui.launchConfigurationTypeImages"
>
<
launchConfigurationTypeImage
icon
="icons/process_icon.gif"
configTypeID
="org.talend.designer.runprocess.jobLaunchConfiguration"
id
="org.talend.designer.runprocess.launchImage"
>
</
launchConfigurationTypeImage
>
</
extension
>
org.eclipse.debug.core.launchConfigurationTypes
用来使自定义的任务类型集成进
eclipse
。
"org.eclipse.debug.ui.launchConfigurationTypeImages
定义了显示的图标。
类
org.talend.designer.core.debug.JobLaunchConfigurationDelegate
实现了接口
IlaunchConfigurationDelegate,
方法
public
void
launch(ILaunchConfiguration
configuration
, String
mode
, ILaunch
launch
, IProgressMonitor
monitor
)
throws
CoreException
定义了要执行的动作,相应的持久化数据在
IlaunchConfiguration
中取得。
ILaunchConfiguration用来进行相应数据的持久化。
实现了此扩展,相当于提供了自定义任务运行的入口,在
eclipse toolbar
的“
Run As”
和“
Debug As”
上就会出现该定义的选项。
org.talend.designer.core.debug.JobLaunchShortcut 实现了接口 IlaunchShorcut, 在2个launch方法实现中定义如何运行任务,类的实现如下:
DebugUITools.launch(config,mode); 中会调用eclipse的添加最近执行列表功能,以及执行JobLaunchConfigurationDelegate的launch()方法。
public
class
JobLaunchShortcut
implements
ILaunchShortcut
...
{
/***//**
*
*/
publicstaticfinalStringJOB_NAME="TALEND_JOB_NAME";
/**//*
*Identifierforjobconfigurationtype
*/
publicstaticfinalStringJOB_DEBUG_LAUNCH_CONFIGURATION_TYPE="org.talend.designer.runprocess.jobLaunchConfiguration";
/***//**
*Locatesalaunchableentityinthegivenselectionandlaunchesanapplicationinthespecifiedmode.
*
*@seeorg.eclipse.debug.ui.ILaunchShortcut#launch(org.eclipse.jface.viewers.ISelection,java.lang.String)
*
*@paramselectionworkbenchselection
*@parammodeoneofthelaunchmodesdefinedbythelaunchmanager
*@seeorg.eclipse.debug.core.ILaunchManager
*/
publicvoidlaunch(ISelectionselection,Stringmode)...{
if(selectioninstanceofIStructuredSelection)...{
Objectobject=((IStructuredSelection)selection).getFirstElement();
if(objectinstanceofRepositoryNode)...{
RepositoryNodenode=(RepositoryNode)object;
launch(node.getObject().getProperty().getItem(),mode);
}
}
}
/***//**
*Locatesalaunchableentityinthegivenactiveeditor,andlaunchesanapplicationinthespecifiedmode.
*
*@seeorg.eclipse.debug.ui.ILaunchShortcut#launch(org.eclipse.ui.IEditorPart,java.lang.String)
*
*@parameditortheactiveeditorintheworkbench
*@parammodeoneofthelaunchmodesdefinedbythelaunchmanager
*@seeorg.eclipse.debug.core.ILaunchManager
*/
publicvoidlaunch(IEditorParteditor,Stringmode)...{
if(editor.getSite().getId().equals(MultiPageTalendEditor.ID))...{
RepositoryEditorInputinput=(RepositoryEditorInput)editor.getEditorInput();
launch(input.getItem(),mode);
}
}
/***//**
*bqianCommentmethod"launch".
*
*@paramobject
*@parammode
*/
privatevoidlaunch(Itemitem,Stringmode)...{
if(iteminstanceofProcessItem)...{
ILaunchConfigurationconfig=findLaunchConfiguration((ProcessItem)item,mode);
if(config!=null)...{
DebugUITools.launch(config,mode);
}
}
}
/***//**
*Ifre-usableconfigurationassociatedwiththeFileandtheprojectexist,thisconfigurationisreturned.
*Otherwiseanewconfigurationiscreated.
*
*@parambin
*@parammode
*@returnare-useableornewconfigor<code>null</code>ifnone
*/
privateILaunchConfigurationfindLaunchConfiguration(ProcessItemfile,Stringmode)...{
ILaunchConfigurationconfiguration=null;
ListcandidateConfigs=Collections.EMPTY_LIST;
try...{
ILaunchConfiguration[]configs=DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurations();
candidateConfigs=newArrayList(configs.length);
for(inti=0;i<configs.length;i++)...{
ILaunchConfigurationconfig=configs[i];
StringprojectName=config.getAttribute(JOB_NAME,(String)null);
if(projectName==null)...{
continue;
}
//StringprogramFile=config.getAttribute(PerlLaunchConfigurationConstants.ATTR_STARTUP_FILE,(String)
//null);
//Stringname=bin.getName();
if(file.getProperty().getLabel().equals(projectName))...{
candidateConfigs.add(config);
}
}
}catch(CoreExceptione)...{
ExceptionHandler.process(e);
}
intcandidateCount=candidateConfigs.size();
if(candidateCount<1)...{
configuration=createConfiguration(file);
}else...{
configuration=(ILaunchConfiguration)candidateConfigs.get(0);
}
returnconfiguration;
}
/***//**
*Createsanewconfigurationassociatedwiththegivenfile.
*
*@paramfile
*@returnILaunchConfiguration
*/
privateILaunchConfigurationcreateConfiguration(ProcessItemfile)...{
ILaunchConfigurationconfig=null;
StringprojectName=file.getProperty().getLabel();
ILaunchConfigurationTypetype=getLaunchManager().getLaunchConfigurationType(JOB_DEBUG_LAUNCH_CONFIGURATION_TYPE);
try...{
if(type!=null)...{
ILaunchConfigurationWorkingCopywc=type.newInstance(null,getLaunchManager()
.generateUniqueLaunchConfigurationNameFrom(projectName));
wc.setAttribute(JOB_NAME,projectName);
//wc.setAttribute(PerlLaunchConfigurationConstants.ATTR_PROJECT_NAME,file.getProject().getName());
//wc.setAttribute(PerlLaunchConfigurationConstants.ATTR_WORKING_DIRECTORY,(String)null);
config=wc.doSave();
}
}catch(CoreExceptione)...{
ExceptionHandler.process(e);
}
returnconfig;
}
/***//**
*MethodtogettheLaunchManager
*
*@returnILaunchManager
*/
protectedILaunchManagergetLaunchManager()...{
returnDebugPlugin.getDefault().getLaunchManager();
}
}
本例中的程序位于talend项目中的"org.talend.designer.core"插件中。使用svn链接
P.S. 该功能的eclipse源码在插件"org.eclipse.debug.ui" 中,可以参考的类:
JavaLaunchShortcut, AbstractLaunchToolbarAction。
http://talendforge.org/svn/tos