View Javadoc

1   /*
2    * $Id: RmiRegistryAgent.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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      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      private boolean createRegistry = true;
49  
50      public RmiRegistryAgent()
51      {
52          super("rmi-registry");
53      }
54  
55      public String getDescription()
56      {
57          return "Rmi Registry: " + serverUri;
58      }
59  
60  
61      public void start() throws MuleException
62      {
63          if (serverUri == null)
64          {
65              throw new InitialisationException(MessageFactory.createStaticMessage("serverUri has not been set, this agent has not been initialized properly."), this);
66          }
67          
68          URI uri;
69          try
70          {
71              uri = new URI(serverUri);
72          }
73          catch (URISyntaxException e)
74          {
75              throw new InitialisationException(e, this);
76          }
77  
78          if (rmiRegistry == null)
79          {
80              try
81              {
82                  if (createRegistry)
83                  {
84                      try
85                      {
86                          rmiRegistry = LocateRegistry.createRegistry(uri.getPort());
87                      }
88                      catch (ExportException e)
89                      {
90                          logger.info("Registry on " + serverUri
91                                      + " already bound. Attempting to use that instead");
92                          rmiRegistry = LocateRegistry.getRegistry(uri.getHost(), uri.getPort());
93                      }
94                  }
95                  else
96                  {
97                      rmiRegistry = LocateRegistry.getRegistry(uri.getHost(), uri.getPort());
98                  }
99              }
100             catch (RemoteException e)
101             {
102                 throw new InitialisationException(e, this);
103             }
104         }
105     }
106 
107     public void stop() throws MuleException
108     {
109         // TODO how do you unbind a registry??
110         rmiRegistry = null;
111     }
112 
113     public void dispose()
114     {
115         // nothing to do
116     }
117 
118     public void initialise() throws InitialisationException
119     {
120         if (StringUtils.isBlank(serverUri))
121         {
122             String theHost = StringUtils.defaultIfEmpty(host, DEFAULT_HOSTNAME);
123             String thePort = StringUtils.defaultIfEmpty(port, String.valueOf(DEFAULT_PORT));
124             serverUri = PROTOCOL_PREFIX + theHost + ":" + thePort;
125         }
126     }
127 
128     public Registry getRmiRegistry()
129     {
130         return rmiRegistry;
131     }
132 
133     public void setRmiRegistry(Registry rmiRegistry)
134     {
135         this.rmiRegistry = rmiRegistry;
136     }
137 
138     public String getServerUri()
139     {
140         return serverUri;
141     }
142 
143     public void setServerUri(String serverUri)
144     {
145         this.serverUri = serverUri;
146     }
147 
148     public boolean isCreateRegistry()
149     {
150         return createRegistry;
151     }
152 
153     public void setCreateRegistry(boolean createRegistry)
154     {
155         this.createRegistry = createRegistry;
156     }
157 
158 
159     public String getHost()
160     {
161         return host;
162     }
163 
164     public void setHost(String host)
165     {
166         this.host = host;
167     }
168 
169 
170     public String getPort()
171     {
172         return port;
173     }
174 
175     public void setPort(String port)
176     {
177         this.port = port;
178     }
179 }