Coverage Report - org.mule.module.management.agent.RmiRegistryAgent
 
Classes in this File Line Coverage Branch Coverage Complexity
RmiRegistryAgent
0%
0/46
0%
0/8
1.625
 
 1  
 /*
 2  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 3  
  * The software in this package is published under the terms of the CPAL v1.0
 4  
  * license, a copy of which has been included with this distribution in the
 5  
  * LICENSE.txt file.
 6  
  */
 7  
 package org.mule.module.management.agent;
 8  
 
 9  
 import org.mule.AbstractAgent;
 10  
 import org.mule.api.MuleException;
 11  
 import org.mule.api.lifecycle.InitialisationException;
 12  
 import org.mule.config.i18n.MessageFactory;
 13  
 import org.mule.util.StringUtils;
 14  
 
 15  
 import java.net.URI;
 16  
 import java.net.URISyntaxException;
 17  
 import java.rmi.RemoteException;
 18  
 import java.rmi.registry.LocateRegistry;
 19  
 import java.rmi.registry.Registry;
 20  
 import java.rmi.server.ExportException;
 21  
 
 22  
 import org.apache.commons.logging.Log;
 23  
 import org.apache.commons.logging.LogFactory;
 24  
 
 25  
 /**
 26  
  * Binds to an existing RMI registry or creates a new one on a defined URI. The
 27  
  * default is <code>rmi://localhost:1099</code>
 28  
  */
 29  
 public class RmiRegistryAgent extends AbstractAgent
 30  
 {
 31  
     /**
 32  
      * logger used by this class
 33  
      */
 34  0
     protected transient Log logger = LogFactory.getLog(getClass());
 35  
 
 36  
     public static final String DEFAULT_HOSTNAME = "localhost";
 37  
     public static final int DEFAULT_PORT = 1099;
 38  
     private static final String PROTOCOL_PREFIX = "rmi://";
 39  
     public static final String DEFAULT_SERVER_URI = PROTOCOL_PREFIX + DEFAULT_HOSTNAME + ":" + DEFAULT_PORT;
 40  
     private Registry rmiRegistry;
 41  
     private String serverUri;
 42  
     private String host;
 43  
     private String port;
 44  0
     private boolean createRegistry = true;
 45  
 
 46  
     public RmiRegistryAgent()
 47  
     {
 48  0
         super("rmi-registry");
 49  0
     }
 50  
 
 51  
     public String getDescription()
 52  
     {
 53  0
         return "Rmi Registry: " + serverUri;
 54  
     }
 55  
 
 56  
 
 57  
     public void start() throws MuleException
 58  
     {
 59  0
         if (serverUri == null)
 60  
         {
 61  0
             throw new InitialisationException(MessageFactory.createStaticMessage("serverUri has not been set, this agent has not been initialized properly."), this);
 62  
         }
 63  
         
 64  
         URI uri;
 65  
         try
 66  
         {
 67  0
             uri = new URI(serverUri);
 68  
         }
 69  0
         catch (URISyntaxException e)
 70  
         {
 71  0
             throw new InitialisationException(e, this);
 72  0
         }
 73  
 
 74  0
         if (rmiRegistry == null)
 75  
         {
 76  
             try
 77  
             {
 78  0
                 if (createRegistry)
 79  
                 {
 80  
                     try
 81  
                     {
 82  0
                         rmiRegistry = LocateRegistry.createRegistry(uri.getPort());
 83  
                     }
 84  0
                     catch (ExportException e)
 85  
                     {
 86  0
                         logger.info("Registry on " + serverUri
 87  
                                     + " already bound. Attempting to use that instead");
 88  0
                         rmiRegistry = LocateRegistry.getRegistry(uri.getHost(), uri.getPort());
 89  0
                     }
 90  
                 }
 91  
                 else
 92  
                 {
 93  0
                     rmiRegistry = LocateRegistry.getRegistry(uri.getHost(), uri.getPort());
 94  
                 }
 95  
             }
 96  0
             catch (RemoteException e)
 97  
             {
 98  0
                 throw new InitialisationException(e, this);
 99  0
             }
 100  
         }
 101  0
     }
 102  
 
 103  
     public void stop() throws MuleException
 104  
     {
 105  
         // TODO how do you unbind a registry??
 106  0
         rmiRegistry = null;
 107  0
     }
 108  
 
 109  
     public void dispose()
 110  
     {
 111  
         // nothing to do
 112  0
     }
 113  
 
 114  
     public void initialise() throws InitialisationException
 115  
     {
 116  0
         if (StringUtils.isBlank(serverUri))
 117  
         {
 118  0
             String theHost = StringUtils.defaultIfEmpty(host, DEFAULT_HOSTNAME);
 119  0
             String thePort = StringUtils.defaultIfEmpty(port, String.valueOf(DEFAULT_PORT));
 120  0
             serverUri = PROTOCOL_PREFIX + theHost + ":" + thePort;
 121  
         }
 122  0
     }
 123  
 
 124  
     public Registry getRmiRegistry()
 125  
     {
 126  0
         return rmiRegistry;
 127  
     }
 128  
 
 129  
     public void setRmiRegistry(Registry rmiRegistry)
 130  
     {
 131  0
         this.rmiRegistry = rmiRegistry;
 132  0
     }
 133  
 
 134  
     public String getServerUri()
 135  
     {
 136  0
         return serverUri;
 137  
     }
 138  
 
 139  
     public void setServerUri(String serverUri)
 140  
     {
 141  0
         this.serverUri = serverUri;
 142  0
     }
 143  
 
 144  
     public boolean isCreateRegistry()
 145  
     {
 146  0
         return createRegistry;
 147  
     }
 148  
 
 149  
     public void setCreateRegistry(boolean createRegistry)
 150  
     {
 151  0
         this.createRegistry = createRegistry;
 152  0
     }
 153  
 
 154  
 
 155  
     public String getHost()
 156  
     {
 157  0
         return host;
 158  
     }
 159  
 
 160  
     public void setHost(String host)
 161  
     {
 162  0
         this.host = host;
 163  0
     }
 164  
 
 165  
 
 166  
     public String getPort()
 167  
     {
 168  0
         return port;
 169  
     }
 170  
 
 171  
     public void setPort(String port)
 172  
     {
 173  0
         this.port = port;
 174  0
     }
 175  
 }