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 2018/02/26 18:54:13 UTC

svn commit: r1825401 [7/11] - in /uima/uima-as/branches/uima-as-3: uimaj-as-activemq/src/main/java/org/apache/uima/adapter/jms/activemq/ uimaj-as-activemq/src/main/java/org/apache/uima/adapter/jms/client/ uimaj-as-activemq/src/main/java/org/apache/uima...

Modified: uima/uima-as/branches/uima-as-3/uimaj-as-core/src/main/java/org/apache/uima/aae/handler/input/MetadataResponseHandler_impl.java
URL: http://svn.apache.org/viewvc/uima/uima-as/branches/uima-as-3/uimaj-as-core/src/main/java/org/apache/uima/aae/handler/input/MetadataResponseHandler_impl.java?rev=1825401&r1=1825400&r2=1825401&view=diff
==============================================================================
--- uima/uima-as/branches/uima-as-3/uimaj-as-core/src/main/java/org/apache/uima/aae/handler/input/MetadataResponseHandler_impl.java (original)
+++ uima/uima-as/branches/uima-as-3/uimaj-as-core/src/main/java/org/apache/uima/aae/handler/input/MetadataResponseHandler_impl.java Mon Feb 26 18:54:11 2018
@@ -19,6 +19,8 @@
 
 package org.apache.uima.aae.handler.input;
 
+import java.io.ByteArrayInputStream;
+
 import org.apache.uima.UIMAFramework;
 import org.apache.uima.aae.UIMAEE_Constants;
 import org.apache.uima.aae.controller.AggregateAnalysisEngineController;
@@ -29,10 +31,12 @@ import org.apache.uima.aae.handler.Handl
 import org.apache.uima.aae.message.AsynchAEMessage;
 import org.apache.uima.aae.message.MessageContext;
 import org.apache.uima.cas.SerialFormat;
+import org.apache.uima.resource.metadata.ResourceMetaData;
 import org.apache.uima.util.Level;
+import org.apache.uima.util.XMLInputSource;
 
 public class MetadataResponseHandler_impl extends HandlerBase {
-  private static final Class CLASS_NAME = MetadataResponseHandler_impl.class;
+  private static final Class<?> CLASS_NAME = MetadataResponseHandler_impl.class;
 
   public MetadataResponseHandler_impl(String aName) {
     super(aName);
@@ -43,6 +47,7 @@ public class MetadataResponseHandler_imp
    * metadata.
    * 
    */
+  @Override
   public void handle(Object anObjectToHandle) {
 
     if (anObjectToHandle instanceof MessageContext) {
@@ -127,8 +132,17 @@ public class MetadataResponseHandler_imp
             if (AsynchAEMessage.Exception == payload) {
               return;
             }
+            ResourceMetaData resource = null;
+            if ( serializationSupportedByRemote == AsynchAEMessage.None ) {
+            	resource = (ResourceMetaData)
+            			((MessageContext) anObjectToHandle).getMessageObjectProperty(AsynchAEMessage.AEMetadata);
+            } else {
+                String analysisEngineMetadata = ((MessageContext) anObjectToHandle).getStringMessage();
+                ByteArrayInputStream bis = new ByteArrayInputStream(analysisEngineMetadata.getBytes());
+                XMLInputSource in1 = new XMLInputSource(bis, null);
+                resource = UIMAFramework.getXMLParser().parseResourceMetaData(in1);
+            }
 
-            String analysisEngineMetadata = ((MessageContext) anObjectToHandle).getStringMessage();
             String fromServer = null;
             if (((MessageContext) anObjectToHandle).propertyExists(AsynchAEMessage.EndpointServer)) {
               fromServer = ((MessageContext) anObjectToHandle)
@@ -140,7 +154,7 @@ public class MetadataResponseHandler_imp
             // The ServerURI set by the service may be its local name for the broker, e.g.
             // tcp://localhost:61616
             ((AggregateAnalysisEngineController) getController()).mergeTypeSystem(
-                    analysisEngineMetadata, fromEndpoint, fromServer);
+                    resource, fromEndpoint, fromServer);
             ((AggregateAnalysisEngineController) getController()).setRemoteSerializationSupported(serializationSupportedByRemote, fromEndpoint, fromServer);
           }
         } else {

Modified: uima/uima-as/branches/uima-as-3/uimaj-as-core/src/main/java/org/apache/uima/aae/handler/input/ProcessRequestHandler_impl.java
URL: http://svn.apache.org/viewvc/uima/uima-as/branches/uima-as-3/uimaj-as-core/src/main/java/org/apache/uima/aae/handler/input/ProcessRequestHandler_impl.java?rev=1825401&r1=1825400&r2=1825401&view=diff
==============================================================================
--- uima/uima-as/branches/uima-as-3/uimaj-as-core/src/main/java/org/apache/uima/aae/handler/input/ProcessRequestHandler_impl.java (original)
+++ uima/uima-as/branches/uima-as-3/uimaj-as-core/src/main/java/org/apache/uima/aae/handler/input/ProcessRequestHandler_impl.java Mon Feb 26 18:54:11 2018
@@ -54,8 +54,12 @@ import org.apache.uima.cas.impl.Serializ
 import org.apache.uima.cas.impl.XmiSerializationSharedData;
 import org.apache.uima.util.Level;
 
+/**
+ * Handles input CAS process request
+ * 
+ */
 public class ProcessRequestHandler_impl extends HandlerBase {
-  private static final Class CLASS_NAME = ProcessRequestHandler_impl.class;
+  private static final Class<?> CLASS_NAME = ProcessRequestHandler_impl.class;
   /*
    * Declare a semaphore which is used to block UIMA AS aggregate receiving thread until 
    * a CAS is fully processed. This semaphore prevents the receiving thread from grabbing
@@ -109,7 +113,7 @@ public class ProcessRequestHandler_impl
       } catch (Exception e) {
       }
 
-      getController().getOutputChannel().sendReply(
+      getController().getOutputChannel(aMessageContext.getEndpoint()).sendReply(
               new InvalidMessageException("No XMI data in message"), casReferenceId,
               parentCasReferenceId, aMessageContext.getEndpoint(), AsynchAEMessage.Process);
       // Dont process this empty message
@@ -388,7 +392,7 @@ public class ProcessRequestHandler_impl
                 "UIMAEE_message_has_cas_refid__INFO",
                 new Object[] { aMessageContext.getEndpoint().getEndpoint() });
       }
-      getController().getOutputChannel().sendReply(
+      getController().getOutputChannel(aMessageContext.getEndpoint()).sendReply(
               new InvalidMessageException("No Cas Reference Id Received From Delegate In message"),
               null, null, aMessageContext.getEndpoint(), AsynchAEMessage.Process);
       return null;
@@ -413,7 +417,7 @@ public class ProcessRequestHandler_impl
       return; // No XMI just return
     }
 
-    CasStateEntry inputCasStateEntry = null;
+//    CasStateEntry inputCasStateEntry = null;
 
     try {
 
@@ -430,6 +434,7 @@ public class ProcessRequestHandler_impl
       // Destination where Free Cas Notification will be sent if the CAS came from a Cas Multiplier
       Endpoint freeCasEndpoint = null;
 
+      CasStateEntry inputCasStateEntry = null;
 
       // CASes generated by a Cas Multiplier will have a CasSequence property set.
       if (aMessageContext.propertyExists(AsynchAEMessage.CasSequence)) {
@@ -519,10 +524,17 @@ public class ProcessRequestHandler_impl
           cse = getController().getLocalCache().createCasStateEntry(casReferenceId);
           // Check if this CAS is a child
           if (aMessageContext.propertyExists(AsynchAEMessage.CasSequence)) {
-            cse.setInputCasReferenceId(inputCasReferenceId);
+            cse.setParentCasReferenceId(inputCasReferenceId);
           }
+          cse.setInputCasReferenceId(casReferenceId);
+          cse.setClientEndpoint(aMessageContext.getEndpoint());
+          
         } else {
           cse = getController().getLocalCache().lookupEntry(casReferenceId);
+          if (aMessageContext.propertyExists(AsynchAEMessage.CasSequence)) {
+        	  cse.setInputCasReferenceId(inputCasStateEntry.getInputCasReferenceId());
+          }
+          
         }
 
         if (getController() instanceof AggregateAnalysisEngineController
@@ -665,8 +677,8 @@ public class ProcessRequestHandler_impl
         // Get the id of the parent Cas
         inputCasReferenceId = aMessageContext
                 .getMessageStringProperty(AsynchAEMessage.InputCasReference);
-        if (cse.getInputCasReferenceId() == null) {
-          cse.setInputCasReferenceId(inputCasReferenceId);
+        if (cse.getParentCasReferenceId() == null) {
+          cse.setParentCasReferenceId(inputCasReferenceId);
         }
 
         if (getController() instanceof AggregateAnalysisEngineController) {
@@ -974,12 +986,14 @@ public class ProcessRequestHandler_impl
               "UIMAEE_release_cas_req__FINE",
               new Object[] { getController().getName(), casReferenceId });
     }
+    System.out.println("..... Service:"+getController().getComponentName()+" Releasing CAS");
+
     getController().releaseNextCas(casReferenceId);
   }
 
   private void handlePingRequest(MessageContext aMessageContext) {
     try {
-      getController().getOutputChannel().sendReply(AsynchAEMessage.Ping,
+      getController().getOutputChannel(aMessageContext.getEndpoint()).sendReply(AsynchAEMessage.Ping,
               aMessageContext.getEndpoint(), null, false);
     } catch (Exception e) {
       if (UIMAFramework.getLogger(CLASS_NAME).isLoggable(Level.WARNING)) {
@@ -1049,10 +1063,11 @@ public class ProcessRequestHandler_impl
               || isHandlerForMessage(messageContext, AsynchAEMessage.Request, AsynchAEMessage.Stop)) {
         int payload = messageContext.getMessageIntProperty(AsynchAEMessage.Payload);
         int command = messageContext.getMessageIntProperty(AsynchAEMessage.Command);
+       	System.out.println("..... Service:"+getController().getComponentName()+" rec'd request msg with payload=CASRefID - command:"+command+" Payload:"+payload);
 
         CacheEntry ce = null;
         if (AsynchAEMessage.CASRefID == payload) {
-        	String cid = null;
+         	String cid = null;
         	// Fetch id of the CAS from the message.
             if ((cid = getCasReferenceId(messageContext)) == null) {
               return; // Invalid message. Nothing to do
@@ -1073,7 +1088,6 @@ public class ProcessRequestHandler_impl
         if (getController().isStopped()) {
           return;
         }
-
         if (AsynchAEMessage.CASRefID == payload) {
           // Fetch id of the CAS from the message.
           if (getCasReferenceId(messageContext) == null) {
@@ -1084,8 +1098,12 @@ public class ProcessRequestHandler_impl
         } else if (AsynchAEMessage.XMIPayload == payload
                 || AsynchAEMessage.BinaryPayload == payload) {
           // Fetch id of the CAS from the message.
-          if (getCasReferenceId(messageContext) == null) {
+         // if (getCasReferenceId(messageContext) == null) {
+            String casReferenceId = getCasReferenceId(messageContext);
+          if (casReferenceId == null) {
             return; // Invalid message. Nothing to do
+          } else {
+        	  System.out.println(".......... Service:"+getController().getComponentName()+" Recv'd CAS:"+casReferenceId+" From Remote:"+messageContext.getMessageStringProperty(AsynchAEMessage.MessageFrom));
           }
           handleProcessRequestFromRemoteClient(messageContext);
         } 
@@ -1112,6 +1130,7 @@ public class ProcessRequestHandler_impl
       // Not a Request nor Command. Delegate to the next handler in the chain
       super.delegate(messageContext);
     } catch (Exception e) {
+    	
       if (UIMAFramework.getLogger(CLASS_NAME).isLoggable(Level.WARNING)) {
         if ( getController() != null ) {
           UIMAFramework.getLogger(CLASS_NAME).logrb(Level.WARNING, CLASS_NAME.getName(),

Modified: uima/uima-as/branches/uima-as-3/uimaj-as-core/src/main/java/org/apache/uima/aae/handler/input/ProcessResponseHandler.java
URL: http://svn.apache.org/viewvc/uima/uima-as/branches/uima-as-3/uimaj-as-core/src/main/java/org/apache/uima/aae/handler/input/ProcessResponseHandler.java?rev=1825401&r1=1825400&r2=1825401&view=diff
==============================================================================
--- uima/uima-as/branches/uima-as-3/uimaj-as-core/src/main/java/org/apache/uima/aae/handler/input/ProcessResponseHandler.java (original)
+++ uima/uima-as/branches/uima-as-3/uimaj-as-core/src/main/java/org/apache/uima/aae/handler/input/ProcessResponseHandler.java Mon Feb 26 18:54:11 2018
@@ -471,8 +471,19 @@ public class ProcessResponseHandler exte
               .getLocalCache().lookupEntry(casReferenceId);
 
       CAS cas = cacheEntry.getCas();
-      String delegateKey = ((AggregateAnalysisEngineController) getController())
-              .lookUpDelegateKey(aMessageContext.getEndpoint().getEndpoint());
+//      String delegateKey = ((AggregateAnalysisEngineController) getController())
+//              .lookUpDelegateKey(aMessageContext.getEndpoint().getEndpoint());
+      String delegateKey = null;
+      if ( aMessageContext.getEndpoint().getEndpoint() == null || aMessageContext.getEndpoint().getEndpoint().trim().length()==0) {
+    	  String fromEndpoint = aMessageContext
+                  .getMessageStringProperty(AsynchAEMessage.MessageFrom);
+    	  delegateKey = ((AggregateAnalysisEngineController) getController())
+                  .lookUpDelegateKey(fromEndpoint);
+      } else {
+          delegateKey = ((AggregateAnalysisEngineController) getController())
+                  .lookUpDelegateKey(aMessageContext.getEndpoint().getEndpoint());
+      }
+      
       Delegate delegate = ((AggregateAnalysisEngineController) getController())
               .lookupDelegate(delegateKey);
       if (casStateEntry != null) {

Modified: uima/uima-as/branches/uima-as-3/uimaj-as-core/src/main/java/org/apache/uima/aae/jmx/JmxManagement.java
URL: http://svn.apache.org/viewvc/uima/uima-as/branches/uima-as-3/uimaj-as-core/src/main/java/org/apache/uima/aae/jmx/JmxManagement.java?rev=1825401&r1=1825400&r2=1825401&view=diff
==============================================================================
--- uima/uima-as/branches/uima-as-3/uimaj-as-core/src/main/java/org/apache/uima/aae/jmx/JmxManagement.java (original)
+++ uima/uima-as/branches/uima-as-3/uimaj-as-core/src/main/java/org/apache/uima/aae/jmx/JmxManagement.java Mon Feb 26 18:54:11 2018
@@ -87,4 +87,6 @@ public interface JmxManagement {
  
   public void addObject(String objectName) throws Exception;
  
+  public boolean isRegistered(ObjectName aName) throws Exception;
+
 }

Modified: uima/uima-as/branches/uima-as-3/uimaj-as-core/src/main/java/org/apache/uima/aae/jmx/JmxManager.java
URL: http://svn.apache.org/viewvc/uima/uima-as/branches/uima-as-3/uimaj-as-core/src/main/java/org/apache/uima/aae/jmx/JmxManager.java?rev=1825401&r1=1825400&r2=1825401&view=diff
==============================================================================
--- uima/uima-as/branches/uima-as-3/uimaj-as-core/src/main/java/org/apache/uima/aae/jmx/JmxManager.java (original)
+++ uima/uima-as/branches/uima-as-3/uimaj-as-core/src/main/java/org/apache/uima/aae/jmx/JmxManager.java Mon Feb 26 18:54:11 2018
@@ -32,6 +32,7 @@ import javax.management.ObjectInstance;
 import javax.management.ObjectName;
 
 import org.apache.uima.UIMAFramework;
+import org.apache.uima.aae.InProcessCache;
 import org.apache.uima.internal.util.JmxMBeanAgent;
 import org.apache.uima.util.Level;
 
@@ -96,6 +97,15 @@ public class JmxManager implements JmxMa
 	    ObjectName objName = new ObjectName(objectName);
 	    mbeanList.add(objName);
   }
+  
+  public boolean isRegistered( ObjectName aName ) throws Exception {
+      synchronized(JmxManager.class) {
+          if (((MBeanServer) platformMBeanServer).isRegistered(aName)) {
+        	  return true;
+          }
+      }
+      return false;
+  }
   public void registerMBean(Object anMBeanToRegister, ObjectName aName) throws Exception {
     if (!isInitialized()) {
       return;
@@ -104,6 +114,13 @@ public class JmxManager implements JmxMa
     try {
       //  use class level locking to make sure one thread at time executes the code
       synchronized(JmxManager.class) {
+    	  if ( anMBeanToRegister instanceof InProcessCache ) {
+              Set instances = ((MBeanServer) platformMBeanServer).queryMBeans(new ObjectName("*,name=" + ((InProcessCache)anMBeanToRegister).getName()), null);
+              if ( instances.size() > 0) {
+            	  return;
+              }
+    	  }
+
         if (((MBeanServer) platformMBeanServer).isRegistered(aName)) {
           ((MBeanServer) platformMBeanServer).unregisterMBean(aName);
         }

Modified: uima/uima-as/branches/uima-as-3/uimaj-as-core/src/main/java/org/apache/uima/aae/message/AsynchAEMessage.java
URL: http://svn.apache.org/viewvc/uima/uima-as/branches/uima-as-3/uimaj-as-core/src/main/java/org/apache/uima/aae/message/AsynchAEMessage.java?rev=1825401&r1=1825400&r2=1825401&view=diff
==============================================================================
--- uima/uima-as/branches/uima-as-3/uimaj-as-core/src/main/java/org/apache/uima/aae/message/AsynchAEMessage.java (original)
+++ uima/uima-as/branches/uima-as-3/uimaj-as-core/src/main/java/org/apache/uima/aae/message/AsynchAEMessage.java Mon Feb 26 18:54:11 2018
@@ -22,12 +22,16 @@ package org.apache.uima.aae.message;
 public interface AsynchAEMessage {
   //public static final String TargetSelectorProperty ="TargetServiceId";
   
+  public static final String Transport = "Transport";
+
   public static final String TotalTimeSpentInAnalytic = "TimeInAnalytic";
 
   public static final String TimeInService = "TimeInService";
 
   public static final String Endpoint = "Endpoint";
 
+  public static final String EndpointName = "EndpointName";
+
   public static final String DelegateStats = "DelegateStats";
 
   public static final String RequestFailed = "RequestFailed";
@@ -40,6 +44,8 @@ public interface AsynchAEMessage {
   
   public static final String Destination = "Destination";
 
+  public static final String DelegateKey = "DelegateKey";
+
   // 5/2013 xcas not used as a serialization form
 //  public static final String XCASREFId = "XCASRefId";
 
@@ -82,6 +88,8 @@ public interface AsynchAEMessage {
   public static final String SkipPendingLists = "SkipPendingLists";
 
   public static final String Payload = "Payload";
+  
+  public static final String FreeCASQueue = "FreeCASQueue";
 
   public static final int XMIPayload = 1000;
 
@@ -117,6 +125,10 @@ public interface AsynchAEMessage {
   public static final int Ping = 2007;
 
   public static final int ServiceInfo = 2008;
+  
+  // this command is used to force stop java queue listener
+  public static final int PoisonPill = 2009;
+
 
   public static final String MessageType = "MessageType";
 

Added: uima/uima-as/branches/uima-as-3/uimaj-as-core/src/main/java/org/apache/uima/aae/message/UimaASClientDirectMessage.java
URL: http://svn.apache.org/viewvc/uima/uima-as/branches/uima-as-3/uimaj-as-core/src/main/java/org/apache/uima/aae/message/UimaASClientDirectMessage.java?rev=1825401&view=auto
==============================================================================
--- uima/uima-as/branches/uima-as-3/uimaj-as-core/src/main/java/org/apache/uima/aae/message/UimaASClientDirectMessage.java (added)
+++ uima/uima-as/branches/uima-as-3/uimaj-as-core/src/main/java/org/apache/uima/aae/message/UimaASClientDirectMessage.java Mon Feb 26 18:54:11 2018
@@ -0,0 +1,100 @@
+/*
+ * 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.aae.message;
+
+import org.apache.uima.as.client.DirectMessage;
+
+public class UimaASClientDirectMessage implements UimaASClientMessage {
+	private DirectMessage message;
+	public UimaASClientDirectMessage(DirectMessage message) {
+		this.message = message;
+	}
+	
+	public int command() {
+		return ((Integer)message.get(AsynchAEMessage.Command)).intValue();
+	}
+
+	@Override
+	public String messageFrom() {
+		// TODO Auto-generated method stub
+		return null;
+	}
+
+	@Override
+	public Object getData() {
+		// TODO Auto-generated method stub
+		return null;
+	}
+
+	@Override
+	public int serializationMethod() {
+		// TODO Auto-generated method stub
+		return -1;
+	}
+
+	public String casReferenceId() {
+		return asString(AsynchAEMessage.CasReference);
+	}
+
+	public int payload() {
+		return asInt(AsynchAEMessage.Payload);
+	}
+
+	public String serverIP() {
+		return null;
+	}
+
+	public Object replyTo() {
+		return null;
+	}
+
+	public boolean serializationSpecified() {
+		// TODO Auto-generated method stub
+		return false;
+	}
+
+	public String asText() {
+		return (String)message.get(AsynchAEMessage.AEMetadata);
+	}
+
+	@Override
+	public Object asObject(String key) {
+		return message.get(key);//AsynchAEMessage.AEMetadata);
+	}
+
+	@Override
+	public boolean contains(String key) {
+		// TODO Auto-generated method stub
+		return message.propertyExists(key);
+	}
+
+	@Override
+	public String asString(String key) {
+		// TODO Auto-generated method stub
+		return message.getAsString(key);
+	}
+
+	@Override
+	public int asInt(String key) {
+		// TODO Auto-generated method stub
+		return message.getAsInt(key);
+	}
+	
+	
+}

Added: uima/uima-as/branches/uima-as-3/uimaj-as-core/src/main/java/org/apache/uima/aae/message/UimaASClientMessage.java
URL: http://svn.apache.org/viewvc/uima/uima-as/branches/uima-as-3/uimaj-as-core/src/main/java/org/apache/uima/aae/message/UimaASClientMessage.java?rev=1825401&view=auto
==============================================================================
--- uima/uima-as/branches/uima-as-3/uimaj-as-core/src/main/java/org/apache/uima/aae/message/UimaASClientMessage.java (added)
+++ uima/uima-as/branches/uima-as-3/uimaj-as-core/src/main/java/org/apache/uima/aae/message/UimaASClientMessage.java Mon Feb 26 18:54:11 2018
@@ -0,0 +1,36 @@
+/*
+ * 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.aae.message;
+
+public interface UimaASClientMessage {
+	public int command();
+	public boolean contains(String key);
+	public String messageFrom();
+	public Object getData();
+	public String asText();
+	public String asString(String key);
+	public int asInt(String key);
+	public Object asObject(String key);
+	public int serializationMethod();
+	public boolean serializationSpecified();
+	public String casReferenceId();
+	public int payload();
+	public String serverIP();
+	public Object replyTo();
+}

Added: uima/uima-as/branches/uima-as-3/uimaj-as-core/src/main/java/org/apache/uima/aae/service/AbstractUimaASService.java
URL: http://svn.apache.org/viewvc/uima/uima-as/branches/uima-as-3/uimaj-as-core/src/main/java/org/apache/uima/aae/service/AbstractUimaASService.java?rev=1825401&view=auto
==============================================================================
--- uima/uima-as/branches/uima-as-3/uimaj-as-core/src/main/java/org/apache/uima/aae/service/AbstractUimaASService.java (added)
+++ uima/uima-as/branches/uima-as-3/uimaj-as-core/src/main/java/org/apache/uima/aae/service/AbstractUimaASService.java Mon Feb 26 18:54:11 2018
@@ -0,0 +1,136 @@
+/*
+ * 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.aae.service;
+
+import java.util.List;
+import java.util.UUID;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.CountDownLatch;
+
+import org.apache.uima.aae.InProcessCache;
+import org.apache.uima.aae.controller.AggregateAnalysisEngineController;
+import org.apache.uima.aae.controller.AnalysisEngineController;
+import org.apache.uima.aae.controller.ControllerStatusListener;
+import org.apache.uima.analysis_engine.metadata.AnalysisEngineMetaData;
+import org.apache.uima.as.client.DirectMessage;
+import org.apache.uima.as.client.Listener;
+import org.apache.uima.as.client.Listener.Type;
+import org.apache.uima.cas.CAS;
+import org.apache.uima.resource.ResourceSpecifier;
+
+public abstract class AbstractUimaASService {
+	public final String id = UUID.randomUUID().toString();
+
+	protected AnalysisEngineController controller;
+	protected ResourceSpecifier resourceSpecifier = null;
+	protected InProcessCache inProcessCache;
+	protected String name;
+
+	private void startListener(Listener listener, AnalysisEngineController ctrl) {
+		listener.start();
+		if (!ctrl.isPrimitive()) {
+			if (listener.getType().equals(Type.Reply)) {
+
+				System.out.println("...Controller:" + ctrl.getComponentName() + " Started Listener type:"
+						+ listener.getType() + " hashcode:" + listener.hashCode() + " Delegate:" + listener.getName());
+			} else {
+				System.out.println("...Controller:" + ctrl.getComponentName() + " Started Listener type:"
+						+ listener.getType() + " hashcode:" + listener.hashCode());
+			}
+		} else {
+			System.out.println("...Controller:" + ctrl.getComponentName() + " Started Listener type:"
+					+ listener.getType() + " hashcode:" + listener.hashCode());
+
+		}
+	}
+
+	private void startListenersFromList(List<Listener> listeners, AnalysisEngineController ctrl) {
+		for (Listener l : listeners) {
+			startListener(l, ctrl);
+		}
+
+	}
+
+	protected void startListeners(AnalysisEngineController ctrl) {
+		if (ctrl instanceof AggregateAnalysisEngineController) {
+			for (AnalysisEngineController c : ((AggregateAnalysisEngineController) ctrl).getChildControllerList()) {
+				if (c instanceof AggregateAnalysisEngineController) {
+					// recurse until last inner aggregate is reached
+					startListeners(c);
+					// now start a listener for each remote delegate. This listener will handle
+					// replies
+					startListenersFromList(c.getAllListeners(), c);
+				}
+			}
+		}
+		startListenersFromList(ctrl.getAllListeners(), ctrl);
+	}
+
+	public void start() throws Exception {
+
+		startListeners(controller);
+
+//		controller.getControllerLatch().release();
+//		controller.initializeVMTransport(1);
+		System.out.println(
+				".........." + controller.getName() + " AbstractUimaASService.start() ............ 1");
+		CountDownLatch latch = new CountDownLatch(1);
+		ControllerStatusListener l = new ControllerStatusListener(latch);
+		controller.addControllerCallbackListener(l);
+		System.out.println(
+				".........." + controller.getName() + " AbstractUimaASService.start() ............ 2");
+
+		if (controller instanceof AggregateAnalysisEngineController) {
+			System.out
+					.println("..........." + controller.getName() + " Aggregate sending GetMeta to delegates");
+			((AggregateAnalysisEngineController) controller).sendRequestForMetadataToRemoteDelegates();
+			System.out
+			.println("..........." + controller.getName() + " Aggregate waiting on a latch.hashcode="+latch.hashCode());
+			if ( controller.isTopLevelComponent()) {
+				latch.await();
+			}
+		}
+		System.out.println(
+				".........." + controller.getComponentName() + " AbstractUimaASService.start() ............ 3");
+
+	}
+
+	public CAS getCAS() throws Exception {
+		return null;
+	}
+
+	public void process(CAS cas, String casReferenceId) throws Exception {
+	}
+
+	public void sendGetMetaRequest() throws Exception {
+	}
+
+	public void collectionProcessComplete() throws Exception {
+	}
+
+	public AnalysisEngineMetaData getMetaData() throws Exception {
+		return null;
+	}
+
+	public void removeFromCache(String casReferenceId) {
+	}
+
+	public void releaseCAS(String casReferenceId, BlockingQueue<DirectMessage> releaseCASQueue) throws Exception {
+	}
+}

Added: uima/uima-as/branches/uima-as-3/uimaj-as-core/src/main/java/org/apache/uima/aae/service/AsynchronousUimaASService.java
URL: http://svn.apache.org/viewvc/uima/uima-as/branches/uima-as-3/uimaj-as-core/src/main/java/org/apache/uima/aae/service/AsynchronousUimaASService.java?rev=1825401&view=auto
==============================================================================
--- uima/uima-as/branches/uima-as-3/uimaj-as-core/src/main/java/org/apache/uima/aae/service/AsynchronousUimaASService.java (added)
+++ uima/uima-as/branches/uima-as-3/uimaj-as-core/src/main/java/org/apache/uima/aae/service/AsynchronousUimaASService.java Mon Feb 26 18:54:11 2018
@@ -0,0 +1,255 @@
+/*
+ * 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.aae.service;
+
+import java.util.HashSet;
+import java.util.Set;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.LinkedBlockingQueue;
+
+import org.apache.uima.aae.InProcessCache;
+import org.apache.uima.aae.InProcessCache.CacheEntry;
+import org.apache.uima.aae.controller.AnalysisEngineController;
+import org.apache.uima.aae.message.AsynchAEMessage;
+import org.apache.uima.aae.message.MessageContext;
+import org.apache.uima.analysis_engine.metadata.AnalysisEngineMetaData;
+import org.apache.uima.as.client.DirectMessage;
+import org.apache.uima.as.client.DirectMessageContext;
+import org.apache.uima.cas.CAS;
+import org.apache.uima.resource.ResourceSpecifier;
+import org.springframework.context.annotation.AnnotationConfigApplicationContext;
+
+public class AsynchronousUimaASService extends AbstractUimaASService 
+implements UimaASService {
+	private static String CLIENT = "Client";
+	Set<AsynchronousUimaASService> allInstancesOfThisClass =
+			new HashSet<>();
+	private int scaleout=1;
+	private BlockingQueue<DirectMessage> processRequestQueue = 
+		new LinkedBlockingQueue<>();
+	private BlockingQueue<DirectMessage> getMetaRequestQueue = 
+		new LinkedBlockingQueue<>();
+	private BlockingQueue<DirectMessage> freeCasQueue = 
+			new LinkedBlockingQueue<>();
+	private BlockingQueue<DirectMessage> replyQueue = 
+	    new LinkedBlockingQueue<>();
+	private String tlc_bean_name;
+	
+    private AnnotationConfigApplicationContext context = 
+    		new AnnotationConfigApplicationContext();
+    
+    private String endpoint;
+    
+	public AsynchronousUimaASService(String endpoint) { 
+		this.endpoint = endpoint;
+	}
+	
+	public void setTopLevelControlleBeanName(String beanName) {
+		tlc_bean_name = beanName;
+	}
+	public AsynchronousUimaASService withResourceSpecifier(ResourceSpecifier resourceSpecifier ) {
+		this.resourceSpecifier = resourceSpecifier;
+		return this;
+	}
+	public ResourceSpecifier getResourceSpecifier() {
+		return resourceSpecifier;
+	}
+	public String getBeanName() {
+		return tlc_bean_name;
+	}
+	public String getEndpoint() {
+		return endpoint;
+	}
+	public void addInstance(AsynchronousUimaASService instance) {
+		allInstancesOfThisClass.add(instance);
+	}
+	public AnnotationConfigApplicationContext getContext() {
+		return context;
+	}
+	
+	public AsynchronousUimaASService withScaleout(int howMany) {
+		this.scaleout = howMany;
+		return this;
+	}
+	public int getScaleout() {
+		return scaleout;
+	}
+	/**
+	 * Allow to plugin an processQueue. This will be done when deploying multiple instances
+	 * of the same service. In this case all service instance will share the same queues.
+	 * @param q
+	 * @return
+	 */
+	public AsynchronousUimaASService withProcessQueue(BlockingQueue<DirectMessage> q) {
+		processRequestQueue = q;
+		return this;
+	}
+	/**
+	 * Allow to plugin an getMetaQueue. This will be done when deploying multiple instances
+	 * of the same service. In this case all service instance will share the same queues.
+	 * @param q
+	 * @return
+	 */
+	public AsynchronousUimaASService withGetMetaQueue(BlockingQueue<DirectMessage> q) {
+		getMetaRequestQueue = q;
+		return this;
+	}
+	/**
+	 * Allow to plugin an replyQueue. This will be done when deploying multiple instances
+	 * of the same service. In this case all service instance will share the same queues.
+	 * @param q
+	 * @return
+	 */
+	public AsynchronousUimaASService withReplyQueue(BlockingQueue<DirectMessage> q) {
+		replyQueue = q;
+		return this;
+	}
+
+	public AsynchronousUimaASService withInProcessCache( InProcessCache inProcessCache ) {
+		this.inProcessCache = inProcessCache;
+		return this;
+	}
+	public void removeFromCache(String casReferenceId) {
+		inProcessCache.remove(casReferenceId);
+		System.out.println("AsynchronousUimaASService.removeFromCache()-cache size:"+inProcessCache.getSize());
+	}
+	private CacheEntry add2Cache( CAS cas, MessageContext messageContext, String casReferenceId ) throws Exception {
+		return inProcessCache.register(cas, messageContext, casReferenceId);
+	}
+	public String getId() {
+		return id;
+	}
+	
+	public BlockingQueue<DirectMessage> getProcessRequestQueue() {
+		return this.processRequestQueue;
+	}
+	public BlockingQueue<DirectMessage> getMetaRequestQueue() {
+		return this.getMetaRequestQueue;
+	}
+	public BlockingQueue<DirectMessage> getReplyQueue() {
+		return this.replyQueue;
+	}
+	public BlockingQueue<DirectMessage> getFreeCasQueue() {
+		return this.freeCasQueue;
+	}
+	public AsynchronousUimaASService withController( AnalysisEngineController controller) {
+		this.controller = controller;
+		return this;
+	}
+	
+	public String getName() {
+		return name;
+	}
+	public AsynchronousUimaASService withName( String aName) {
+		this.name = aName;
+		return this;
+	}
+	public CAS getCAS() throws Exception {
+		return null;
+	}
+
+	public void process(CAS cas, String casReferenceId) throws Exception {
+
+		DirectMessage processMessage = 
+				new DirectMessage().
+				withCommand(AsynchAEMessage.Process).
+				withMessageType(AsynchAEMessage.Request).
+				withOrigin(CLIENT).
+				withPayload(AsynchAEMessage.CASRefID).
+				withCasReferenceId(casReferenceId).
+				withReplyQueue(replyQueue);
+
+		DirectMessageContext messageContext = 
+				new DirectMessageContext(processMessage, CLIENT,"");
+		add2Cache(cas, messageContext, casReferenceId);
+		
+		processRequestQueue.add(processMessage);
+	}
+
+	public void sendGetMetaRequest() throws Exception {
+		DirectMessage getMetaMessage = 
+				new DirectMessage().
+				withCommand(AsynchAEMessage.GetMeta).
+				withMessageType(AsynchAEMessage.Request).
+				withOrigin(CLIENT).
+				withPayload(AsynchAEMessage.None).
+				withReplyQueue(replyQueue);
+		
+		getMetaRequestQueue.add(getMetaMessage);
+	}
+	public void collectionProcessComplete() throws Exception {
+		DirectMessage cpcMessage = 
+				new DirectMessage().
+				withCommand(AsynchAEMessage.CollectionProcessComplete).
+				withMessageType(AsynchAEMessage.Request).
+				withOrigin(CLIENT).
+				withPayload(AsynchAEMessage.None).
+				withReplyQueue(replyQueue);
+		
+		processRequestQueue.add(cpcMessage);
+	}
+	public void releaseCAS(String casReferenceId, BlockingQueue<DirectMessage> releaseCASQueue ) throws Exception {
+		DirectMessage getMetaMessage = 
+				new DirectMessage().
+				withCommand(AsynchAEMessage.ReleaseCAS).
+				withMessageType(AsynchAEMessage.Request).
+				withPayload(AsynchAEMessage.CASRefID).
+				withCasReferenceId(casReferenceId).
+				withOrigin(CLIENT).
+				withPayload(AsynchAEMessage.None);
+		
+		releaseCASQueue.add(getMetaMessage);
+	}
+
+	public AnalysisEngineMetaData getMetaData() throws Exception {
+		DirectMessage getMetaMessage = 
+				new DirectMessage().
+				    withCommand(AsynchAEMessage.GetMeta).
+				    withMessageType(AsynchAEMessage.Request).
+				    withOrigin(CLIENT).
+				    withReplyQueue(replyQueue).
+				    withPayload(AsynchAEMessage.None);
+		
+		return getMetaData(getMetaMessage);
+	}
+	
+	public AnalysisEngineMetaData getMetaData(DirectMessage getMetaMessage) throws InterruptedException {
+
+	//	getMetaMessage.put(AsynchAEMessage.Endpoint, replyQueue);
+		getMetaRequestQueue.add(getMetaMessage);
+
+		DirectMessage dm = replyQueue.take();
+		return (AnalysisEngineMetaData)dm.get(AsynchAEMessage.AEMetadata);
+	}
+	public void quiesce() throws Exception {
+		controller.quiesceAndStop();
+		context.destroy();
+	}
+
+	public void stop() throws Exception {
+		for ( AsynchronousUimaASService instance : allInstancesOfThisClass) {
+			instance.stop();
+		}
+		controller.getControllerLatch().release();
+        controller.terminate();
+
+        context.destroy();
+	}
+
+}

Added: uima/uima-as/branches/uima-as-3/uimaj-as-core/src/main/java/org/apache/uima/aae/service/ScaleoutSpecification.java
URL: http://svn.apache.org/viewvc/uima/uima-as/branches/uima-as-3/uimaj-as-core/src/main/java/org/apache/uima/aae/service/ScaleoutSpecification.java?rev=1825401&view=auto
==============================================================================
--- uima/uima-as/branches/uima-as-3/uimaj-as-core/src/main/java/org/apache/uima/aae/service/ScaleoutSpecification.java (added)
+++ uima/uima-as/branches/uima-as-3/uimaj-as-core/src/main/java/org/apache/uima/aae/service/ScaleoutSpecification.java Mon Feb 26 18:54:11 2018
@@ -0,0 +1,47 @@
+/*
+ * 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.aae.service;
+
+public class ScaleoutSpecification {
+	int processScaleout = 1;
+	int getMetaScaleout = 1;
+	int freeCasScaleout = 1;
+	public int getProcessScaleout() {
+		return processScaleout;
+	}
+	public ScaleoutSpecification withProcessScaleout(int processScaleout) {
+		this.processScaleout = processScaleout;
+		return this;
+	}
+	public int getGetMetaScaleout() {
+		return getMetaScaleout;
+	}
+	public ScaleoutSpecification withGetMetaScaleout(int getMetaScaleout) {
+		this.getMetaScaleout = getMetaScaleout;
+		return this;
+	}
+	public int getFreeCasScaleout() {
+		return freeCasScaleout;
+	}
+	public ScaleoutSpecification withFreeCasScaleout(int freeCasScaleout) {
+		this.freeCasScaleout = freeCasScaleout;
+		return this;
+	}
+	
+}

Added: uima/uima-as/branches/uima-as-3/uimaj-as-core/src/main/java/org/apache/uima/aae/service/ServiceNotFoundException.java
URL: http://svn.apache.org/viewvc/uima/uima-as/branches/uima-as-3/uimaj-as-core/src/main/java/org/apache/uima/aae/service/ServiceNotFoundException.java?rev=1825401&view=auto
==============================================================================
--- uima/uima-as/branches/uima-as-3/uimaj-as-core/src/main/java/org/apache/uima/aae/service/ServiceNotFoundException.java (added)
+++ uima/uima-as/branches/uima-as-3/uimaj-as-core/src/main/java/org/apache/uima/aae/service/ServiceNotFoundException.java Mon Feb 26 18:54:11 2018
@@ -0,0 +1,30 @@
+/*
+ * 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.aae.service;
+
+public class ServiceNotFoundException extends RuntimeException {
+
+	/**
+	 * 
+	 */
+	private static final long serialVersionUID = 1L;
+	public ServiceNotFoundException(String msg) {
+		super(msg);
+	}
+}

Added: uima/uima-as/branches/uima-as-3/uimaj-as-core/src/main/java/org/apache/uima/aae/service/ServiceRegistry.java
URL: http://svn.apache.org/viewvc/uima/uima-as/branches/uima-as-3/uimaj-as-core/src/main/java/org/apache/uima/aae/service/ServiceRegistry.java?rev=1825401&view=auto
==============================================================================
--- uima/uima-as/branches/uima-as-3/uimaj-as-core/src/main/java/org/apache/uima/aae/service/ServiceRegistry.java (added)
+++ uima/uima-as/branches/uima-as-3/uimaj-as-core/src/main/java/org/apache/uima/aae/service/ServiceRegistry.java Mon Feb 26 18:54:11 2018
@@ -0,0 +1,31 @@
+/*
+ * 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.aae.service;
+
+import java.util.List;
+import java.util.Map;
+
+public interface ServiceRegistry {
+	public void register(UimaASService service);
+	public void unregister(UimaASService service);
+	public UimaASService lookupById(String serviceId);
+	public UimaASService lookupByEndpoint(String serviceEndpoint);
+	public Map<String, List<UimaASService>> getServiceList();
+	public void clear();
+}

Added: uima/uima-as/branches/uima-as-3/uimaj-as-core/src/main/java/org/apache/uima/aae/service/UimaASService.java
URL: http://svn.apache.org/viewvc/uima/uima-as/branches/uima-as-3/uimaj-as-core/src/main/java/org/apache/uima/aae/service/UimaASService.java?rev=1825401&view=auto
==============================================================================
--- uima/uima-as/branches/uima-as-3/uimaj-as-core/src/main/java/org/apache/uima/aae/service/UimaASService.java (added)
+++ uima/uima-as/branches/uima-as-3/uimaj-as-core/src/main/java/org/apache/uima/aae/service/UimaASService.java Mon Feb 26 18:54:11 2018
@@ -0,0 +1,46 @@
+/*
+ * 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.aae.service;
+
+import java.util.concurrent.BlockingQueue;
+
+import org.apache.uima.analysis_engine.metadata.AnalysisEngineMetaData;
+import org.apache.uima.as.client.DirectMessage;
+import org.apache.uima.cas.CAS;
+
+public interface UimaASService {
+	enum ServiceMode {
+		Asynchronous,
+		Synchronous
+	};
+	public String getEndpoint();
+	public String getId();
+	public void start() throws Exception;
+	public void stop() throws Exception;
+	public void quiesce() throws Exception;
+	public String getName();
+	public CAS getCAS() throws Exception;
+	public void process(CAS cas, String casReferenceId) throws Exception;
+	public void sendGetMetaRequest() throws Exception;
+	public void collectionProcessComplete() throws Exception;
+	public void releaseCAS(String casReferenceId, BlockingQueue<DirectMessage> releaseCASQueue ) throws Exception;
+	public AnalysisEngineMetaData getMetaData() throws Exception; 
+	public void removeFromCache(String casReferenceId);
+
+}

Added: uima/uima-as/branches/uima-as-3/uimaj-as-core/src/main/java/org/apache/uima/aae/service/UimaAsServiceRegistry.java
URL: http://svn.apache.org/viewvc/uima/uima-as/branches/uima-as-3/uimaj-as-core/src/main/java/org/apache/uima/aae/service/UimaAsServiceRegistry.java?rev=1825401&view=auto
==============================================================================
--- uima/uima-as/branches/uima-as-3/uimaj-as-core/src/main/java/org/apache/uima/aae/service/UimaAsServiceRegistry.java (added)
+++ uima/uima-as/branches/uima-as-3/uimaj-as-core/src/main/java/org/apache/uima/aae/service/UimaAsServiceRegistry.java Mon Feb 26 18:54:11 2018
@@ -0,0 +1,109 @@
+/*
+ * 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.aae.service;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.concurrent.ConcurrentHashMap;
+
+public class UimaAsServiceRegistry  implements ServiceRegistry {
+	private Map<String, List<UimaASService>> serviceRegistry = null;
+	
+	private UimaAsServiceRegistry() {
+		serviceRegistry = new ConcurrentHashMap<>(); 
+	}
+	
+	private static class UimaAsServiceRegistrySingleton {
+		private UimaAsServiceRegistrySingleton(){}
+		private static final UimaAsServiceRegistry instance =
+				new UimaAsServiceRegistry();
+	}
+	public static ServiceRegistry getInstance() {
+		return UimaAsServiceRegistrySingleton.instance;
+	}
+	public synchronized void register(UimaASService service) {
+		System.out.println("*********** Adding Service:"+service.getName()+" Key:"+service.getEndpoint()+" To Registry:"+serviceRegistry.hashCode());
+		List<UimaASService> list;
+		if ( serviceRegistry.containsKey(service.getEndpoint())) {
+			list = serviceRegistry.get(service.getEndpoint());
+		} else {
+			list = Collections.synchronizedList(new ArrayList<UimaASService>());
+			serviceRegistry.put(service.getEndpoint(), list);
+		}
+		list.add( service);
+	}
+	public synchronized void unregister(UimaASService service) {
+		Iterator<Entry<String, List<UimaASService>>> iterator = 
+				serviceRegistry.entrySet().iterator();
+		
+		while( iterator.hasNext() ) {
+			Iterator<UimaASService> listIterator = iterator.next().getValue().iterator();
+			while( listIterator.hasNext()) {
+				if ( listIterator.next().getId().equals(service.getId())) {
+					listIterator.remove();
+					System.out.println("*********** Removed Service:"+service.getName()+" Key:"+service.getEndpoint()+" To Registry:"+serviceRegistry.hashCode());
+
+					return;
+				}
+			}	
+		}
+	}
+	public synchronized UimaASService lookupById(String serviceId) {
+		Iterator<Entry<String, List<UimaASService>>> iterator = 
+				serviceRegistry.entrySet().iterator();
+		
+		while( iterator.hasNext() ) {
+			Iterator<UimaASService> listIterator = iterator.next().getValue().iterator();
+			while( listIterator.hasNext()) {
+				UimaASService service = listIterator.next();
+				if ( service.getId().equals(serviceId)) {
+					return service;
+				}
+			}	
+
+		}
+		throw new ServiceNotFoundException("Service with id "+serviceId+" not found in ServiceRegistry");
+	}
+	public synchronized UimaASService lookupByEndpoint(String serviceEndpoint) {
+		Iterator<Entry<String, List<UimaASService>>> iterator = 
+				serviceRegistry.entrySet().iterator();
+		
+		while( iterator.hasNext() ) {
+			Iterator<UimaASService> listIterator = iterator.next().getValue().iterator();
+			while( listIterator.hasNext()) {
+				UimaASService service = listIterator.next();
+				if ( service.getEndpoint().equals(serviceEndpoint)) {
+					return service;
+				}
+			}	
+		}
+		throw new ServiceNotFoundException("Service with name "+serviceEndpoint+" not found in ServiceRegistry");
+	}
+	public synchronized Map<String, List<UimaASService>> getServiceList() {
+		return serviceRegistry;
+	}
+	public synchronized void clear() {
+		serviceRegistry.clear();
+	}
+
+}