1 8 9 package mx4j.examples.tools.adaptor.http; 10 11 import java.math.BigDecimal ; 12 import java.math.BigInteger ; 13 import java.net.MalformedURLException ; 14 import java.net.URL ; 15 import java.util.ArrayList ; 16 import java.util.Date ; 17 import java.util.HashMap ; 18 import java.util.List ; 19 import java.util.Map ; 20 import javax.management.Attribute ; 21 import javax.management.JMException ; 22 import javax.management.MBeanNotificationInfo ; 23 import javax.management.MBeanServer ; 24 import javax.management.MBeanServerFactory ; 25 import javax.management.NotificationBroadcasterSupport ; 26 import javax.management.ObjectName ; 27 import javax.management.openmbean.CompositeData ; 28 import javax.management.openmbean.CompositeDataSupport ; 29 import javax.management.openmbean.CompositeType ; 30 import javax.management.openmbean.OpenDataException ; 31 import javax.management.openmbean.OpenType ; 32 import javax.management.openmbean.SimpleType ; 33 34 import mx4j.tools.stats.TimedStatisticsRecorder; 35 36 41 public class HttpAdaptor 42 { 43 private int port = 8080; 44 45 private String host = "localhost"; 46 47 private String path = null, pathInJar = null; 48 49 public static interface TestClassMBean 50 { 51 public URL getURL(); 52 53 public void setURL(URL url); 54 55 public String getStr(); 56 57 public String [] getStrArray(); 58 59 public Double getDouble(); 60 61 public boolean isTrue(); 62 63 public void setStr(String str); 64 65 public void setStrArray(String [] str); 66 67 public Boolean aMethod(String string); 68 69 public void anotherMethod(String string, int test); 70 71 public Map getaMap(); 72 73 public List getaList(); 74 75 public Date getDate(); 76 77 public void setDate(Date date); 78 79 public BigInteger getBigInteger(); 80 81 public void setBigInteger(BigInteger integer); 82 83 public BigDecimal getBigDecimal(); 84 85 public void setBigDecimal(BigDecimal decimal); 86 87 public CompositeData getCompositeData(); 88 89 public void setCompositeData(CompositeData composite); 90 } 91 92 public static class TestClass extends NotificationBroadcasterSupport implements TestClassMBean 93 { 94 private String [] strArray = new String []{"first", "second"}; 95 private String str; 96 private URL url; 97 private List list = new ArrayList (); 98 private Map map = new HashMap (); 99 private Date date = new Date (); 100 private BigInteger bigInteger = new BigInteger ("123456789101112131415"); 101 private BigDecimal bigDecimal = new BigDecimal ("123456789101112131415.987654321"); 102 private CompositeData compositeData = null; 103 104 public TestClass(String str, URL 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 type = new CompositeType ("My type", 117 "My type", 118 new String []{"item1", "item2"}, 119 new String []{"item1", "item2"}, 120 new OpenType []{SimpleType.STRING, SimpleType.STRING}); 121 compositeData = new CompositeDataSupport (type, new String []{"item1", "item2"}, new Object []{"item value 1", "item value 2"}); 122 } 123 catch (OpenDataException e) 124 { 125 e.printStackTrace(); 126 } 127 128 } 129 130 public void setCompositeData(CompositeData compositeData) 131 { 132 this.compositeData = compositeData; 133 } 134 135 public CompositeData getCompositeData() 136 { 137 return compositeData; 138 } 139 140 public void setBigInteger(BigInteger bigInteger) 141 { 142 this.bigInteger = bigInteger; 143 } 144 145 public BigInteger getBigInteger() 146 { 147 return bigInteger; 148 } 149 150 public void setBigDecimal(BigDecimal bigDecimal) 151 { 152 this.bigDecimal = bigDecimal; 153 } 154 155 public BigDecimal getBigDecimal() 156 { 157 return bigDecimal; 158 } 159 160 public void setDate(Date date) 161 { 162 this.date = date; 163 } 164 165 public Date getDate() 166 { 167 return date; 168 } 169 170 public void setURL(URL url) 171 { 172 this.url = url; 173 } 174 175 public URL getURL() 176 { 177 return url; 178 } 179 180 public String getStr() 181 { 182 return str; 183 } 184 185 public void setStr(String str) 186 { 187 this.str = str; 188 } 189 190 public String [] getStrArray() 191 { 192 return strArray; 193 } 194 195 public void setStrArray(String [] strArray) 196 { 197 this.strArray = strArray; 198 } 199 200 public Double getDouble() 201 { 202 return new Double (100 * Math.random()); 203 } 204 205 public boolean isTrue() 206 { 207 return true; 208 } 209 210 public Boolean aMethod(String string) 211 { 212 return new Boolean (string.equals("true")); 213 } 214 215 public void anotherMethod(String string, int test) 216 { 217 this.str = string; 218 } 219 220 public MBeanNotificationInfo [] getNotificationInfo() 221 { 222 MBeanNotificationInfo [] notifications = new MBeanNotificationInfo [1]; 223 notifications[0] = new MBeanNotificationInfo (new String []{"test1" 224 , "test2"}, "name", "test"); 225 return notifications; 226 } 227 228 public Map getaMap() 229 { 230 return map; 231 } 232 233 public List getaList() 234 { 235 return list; 236 } 237 238 } 239 240 244 public HttpAdaptor(String 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 267 public void start() throws JMException , MalformedURLException 268 { 269 MBeanServer server = MBeanServerFactory.createMBeanServer("test"); 271 ObjectName serverName = new ObjectName ("Http:name=HttpAdaptor"); 272 server.createMBean("mx4j.tools.adaptor.http.HttpAdaptor", serverName, null); 273 if (port > 0) 275 { 276 server.setAttribute(serverName, new Attribute ("Port", new Integer (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 ("Host", host)); 285 } 286 else 287 { 288 System.out.println("Incorrect null hostname"); 289 } 290 ObjectName processorName = new ObjectName ("Http:name=XSLTProcessor"); 292 server.createMBean("mx4j.tools.adaptor.http.XSLTProcessor", processorName, null); 293 if (path != null) 294 { 295 server.setAttribute(processorName, new Attribute ("File", path)); 296 } 297 server.setAttribute(processorName, new Attribute ("UseCache", new Boolean (false))); 298 if (pathInJar != null) 299 { 300 server.setAttribute(processorName, new Attribute ("PathInJar", pathInJar)); 301 } 302 server.setAttribute(serverName, new Attribute ("ProcessorName", processorName)); 303 304 TestClass test1 = new TestClass("t1", new URL ("http://mx4j.sourceforge.net")); 306 TestClass test2 = new TestClass("t1", new URL ("http://www.sourceforge.net/projects/mx4j")); 307 server.registerMBean(test1, new ObjectName ("Test:name=test1")); 308 server.registerMBean(test2, new ObjectName ("Test:name=test2")); 309 310 TimedStatisticsRecorder recoder = new TimedStatisticsRecorder(); 312 recoder.setObservedObject(new ObjectName ("Test:name=test1")); 313 recoder.setObservedAttribute("Double"); 314 server.registerMBean(recoder, new ObjectName ("Test:name=test1recorder")); 315 server.invoke(new ObjectName ("Test:name=test1recorder"), "start", null, null); 316 317 319 server.invoke(serverName, "addAuthorization", new Object []{"mx4j", "mx4j"}, new String []{"java.lang.String", "java.lang.String"}); 321 322 325 server.invoke(serverName, "start", null, null); 327 } 328 329 public static void main(String [] str) throws Exception 330 { 331 HttpAdaptor adaptor = new HttpAdaptor(str); 332 adaptor.start(); 333 } 334 } 335 | Popular Tags |