1
2
3
4
5
6
7
8
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
30
31
32 public class RmiRegistryAgent implements UMOAgent
33 {
34
35
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
68 }
69
70 public void unregistered()
71 {
72
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
120 rmiRegistry = null;
121 }
122
123 public void dispose()
124 {
125
126 }
127
128 public void initialise() throws InitialisationException {
129 if (StringUtils.isNotBlank(serverUri))
130 {
131 return;
132 }
133
134 if (StringUtils.isNotBlank(host) && StringUtils.isNotBlank(port))
135 {
136 serverUri = PROTOCOL_PREFIX + host + ":" + port;
137 }
138 else
139 {
140 serverUri = DEFAULT_SERVER_URI;
141 }
142
143 }
144
145 public Registry getRmiRegistry()
146 {
147 return rmiRegistry;
148 }
149
150 public void setRmiRegistry(Registry rmiRegistry)
151 {
152 this.rmiRegistry = rmiRegistry;
153 }
154
155 public String getServerUri()
156 {
157 return serverUri;
158 }
159
160 public void setServerUri(String serverUri)
161 {
162 this.serverUri = serverUri;
163 }
164
165 public boolean isCreateRegistry()
166 {
167 return createRegistry;
168 }
169
170 public void setCreateRegistry(boolean createRegistry)
171 {
172 this.createRegistry = createRegistry;
173 }
174
175
176 public String getHost()
177 {
178 return host;
179 }
180
181 public void setHost(String host)
182 {
183 this.host = host;
184 }
185
186
187 public String getPort()
188 {
189 return port;
190 }
191
192 public void setPort(String port)
193 {
194 this.port = port;
195 }
196 }