KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > examples > tools > adaptor > http > SSLHttpAdaptor


1 /*
2  * Copyright (C) The MX4J Contributors.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8 package mx4j.examples.tools.adaptor.http;
9
10 import javax.management.Attribute JavaDoc;
11 import javax.management.JMException JavaDoc;
12 import javax.management.MBeanNotificationInfo JavaDoc;
13 import javax.management.MBeanServer JavaDoc;
14 import javax.management.MBeanServerFactory JavaDoc;
15 import javax.management.MBeanServerInvocationHandler JavaDoc;
16 import javax.management.NotificationBroadcasterSupport JavaDoc;
17 import javax.management.ObjectName JavaDoc;
18
19 import mx4j.tools.adaptor.ssl.SSLAdaptorServerSocketFactoryMBean;
20
21 /**
22  * Example as how to use the HttpAdaptor and the XSLTProcessor with
23  * SSL support. This example assumes that you have created a keystore
24  * as described in the documentation.
25  *
26  * @version $Revision: 1.4 $
27  */

28 public class SSLHttpAdaptor
29 {
30    private int port = 8080;
31
32    private String JavaDoc host = "localhost";
33
34    private String JavaDoc path = null, pathInJar = null;
35
36    private static interface TestClassMBean
37    {
38       public String JavaDoc getStr();
39
40       public Double JavaDoc getDouble();
41
42       public boolean isTrue();
43
44       public void setStr(String JavaDoc str);
45
46       public Boolean JavaDoc aMethod(String JavaDoc string);
47
48       public void anotherMethod(String JavaDoc string, int test);
49    }
50
51    public static class TestClass extends NotificationBroadcasterSupport JavaDoc implements TestClassMBean
52    {
53       private String JavaDoc str;
54
55       public TestClass(String JavaDoc str)
56       {
57          this.str = str;
58       }
59
60       public String JavaDoc getStr()
61       {
62          return str;
63       }
64
65       public void setStr(String JavaDoc str)
66       {
67          this.str = str;
68       }
69
70       public Double JavaDoc getDouble()
71       {
72          return new Double JavaDoc(0);
73       }
74
75       public boolean isTrue()
76       {
77          return true;
78       }
79
80       public Boolean JavaDoc aMethod(String JavaDoc string)
81       {
82          return new Boolean JavaDoc(string.equals("true"));
83       }
84
85       public void anotherMethod(String JavaDoc string, int test)
86       {
87          this.str = string;
88       }
89
90       public MBeanNotificationInfo JavaDoc[] getNotificationInfo()
91       {
92          MBeanNotificationInfo JavaDoc[] notifications = new MBeanNotificationInfo JavaDoc[1];
93          notifications[0] = new MBeanNotificationInfo JavaDoc(new String JavaDoc[]{"test1"
94                                                                    , "test2"}, "name", "test");
95          return notifications;
96       }
97
98    }
99
100    /**
101     * Creates a new SSLHttpAdaptor example. You can optionally pass the host/port as
102     * java -cp CLASSPATH adaptor.http.HttpAdaptor localhost 8080 path
103     */

104    public SSLHttpAdaptor(String JavaDoc args[])
105    {
106       if (args.length > 0)
107       {
108          host = args[0];
109       }
110       if (args.length > 1)
111       {
112          port = Integer.parseInt(args[1]);
113       }
114       if (args.length > 2)
115       {
116          path = args[2];
117       }
118       if (args.length > 3)
119       {
120          pathInJar = args[3];
121       }
122    }
123
124    /**
125     * Starts the http server
126     */

127    public void start() throws JMException JavaDoc
128    {
129       // creates new server
130
MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer("test");
131       ObjectName JavaDoc serverName = new ObjectName JavaDoc("Http:name=HttpAdaptor");
132       server.createMBean("mx4j.tools.adaptor.http.HttpAdaptor", serverName, null);
133       // set attributes
134
if (port > 0)
135       {
136          server.setAttribute(serverName, new Attribute JavaDoc("Port", new Integer JavaDoc(port)));
137       }
138       else
139       {
140          System.out.println("Incorrect port value " + port);
141       }
142       if (host != null)
143       {
144          server.setAttribute(serverName, new Attribute JavaDoc("Host", host));
145       }
146       else
147       {
148          System.out.println("Incorrect null hostname");
149       }
150       // set the XSLTProcessor. If you want to use pure XML comment this out
151
ObjectName JavaDoc processorName = new ObjectName JavaDoc("Http:name=XSLTProcessor");
152       server.createMBean("mx4j.tools.adaptor.http.XSLTProcessor", processorName, null);
153       if (path != null)
154       {
155          server.setAttribute(processorName, new Attribute JavaDoc("File", path));
156       }
157       server.setAttribute(processorName, new Attribute JavaDoc("UseCache", new Boolean JavaDoc(false)));
158       if (pathInJar != null)
159       {
160          server.setAttribute(processorName, new Attribute JavaDoc("PathInJar", pathInJar));
161       }
162       server.setAttribute(serverName, new Attribute JavaDoc("ProcessorName", processorName));
163
164       // add a couple of MBeans
165
TestClass test1 = new TestClass("t1");
166       TestClass test2 = new TestClass("t2");
167       server.registerMBean(test1, new ObjectName JavaDoc("Test:name=test1"));
168       server.registerMBean(test2, new ObjectName JavaDoc("Test:name=test2"));
169
170       // add user names
171
//server.invoke(serverName, "addAuthorization", new Object[] {"mx4j", "mx4j"}, new String[] {"java.lang.String", "java.lang.String"});
172

173       // use basic authentication
174
//server.setAttribute(serverName, new Attribute("AuthenticationMethod", "basic"));
175

176       // SSL support
177
ObjectName JavaDoc sslFactory = new ObjectName JavaDoc("Adaptor:service=SSLServerSocketFactory");
178       server.createMBean("mx4j.tools.adaptor.ssl.SSLAdaptorServerSocketFactory", sslFactory, null);
179
180       SSLAdaptorServerSocketFactoryMBean factory = (SSLAdaptorServerSocketFactoryMBean)MBeanServerInvocationHandler.newProxyInstance(server, sslFactory, SSLAdaptorServerSocketFactoryMBean.class, false);
181       // Customize the values below
182
factory.setKeyStoreName("certs");
183       factory.setKeyStorePassword("mx4j");
184
185       server.setAttribute(serverName, new Attribute JavaDoc("SocketFactoryName", sslFactory));
186
187       // starts the server
188
server.invoke(serverName, "start", null, null);
189    }
190
191    public static void main(String JavaDoc[] str) throws JMException JavaDoc
192    {
193       SSLHttpAdaptor adaptor = new SSLHttpAdaptor(str);
194       adaptor.start();
195    }
196 }
197
198
Popular Tags