Kettle3.2组件加载源代码分析
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Kettl e组件加载
2010年11月
廖佳北京邮电大学计算机学院TSEG jliao422@
1.根据配置文件加载组件信息
1.1.方法入口
1.1.1.Spoon类main()
调用initPlugins()
1.1.
2.Spoon类initPlugins()
//初始化Step加载器
try {
StepLoader.init();
} catch (KettleException e) {
throw new KettleException(Messages.getString
("Spoon.Log.ErrorLoadingAndHaltSystem"), e);
}
//初始化JobEntry加载器
try {
JobEntryLoader.init();
} catch (KettleException e) {
throw new KettleException
("Error loading job entries & plugins... halting Spoon!", e); }
1.1.3.Steps加载
1.1.3.1.StepLoader类init()
StepLoader loader = getInstance(pluginDirectory);
//加载Kettle中steps
loader.readNatives();
loader.readPlugins();
1.1.3.
2.StepLoader类ReadNatives ()
//从BaseStep类steps变量获取每个step信息并加入pluginList(List<StepPlugin>类型)
for (int i = 0; i < BaseStep.steps.length; i++)
{
StepPluginMeta pluginMeta = BaseStep.steps[i];
String id[] = pluginMeta.getId();
String long_desc = pluginMeta.getLongDesc();
String tooltip = pluginMeta.getTooltipDesc();
String iconfile = pluginMeta.getImageFileName();;
String classname = pluginMeta.getClassName().getName();
String directory = null;
String jarfiles[] = null;
String category = pluginMeta.getCategory();
StepPlugin sp = new StepPlugin(StepPlugin.TYPE_NATIVE, id, long_desc, tooltip, directory, jarfiles, iconfile, classname, category, null);
pluginList.add(sp);
}
1.1.3.3.BaseStep类steps变量
public static StepPluginMeta[] steps = null;
//BaseStep类静态块
static {
synchronized (BaseStep.class) {
try {
//读取src/org/pentaho/di/core/config/kettle-config.xml内容
//annotation相关config
ConfigManager<?> stepsAnntCfg = KettleConfig.getInstance() .getManager("steps-annotation-config");
Collection<StepPluginMeta> mainSteps = stepsAnntCfg
.loadAs(StepPluginMeta.class);
//读取src/org/pentaho/di/core/config/kettle-config.xml内容
//steps配置文件地址相关config
ConfigManager<?> stepsCfg = KettleConfig.getInstance()
.getManager("steps-xml-config");
//step规则src/org/pentaho/di/core/config/steps-rules.xml
//读取src/kettle-steps.xml
Collection<StepPluginMeta> csteps = stepsCfg
.loadAs(StepPluginMeta.class);
mainSteps.addAll(csteps);
steps = mainSteps.toArray(
new StepPluginMeta[mainSteps.size()]);
} catch (KettleConfigException e) {
…
}
}
}
1.1.3.4.src/kettle-steps.xml
配置文件指名了每个step的对应的类、分类、描述、tip信息、图标。
<step id="Abort">
<description>
ognl:@org.pentaho.di.trans.step.Messages@getString(
"BaseStep.TypeLongDesc.Abort")
</description>
<class-name>org.pentaho.di.trans.steps.abort.AbortMeta
</class-name>
<category>
ognl:@org.pentaho.di.trans.step.StepCategory@FLOW.getName() </category>
<tooltip>
ognl:@org.pentaho.di.trans.step.Messages@getString(
"BaseStep.TypeTooltipDesc.Abort")
</tooltip>
<image-uri>ui/images/ABR.png</image-uri>
</step>
1.1.3.5.src/org/pentaho/di/trans/step/messages
src/org/pentaho/di/trans/step/messages目录储存各语系对应各step的名字和提示信息。
org.pentaho.di.trans.step.Messages@getString("BaseStep.TypeLongDesc.Abort")对应:BaseStep.TypeLongDesc.Abort=Abort
org.pentaho.di.trans.step.Messages@getString("BaseStep.TypeTooltipDesc.Abor t")对应:BaseStep.TypeTooltipDesc.Abort=Abort a transformation
1.1.4.JobEntries加载。
类比Steps加载。
最终也是将JobEntries读入pluginList(List<JobPlugin>类型)变量。
2.组件界面更新
2.1.界面
用户新建/打开一个Job/Transformation后选择Design(或者Spoon上一次关闭时Job/Transformation编辑区并未关闭),编辑区左边即出现可以使用的组件列表,如下所示:
2.2.代码
org.pentaho.di.ui.spoon包Spoon类refreshCoreObjects(),在用户点击Design 后加载或刷新Job/Transformation组件。
2.2.1.主要变量
1、designSelected:用户当前是否选择了Design图标
2、coreObjectsTree:保存可用组件的树形结构
3、showTrans:当前是否展示Trans可用组件
4、showJob:当前是否展示Job可用组件
5、previousShowTrans:上一次刷新展示的是否为Trans组件
6、previousShowJob:上一次刷新展示的是否为Job组件
2.2.2.核心代码
public void refreshCoreObjects() {
if (shell.isDisposed())
return;
//若选择design则刷新组件树结构
if (!designSelected)
return;
//若组件树结构不存在则初始化,见标题2.3
if (coreObjectsTree == null || coreObjectsTree.isDisposed()) { addCoreObjectsTree();
}
//当前编辑对象,若为Trans流程则返回true
showTrans = getActiveTransformation() != null;
//当前编辑对象,若为Job流程则返回true
showJob = getActiveJob() != null;
//当前编辑对象与上一次更新时一致,则返回,不用更新组件树结构
if (showTrans == previousShowTrans && showJob == previousShowJob) { return;
}
//删除(释放)当前树结构中的组件
TreeItem[] expandItems = coreObjectsTree.getItems();
for (int i = 0; i < expandItems.length; i++) {
TreeItem item = expandItems[i];
item.dispose();
}
//加载Trans组件,见标题3
if (showTrans) {
...
}
//加载Job组件,见标题4
if (showJob) {
}
//更新组件界面
yout(true, true);
//更新历史信息
previousShowTrans = showTrans;
previousShowJob = showJob;
}
2.3.组件树结构初始化
org.pentaho.di.ui.spoon包Spoon类addCoreObjectsTree()中加载鼠标事件监听器(鼠标点击时展开/折叠对应目录、鼠标悬停时显示Tip图标及注释),同时调用addDragSourceToTree()添加鼠标拖拽事件(将组件拖拽至编辑区)的监听器。
addDragSourceToTree()方法调delegates.tree.addDragSourceToTree(),依靠SpoonDelegates中SpoonTreeDelegate对象的addDragSourceToTree()实现拖拽功能。
初始设置还包括,是否展开树结构。
该设置对应org.pentaho.di.ui.core包中default.properties配置文件中的AutoCollapseCoreObjectsTree属性。
组件加载
2.4.Job组件界面更新
2.4.1.主要变量
1、baseJobEntries[]:储存所有JobEntry。
其中红色方框category表示该JobEntry 对应基本类别。
2、baseCategories[]:储存所有Job组件类别。
2.4.2.核心代码
//获取用户本地语言
final String locale = LanguageChoice.getInstance() .getDefaultLocale().toString().toLowerCase();
//获取全部JobPlugin见2.4.3
JobEntryLoader jobEntryLoader = JobEntryLoader.getInstance(); JobPlugin baseJobEntries[] = jobEntryLoader
.getJobEntriesWithType(JobPlugin.TYPE_ALL);
final String baseCategories[] = jobEntryLoader.getCategories( JobPlugin.TYPE_ALL, locale);
//按语系按,对类别排序,然后对组件名称排序
Arrays.sort(baseJobEntries, new Comparator<JobPlugin>() { public int compare(JobPlugin one, JobPlugin two) { ...
}
});
TreeItem generalItem = null;
//设置各类别图标、名称
for (int i = 0; i < baseCategories.length; i++) { TreeItem item = new TreeItem(coreObjectsTree, SWT.NONE);
item.setText(baseCategories[i]);
item.setImage(GUIResource.getInstance().getImageArrow());
...
//设置各组件图标、名称、描述
for (int j = 0; j < baseJobEntries.length; j++) { TreeItem stepItem = new TreeItem(item, SWT.NONE);
stepItem.setImage(jobEntryImage);
stepItem.setText(pluginName);
stepItem.addListener(SWT.Selection, new Listener() { ...
});
...
}
}
...
2.4.
3.JobEntryLoader类getJobEntriesWithType()
最终从pluginList中获取所有JobEntries信息。
public int nrJobEntriesWithType(int type)
{
int nr = 0;
for (int i = 0; i < pluginList.size(); i++)
{
JobPlugin sp = (JobPlugin) pluginList.get(i);
if (sp.getType() == type || type == JobPlugin.TYPE_ALL) nr++;
}
return nr;
}
2.5.Transformation组件界面更新
2.5.1.主要变量
1、basesteps[]:储存所有steps。
2、basecat[]:储存所有trans组件类别。
类比1.4.1。
2.5.2.核心代码
类比1.4.2。