001: /*
002: * Copyright (C) The MX4J Contributors.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the MX4J License version 1.0.
006: * See the terms of the MX4J License in the documentation provided with this software.
007: */
008: package mx4j.examples.tools.adaptor.http;
009:
010: import javax.management.Attribute;
011: import javax.management.JMException;
012: import javax.management.MBeanNotificationInfo;
013: import javax.management.MBeanServer;
014: import javax.management.MBeanServerFactory;
015: import javax.management.MBeanServerInvocationHandler;
016: import javax.management.NotificationBroadcasterSupport;
017: import javax.management.ObjectName;
018:
019: import mx4j.tools.adaptor.ssl.SSLAdaptorServerSocketFactoryMBean;
020:
021: /**
022: * Example as how to use the HttpAdaptor and the XSLTProcessor with
023: * SSL support. This example assumes that you have created a keystore
024: * as described in the documentation.
025: *
026: * @version $Revision: 1.1 $
027: */
028: public class SSLHttpAdaptor {
029: private int port = 8080;
030:
031: private String host = "localhost";
032:
033: private String path = null, pathInJar = null;
034:
035: private static interface TestClassMBean {
036: public String getStr();
037:
038: public Double getDouble();
039:
040: public boolean isTrue();
041:
042: public void setStr(String str);
043:
044: public Boolean aMethod(String string);
045:
046: public void anotherMethod(String string, int test);
047: }
048:
049: public static class TestClass extends
050: NotificationBroadcasterSupport implements TestClassMBean {
051: private String str;
052:
053: public TestClass(String str) {
054: this .str = str;
055: }
056:
057: public String getStr() {
058: return str;
059: }
060:
061: public void setStr(String str) {
062: this .str = str;
063: }
064:
065: public Double getDouble() {
066: return new Double(0);
067: }
068:
069: public boolean isTrue() {
070: return true;
071: }
072:
073: public Boolean aMethod(String string) {
074: return new Boolean(string.equals("true"));
075: }
076:
077: public void anotherMethod(String string, int test) {
078: this .str = string;
079: }
080:
081: public MBeanNotificationInfo[] getNotificationInfo() {
082: MBeanNotificationInfo[] notifications = new MBeanNotificationInfo[1];
083: notifications[0] = new MBeanNotificationInfo(new String[] {
084: "test1", "test2" }, "name", "test");
085: return notifications;
086: }
087:
088: }
089:
090: /**
091: * Creates a new SSLHttpAdaptor example. You can optionally pass the host/port as
092: * java -cp CLASSPATH adaptor.http.HttpAdaptor localhost 8080 path
093: */
094: public SSLHttpAdaptor(String args[]) {
095: if (args.length > 0) {
096: host = args[0];
097: }
098: if (args.length > 1) {
099: port = Integer.parseInt(args[1]);
100: }
101: if (args.length > 2) {
102: path = args[2];
103: }
104: if (args.length > 3) {
105: pathInJar = args[3];
106: }
107: }
108:
109: /**
110: * Starts the http server
111: */
112: public void start() throws JMException {
113: // creates new server
114: MBeanServer server = MBeanServerFactory
115: .createMBeanServer("test");
116: ObjectName serverName = new ObjectName("Http:name=HttpAdaptor");
117: server.createMBean("mx4j.tools.adaptor.http.HttpAdaptor",
118: serverName, null);
119: // set attributes
120: if (port > 0) {
121: server.setAttribute(serverName, new Attribute("Port",
122: new Integer(port)));
123: } else {
124: System.out.println("Incorrect port value " + port);
125: }
126: if (host != null) {
127: server
128: .setAttribute(serverName, new Attribute("Host",
129: host));
130: } else {
131: System.out.println("Incorrect null hostname");
132: }
133: // set the XSLTProcessor. If you want to use pure XML comment this out
134: ObjectName processorName = new ObjectName(
135: "Http:name=XSLTProcessor");
136: server.createMBean("mx4j.tools.adaptor.http.XSLTProcessor",
137: processorName, null);
138: if (path != null) {
139: server.setAttribute(processorName, new Attribute("File",
140: path));
141: }
142: server.setAttribute(processorName, new Attribute("UseCache",
143: new Boolean(false)));
144: if (pathInJar != null) {
145: server.setAttribute(processorName, new Attribute(
146: "PathInJar", pathInJar));
147: }
148: server.setAttribute(serverName, new Attribute("ProcessorName",
149: processorName));
150:
151: // add a couple of MBeans
152: TestClass test1 = new TestClass("t1");
153: TestClass test2 = new TestClass("t2");
154: server.registerMBean(test1, new ObjectName("Test:name=test1"));
155: server.registerMBean(test2, new ObjectName("Test:name=test2"));
156:
157: // add user names
158: //server.invoke(serverName, "addAuthorization", new Object[] {"mx4j", "mx4j"}, new String[] {"java.lang.String", "java.lang.String"});
159:
160: // use basic authentication
161: //server.setAttribute(serverName, new Attribute("AuthenticationMethod", "basic"));
162:
163: // SSL support
164: ObjectName sslFactory = new ObjectName(
165: "Adaptor:service=SSLServerSocketFactory");
166: server.createMBean(
167: "mx4j.tools.adaptor.ssl.SSLAdaptorServerSocketFactory",
168: sslFactory, null);
169:
170: SSLAdaptorServerSocketFactoryMBean factory = (SSLAdaptorServerSocketFactoryMBean) MBeanServerInvocationHandler
171: .newProxyInstance(server, sslFactory,
172: SSLAdaptorServerSocketFactoryMBean.class, false);
173: // Customize the values below
174: factory.setKeyStoreName("certs");
175: factory.setKeyStorePassword("mx4j");
176:
177: server.setAttribute(serverName, new Attribute(
178: "SocketFactoryName", sslFactory));
179:
180: // starts the server
181: server.invoke(serverName, "start", null, null);
182: }
183:
184: public static void main(String[] str) throws JMException {
185: SSLHttpAdaptor adaptor = new SSLHttpAdaptor(str);
186: adaptor.start();
187: }
188: }
|