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