You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@uima.apache.org by cw...@apache.org on 2015/01/13 15:19:41 UTC
svn commit: r1651354 - in
/uima/sandbox/uima-ducc/trunk/uima-ducc-common/src/main/java/org/apache/uima/ducc/common/main:
DuccJobService.java DuccService.java
Author: cwiklik
Date: Tue Jan 13 14:19:41 2015
New Revision: 1651354
URL: http://svn.apache.org/r1651354
Log:
UIMA-4066 modified to support DUCC isolation from user code
Added:
uima/sandbox/uima-ducc/trunk/uima-ducc-common/src/main/java/org/apache/uima/ducc/common/main/DuccJobService.java (with props)
Modified:
uima/sandbox/uima-ducc/trunk/uima-ducc-common/src/main/java/org/apache/uima/ducc/common/main/DuccService.java
Added: uima/sandbox/uima-ducc/trunk/uima-ducc-common/src/main/java/org/apache/uima/ducc/common/main/DuccJobService.java
URL: http://svn.apache.org/viewvc/uima/sandbox/uima-ducc/trunk/uima-ducc-common/src/main/java/org/apache/uima/ducc/common/main/DuccJobService.java?rev=1651354&view=auto
==============================================================================
--- uima/sandbox/uima-ducc/trunk/uima-ducc-common/src/main/java/org/apache/uima/ducc/common/main/DuccJobService.java (added)
+++ uima/sandbox/uima-ducc/trunk/uima-ducc-common/src/main/java/org/apache/uima/ducc/common/main/DuccJobService.java Tue Jan 13 14:19:41 2015
@@ -0,0 +1,144 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.uima.ducc.common.main;
+
+import java.io.File;
+import java.lang.reflect.Method;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.util.ArrayList;
+
+import org.apache.uima.ducc.user.jp.iface.IProcessContainer;
+
+/**
+ * Main program that is used to launch Job Process(JP).
+ *
+ */
+public class DuccJobService {
+ boolean DEBUG = false;
+ public static URLClassLoader create(String classPath)
+ throws MalformedURLException {
+ return create(classPath.split(":"));
+ }
+
+ public static URLClassLoader create(String[] classPathElements)
+ throws MalformedURLException {
+ ArrayList<URL> urlList = new ArrayList<URL>(classPathElements.length);
+ for (String element : classPathElements) {
+ if (element.endsWith("*")) {
+ File dir = new File(element.substring(0, element.length() - 1));
+ File[] files = dir.listFiles(); // Will be null if missing or
+ // not a dir
+ if (files != null) {
+ for (File f : files) {
+ if (f.getName().endsWith(".jar")) {
+ urlList.add(f.toURI().toURL());
+ }
+ }
+ }
+ } else {
+ File f = new File(element);
+ if (f.exists()) {
+ urlList.add(f.toURI().toURL());
+ }
+ }
+ }
+ URL[] urls = new URL[urlList.size()];
+ return new URLClassLoader(urlList.toArray(urls), ClassLoader
+ .getSystemClassLoader().getParent());
+ }
+
+ /*
+ * Dump all the URLs
+ */
+ public static void dump(ClassLoader cl, int numLevels) {
+ int n = 0;
+ for (URLClassLoader ucl = (URLClassLoader) cl; ucl != null
+ && ++n <= numLevels; ucl = (URLClassLoader) ucl.getParent()) {
+ System.out.println("Class-loader " + n + " has "
+ + ucl.getURLs().length + " urls:");
+ for (URL u : ucl.getURLs()) {
+ System.out.println(" " + u);
+ }
+ }
+ }
+
+ public void start(String[] args) throws Exception {
+ // cache current context classloader
+ ClassLoader sysCL = Thread.currentThread().getContextClassLoader();
+ // Fetch a classpath for the fenced Ducc container
+ String duccContainerClasspath = System
+ .getProperty("ducc.deploy.container.classpath");
+ URLClassLoader ucl = create(duccContainerClasspath);
+ if ( System.getProperty("DEBUG") != null ) {
+ DEBUG = true;
+ }
+ if (DEBUG) {
+ dump(ucl, 4);
+ }
+
+ Thread.currentThread().setContextClassLoader(ucl);
+ // Create DuccService instance
+ Class<?> classToLaunch = ucl.loadClass("org.apache.uima.ducc.common.main.DuccService");
+ Object duccContainerInstance = classToLaunch.newInstance();
+
+ // initialize Ducc fenced container. It calls component's Configuration class
+ Method bootMethod = classToLaunch.getMethod("boot", String[].class);
+ bootMethod.invoke(duccContainerInstance, (Object) args);
+
+ // below property is set by component's Configuration class. It can also
+ // be provided on the command line in case a custom processor is needed.
+ String processorClass = System
+ .getProperty("ducc.deploy.processor.class");
+
+ // Instantiate process container where the actual analysis will be done.
+ // Currently there are two containers:
+ // 1 - UimaProcessContainer - used for pieces parts (UIMA only)
+ // 2 - UimaASProcessContainer - used for DD jobs
+ //
+ // NOTE: the container class is loaded by the main System classloader
+ // and requires uima-ducc-user jar to be in the System classpath.
+ // --------------------------------------------------------------------
+ // load the process container class using the initial system class loader
+ Class<?> processorClz = sysCL.loadClass(processorClass);
+ IProcessContainer pc = (IProcessContainer) processorClz.newInstance();
+
+ // Call DuccService.setProcessor() to hand-off instance of the
+ // process container to the component along with this process args
+ Method setProcessorMethod = classToLaunch.getMethod("setProcessor",
+ Object.class, String[].class);
+ setProcessorMethod.invoke(duccContainerInstance, pc, args);
+ // Call DuccService.start() to initialize worker threads and to
+ // start fetching Work Items from a JD for processing.
+ Method startMethod = classToLaunch.getMethod("start");// ,
+ startMethod.invoke(duccContainerInstance);
+
+ }
+
+ public static void main(String[] args) {
+ try {
+ DuccJobService service = new DuccJobService();
+ service.start(args);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+}
Propchange: uima/sandbox/uima-ducc/trunk/uima-ducc-common/src/main/java/org/apache/uima/ducc/common/main/DuccJobService.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: uima/sandbox/uima-ducc/trunk/uima-ducc-common/src/main/java/org/apache/uima/ducc/common/main/DuccService.java
URL: http://svn.apache.org/viewvc/uima/sandbox/uima-ducc/trunk/uima-ducc-common/src/main/java/org/apache/uima/ducc/common/main/DuccService.java?rev=1651354&r1=1651353&r2=1651354&view=diff
==============================================================================
--- uima/sandbox/uima-ducc/trunk/uima-ducc-common/src/main/java/org/apache/uima/ducc/common/main/DuccService.java (original)
+++ uima/sandbox/uima-ducc/trunk/uima-ducc-common/src/main/java/org/apache/uima/ducc/common/main/DuccService.java Tue Jan 13 14:19:41 2015
@@ -30,6 +30,7 @@ import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.uima.ducc.common.NodeIdentity;
import org.apache.uima.ducc.common.component.AbstractDuccComponent;
+import org.apache.uima.ducc.common.component.IJobProcessor;
import org.apache.uima.ducc.common.exception.DuccComponentInitializationException;
import org.apache.uima.ducc.common.utils.DuccLogger;
import org.apache.uima.ducc.common.utils.Utils;
@@ -59,6 +60,7 @@ public class DuccService extends Abstrac
private static DuccLogger globalLogger = null;
private ApplicationContext context;
Map<String,AbstractDuccComponent> duccComponents = null;
+ private String[] args = null;
public DuccService() {
super("");
@@ -154,24 +156,28 @@ public class DuccService extends Abstrac
context =
new AnnotationConfigApplicationContext(configClasses);
// Extract all Ducc components from Spring container
-// Map<String,AbstractDuccComponent> duccComponents =
duccComponents =
context.getBeansOfType(AbstractDuccComponent.class);
- // Start all components
+ // Start all components except for JP component
for(Map.Entry<String, AbstractDuccComponent> duccComponent: duccComponents.entrySet()) {
- getDuccLogger().info(methodName, null, "... Starting Component: ", duccComponent.getKey());
- if ( args != null && args.length > 0 ) {
- duccComponent.getValue().start(this,args);
- } else {
- duccComponent.getValue().start(this);
+ String key = duccComponent.getKey();
+ // The job-process is what used to be uima-as to identify the type of component
+ // The new job-process is the JP component. JP components are started differently
+ // from the rest of Ducc daemons.
+ if ( !(duccComponent.getValue() instanceof IJobProcessor) ) {
+ getDuccLogger().info(methodName, null, "... Starting Component: ", duccComponent.getKey());
+ if ( args != null && args.length > 0 ) {
+ duccComponent.getValue().start(this,args);
+ } else {
+ duccComponent.getValue().start(this);
+ }
+ getDuccLogger().info(methodName, null, "... Component started: ", duccComponent.getKey());
}
- getDuccLogger().info(methodName, null, "... Component started: ", duccComponent.getKey());
}
System.out.println("Starting Camel. Use ctrl + c to terminate the JVM.\n");
- // run until you terminate the JVM
- getDuccLogger().info(methodName, null, "Starting Camel. Use ctrl + c to terminate the JVM.\n");
- main.start();
-
+ // run until you terminate the JVM
+ getDuccLogger().info(methodName, null, "Starting Camel. Use ctrl + c to terminate the JVM.\n");
+ main.start();
}
public AbstractDuccComponent getComponentInstance(String componentKey) {
// Extract all Ducc components from Spring container
@@ -184,6 +190,51 @@ public class DuccService extends Abstrac
}
return null;
}
+ /**
+ * This method is only called when launching a JP.
+ * @param instanceType
+ * @return
+ */
+ public AbstractDuccComponent getComponentByInstanceType(Class<?> instanceType) {
+ // Extract all Ducc components from Spring container
+ Map<String,AbstractDuccComponent> duccComponents =
+ context.getBeansOfType(AbstractDuccComponent.class);
+ for(Map.Entry<String, AbstractDuccComponent> duccComponent: duccComponents.entrySet()) {
+ return duccComponent.getValue();
+ }
+ return null;
+ }
+ /**
+ * This method is only called when launching a JP.
+ * @param ipc - instance of IProcessContainer
+ * @param args - program args
+ * @throws Exception
+ */
+ public void setProcessor(Object ipc, String[] args) throws Exception {
+ AbstractDuccComponent duccComponent = getComponentByInstanceType(IJobProcessor.class);
+ if ( duccComponent instanceof IJobProcessor ) {
+ // store program args which will be used in start() method below
+ this.args = args;
+ // hand-off instance of IProcessContainer to the IJobProcessor
+ ((IJobProcessor)duccComponent).setProcessor(ipc, args);
+ getDuccLogger().info("setProcessor", null, "... Component started: job-process");
+ }
+ }
+ /**
+ * This method is only called when launching a JP. It starts the JP including
+ * initialization and starts all worker threads.
+ *
+ * @throws Exception
+ */
+ public void start() throws Exception {
+ AbstractDuccComponent duccComponent = getComponentByInstanceType(IJobProcessor.class);
+ if ( duccComponent instanceof IJobProcessor ) {
+ // initialize JP and start work threads to begin processing
+ duccComponent.start(this, args);
+ getDuccLogger().info("setProcessor", null, "... Component started: job-process");
+ }
+
+ }
public void stop() throws Exception {
if ( main.isStarted() ) {
List<CamelContext> ctxList = main.getCamelContexts();