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/14 18:15:11 UTC

svn commit: r1651735 - in /uima/sandbox/uima-ducc/trunk/uima-ducc-user/src/main/java/org/apache/uima/ducc/user/jp: UimaASProcessContainer.java UimaProcessContainer.java

Author: cwiklik
Date: Wed Jan 14 17:15:11 2015
New Revision: 1651735

URL: http://svn.apache.org/r1651735
Log:
UIMA-4066 modify process,initialize,deploy,stop to set current context classloader to the CL that loaded the class before calling user code. 

Modified:
    uima/sandbox/uima-ducc/trunk/uima-ducc-user/src/main/java/org/apache/uima/ducc/user/jp/UimaASProcessContainer.java
    uima/sandbox/uima-ducc/trunk/uima-ducc-user/src/main/java/org/apache/uima/ducc/user/jp/UimaProcessContainer.java

Modified: uima/sandbox/uima-ducc/trunk/uima-ducc-user/src/main/java/org/apache/uima/ducc/user/jp/UimaASProcessContainer.java
URL: http://svn.apache.org/viewvc/uima/sandbox/uima-ducc/trunk/uima-ducc-user/src/main/java/org/apache/uima/ducc/user/jp/UimaASProcessContainer.java?rev=1651735&r1=1651734&r2=1651735&view=diff
==============================================================================
--- uima/sandbox/uima-ducc/trunk/uima-ducc-user/src/main/java/org/apache/uima/ducc/user/jp/UimaASProcessContainer.java (original)
+++ uima/sandbox/uima-ducc/trunk/uima-ducc-user/src/main/java/org/apache/uima/ducc/user/jp/UimaASProcessContainer.java Wed Jan 14 17:15:11 2015
@@ -82,13 +82,25 @@ implements IProcessContainer {
 	  return threadAffinity;
 	}	
 	public int initialize(String[] args) throws Exception {
-		// Get DDs and also extract scaleout property from DD
-		deploymentDescriptors = getDescriptors(args);
-		ids = new String[deploymentDescriptors.length];
-	    
+		// save current context cl and inject System classloader as
+		// a context cl before calling user code. This is done in 
+		// user code needs to load resources 
+		ClassLoader savedCL = Thread.currentThread().getContextClassLoader();
+		Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
+		try {
+			// Get DDs and also extract scaleout property from DD
+			deploymentDescriptors = getDescriptors(args);
+			ids = new String[deploymentDescriptors.length];
+			
+		} finally {
+			// restore context CL 
+			Thread.currentThread().setContextClassLoader(savedCL);
+			
+		}
 		return scaleout;
 	}
 	public int initialize(Properties props, String[] args) throws Exception {
+
 		// generate Spring context file once
 		synchronized( UimaASProcessContainer.class) {
 			if ( !initialized ) {
@@ -122,6 +134,11 @@ implements IProcessContainer {
 	 * @throws Exception
 	 */
 	public void deploy(String duccHome) throws Exception {
+		// save current context cl and inject System classloader as
+		// a context cl before calling user code. This is done in 
+		// user code needs to load resources 
+		ClassLoader savedCL = Thread.currentThread().getContextClassLoader();
+		Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
 		// deploy singleUIMA-AS Version instance of embedded broker
 		synchronized( UimaASProcessContainer.class) {
 			try {
@@ -155,6 +172,10 @@ implements IProcessContainer {
 				e.printStackTrace();
 				throw new RuntimeException(e);
 
+			} finally {
+				// restore context CL 
+				Thread.currentThread().setContextClassLoader(savedCL);
+
 			}
 			//	Pin thread to its own CAS serializer
 			serializerMap.put( Thread.currentThread().getId(), new UimaSerializer());
@@ -261,32 +282,42 @@ implements IProcessContainer {
 	 * @throws Exception
 	 */
 	public void stop() throws Exception {
+		// save current context cl and inject System classloader as
+		// a context cl before calling user code. This is done in 
+		// user code needs to load resources 
+		ClassLoader savedCL = Thread.currentThread().getContextClassLoader();
+		Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
 		// stops the broker
 		//brokerContainer.stop();
-		synchronized(UimaASProcessContainer.class) {
-			if ( brokerRunning ) {
-				System.out.println("Stopping UIMA_AS Client");
-				try {
-					// Prevent UIMA-AS from exiting
-					System.setProperty("dontKill", "true");
-					uimaASClient.stop();
+		try {
+			synchronized(UimaASProcessContainer.class) {
+				if ( brokerRunning ) {
+					System.out.println("Stopping UIMA_AS Client");
+					try {
+						// Prevent UIMA-AS from exiting
+						System.setProperty("dontKill", "true");
+						uimaASClient.stop();
 
-				} catch (Exception e) {
-					e.printStackTrace();
-				}
-				
-				System.out.println("Stopping Broker");
+					} catch (Exception e) {
+						e.printStackTrace();
+					}
+					
+					System.out.println("Stopping Broker");
 
-				Method stopMethod = classToLaunch.getMethod("stop");
-				stopMethod.invoke(brokerInstance);
-				
-				Method waitMethod = classToLaunch.getMethod("waitUntilStopped");
-				waitMethod.invoke(brokerInstance);
+					Method stopMethod = classToLaunch.getMethod("stop");
+					stopMethod.invoke(brokerInstance);
+					
+					Method waitMethod = classToLaunch.getMethod("waitUntilStopped");
+					waitMethod.invoke(brokerInstance);
 
-				brokerRunning = false;
+					brokerRunning = false;
+				}
 			}
+			
+		} finally {
+			// restore context CL 
+			Thread.currentThread().setContextClassLoader(savedCL);
 		}
-
 	}
 	/**
 	 * This method is called via reflection and delegates processing to the colocated
@@ -296,6 +327,11 @@ implements IProcessContainer {
 	 * @throws Exception
 	 */
 	public List<Properties> process(Object xmi) throws Exception {
+		// save current context cl and inject System classloader as
+		// a context cl before calling user code. This is done in 
+		// user code needs to load resources 
+		ClassLoader savedCL = Thread.currentThread().getContextClassLoader();
+		Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
 		CAS cas = uimaASClient.getCAS();   // fetch a new CAS from the client's Cas Pool
 		try {
 			// reset last error
@@ -330,6 +366,9 @@ implements IProcessContainer {
 			if ( cas != null) {
 				cas.release();
 			}
+			// restore context CL 
+			Thread.currentThread().setContextClassLoader(savedCL);
+
 		}
 	}
     

Modified: uima/sandbox/uima-ducc/trunk/uima-ducc-user/src/main/java/org/apache/uima/ducc/user/jp/UimaProcessContainer.java
URL: http://svn.apache.org/viewvc/uima/sandbox/uima-ducc/trunk/uima-ducc-user/src/main/java/org/apache/uima/ducc/user/jp/UimaProcessContainer.java?rev=1651735&r1=1651734&r2=1651735&view=diff
==============================================================================
--- uima/sandbox/uima-ducc/trunk/uima-ducc-user/src/main/java/org/apache/uima/ducc/user/jp/UimaProcessContainer.java (original)
+++ uima/sandbox/uima-ducc/trunk/uima-ducc-user/src/main/java/org/apache/uima/ducc/user/jp/UimaProcessContainer.java Wed Jan 14 17:15:11 2015
@@ -181,6 +181,11 @@ implements IProcessContainer {
 	}
 	  
 	public void deploy(String duccHome) throws Exception {
+		// save current context cl and inject System classloader as
+		// a context cl before calling user code. This is done in 
+		// user code needs to load resources 
+		ClassLoader savedCL = Thread.currentThread().getContextClassLoader();
+		Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
 		ResourceSpecifier rSpecifier = null;
 	    HashMap<String,Object> paramsMap = 
 				new HashMap<String,Object>();
@@ -219,12 +224,18 @@ implements IProcessContainer {
 
 		} finally {
 			sharedInitSemaphore.release();
-			
+			// restore context CL 
+			Thread.currentThread().setContextClassLoader(savedCL);
 		}
 		
 	}
 
 	public void stop() throws Exception {
+		// save current context cl and inject System classloader as
+		// a context cl before calling user code. This is done in 
+		// user code needs to load resources 
+		ClassLoader savedCL = Thread.currentThread().getContextClassLoader();
+		Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
 		try {
 			AnalysisEngine ae = instanceMap.checkout();
 			if ( ae != null ) {
@@ -232,10 +243,19 @@ implements IProcessContainer {
 			}
 		} catch( Exception e) {
 			
+		} finally {
+			// restore context CL 
+			Thread.currentThread().setContextClassLoader(savedCL);
+
 		}
 	}
 
 	public List<Properties> process(Object xmi) throws Exception {
+		// save current context cl and inject System classloader as
+		// a context cl before calling user code. This is done in 
+		// user code needs to load resources 
+		ClassLoader savedCL = Thread.currentThread().getContextClassLoader();
+		Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
 		AnalysisEngine ae = null;
 		latch.await();
 		CAS cas = casPool.getCas();
@@ -288,6 +308,8 @@ implements IProcessContainer {
 			if (cas != null) {
 				casPool.releaseCas(cas);
 			}
+			// restore context CL 
+			Thread.currentThread().setContextClassLoader(savedCL);
 		}
 	}
 	   private List<AnalysisEnginePerformanceMetrics> getMetrics(AnalysisEngine ae)