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