public void executeAsynch()

in taverna-beanshell-activity/src/main/java/org/apache/taverna/activities/beanshell/BeanshellActivity.java [125:232]


	public void executeAsynch(final Map<String, T2Reference> data,
			final AsynchronousActivityCallback callback) {
		callback.requestRun(new Runnable() {

			public void run() {

				// Workflow run identifier (needed when classloader sharing is
				// set to 'workflow').
				String procID = callback.getParentProcessIdentifier();
				String workflowRunID;
				if (procID.contains(":")) {
					workflowRunID = procID.substring(0, procID.indexOf(':'));
				} else {
					workflowRunID = procID; // for tests, will be an empty
											// string
				}

				synchronized (interpreter) {

					// Configure the classloader for executing the Beanshell
					if (classLoader == null) {
						try {
							classLoader = findClassLoader(json, workflowRunID);
							interpreter.setClassLoader(classLoader);
						} catch (RuntimeException rex) {
							String message = "Unable to obtain the classloader for Beanshell service";
							callback.fail(message, rex);
							return;
						}
					}

					ReferenceService referenceService = callback.getContext()
							.getReferenceService();

					Map<String, T2Reference> outputData = new HashMap<String, T2Reference>();

					clearInterpreter();
					try {
						// set inputs
						for (String inputName : data.keySet()) {
							ActivityInputPort inputPort = getInputPort(inputName);
							Object input = referenceService.renderIdentifier(
									data.get(inputName),
									inputPort.getTranslatedElementClass(),
									callback.getContext());
							inputName = sanatisePortName(inputName);
							interpreter.set(inputName, input);
						}
						// run
						interpreter.eval(json.get("script").asText());
						// get outputs
						for (OutputPort outputPort : getOutputPorts()) {
							String name = outputPort.getName();
							Object value = interpreter.get(name);
							if (value == null) {
								ErrorDocumentService errorDocService = referenceService
										.getErrorDocumentService();
								value = errorDocService.registerError(
										"No value produced for output variable "
												+ name, outputPort.getDepth(),
										callback.getContext());
							}
							outputData.put(name, referenceService.register(
									value, outputPort.getDepth(), true,
									callback.getContext()));
						}
						callback.receiveResult(outputData, new int[0]);
					} catch (EvalError e) {
						logger.error(e);
						try {
							int lineNumber = e.getErrorLineNumber();

							callback.fail("Line " + lineNumber + ": "
									+ determineMessage(e));
						} catch (NullPointerException e2) {
							callback.fail(determineMessage(e));
						}
					} catch (ReferenceServiceException e) {
						callback.fail(
								"Error accessing beanshell input/output data for "
										+ this, e);
					}
					clearInterpreter();
				}
			}

			/**
			 * Removes any invalid characters from the port name. For example,
			 * xml-text would become xmltext.
			 * 
			 * @param name
			 * @return
			 */
			private String sanatisePortName(String name) {
				String result = name;
				if (Pattern.matches("\\w++", name) == false) {
					result = "";
					for (char c : name.toCharArray()) {
						if (Character.isLetterOrDigit(c) || c == '_') {
							result += c;
						}
					}
				}
				return result;
			}
		});

	}