Coverage Report - org.mule.management.agents.RmiRegistryAgent
 
Classes in this File Line Coverage Branch Coverage Complexity
RmiRegistryAgent
0%
0/50
0%
0/4
1.526
 
 1  
 /*
 2  
  * $Id: RmiRegistryAgent.java 7976 2007-08-21 14:26:13Z 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.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  0
 public class RmiRegistryAgent implements UMOAgent
 33  
 {
 34  
     /**
 35  
      * logger used by this class
 36  
      */
 37  0
     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  0
     private String name = "RMI Agent";
 44  
     private Registry rmiRegistry;
 45  
     private String serverUri;
 46  
     private String host;
 47  
     private String port;
 48  0
     private boolean createRegistry = true;
 49  
 
 50  
     public String getName()
 51  
     {
 52  0
         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  0
         return "Rmi Registry: " + serverUri;
 63  
     }
 64  
 
 65  
     public void registered()
 66  
     {
 67  
         // nothing to do
 68  0
     }
 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  0
             uri = new URI(serverUri);
 82  
         }
 83  0
         catch (URISyntaxException e)
 84  
         {
 85  0
             throw new InitialisationException(e, this);
 86  0
         }
 87  
 
 88  0
         if (rmiRegistry == null)
 89  
         {
 90  
             try
 91  
             {
 92  0
                 if (createRegistry)
 93  
                 {
 94  
                     try
 95  
                     {
 96  0
                         rmiRegistry = LocateRegistry.createRegistry(uri.getPort());
 97  
                     }
 98  0
                     catch (ExportException e)
 99  
                     {
 100  0
                         logger.info("Registry on " + serverUri
 101  
                                     + " already bound. Attempting to use that instead");
 102  0
                         rmiRegistry = LocateRegistry.getRegistry(uri.getHost(), uri.getPort());
 103  0
                     }
 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  0
             }
 114  
         }
 115  0
     }
 116  
 
 117  
     public void stop() throws UMOException
 118  
     {
 119  
         // TODO how do you unbind a registry??
 120  0
         rmiRegistry = null;
 121  0
     }
 122  
 
 123  
     public void dispose()
 124  
     {
 125  
         // nothing to do
 126  0
     }
 127  
 
 128  
     public void initialise() throws InitialisationException {
 129  0
         if (StringUtils.isNotBlank(serverUri))
 130  
         {
 131  0
             return;
 132  
         }
 133  
 
 134  0
         if (StringUtils.isNotBlank(host) && StringUtils.isNotBlank(port))
 135  
         {
 136  0
             serverUri = PROTOCOL_PREFIX + host + ":" + port;
 137  
         }
 138  
         else
 139  
         {
 140  0
             serverUri = DEFAULT_SERVER_URI;
 141  
         }
 142  
 
 143  0
     }
 144  
 
 145  
     public Registry getRmiRegistry()
 146  
     {
 147  0
         return rmiRegistry;
 148  
     }
 149  
 
 150  
     public void setRmiRegistry(Registry rmiRegistry)
 151  
     {
 152  0
         this.rmiRegistry = rmiRegistry;
 153  0
     }
 154  
 
 155  
     public String getServerUri()
 156  
     {
 157  0
         return serverUri;
 158  
     }
 159  
 
 160  
     public void setServerUri(String serverUri)
 161  
     {
 162  0
         this.serverUri = serverUri;
 163  0
     }
 164  
 
 165  
     public boolean isCreateRegistry()
 166  
     {
 167  0
         return createRegistry;
 168  
     }
 169  
 
 170  
     public void setCreateRegistry(boolean createRegistry)
 171  
     {
 172  0
         this.createRegistry = createRegistry;
 173  0
     }
 174  
 
 175  
 
 176  
     public String getHost()
 177  
     {
 178  0
         return host;
 179  
     }
 180  
 
 181  
     public void setHost(String host)
 182  
     {
 183  0
         this.host = host;
 184  0
     }
 185  
 
 186  
 
 187  
     public String getPort()
 188  
     {
 189  0
         return port;
 190  
     }
 191  
 
 192  
     public void setPort(String port)
 193  
     {
 194  0
         this.port = port;
 195  0
     }
 196  
 }