Author: kstam
Date: Wed Aug 28 12:48:44 2013
New Revision: 1518186

URL: http://svn.apache.org/r1518186
Log:
JUDDI-497 adding mbean for the service cache

Modified:
    
juddi/trunk/juddi-client/src/main/java/org/apache/juddi/v3/client/config/UDDIClient.java
    
juddi/trunk/juddi-client/src/main/java/org/apache/juddi/v3/client/mapping/ServiceLocator.java
    
juddi/trunk/juddi-client/src/main/java/org/apache/juddi/v3/client/mapping/UDDIServiceCache.java

Modified: 
juddi/trunk/juddi-client/src/main/java/org/apache/juddi/v3/client/config/UDDIClient.java
URL: 
http://svn.apache.org/viewvc/juddi/trunk/juddi-client/src/main/java/org/apache/juddi/v3/client/config/UDDIClient.java?rev=1518186&r1=1518185&r2=1518186&view=diff
==============================================================================
--- 
juddi/trunk/juddi-client/src/main/java/org/apache/juddi/v3/client/config/UDDIClient.java
 (original)
+++ 
juddi/trunk/juddi-client/src/main/java/org/apache/juddi/v3/client/config/UDDIClient.java
 Wed Aug 28 12:48:44 2013
@@ -18,6 +18,7 @@ package org.apache.juddi.v3.client.confi
 
 import java.rmi.RemoteException;
 import java.util.Collection;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Properties;
@@ -31,6 +32,8 @@ import org.apache.commons.logging.LogFac
 import org.apache.juddi.v3.annotations.AnnotationProcessor;
 import org.apache.juddi.v3.client.ClassUtil;
 import org.apache.juddi.v3.client.embed.EmbeddedRegistry;
+import org.apache.juddi.v3.client.mapping.ServiceLocator;
+import org.apache.juddi.v3.client.mapping.URLLocalizerDefaultImpl;
 import org.apache.juddi.v3.client.transport.InVMTransport;
 import org.apache.juddi.v3.client.transport.Transport;
 import org.apache.juddi.v3.client.transport.TransportException;
@@ -43,12 +46,36 @@ public class UDDIClient {
     private ClientConfig clientConfig = null;
     private String CONFIG_FILE = "META-INF/uddi.xml";
     private Properties properties = null;
+    private static Map<String,ServiceLocator> serviceLocators = new 
HashMap<String,ServiceLocator>();
        
     public UDDIClient() throws ConfigurationException {
        super();
                clientConfig = new ClientConfig(CONFIG_FILE, properties);
                UDDIClientContainer.addClient(this);
     }
+    
+    /**
+     * Uses the client config, and looks for a clerk called "default"
+     * @return
+     * @throws ConfigurationException
+     */
+    public synchronized ServiceLocator getServiceLocator() throws 
ConfigurationException {
+       return getServiceLocator(null);
+    }
+    /**
+     * @param clerkName - if null defaults to "default"
+     * @return
+     * @throws ConfigurationException
+     */
+    public synchronized ServiceLocator getServiceLocator(String clerkName) 
throws ConfigurationException 
+    {
+       UDDIClerk clerk = getClerk(clerkName);
+       if (! serviceLocators.containsKey(clerk.getName())) {
+               ServiceLocator serviceLocator = new ServiceLocator(clerk, new 
URLLocalizerDefaultImpl(), properties);
+           serviceLocators.put(clerk.getName(), serviceLocator);
+       }
+       return serviceLocators.get(clerk.getName());
+    }
        /**
         * Manages the clerks. Initiates reading the client configuration from 
the uddi.xml.
         * @throws ConfigurationException 

Modified: 
juddi/trunk/juddi-client/src/main/java/org/apache/juddi/v3/client/mapping/ServiceLocator.java
URL: 
http://svn.apache.org/viewvc/juddi/trunk/juddi-client/src/main/java/org/apache/juddi/v3/client/mapping/ServiceLocator.java?rev=1518186&r1=1518185&r2=1518186&view=diff
==============================================================================
--- 
juddi/trunk/juddi-client/src/main/java/org/apache/juddi/v3/client/mapping/ServiceLocator.java
 (original)
+++ 
juddi/trunk/juddi-client/src/main/java/org/apache/juddi/v3/client/mapping/ServiceLocator.java
 Wed Aug 28 12:48:44 2013
@@ -50,18 +50,22 @@ public class ServiceLocator {
        private UDDIServiceCache serviceCache = null;
        private SelectionPolicy selectionPolicy = null;
        
-       public ServiceLocator(UDDIClerk clerk, URLLocalizer urlLocalizer, 
Properties properties) throws ClassNotFoundException, IllegalArgumentException, 
SecurityException, InstantiationException, IllegalAccessException, 
InvocationTargetException, NoSuchMethodException, 
DatatypeConfigurationException, MalformedURLException, RemoteException, 
ConfigurationException, WSDLException, TransportException, Exception {
+       public ServiceLocator(UDDIClerk clerk, URLLocalizer urlLocalizer, 
Properties properties) throws ConfigurationException  {
                super();
 
-               this.clerk = clerk;
-               this.properties = properties;
-               
-               serviceCache = new UDDIServiceCache(clerk, urlLocalizer, 
properties);
-               String policy = 
properties.getProperty("juddi.client.selection.policy", 
"org.apache.juddi.v3.client.mapping.PolicyLocalFirst");
-               @SuppressWarnings("unchecked")
-               Class<? extends SelectionPolicy> selectionPolicyClass = 
(Class<? extends SelectionPolicy>)
-                       ClassUtil.forName(policy, this.getClass());
-               selectionPolicy =  
selectionPolicyClass.getConstructor(Properties.class).newInstance(properties);
+               try {
+                       this.clerk = clerk;
+                       this.properties = properties;
+                       
+                       serviceCache = new UDDIServiceCache(clerk, 
urlLocalizer, properties);
+                       String policy = 
properties.getProperty("juddi.client.selection.policy", 
"org.apache.juddi.v3.client.mapping.PolicyLocalFirst");
+                       @SuppressWarnings("unchecked")
+                       Class<? extends SelectionPolicy> selectionPolicyClass = 
(Class<? extends SelectionPolicy>)
+                               ClassUtil.forName(policy, this.getClass());
+                       selectionPolicy =  
selectionPolicyClass.getConstructor(Properties.class).newInstance(properties);
+               } catch (Exception e) {
+                       throw new ConfigurationException(e.getMessage(),e);
+               }
        }
        
        public void shutdown() throws RemoteException, ConfigurationException, 
TransportException {

Modified: 
juddi/trunk/juddi-client/src/main/java/org/apache/juddi/v3/client/mapping/UDDIServiceCache.java
URL: 
http://svn.apache.org/viewvc/juddi/trunk/juddi-client/src/main/java/org/apache/juddi/v3/client/mapping/UDDIServiceCache.java?rev=1518186&r1=1518185&r2=1518186&view=diff
==============================================================================
--- 
juddi/trunk/juddi-client/src/main/java/org/apache/juddi/v3/client/mapping/UDDIServiceCache.java
 (original)
+++ 
juddi/trunk/juddi-client/src/main/java/org/apache/juddi/v3/client/mapping/UDDIServiceCache.java
 Wed Aug 28 12:48:44 2013
@@ -14,14 +14,22 @@
  */
 package org.apache.juddi.v3.client.mapping;
 
+import java.lang.management.ManagementFactory;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.rmi.RemoteException;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Properties;
+import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 
+import javax.management.InstanceAlreadyExistsException;
+import javax.management.MBeanRegistrationException;
+import javax.management.MBeanServer;
+import javax.management.MBeanServerFactory;
+import javax.management.NotCompliantMBeanException;
+import javax.management.ObjectName;
 import javax.wsdl.Definition;
 import javax.wsdl.WSDLException;
 import javax.xml.datatype.DatatypeConfigurationException;
@@ -33,7 +41,6 @@ import javax.xml.ws.Endpoint;
 import org.apache.commons.configuration.ConfigurationException;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-import org.apache.juddi.v3.client.config.Property;
 import org.apache.juddi.v3.client.config.UDDIClerk;
 import org.apache.juddi.v3.client.config.UDDIKeyConvention;
 import org.apache.juddi.v3.client.subscription.SubscriptionCallbackListener;
@@ -47,7 +54,7 @@ import org.uddi.sub_v3.SubscriptionFilte
 /**
  * @author <a href="mailto:[email protected]";>Kurt T Stam</a>
  */
-public class UDDIServiceCache {
+public class UDDIServiceCache implements UDDIServiceCacheMBean {
        
        private Endpoint endpoint = null;
        private String bindingKey = null;
@@ -59,6 +66,7 @@ public class UDDIServiceCache {
        URL serviceUrl = null;
        private ConcurrentHashMap<String, Topology> serviceLocationMap = new 
ConcurrentHashMap<String, Topology>();
        private static List<String> endpoints = new ArrayList<String>();
+       private ObjectName mbeanName = null;
        
        public UDDIClerk getClerk() {
                return clerk;
@@ -74,8 +82,14 @@ public class UDDIServiceCache {
                this.urlLocalizer = urlLocalizer;
                this.properties = properties;
                this.subscriptionKey = 
UDDIKeyConvention.getSubscriptionKey(properties);
-               
+               if (clerk!=null) {
+                       mbeanName = new 
ObjectName("apache.juddi.client:type=UDDIServerCache-" + clerk.getManagerName() 
+ "-" + clerk.getName());
+               } else {
+                       mbeanName = new 
ObjectName("apache.juddi.client:type=UDDIServerCache-" + this);
+               }
+               getServer().registerMBean(this, mbeanName);
                init();
+               
        }
 
        private void init() throws DatatypeConfigurationException, 
MalformedURLException, WSDLException, RemoteException, ConfigurationException, 
TransportException, Exception {
@@ -171,6 +185,61 @@ public class UDDIServiceCache {
        public void unRegisterSubscription() {
                clerk.unRegisterSubscription(subscriptionKey);
        }
+
+       @Override
+       public int getServiceCacheSize() {
+               return serviceLocationMap.size();
+       }
+
+       @Override
+       public Set<String> getCacheEntries() {
+               return serviceLocationMap.keySet();
+       }
+
+       @Override
+       public void resetCache() {
+               serviceLocationMap.clear();
+       }
+       
+       protected void registerMBean() {
+        MBeanServer mbeanServer = null;
+        
+        mbeanServer = getServer();
+        if (mbeanServer == null) {
+            try {
+//                mbeanServer = MBeanServerLocator.locateJBoss();
+            } catch (IllegalStateException ise) {
+                // If we can't find a JBoss MBeanServer, just return
+                // Needed for unit tests
+                return;
+            }            
+        }
+        
+        try {
+               if (! mbeanServer.isRegistered(mbeanName))
+                       mbeanServer.registerMBean(this, mbeanName);
+        } catch (InstanceAlreadyExistsException e) {
+            log.warn("", e);
+        } catch (MBeanRegistrationException e) {
+            log.warn("", e);
+        } catch (NotCompliantMBeanException e) {
+            log.warn("", e);
+        }   
+    }
+       
+       private MBeanServer getServer() {
+        MBeanServer mbserver = null;
+        ArrayList<MBeanServer> mbservers = 
MBeanServerFactory.findMBeanServer(null);
+        if (mbservers.size() > 0) {
+            mbserver = (MBeanServer) mbservers.get(0);
+        }
+        if (mbserver != null && log.isDebugEnabled()) {
+               log.debug("Found MBean server");
+        } else {
+               mbserver = ManagementFactory.getPlatformMBeanServer();
+        }
+        return mbserver;
+    }
        
        
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to