因为实在没有充裕时间,本文的内容只能点到即止。
在ecipse中,一个任务的运行可能有几种方式可供选择,最常用的就是”Java Application”,”Junit”,”Eclipse Application” 等。运用eclipse的插件机制,可以在其中定义自己的任务类型。
以TOS(Talend Open Studio,
http://www.talend.com)中的Job运行为例
<
extension
id ="org.talend.designer.runprocess.debug"
name ="Talend Job Debugger"
point ="org.eclipse.debug.core.launchConfigurationTypes" >
< launchConfigurationType
name ="Talend Job"
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 >
id ="org.talend.designer.runprocess.debug"
name ="Talend Job Debugger"
point ="org.eclipse.debug.core.launchConfigurationTypes" >
< launchConfigurationType
name ="Talend Job"
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用来进行相应数据的持久化。
2 定义
org.eclipse.debug.ui.launchShortcuts
扩展
<
extension
point ="org.eclipse.debug.ui.launchShortcuts" >
< shortcut
category ="org.talend.rcp.perspective"
class ="org.talend.designer.core.debug.JobLaunchShortcut"
icon ="platform:/plugin/org.talend.core/icons/process_icon.gif"
id ="org.talend.designer.runprocess.debug.JobLaunchShortcut"
label ="Job"
modes ="run,debug" >
< contextualLaunch >
< enablement >
< with variable ="selection" >
< count value ="1" />
</ with >
</ enablement >
</ contextualLaunch >
</ shortcut >
</ extension >
point ="org.eclipse.debug.ui.launchShortcuts" >
< shortcut
category ="org.talend.rcp.perspective"
class ="org.talend.designer.core.debug.JobLaunchShortcut"
icon ="platform:/plugin/org.talend.core/icons/process_icon.gif"
id ="org.talend.designer.runprocess.debug.JobLaunchShortcut"
label ="Job"
modes ="run,debug" >
< contextualLaunch >
< enablement >
< with variable ="selection" >
< count value ="1" />
</ with >
</ enablement >
</ contextualLaunch >
</ shortcut >
</ extension >
实现了此扩展,相当于提供了自定义任务运行的入口,在
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
...
{
/** *//**
*
*/
public static final String JOB_NAME = "TALEND_JOB_NAME";
/**//*
* Identifier for job configuration type
*/
public static final String JOB_DEBUG_LAUNCH_CONFIGURATION_TYPE = "org.talend.designer.runprocess.jobLaunchConfiguration";
/** *//**
* Locates a launchable entity in the given selection and launches an application in the specified mode.
*
* @see org.eclipse.debug.ui.ILaunchShortcut#launch(org.eclipse.jface.viewers.ISelection, java.lang.String)
*
* @param selection workbench selection
* @param mode one of the launch modes defined by the launch manager
* @see org.eclipse.debug.core.ILaunchManager
*/
public void launch(ISelection selection, String mode) ...{
if (selection instanceof IStructuredSelection) ...{
Object object = ((IStructuredSelection) selection).getFirstElement();
if (object instanceof RepositoryNode) ...{
RepositoryNode node = (RepositoryNode) object;
launch(node.getObject().getProperty().getItem(), mode);
}
}
}
/** *//**
* Locates a launchable entity in the given active editor, and launches an application in the specified mode.
*
* @see org.eclipse.debug.ui.ILaunchShortcut#launch(org.eclipse.ui.IEditorPart, java.lang.String)
*
* @param editor the active editor in the workbench
* @param mode one of the launch modes defined by the launch manager
* @see org.eclipse.debug.core.ILaunchManager
*/
public void launch(IEditorPart editor, String mode) ...{
if (editor.getSite().getId().equals(MultiPageTalendEditor.ID)) ...{
RepositoryEditorInput input = (RepositoryEditorInput) editor.getEditorInput();
launch(input.getItem(), mode);
}
}
/** *//**
* bqian Comment method "launch".
*
* @param object
* @param mode
*/
private void launch(Item item, String mode) ...{
if (item instanceof ProcessItem) ...{
ILaunchConfiguration config = findLaunchConfiguration((ProcessItem) item, mode);
if (config != null) ...{
DebugUITools.launch(config, mode);
}
}
}
/** *//**
* If re-usable configuration associated with the File and the project exist, this configuration is returned.
* Otherwise a new configuration is created.
*
* @param bin
* @param mode
* @return a re-useable or new config or <code>null</code> if none
*/
private ILaunchConfiguration findLaunchConfiguration(ProcessItem file, String mode) ...{
ILaunchConfiguration configuration = null;
List candidateConfigs = Collections.EMPTY_LIST;
try ...{
ILaunchConfiguration[] configs = DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurations();
candidateConfigs = new ArrayList(configs.length);
for (int i = 0; i < configs.length; i++) ...{
ILaunchConfiguration config = configs[i];
String projectName = config.getAttribute(JOB_NAME, (String) null);
if (projectName == null) ...{
continue;
}
// String programFile = config.getAttribute(PerlLaunchConfigurationConstants.ATTR_STARTUP_FILE, (String)
// null);
// String name = bin.getName();
if (file.getProperty().getLabel().equals(projectName)) ...{
candidateConfigs.add(config);
}
}
} catch (CoreException e) ...{
ExceptionHandler.process(e);
}
int candidateCount = candidateConfigs.size();
if (candidateCount < 1) ...{
configuration = createConfiguration(file);
} else ...{
configuration = (ILaunchConfiguration) candidateConfigs.get(0);
}
return configuration;
}
/** *//**
* Creates a new configuration associated with the given file.
*
* @param file
* @return ILaunchConfiguration
*/
private ILaunchConfiguration createConfiguration(ProcessItem file) ...{
ILaunchConfiguration config = null;
String projectName = file.getProperty().getLabel();
ILaunchConfigurationType type = getLaunchManager().getLaunchConfigurationType(JOB_DEBUG_LAUNCH_CONFIGURATION_TYPE);
try ...{
if (type != null) ...{
ILaunchConfigurationWorkingCopy wc = 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 (CoreException e) ...{
ExceptionHandler.process(e);
}
return config;
}
/** *//**
* Method to get the LaunchManager
*
* @return ILaunchManager
*/
protected ILaunchManager getLaunchManager() ...{
return DebugPlugin.getDefault().getLaunchManager();
}
}
/** *//**
*
*/
public static final String JOB_NAME = "TALEND_JOB_NAME";
/**//*
* Identifier for job configuration type
*/
public static final String JOB_DEBUG_LAUNCH_CONFIGURATION_TYPE = "org.talend.designer.runprocess.jobLaunchConfiguration";
/** *//**
* Locates a launchable entity in the given selection and launches an application in the specified mode.
*
* @see org.eclipse.debug.ui.ILaunchShortcut#launch(org.eclipse.jface.viewers.ISelection, java.lang.String)
*
* @param selection workbench selection
* @param mode one of the launch modes defined by the launch manager
* @see org.eclipse.debug.core.ILaunchManager
*/
public void launch(ISelection selection, String mode) ...{
if (selection instanceof IStructuredSelection) ...{
Object object = ((IStructuredSelection) selection).getFirstElement();
if (object instanceof RepositoryNode) ...{
RepositoryNode node = (RepositoryNode) object;
launch(node.getObject().getProperty().getItem(), mode);
}
}
}
/** *//**
* Locates a launchable entity in the given active editor, and launches an application in the specified mode.
*
* @see org.eclipse.debug.ui.ILaunchShortcut#launch(org.eclipse.ui.IEditorPart, java.lang.String)
*
* @param editor the active editor in the workbench
* @param mode one of the launch modes defined by the launch manager
* @see org.eclipse.debug.core.ILaunchManager
*/
public void launch(IEditorPart editor, String mode) ...{
if (editor.getSite().getId().equals(MultiPageTalendEditor.ID)) ...{
RepositoryEditorInput input = (RepositoryEditorInput) editor.getEditorInput();
launch(input.getItem(), mode);
}
}
/** *//**
* bqian Comment method "launch".
*
* @param object
* @param mode
*/
private void launch(Item item, String mode) ...{
if (item instanceof ProcessItem) ...{
ILaunchConfiguration config = findLaunchConfiguration((ProcessItem) item, mode);
if (config != null) ...{
DebugUITools.launch(config, mode);
}
}
}
/** *//**
* If re-usable configuration associated with the File and the project exist, this configuration is returned.
* Otherwise a new configuration is created.
*
* @param bin
* @param mode
* @return a re-useable or new config or <code>null</code> if none
*/
private ILaunchConfiguration findLaunchConfiguration(ProcessItem file, String mode) ...{
ILaunchConfiguration configuration = null;
List candidateConfigs = Collections.EMPTY_LIST;
try ...{
ILaunchConfiguration[] configs = DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurations();
candidateConfigs = new ArrayList(configs.length);
for (int i = 0; i < configs.length; i++) ...{
ILaunchConfiguration config = configs[i];
String projectName = config.getAttribute(JOB_NAME, (String) null);
if (projectName == null) ...{
continue;
}
// String programFile = config.getAttribute(PerlLaunchConfigurationConstants.ATTR_STARTUP_FILE, (String)
// null);
// String name = bin.getName();
if (file.getProperty().getLabel().equals(projectName)) ...{
candidateConfigs.add(config);
}
}
} catch (CoreException e) ...{
ExceptionHandler.process(e);
}
int candidateCount = candidateConfigs.size();
if (candidateCount < 1) ...{
configuration = createConfiguration(file);
} else ...{
configuration = (ILaunchConfiguration) candidateConfigs.get(0);
}
return configuration;
}
/** *//**
* Creates a new configuration associated with the given file.
*
* @param file
* @return ILaunchConfiguration
*/
private ILaunchConfiguration createConfiguration(ProcessItem file) ...{
ILaunchConfiguration config = null;
String projectName = file.getProperty().getLabel();
ILaunchConfigurationType type = getLaunchManager().getLaunchConfigurationType(JOB_DEBUG_LAUNCH_CONFIGURATION_TYPE);
try ...{
if (type != null) ...{
ILaunchConfigurationWorkingCopy wc = 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 (CoreException e) ...{
ExceptionHandler.process(e);
}
return config;
}
/** *//**
* Method to get the LaunchManager
*
* @return ILaunchManager
*/
protected ILaunchManager getLaunchManager() ...{
return DebugPlugin.getDefault().getLaunchManager();
}
}
本例中的程序位于talend项目中的"org.talend.designer.core"插件中。使用svn链接
P.S. 该功能的eclipse源码在插件"org.eclipse.debug.ui" 中,可以参考的类:
JavaLaunchShortcut, AbstractLaunchToolbarAction。
http://talendforge.org/svn/tos