KickJava   Java API By Example, From Geeks To Geeks.

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


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
9 package mx4j.examples.tools.adaptor.http;
10
11 import java.math.BigDecimal JavaDoc;
12 import java.math.BigInteger JavaDoc;
13 import java.net.MalformedURLException JavaDoc;
14 import java.net.URL JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.Date JavaDoc;
17 import java.util.HashMap JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Map JavaDoc;
20 import javax.management.Attribute JavaDoc;
21 import javax.management.JMException JavaDoc;
22 import javax.management.MBeanNotificationInfo JavaDoc;
23 import javax.management.MBeanServer JavaDoc;
24 import javax.management.MBeanServerFactory JavaDoc;
25 import javax.management.NotificationBroadcasterSupport JavaDoc;
26 import javax.management.ObjectName JavaDoc;
27 import javax.management.openmbean.CompositeData JavaDoc;
28 import javax.management.openmbean.CompositeDataSupport JavaDoc;
29 import javax.management.openmbean.CompositeType JavaDoc;
30 import javax.management.openmbean.OpenDataException JavaDoc;
31 import javax.management.openmbean.OpenType JavaDoc;
32 import javax.management.openmbean.SimpleType JavaDoc;
33
34 import mx4j.tools.stats.TimedStatisticsRecorder;
35
36 /**
37  * Example as how to use the HttpAdaptor and the XSLTProcessor
38  *
39  * @version $Revision: 1.4 $
40  */

41 public class HttpAdaptor
42 {
43    private int port = 8080;
44
45    private String JavaDoc host = "localhost";
46
47    private String JavaDoc path = null, pathInJar = null;
48
49    public static interface TestClassMBean
50    {
51       public URL JavaDoc getURL();
52
53       public void setURL(URL JavaDoc url);
54
55       public String JavaDoc getStr();
56
57       public String JavaDoc[] getStrArray();
58
59       public Double JavaDoc getDouble();
60
61       public boolean isTrue();
62
63       public void setStr(String JavaDoc str);
64
65       public void setStrArray(String JavaDoc[] str);
66
67       public Boolean JavaDoc aMethod(String JavaDoc string);
68
69       public void anotherMethod(String JavaDoc string, int test);
70
71       public Map JavaDoc getaMap();
72
73       public List JavaDoc getaList();
74
75       public Date JavaDoc getDate();
76
77       public void setDate(Date JavaDoc date);
78
79       public BigInteger JavaDoc getBigInteger();
80
81       public void setBigInteger(BigInteger JavaDoc integer);
82
83       public BigDecimal JavaDoc getBigDecimal();
84
85       public void setBigDecimal(BigDecimal JavaDoc decimal);
86
87       public CompositeData JavaDoc getCompositeData();
88
89       public void setCompositeData(CompositeData JavaDoc composite);
90    }
91
92    public static class TestClass extends NotificationBroadcasterSupport JavaDoc implements TestClassMBean
93    {
94       private String JavaDoc[] strArray = new String JavaDoc[]{"first", "second"};
95       private String JavaDoc str;
96       private URL JavaDoc url;
97       private List JavaDoc list = new ArrayList JavaDoc();
98       private Map JavaDoc map = new HashMap JavaDoc();
99       private Date JavaDoc date = new Date JavaDoc();
100       private BigInteger JavaDoc bigInteger = new BigInteger JavaDoc("123456789101112131415");
101       private BigDecimal JavaDoc bigDecimal = new BigDecimal JavaDoc("123456789101112131415.987654321");
102       private CompositeData JavaDoc compositeData = null;
103
104       public TestClass(String JavaDoc str, URL JavaDoc url)
105       {
106          this.str = str;
107          this.url = url;
108          list.add("a");
109          list.add("b");
110          list.add("c");
111          map.put("1", "a");
112          map.put("2", "b");
113          map.put("3", "c");
114          try
115          {
116             CompositeType JavaDoc type = new CompositeType JavaDoc("My type",
117                                                    "My type",
118                                                    new String JavaDoc[]{"item1", "item2"},
119                                                    new String JavaDoc[]{"item1", "item2"},
120                                                    new OpenType JavaDoc[]{SimpleType.STRING, SimpleType.STRING});
121             compositeData = new CompositeDataSupport JavaDoc(type, new String JavaDoc[]{"item1", "item2"}, new Object JavaDoc[]{"item value 1", "item value 2"});
122          }
123          catch (OpenDataException JavaDoc e)
124          {
125             e.printStackTrace();
126          }
127
128       }
129
130       public void setCompositeData(CompositeData JavaDoc compositeData)
131       {
132          this.compositeData = compositeData;
133       }
134
135       public CompositeData JavaDoc getCompositeData()
136       {
137          return compositeData;
138       }
139
140       public void setBigInteger(BigInteger JavaDoc bigInteger)
141       {
142          this.bigInteger = bigInteger;
143       }
144
145       public BigInteger JavaDoc getBigInteger()
146       {
147          return bigInteger;
148       }
149
150       public void setBigDecimal(BigDecimal JavaDoc bigDecimal)
151       {
152          this.bigDecimal = bigDecimal;
153       }
154
155       public BigDecimal JavaDoc getBigDecimal()
156       {
157          return bigDecimal;
158       }
159
160       public void setDate(Date JavaDoc date)
161       {
162          this.date = date;
163       }
164
165       public Date JavaDoc getDate()
166       {
167          return date;
168       }
169
170       public void setURL(URL JavaDoc url)
171       {
172          this.url = url;
173       }
174
175       public URL JavaDoc getURL()
176       {
177          return url;
178       }
179
180       public String JavaDoc getStr()
181       {
182          return str;
183       }
184
185       public void setStr(String JavaDoc str)
186       {
187          this.str = str;
188       }
189
190       public String JavaDoc[] getStrArray()
191       {
192          return strArray;
193       }
194
195       public void setStrArray(String JavaDoc[] strArray)
196       {
197          this.strArray = strArray;
198       }
199
200       public Double JavaDoc getDouble()
201       {
202          return new Double JavaDoc(100 * Math.random());
203       }
204
205       public boolean isTrue()
206       {
207          return true;
208       }
209
210       public Boolean JavaDoc aMethod(String JavaDoc string)
211       {
212          return new Boolean JavaDoc(string.equals("true"));
213       }
214
215       public void anotherMethod(String JavaDoc string, int test)
216       {
217          this.str = string;
218       }
219
220       public MBeanNotificationInfo JavaDoc[] getNotificationInfo()
221       {
222          MBeanNotificationInfo JavaDoc[] notifications = new MBeanNotificationInfo JavaDoc[1];
223          notifications[0] = new MBeanNotificationInfo JavaDoc(new String JavaDoc[]{"test1"
224                                                                    , "test2"}, "name", "test");
225          return notifications;
226       }
227
228       public Map JavaDoc getaMap()
229       {
230          return map;
231       }
232
233       public List JavaDoc getaList()
234       {
235          return list;
236       }
237
238    }
239
240    /**
241     * Creates a new HttpAdaptor example. You can optionally pass the host/port as
242     * java -cp CLASSPATH adaptor.http.HttpAdaptor localhost 8080 path
243     */

244    public HttpAdaptor(String JavaDoc args[])
245    {
246       if (args.length > 0)
247       {
248          host = args[0];
249       }
250       if (args.length > 1)
251       {
252          port = Integer.parseInt(args[1]);
253       }
254       if (args.length > 2)
255       {
256          path = args[2];
257       }
258       if (args.length > 3)
259       {
260          pathInJar = args[3];
261       }
262    }
263
264    /**
265     * Starts the http server
266     */

267    public void start() throws JMException JavaDoc, MalformedURLException JavaDoc
268    {
269       // creates new server
270
MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer("test");
271       ObjectName JavaDoc serverName = new ObjectName JavaDoc("Http:name=HttpAdaptor");
272       server.createMBean("mx4j.tools.adaptor.http.HttpAdaptor", serverName, null);
273       // set attributes
274
if (port > 0)
275       {
276          server.setAttribute(serverName, new Attribute JavaDoc("Port", new Integer JavaDoc(port)));
277       }
278       else
279       {
280          System.out.println("Incorrect port value " + port);
281       }
282       if (host != null)
283       {
284          server.setAttribute(serverName, new Attribute JavaDoc("Host", host));
285       }
286       else
287       {
288          System.out.println("Incorrect null hostname");
289       }
290       // set the XSLTProcessor. If you want to use pure XML comment this out
291
ObjectName JavaDoc processorName = new ObjectName JavaDoc("Http:name=XSLTProcessor");
292       server.createMBean("mx4j.tools.adaptor.http.XSLTProcessor", processorName, null);
293       if (path != null)
294       {
295          server.setAttribute(processorName, new Attribute JavaDoc("File", path));
296       }
297       server.setAttribute(processorName, new Attribute JavaDoc("UseCache", new Boolean JavaDoc(false)));
298       if (pathInJar != null)
299       {
300          server.setAttribute(processorName, new Attribute JavaDoc("PathInJar", pathInJar));
301       }
302       server.setAttribute(serverName, new Attribute JavaDoc("ProcessorName", processorName));
303
304       // add a couple of MBeans
305
TestClass test1 = new TestClass("t1", new URL JavaDoc("http://mx4j.sourceforge.net"));
306       TestClass test2 = new TestClass("t1", new URL JavaDoc("http://www.sourceforge.net/projects/mx4j"));
307       server.registerMBean(test1, new ObjectName JavaDoc("Test:name=test1"));
308       server.registerMBean(test2, new ObjectName JavaDoc("Test:name=test2"));
309
310       // add a stats MBean
311
TimedStatisticsRecorder recoder = new TimedStatisticsRecorder();
312       recoder.setObservedObject(new ObjectName JavaDoc("Test:name=test1"));
313       recoder.setObservedAttribute("Double");
314       server.registerMBean(recoder, new ObjectName JavaDoc("Test:name=test1recorder"));
315       server.invoke(new ObjectName JavaDoc("Test:name=test1recorder"), "start", null, null);
316
317       // add a couple of MBeans
318

319       // add user names
320
server.invoke(serverName, "addAuthorization", new Object JavaDoc[]{"mx4j", "mx4j"}, new String JavaDoc[]{"java.lang.String", "java.lang.String"});
321
322       // use basic authentication
323
//server.setAttribute(serverName, new Attribute("AuthenticationMethod", "basic"));
324

325       // starts the server
326
server.invoke(serverName, "start", null, null);
327    }
328
329    public static void main(String JavaDoc[] str) throws Exception JavaDoc
330    {
331       HttpAdaptor adaptor = new HttpAdaptor(str);
332       adaptor.start();
333    }
334 }
335
Popular Tags