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