View Javadoc

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  public class RmiRegistryAgent implements UMOAgent
33  {
34      /**
35       * logger used by this class
36       */
37      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      private String name = "RMI Agent";
44      private Registry rmiRegistry;
45      private String serverUri;
46      private String host;
47      private String port;
48      private boolean createRegistry = true;
49  
50      public String getName()
51      {
52          return name;
53      }
54  
55      public void setName(String name)
56      {
57          this.name = name;
58      }
59  
60      public String getDescription()
61      {
62          return "Rmi Registry: " + serverUri;
63      }
64  
65      public void registered()
66      {
67          // nothing to do
68      }
69  
70      public void unregistered()
71      {
72          // nothing to do
73      }
74  
75      public void start() throws UMOException
76      {
77          URI uri;
78  
79          try
80          {
81              uri = new URI(serverUri);
82          }
83          catch (URISyntaxException e)
84          {
85              throw new InitialisationException(e, this);
86          }
87  
88          if (rmiRegistry == null)
89          {
90              try
91              {
92                  if (createRegistry)
93                  {
94                      try
95                      {
96                          rmiRegistry = LocateRegistry.createRegistry(uri.getPort());
97                      }
98                      catch (ExportException e)
99                      {
100                         logger.info("Registry on " + serverUri
101                                     + " already bound. Attempting to use that instead");
102                         rmiRegistry = LocateRegistry.getRegistry(uri.getHost(), uri.getPort());
103                     }
104                 }
105                 else
106                 {
107                     rmiRegistry = LocateRegistry.getRegistry(uri.getHost(), uri.getPort());
108                 }
109             }
110             catch (RemoteException e)
111             {
112                 throw new InitialisationException(e, this);
113             }
114         }
115     }
116 
117     public void stop() throws UMOException
118     {
119         // TODO how do you unbind a registry??
120         rmiRegistry = null;
121     }
122 
123     public void dispose()
124     {
125         // nothing to do
126     }
127 
128     public void initialise() throws InitialisationException {
129         if (StringUtils.isNotBlank(serverUri))
130         {
131             return;
132         }
133 
134         String theHost = StringUtils.defaultIfEmpty(host, DEFAULT_HOSTNAME);
135         String thePort = StringUtils.defaultIfEmpty(port, String.valueOf(DEFAULT_PORT));
136 
137         serverUri = PROTOCOL_PREFIX + theHost + ":" + thePort;
138     }
139 
140     public Registry getRmiRegistry()
141     {
142         return rmiRegistry;
143     }
144 
145     public void setRmiRegistry(Registry rmiRegistry)
146     {
147         this.rmiRegistry = rmiRegistry;
148     }
149 
150     public String getServerUri()
151     {
152         return serverUri;
153     }
154 
155     public void setServerUri(String serverUri)
156     {
157         this.serverUri = serverUri;
158     }
159 
160     public boolean isCreateRegistry()
161     {
162         return createRegistry;
163     }
164 
165     public void setCreateRegistry(boolean createRegistry)
166     {
167         this.createRegistry = createRegistry;
168     }
169 
170 
171     public String getHost()
172     {
173         return host;
174     }
175 
176     public void setHost(String host)
177     {
178         this.host = host;
179     }
180 
181 
182     public String getPort()
183     {
184         return port;
185     }
186 
187     public void setPort(String port)
188     {
189         this.port = port;
190     }
191 }