001: /*
002: * Created on Jun 6, 2005
003: *
004: * Listen server initialization and shutdown in order
005: * to handle events needed for HTML and/or RMI/JRMP
006: * adaptor registration/shutdown.
007: */
008: package org.enhydra.listener;
009:
010: import org.apache.catalina.LifecycleEvent;
011: import org.apache.catalina.LifecycleListener;
012:
013: import javax.management.MBeanServer;
014: import javax.management.ObjectName;
015: import javax.management.Attribute;
016: import javax.management.MBeanServerFactory;
017:
018: import java.io.IOException;
019: import java.io.File;
020: import java.io.FileInputStream;
021: import java.io.FileOutputStream;
022:
023: import java.util.Properties;
024:
025: import java.util.HashMap;
026: import mx4j.adaptor.rmi.jrmp.JRMPAdaptor;
027: import mx4j.tools.adaptor.security.interceptor.UserPasswordAdaptorInterceptor;
028: import mx4j.security.SimplePasswordStore;
029:
030: /**
031: * @author Slobodan Vujasinovic
032: */
033: public class AdaptorListener implements LifecycleListener {
034:
035: MBeanServer mserver;
036:
037: private int httpport = -1;
038: private int jrmpport = -1;
039:
040: private String httphost = "localhost";
041: private String jrmphost = "localhost";
042:
043: private String authmode = "none";
044: private String authuser = null;
045: private String authpassword = null;
046:
047: private boolean useXSLTProcessor = true;
048:
049: private String propFile = null;
050: private File propsF = null;
051: Properties props = new Properties();
052:
053: ObjectName httpServerName = null;
054: ObjectName jrmpServerName = null;
055:
056: JRMPAdaptor adaptor = null;
057:
058: /**
059: *
060: */
061: public AdaptorListener() {
062: mserver = getMBeanServer();
063: }
064:
065: /** Enable the MX4J HTTP internal adapter
066: */
067: public void setHttpPort(int i) {
068: httpport = i;
069: }
070:
071: public int getHttpPort() {
072: return httpport;
073: }
074:
075: public void setHttpHost(String host) {
076: this .httphost = host;
077: }
078:
079: public String getHttpHost() {
080: return httphost;
081: }
082:
083: public boolean getUseXSLTProcessor() {
084: return useXSLTProcessor;
085: }
086:
087: public void setUseXSLTProcessor(boolean uxsltp) {
088: useXSLTProcessor = uxsltp;
089: }
090:
091: /*
092: * Enable the MX4J JRMP internal adapter
093: */
094: public void setJrmpPort(int i) {
095: jrmpport = i;
096: }
097:
098: public int getJrmpPort() {
099: return jrmpport;
100: }
101:
102: public void setJrmpHost(String host) {
103: this .jrmphost = host;
104: }
105:
106: public String getJrmpHost() {
107: return jrmphost;
108: }
109:
110: /*
111: * Enable the MX4J JRMP internal adapter
112: */
113: public void setAuthMode(String mode) {
114: authmode = mode;
115: }
116:
117: public String getAuthMode() {
118: return authmode;
119: }
120:
121: public void setAuthUser(String user) {
122: authuser = user;
123: }
124:
125: public String getAuthUser() {
126: return authuser;
127: }
128:
129: public void setAuthPassword(String password) {
130: authpassword = password;
131: }
132:
133: public String getAuthPassword() {
134: return authpassword;
135: }
136:
137: /** Load a .properties file into and set the values
138: * into adaptor configuration.
139: */
140: public void setPropertiesFile(String p) {
141: propFile = p;
142: loadPropertiesFile();
143: }
144:
145: public String getPropertiesFile() {
146: return propFile;
147: }
148:
149: private boolean checkPropertiesFile() {
150: if (propFile == null) {
151: return false;
152: }
153: propsF = new File(propFile);
154: if (!propsF.isAbsolute()) {
155: String home = System.getProperty("catalina.base");
156: if (home == null) {
157: return false;
158: }
159: propsF = new File(home, propFile);
160: }
161: return propsF.exists();
162: }
163:
164: private void loadPropertiesFile() {
165: if (!checkPropertiesFile()) {
166: return;
167: }
168: try {
169: props.load(new FileInputStream(propsF));
170: if (props.containsKey("jrmpPort")) {
171: setJrmpPort(new Integer(getProperty("jrmpPort"))
172: .intValue());
173: }
174: if (props.containsKey("jrmpHost")) {
175: setJrmpHost(getProperty("jrmpHost"));
176: }
177: if (props.containsKey("httpPort")) {
178: setHttpPort(new Integer(getProperty("httpPort"))
179: .intValue());
180: }
181: if (props.containsKey("httpHost")) {
182: setHttpHost(getProperty("httpHost"));
183: }
184: if (props.containsKey("authMode")) {
185: setAuthMode(getProperty("authMode"));
186: }
187: if (props.containsKey("authUser")) {
188: setAuthUser(getProperty("authUser"));
189: }
190: if (props.containsKey("authPassword")) {
191: setAuthPassword(getProperty("authPassword"));
192: }
193: } catch (IOException ex) {
194:
195: }
196: }
197:
198: public void saveProperties() {
199: if (propsF == null) {
200: return;
201: }
202: File outFile = new File(propsF.getParentFile(), propsF
203: .getName()
204: + ".save");
205: try {
206: props.store(new FileOutputStream(outFile),
207: "AUTOMATICALLY GENERATED");
208: } catch (IOException ex) {
209: }
210: }
211:
212: /**
213: * Set a name/value as a property
214: */
215: public void setProperty(String n, String v) {
216: props.put(n, v);
217: saveProperties();
218: }
219:
220: /**
221: * Retrieve a property.
222: */
223: public String getProperty(String name) {
224: String result = null;
225: if (name != null) {
226: result = (String) props.get(name);
227: }
228: return result;
229: }
230:
231: /**
232: * Listener method implementation
233: */
234: public void lifecycleEvent(LifecycleEvent arg0) {
235: if ("after_start".equals(arg0.getType())) {
236: registerAdaptors();
237: } else if ("after_stop".equals(arg0.getType())) {
238: unRegisterAdaptors();
239: }
240: }
241:
242: private void registerAdaptors() {
243: loadHttpAdaptor();
244: loadRmiJrmpAdaptor();
245: }
246:
247: private void unRegisterAdaptors() {
248: try {
249: if (httpServerName != null) {
250: mserver.invoke(httpServerName, "stop", null, null);
251: }
252: if (jrmpServerName != null) {
253: mserver.invoke(jrmpServerName, "stop", null, null);
254: }
255: if (adaptor != null) {
256: adaptor.stop();
257: }
258: } catch (Throwable t) {
259:
260: }
261: }
262:
263: private MBeanServer getMBeanServer() {
264: MBeanServer server;
265: if (MBeanServerFactory.findMBeanServer(null).size() > 0) {
266: server = (MBeanServer) MBeanServerFactory.findMBeanServer(
267: null).get(0);
268: } else {
269: server = MBeanServerFactory.createMBeanServer();
270: }
271: return (server);
272: }
273:
274: private void registerObject(String className, ObjectName oName)
275: throws Exception {
276: Class c = Class.forName(className);
277: Object o = c.newInstance();
278: mserver.registerMBean(o, oName);
279: }
280:
281: private void loadHttpAdaptor() {
282: boolean httpAdapterLoaded = false;
283: if (httpport != -1) {
284: try {
285: httpServerName = new ObjectName("Http:name=HttpAdaptor");
286: if (!mserver.isRegistered(httpServerName)) {
287: registerObject("mx4j.adaptor.http.HttpAdaptor",
288: httpServerName);
289: }
290:
291: if (httphost != null) {
292: mserver.setAttribute(httpServerName, new Attribute(
293: "Host", httphost));
294: }
295: mserver.setAttribute(httpServerName, new Attribute(
296: "Port", new Integer(httpport)));
297:
298: if ("none".equals(authmode) || "basic".equals(authmode)
299: || "digest".equals(authmode))
300: mserver.setAttribute(httpServerName, new Attribute(
301: "AuthenticationMethod", authmode));
302:
303: if (authuser != null && authpassword != null) {
304: mserver.invoke(httpServerName, "addAuthorization",
305: new Object[] { authuser, authpassword },
306: new String[] { "java.lang.String",
307: "java.lang.String" });
308: }
309:
310: if (useXSLTProcessor) {
311: ObjectName processorName = new ObjectName(
312: "Http:name=XSLTProcessor");
313: if (!mserver.isRegistered(processorName)) {
314: registerObject(
315: "mx4j.adaptor.http.XSLTProcessor",
316: processorName);
317: }
318: mserver.setAttribute(httpServerName, new Attribute(
319: "ProcessorName", processorName));
320: }
321:
322: // starts the server
323: mserver.invoke(httpServerName, "start", null, null);
324:
325: httpAdapterLoaded = true;
326:
327: } catch (Throwable t) {
328: httpServerName = null;
329: }
330: }
331:
332: }
333:
334: private void loadRmiJrmpAdaptor() {
335: boolean jrmpAdapterLoaded = false;
336: if (jrmpport != -1) {
337: try {
338: ObjectName jrmpServerName = new ObjectName(
339: "Naming:name=rmiregistry");
340: if (!mserver.isRegistered(jrmpServerName)) {
341: registerObject("mx4j.tools.naming.NamingService",
342: jrmpServerName);
343: }
344:
345: mserver.setAttribute(jrmpServerName, new Attribute(
346: "Port", new Integer(jrmpport)));
347: mserver.invoke(jrmpServerName, "start", null, null);
348:
349: // Create the JRMP adaptor
350: adaptor = new JRMPAdaptor();
351: adaptor
352: .putJNDIProperty(
353: javax.naming.Context.INITIAL_CONTEXT_FACTORY,
354: "com.sun.jndi.rmi.registry.RegistryContextFactory");
355: adaptor
356: .putNamingProperty(
357: javax.naming.Context.INITIAL_CONTEXT_FACTORY,
358: "com.sun.jndi.rmi.registry.RegistryContextFactory");
359: ObjectName adaptorName = new ObjectName(
360: "Adaptor:protocol=JRMP");
361: String jndiName = "jrmp";
362: adaptor.setJNDIName(jndiName);
363: adaptor.putJNDIProperty(
364: javax.naming.Context.PROVIDER_URL, "rmi://"
365: + jrmphost + ":" + jrmpport);
366: adaptor.putNamingProperty(
367: javax.naming.Context.PROVIDER_URL, "rmi://"
368: + jrmphost + ":" + jrmpport);
369:
370: if (!"none".equals(authmode)) {
371: String user = null;
372: char[] password = null;
373: if (authuser != null) {
374: user = authuser;
375: } else {
376: user = "";
377: }
378: if (authpassword != null) {
379: password = authpassword.toCharArray();
380: } else {
381: password = "".toCharArray();
382: }
383:
384: HashMap map = new HashMap();
385: map.put(user, password);
386: UserPasswordAdaptorInterceptor intr = new UserPasswordAdaptorInterceptor(
387: map, new SimplePasswordStore(map));
388: ObjectName interceptorName = new ObjectName(
389: "AdaptorInterceptor:type=timing");
390: intr.setObjectName(interceptorName);
391: intr.setEnabled(true);
392: adaptor.addInterceptor(intr);
393: }
394:
395: // Registers the JRMP adaptor in JNDI and starts it
396: adaptor.setMBeanServer(mserver);
397: adaptor.start();
398:
399: jrmpAdapterLoaded = true;
400:
401: } catch (Exception ex) {
402: jrmpServerName = null;
403: adaptor = null;
404: }
405: }
406:
407: }
408:
409: }
|