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:
009: package mx4j.examples.tools.adaptor.http;
010:
011: import java.math.BigDecimal;
012: import java.math.BigInteger;
013: import java.net.MalformedURLException;
014: import java.net.URL;
015: import java.util.ArrayList;
016: import java.util.Date;
017: import java.util.HashMap;
018: import java.util.List;
019: import java.util.Map;
020: import javax.management.Attribute;
021: import javax.management.JMException;
022: import javax.management.MBeanNotificationInfo;
023: import javax.management.MBeanServer;
024: import javax.management.MBeanServerFactory;
025: import javax.management.NotificationBroadcasterSupport;
026: import javax.management.ObjectName;
027: import javax.management.openmbean.CompositeData;
028: import javax.management.openmbean.CompositeDataSupport;
029: import javax.management.openmbean.CompositeType;
030: import javax.management.openmbean.OpenDataException;
031: import javax.management.openmbean.OpenType;
032: import javax.management.openmbean.SimpleType;
033:
034: import mx4j.tools.stats.TimedStatisticsRecorder;
035:
036: /**
037: * Example as how to use the HttpAdaptor and the XSLTProcessor
038: *
039: * @version $Revision: 1.1 $
040: */
041: public class HttpAdaptor {
042: private int port = 8080;
043:
044: private String host = "localhost";
045:
046: private String path = null, pathInJar = null;
047:
048: public static interface TestClassMBean {
049: public URL getURL();
050:
051: public void setURL(URL url);
052:
053: public String getStr();
054:
055: public String[] getStrArray();
056:
057: public Double getDouble();
058:
059: public boolean isTrue();
060:
061: public void setStr(String str);
062:
063: public void setStrArray(String[] str);
064:
065: public Boolean aMethod(String string);
066:
067: public void anotherMethod(String string, int test);
068:
069: public Map getaMap();
070:
071: public List getaList();
072:
073: public Date getDate();
074:
075: public void setDate(Date date);
076:
077: public BigInteger getBigInteger();
078:
079: public void setBigInteger(BigInteger integer);
080:
081: public BigDecimal getBigDecimal();
082:
083: public void setBigDecimal(BigDecimal decimal);
084:
085: public CompositeData getCompositeData();
086:
087: public void setCompositeData(CompositeData composite);
088: }
089:
090: public static class TestClass extends
091: NotificationBroadcasterSupport implements TestClassMBean {
092: private String[] strArray = new String[] { "first", "second" };
093: private String str;
094: private URL url;
095: private List list = new ArrayList();
096: private Map map = new HashMap();
097: private Date date = new Date();
098: private BigInteger bigInteger = new BigInteger(
099: "123456789101112131415");
100: private BigDecimal bigDecimal = new BigDecimal(
101: "123456789101112131415.987654321");
102: private CompositeData compositeData = null;
103:
104: public TestClass(String str, URL url) {
105: this .str = str;
106: this .url = url;
107: list.add("a");
108: list.add("b");
109: list.add("c");
110: map.put("1", "a");
111: map.put("2", "b");
112: map.put("3", "c");
113: try {
114: CompositeType type = new CompositeType("My type",
115: "My type", new String[] { "item1", "item2" },
116: new String[] { "item1", "item2" },
117: new OpenType[] { SimpleType.STRING,
118: SimpleType.STRING });
119: compositeData = new CompositeDataSupport(type,
120: new String[] { "item1", "item2" },
121: new Object[] { "item value 1", "item value 2" });
122: } catch (OpenDataException e) {
123: e.printStackTrace();
124: }
125:
126: }
127:
128: public void setCompositeData(CompositeData compositeData) {
129: this .compositeData = compositeData;
130: }
131:
132: public CompositeData getCompositeData() {
133: return compositeData;
134: }
135:
136: public void setBigInteger(BigInteger bigInteger) {
137: this .bigInteger = bigInteger;
138: }
139:
140: public BigInteger getBigInteger() {
141: return bigInteger;
142: }
143:
144: public void setBigDecimal(BigDecimal bigDecimal) {
145: this .bigDecimal = bigDecimal;
146: }
147:
148: public BigDecimal getBigDecimal() {
149: return bigDecimal;
150: }
151:
152: public void setDate(Date date) {
153: this .date = date;
154: }
155:
156: public Date getDate() {
157: return date;
158: }
159:
160: public void setURL(URL url) {
161: this .url = url;
162: }
163:
164: public URL getURL() {
165: return url;
166: }
167:
168: public String getStr() {
169: return str;
170: }
171:
172: public void setStr(String str) {
173: this .str = str;
174: }
175:
176: public String[] getStrArray() {
177: return strArray;
178: }
179:
180: public void setStrArray(String[] strArray) {
181: this .strArray = strArray;
182: }
183:
184: public Double getDouble() {
185: return new Double(100 * Math.random());
186: }
187:
188: public boolean isTrue() {
189: return true;
190: }
191:
192: public Boolean aMethod(String string) {
193: return new Boolean(string.equals("true"));
194: }
195:
196: public void anotherMethod(String string, int test) {
197: this .str = string;
198: }
199:
200: public MBeanNotificationInfo[] getNotificationInfo() {
201: MBeanNotificationInfo[] notifications = new MBeanNotificationInfo[1];
202: notifications[0] = new MBeanNotificationInfo(new String[] {
203: "test1", "test2" }, "name", "test");
204: return notifications;
205: }
206:
207: public Map getaMap() {
208: return map;
209: }
210:
211: public List getaList() {
212: return list;
213: }
214:
215: }
216:
217: /**
218: * Creates a new HttpAdaptor example. You can optionally pass the host/port as
219: * java -cp CLASSPATH adaptor.http.HttpAdaptor localhost 8080 path
220: */
221: public HttpAdaptor(String args[]) {
222: if (args.length > 0) {
223: host = args[0];
224: }
225: if (args.length > 1) {
226: port = Integer.parseInt(args[1]);
227: }
228: if (args.length > 2) {
229: path = args[2];
230: }
231: if (args.length > 3) {
232: pathInJar = args[3];
233: }
234: }
235:
236: /**
237: * Starts the http server
238: */
239: public void start() throws JMException, MalformedURLException {
240: // creates new server
241: MBeanServer server = MBeanServerFactory
242: .createMBeanServer("test");
243: ObjectName serverName = new ObjectName("Http:name=HttpAdaptor");
244: server.createMBean("mx4j.tools.adaptor.http.HttpAdaptor",
245: serverName, null);
246: // set attributes
247: if (port > 0) {
248: server.setAttribute(serverName, new Attribute("Port",
249: new Integer(port)));
250: } else {
251: System.out.println("Incorrect port value " + port);
252: }
253: if (host != null) {
254: server
255: .setAttribute(serverName, new Attribute("Host",
256: host));
257: } else {
258: System.out.println("Incorrect null hostname");
259: }
260: // set the XSLTProcessor. If you want to use pure XML comment this out
261: ObjectName processorName = new ObjectName(
262: "Http:name=XSLTProcessor");
263: server.createMBean("mx4j.tools.adaptor.http.XSLTProcessor",
264: processorName, null);
265: if (path != null) {
266: server.setAttribute(processorName, new Attribute("File",
267: path));
268: }
269: server.setAttribute(processorName, new Attribute("UseCache",
270: new Boolean(false)));
271: if (pathInJar != null) {
272: server.setAttribute(processorName, new Attribute(
273: "PathInJar", pathInJar));
274: }
275: server.setAttribute(serverName, new Attribute("ProcessorName",
276: processorName));
277:
278: // add a couple of MBeans
279: TestClass test1 = new TestClass("t1", new URL(
280: "http://mx4j.sourceforge.net"));
281: TestClass test2 = new TestClass("t1", new URL(
282: "http://www.sourceforge.net/projects/mx4j"));
283: server.registerMBean(test1, new ObjectName("Test:name=test1"));
284: server.registerMBean(test2, new ObjectName("Test:name=test2"));
285:
286: // add a stats MBean
287: TimedStatisticsRecorder recoder = new TimedStatisticsRecorder();
288: recoder.setObservedObject(new ObjectName("Test:name=test1"));
289: recoder.setObservedAttribute("Double");
290: server.registerMBean(recoder, new ObjectName(
291: "Test:name=test1recorder"));
292: server.invoke(new ObjectName("Test:name=test1recorder"),
293: "start", null, null);
294:
295: // add a couple of MBeans
296:
297: // add user names
298: server.invoke(serverName, "addAuthorization", new Object[] {
299: "mx4j", "mx4j" }, new String[] { "java.lang.String",
300: "java.lang.String" });
301:
302: // use basic authentication
303: //server.setAttribute(serverName, new Attribute("AuthenticationMethod", "basic"));
304:
305: // starts the server
306: server.invoke(serverName, "start", null, null);
307: }
308:
309: public static void main(String[] str) throws Exception {
310: HttpAdaptor adaptor = new HttpAdaptor(str);
311: adaptor.start();
312: }
313: }
|