1
2
3
4
5
6
7
8
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
31
32
33 public class RmiRegistryAgent extends AbstractAgent
34 {
35
36
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-server");
53 }
54
55 public String getDescription()
56 {
57 return "Rmi Registry: " + serverUri;
58 }
59
60 public void registered()
61 {
62
63 }
64
65 public void unregistered()
66 {
67
68 }
69
70 public void start() throws MuleException
71 {
72 if (serverUri == null)
73 {
74 throw new InitialisationException(MessageFactory.createStaticMessage("serverUri has not been set, this agent has not been initialized properly."), this);
75 }
76
77 URI uri;
78 try
79 {
80 uri = new URI(serverUri);
81 }
82 catch (URISyntaxException e)
83 {
84 throw new InitialisationException(e, this);
85 }
86
87 if (rmiRegistry == null)
88 {
89 try
90 {
91 if (createRegistry)
92 {
93 try
94 {
95 rmiRegistry = LocateRegistry.createRegistry(uri.getPort());
96 }
97 catch (ExportException e)
98 {
99 logger.info("Registry on " + serverUri
100 + " already bound. Attempting to use that instead");
101 rmiRegistry = LocateRegistry.getRegistry(uri.getHost(), uri.getPort());
102 }
103 }
104 else
105 {
106 rmiRegistry = LocateRegistry.getRegistry(uri.getHost(), uri.getPort());
107 }
108 }
109 catch (RemoteException e)
110 {
111 throw new InitialisationException(e, this);
112 }
113 }
114 }
115
116 public void stop() throws MuleException
117 {
118
119 rmiRegistry = null;
120 }
121
122 public void dispose()
123 {
124
125 }
126
127 public void initialise() throws InitialisationException
128 {
129 if (StringUtils.isBlank(serverUri))
130 {
131 String theHost = StringUtils.defaultIfEmpty(host, DEFAULT_HOSTNAME);
132 String thePort = StringUtils.defaultIfEmpty(port, String.valueOf(DEFAULT_PORT));
133 serverUri = PROTOCOL_PREFIX + theHost + ":" + thePort;
134 }
135 }
136
137 public Registry getRmiRegistry()
138 {
139 return rmiRegistry;
140 }
141
142 public void setRmiRegistry(Registry rmiRegistry)
143 {
144 this.rmiRegistry = rmiRegistry;
145 }
146
147 public String getServerUri()
148 {
149 return serverUri;
150 }
151
152 public void setServerUri(String serverUri)
153 {
154 this.serverUri = serverUri;
155 }
156
157 public boolean isCreateRegistry()
158 {
159 return createRegistry;
160 }
161
162 public void setCreateRegistry(boolean createRegistry)
163 {
164 this.createRegistry = createRegistry;
165 }
166
167
168 public String getHost()
169 {
170 return host;
171 }
172
173 public void setHost(String host)
174 {
175 this.host = host;
176 }
177
178
179 public String getPort()
180 {
181 return port;
182 }
183
184 public void setPort(String port)
185 {
186 this.port = port;
187 }
188 }