⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 javasrcmodeldigester.java

📁 一个java写的business process management系统
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
		CodeLine throwsLine = new CodeLine("throw new Flow4JRuntimeException(\"" +
				"start node \\\"\" + startFlowletName + \"\\\" not found in flow \\\"\" + this.getClass().getName() + \"\\\"" +
				"\");");

		((JavaSrcVisitor)visitor).getCodeBlock().addFragment(throwsLine);
	}


	/**
	 * Serializes the class's java source code to the given writer.
	 * @param writer the writer were the sources of the java file should 
	 * be written
	 */
	public void serialize(BufferedWriter writer) throws IOException {
		javaFile.serialize(writer);
	}

	/**
	 * generates the "setProperty" calls
	 * @param flowletBind	the property holder binding object
	 * @param codeBlock the code block where the statement should be palced
	 * @param fieldName the name of the field whose setProperty method is generated
	 */
	static private void generatePropertySetterCalls(IPropertyHolderBind flowletBind, CodeBlock codeBlock, String fieldName) {
		List properties = flowletBind.getProperties();
		if (properties == null || properties.isEmpty())
			return;
			
		for (Iterator iter = properties.iterator(); iter.hasNext();) {
			PropertyBind propBind = (PropertyBind) iter.next();
			codeBlock.addFragment(new CodeLine(fieldName + ".setProperty(\""
							+ propBind.getName() + "\", \""
							+ propBind.getValue() + "\");"));
		}
	}
	

	/**
	 * Generates the class's javadoc block
	 *
	 */
	private void generateClassJavadoc() {
		JavadocBlock.XDocletTag xdTag;
		JavadocBlock javadoc = javaFile.getJavaClass().getJavadoc();
		javadoc.addTag(xdTag = javadoc.new XDocletTag(FLOW4J_XDOCLET_PREFIX+"flow"));
		xdTag.addParam("name", flowModelBind.getFlowName());
	}





















	/**
	 * This visitor is passed around between the flow nodes.
	 * Flowlets individually adjust the code  block where
	 * the following flowlet should put it's code.
	 */
	public class JavaSrcVisitor implements IFlowVisitor {
		CodeBlock codeBlock;
		
		JavaSrcVisitor(CodeBlock codeBlock) {
			this.codeBlock = codeBlock;
		}

		public CodeBlock getCodeBlock() {
			return codeBlock;
		}

		public void setCodeBlock(CodeBlock block) {
			codeBlock = block;
		}

	}









	private class StartFlowletNode extends FlowStructureModelDigester.StartFlowletNode {
		
		StartFlowletNode(IFlowletBind flowletBind, FlowModelBind flowModelBind) {
			super(flowletBind, flowModelBind);
		}

		public void visit(IFlowVisitor visitor) {
			if (isVisited())
				return;
			StartFlowletBind flowletBind = (StartFlowletBind)getFlowletBind();
			String labelText = flowletBind.getLabel().getText();
			String methodName = flowletBind.getId(getFlowModelBind()) + "_" + labelText;

			//	method for startFlowlet
			Method method = new Method(methodName);
			method.addModifier(Consts.MODIFIER_STATIC);
			method.addModifier(Consts.MODIFIER_PRIVATE);
			method.addModifier(Consts.MODIFIER_FINAL);
			method.addMethodParameter(new MethodParameter("FlowDictionary", "dictionary"));
			javaFile.getJavaClass().addMethod(method);

			createJavadoc(flowletBind);
			
			setVisited(true);
			((JavaSrcVisitor)visitor).setCodeBlock(method.getCodeBlock());
			super.visit(visitor);
		}
		
		private void createJavadoc(StartFlowletBind flowletBind) {
			if (flowletBind.getProperties() == null)
				return;
			JavadocBlock javadoc = javaFile.getJavaClass().getJavadoc();
			JavadocBlock.XDocletTag xdTag = javadoc.new XDocletTag(FLOW4J_XDOCLET_PREFIX + "start-flowlet");
			javadoc.addTag(xdTag);
			String propNames = "";
			for (Iterator iter = flowletBind.getProperties().iterator(); iter.hasNext();) {
				PropertyBind propBind = (PropertyBind) iter.next();
				xdTag.addParam("value-" + propBind.getName(), propBind.getValue());
				propNames += propBind.getName();
				if (iter.hasNext())
					propNames += ",";
			}
			xdTag.addParam("propertyNames", propNames);
		}
	}




	private class JavaTaskFlowletNode extends FlowStructureModelDigester.JavaTaskFlowletNode {
		public JavaTaskFlowletNode(IFlowletBind flowletBind, FlowModelBind flowModelBind) {
			super(flowletBind, flowModelBind);
		}

		public void visit(IFlowVisitor visitor) {
			if (isVisited())
				return;
			JavaTaskFlowletBind flowletBind = (JavaTaskFlowletBind)getFlowletBind();
			String className = flowletBind.getLabel().getText();
			int taskIndex = getFlowModelBind().getJavaTaskFlowlets().indexOf(flowletBind);
			int lastDotPos = className.lastIndexOf(".");
			String taskName = lastDotPos == -1 ?  className : className.substring(lastDotPos+1);

			ITaskFlowlet taskFlowlet = null;
			try {
				Class taskClass =
					taskClassLoader == null
						? Class.forName(className)
						: Class.forName(className, true, taskClassLoader);

				taskFlowlet =
					(ITaskFlowlet) taskClass.newInstance();
			} catch (ClassNotFoundException e) {
				// ignore
			} catch (Exception e) {
				e.printStackTrace();
			}

			String fieldName = "javatask" + taskIndex + "_" + taskName;
			if (! javaFile.getJavaClass().hasField(fieldName)) {
				//	add field for task singleton
				Field taskSingletonField = new Field(fieldName);
				taskSingletonField.addModifier(Consts.MODIFIER_STATIC);
				taskSingletonField.addModifier(Consts.MODIFIER_PRIVATE);
				taskSingletonField.setType("ITaskFlowlet");
				javaFile.getJavaClass().addField(taskSingletonField);
				//	add line to static initializer
				CodeBlock staticInitializer = javaFile.getJavaClass().getStaticInitializer();
				staticInitializer.addFragment(new CodeLine(
					fieldName + " = FlowManager.registerTaskFlowlet(" + className + ".class);"));
				//	set the tasks properties
				if (taskFlowlet != null) {
					generatePropertySetterCalls(flowletBind, staticInitializer, fieldName);
				}
			}

			((JavaSrcVisitor)visitor).getCodeBlock().addFragment(new CodeLine(fieldName + ".execute(dictionary);"));
			
			createJavadoc(flowletBind, className, taskFlowlet);
			setVisited(true);
			super.visit(visitor);
		}
		
		
		
		
		private void createJavadoc(JavaTaskFlowletBind flowletBind, String className, ITaskFlowlet taskFlowlet) {
			JavadocBlock javadoc = javaFile.getJavaClass().getJavadoc();
			JavadocBlock.XDocletTag xdTag = javadoc.new XDocletTag(FLOW4J_XDOCLET_PREFIX + "task-flowlet");
			javadoc.addTag(xdTag);
			xdTag.addParam("class", className);

			if (taskFlowlet == null) {
				xdTag.addParam("classnotfound", "true");
				return;
			}
			
			try {
				//	description
				if (taskFlowlet.getDescription() != null)
					xdTag.addParam("description", taskFlowlet.getDescription());
				//	properties
				TaskPropertyDescriptors descs = taskFlowlet.getPropertyDescriptors();
				Map taskProperties = new HashMap();
				if (descs != null && !descs.isEmpty()) {
					String names = "";
					for (Iterator iter = descs.iterator(); iter.hasNext();) {
						TaskPropertyDescriptor desc = (TaskPropertyDescriptor)iter.next();
						String value = flowletBind.getProperty(desc.getName());
						taskProperties.put(desc.getName(), value);
						if (value != null)
							xdTag.addParam("value-" + desc.getName(), value);
						if (desc.getDescription() != null)
							xdTag.addParam("desc-" + desc.getName(), desc.getDescription());
						names += desc.getName();
						if (iter.hasNext())
							names += ",";
					}
					xdTag.addParam("propertyNames", names);
				}
				//	set the parameterized task name
				if (taskFlowlet.getName() != null) {
					//String labelText = taskFlowlet.getName() != null ? taskFlowlet.getName() : className;
					JavadocBlock.XDocletTag.Parameter  nameParam = xdTag.addParam("name", taskFlowlet.getName());
					nameParam.setValue(Util.replaceProperties(nameParam.getValue(), taskProperties));
				}
				
				//	input Parameters
				TaskParameterDescriptors inDescs = taskFlowlet.getInputParameterDescriptors();
				if (inDescs != null && !inDescs.isEmpty()) {
					String names = "";
					for (Iterator iter = inDescs.iterator(); iter.hasNext();) {
						TaskParameterDescriptor desc = (TaskParameterDescriptor)iter.next();
						if (desc.getType() != null)
							xdTag.addParam("type-" + desc.getName(), desc.getType());
						if (desc.getDescription() != null)
							xdTag.addParam("desc-" + desc.getName(), desc.getDescription());
						names += desc.getName();
						if (iter.hasNext())
							names += ",";
					}
					xdTag.addParam("inputParameterNames", names);
				}
				//	output Parameters
				TaskParameterDescriptors outDescs = taskFlowlet.getOutputParameterDescriptors();
				if (outDescs != null && !outDescs.isEmpty()) {
					String names = "";
					for (Iterator iter = outDescs.iterator(); iter.hasNext();) {
						TaskParameterDescriptor desc = (TaskParameterDescriptor)iter.next();
						if (desc.getType() != null)
							xdTag.addParam("type-" + desc.getName(), desc.getType());
						if (desc.getDescription() != null)
							xdTag.addParam("desc-" + desc.getName(), desc.getDescription());
						names += desc.getName();
						if (iter.hasNext())
							names += ",";
					}
					xdTag.addParam("outputParameterNames", names);
				}
			} catch (IllegalStateException e) {
				e.printStackTrace();
			}
		}
	}





	
	private class ScriptTaskFlowletNode extends FlowStructureModelDigester.ScriptTaskFlowletNode {
		public ScriptTaskFlowletNode(IFlowletBind flowletBind, FlowModelBind flowModelBind) {
			super(flowletBind, flowModelBind);
		}
	
		public void visit(IFlowVisitor visitor) {
			if (isVisited())
				return;

			ScriptTaskFlowletBind flowletBind = (ScriptTaskFlowletBind)getFlowletBind();
			String scriptRelPath = flowletBind.getLabel().getText();
			String extension = scriptRelPath.substring(scriptRelPath.lastIndexOf('.')+1);

			bsfNeeded = true;
			BSFEngineDescriptor bsfEngineDesc = Flow4JBSFManager.getInstance().getEngineDescriptorByExtension(extension);
			
			String scriptTextFieldName = flowletBind.getId(getFlowModelBind()) + "_text";
			addStaticScriptTextField(scriptTextFieldName, scriptRelPath);
			String methodName = flowletBind.getId(getFlowModelBind()) + scriptRelPath.replace('/', '_').replace(' ', '_').replace('.', '$');
			Method method = new Method(methodName);
			method.addModifier(Consts.MODIFIER_STATIC);
			method.addModifier(Consts.MODIFIER_PRIVATE);
			method.addModifier(Consts.MODIFIER_FINAL);
			method.addMethodParameter(new MethodParameter("FlowDictionary", "dictionary"));
			method.addThrowsExceptionType("Flow4JRuntimeException");
			javaFile.getJavaClass().addMethod(method);
			
			generateBSFEngineCode(method.getCodeBlock(), bsfEngineDesc);
			TryCatch tryCatchBlock = new TryCatch();
			method.getCodeBlock().addFragment(tryCatchBlock);
			tryCatchBlock.getTryBlock().addFragment(new CodeLine("bsfManager.declareBean(\"dictionary\", dictionary, FlowDictionary.class);"));
			String scriptContent = "scriptText";
			StringBuffer execBuf = new StringBuffer();
			execBuf.append("bsfManager.exec(\"")
			.append(bsfEngineDesc.getLanguage()).append("\", ")
			.append("\"").append(scriptRelPath).append("\", ").append("0, 0, ")
			.append(scriptTextFieldName).append(");");
			tryCatchBlock.getTryBlock().addFragment(new CodeLine(execBuf.toString()));
			
			CatchBlock catchBlock = new CatchBlock("org.apache.bsf.BSFException", "e");
			catchBlock.getCatchBody().addFragment(new CodeLine("throw new Flow4JRuntimeException(\"Error while script execution (" + scriptRelPath + ")\", e);"));
			tryCatchBlock.addCatchBlock(catchBlock);
			
			((JavaSrcVisitor)visitor).getCodeBlock().addFragment(new CodeLine(methodName + "(dictionary);"));

			setVisited(true);
			super.visit(visitor);
		}
		
		
		private void addStaticScriptTextField(String fieldName, String scriptRelPath) throws Flow4JException {
			Field field = new Field(fieldName);
			field.addModifier(Consts.MODIFIER_STATIC);
			field.addModifier(Consts.MODIFIER_PRIVATE);
			field.setType("String");
			javaFile.getJavaClass().addField(field);
			javaFile.getJavaClass().getStaticInitializer().addFragment(
				new CodeLine(fieldName + " = net.orthanc.flow4j.base.IOUtils.getResourceAsString(" + javaFile.getJavaClass().getClassName() + ".class.getClassLoader(), \"" + scriptRelPath + "\");"));

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -