1
2
3
4
5
6
7 package org.mule.module.management.agent;
8
9 import org.mule.AbstractAgent;
10 import org.mule.api.MuleException;
11 import org.mule.api.lifecycle.InitialisationException;
12 import org.mule.config.i18n.MessageFactory;
13 import org.mule.util.StringUtils;
14
15 import java.net.URI;
16 import java.net.URISyntaxException;
17 import java.rmi.RemoteException;
18 import java.rmi.registry.LocateRegistry;
19 import java.rmi.registry.Registry;
20 import java.rmi.server.ExportException;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24
25
26
27
28
29 public class RmiRegistryAgent extends AbstractAgent
30 {
31
32
33
34 protected transient Log logger = LogFactory.getLog(getClass());
35
36 public static final String DEFAULT_HOSTNAME = "localhost";
37 public static final int DEFAULT_PORT = 1099;
38 private static final String PROTOCOL_PREFIX = "rmi://";
39 public static final String DEFAULT_SERVER_URI = PROTOCOL_PREFIX + DEFAULT_HOSTNAME + ":" + DEFAULT_PORT;
40 private Registry rmiRegistry;
41 private String serverUri;
42 private String host;
43 private String port;
44 private boolean createRegistry = true;
45
46 public RmiRegistryAgent()
47 {
48 super("rmi-registry");
49 }
50
51 public String getDescription()
52 {
53 return "Rmi Registry: " + serverUri;
54 }
55
56
57 public void start() throws MuleException
58 {
59 if (serverUri == null)
60 {
61 throw new InitialisationException(MessageFactory.createStaticMessage("serverUri has not been set, this agent has not been initialized properly."), this);
62 }
63
64 URI uri;
65 try
66 {
67 uri = new URI(serverUri);
68 }
69 catch (URISyntaxException e)
70 {
71 throw new InitialisationException(e, this);
72 }
73
74 if (rmiRegistry == null)
75 {
76 try
77 {
78 if (createRegistry)
79 {
80 try
81 {
82 rmiRegistry = LocateRegistry.createRegistry(uri.getPort());
83 }
84 catch (ExportException e)
85 {
86 logger.info("Registry on " + serverUri
87 + " already bound. Attempting to use that instead");
88 rmiRegistry = LocateRegistry.getRegistry(uri.getHost(), uri.getPort());
89 }
90 }
91 else
92 {
93 rmiRegistry = LocateRegistry.getRegistry(uri.getHost(), uri.getPort());
94 }
95 }
96 catch (RemoteException e)
97 {
98 throw new InitialisationException(e, this);
99 }
100 }
101 }
102
103 public void stop() throws MuleException
104 {
105
106 rmiRegistry = null;
107 }
108
109 public void dispose()
110 {
111
112 }
113
114 public void initialise() throws InitialisationException
115 {
116 if (StringUtils.isBlank(serverUri))
117 {
118 String theHost = StringUtils.defaultIfEmpty(host, DEFAULT_HOSTNAME);
119 String thePort = StringUtils.defaultIfEmpty(port, String.valueOf(DEFAULT_PORT));
120 serverUri = PROTOCOL_PREFIX + theHost + ":" + thePort;
121 }
122 }
123
124 public Registry getRmiRegistry()
125 {
126 return rmiRegistry;
127 }
128
129 public void setRmiRegistry(Registry rmiRegistry)
130 {
131 this.rmiRegistry = rmiRegistry;
132 }
133
134 public String getServerUri()
135 {
136 return serverUri;
137 }
138
139 public void setServerUri(String serverUri)
140 {
141 this.serverUri = serverUri;
142 }
143
144 public boolean isCreateRegistry()
145 {
146 return createRegistry;
147 }
148
149 public void setCreateRegistry(boolean createRegistry)
150 {
151 this.createRegistry = createRegistry;
152 }
153
154
155 public String getHost()
156 {
157 return host;
158 }
159
160 public void setHost(String host)
161 {
162 this.host = host;
163 }
164
165
166 public String getPort()
167 {
168 return port;
169 }
170
171 public void setPort(String port)
172 {
173 this.port = port;
174 }
175 }