KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.IOException JavaDoc;
11 import java.util.ArrayList JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.List JavaDoc;
14 import java.util.Set JavaDoc;
15 import javax.management.Attribute JavaDoc;
16 import javax.management.MBeanServer JavaDoc;
17 import javax.management.MBeanServerFactory JavaDoc;
18 import javax.management.MBeanServerInvocationHandler JavaDoc;
19 import javax.management.ObjectInstance JavaDoc;
20 import javax.management.ObjectName JavaDoc;
21 import javax.management.Query JavaDoc;
22 import javax.management.relation.RelationServiceMBean JavaDoc;
23 import javax.management.relation.Role JavaDoc;
24 import javax.management.relation.RoleInfo JavaDoc;
25 import javax.management.relation.RoleList JavaDoc;
26 import javax.management.relation.RoleResult JavaDoc;
27
28 import mx4j.tools.adaptor.http.DefaultProcessor;
29 import mx4j.tools.adaptor.http.XSLTProcessor;
30
31 /**
32  * Example as how to use the HttpAdaptor and the XSLTProcessor and the RelationProcessor
33  * To use this example please add as arguments to the command line in this order <host><port><path to xsl files>
34  *
35  * @version $Revision: 1.3 $
36  */

37 public class RelationServiceAdaptor
38 {
39    private MBeanServer JavaDoc m_server = null;
40    private RelationServiceMBean JavaDoc m_proxy = null;
41    private mx4j.tools.adaptor.http.HttpAdaptor m_adaptor = null;
42    private DefaultProcessor m_processor = null;
43    private XSLTProcessor m_xsltProcessor = null;
44    private ObjectName JavaDoc httpAdaptorObjectName = null;
45    private ObjectName JavaDoc processorName = null;
46    private ObjectName JavaDoc m_relationServiceObjectName = null;
47
48    public RelationServiceAdaptor()
49    {
50       m_server = MBeanServerFactory.createMBeanServer("MyAdaptorTests");
51       m_adaptor = new mx4j.tools.adaptor.http.HttpAdaptor();
52       m_processor = new DefaultProcessor();
53       m_xsltProcessor = new XSLTProcessor();
54    }
55
56    public void startTests(String JavaDoc[] args)
57    {
58       int defaultPort = 1999;
59       String JavaDoc defaultHost = "localhost";
60       String JavaDoc defaultPath = ".";
61       if (args.length > 0)
62       {
63          defaultHost = args[0];
64       }
65       if (args.length > 1)
66       {
67          defaultPort = Integer.parseInt(args[1]);
68       }
69       if (args.length > 2)
70       {
71          defaultPath = args[2];
72       }
73
74       try
75       {
76          System.out.println("Building the objectNames and registering the HttpAdaptor, and XSLTProcessor");
77          // build object names
78
httpAdaptorObjectName = new ObjectName JavaDoc("Server:name=HttpAdaptor");
79          processorName = new ObjectName JavaDoc("processor:name=XSLTProcessor");
80
81          // register adaptor in server
82
m_server.registerMBean(m_adaptor, httpAdaptorObjectName);
83          m_server.registerMBean(m_xsltProcessor, processorName);
84
85          m_server.setAttribute(processorName, new Attribute JavaDoc("File", defaultPath));
86
87          m_adaptor.setPort(defaultPort);
88          m_adaptor.setHost(defaultHost);
89          m_adaptor.setProcessor(m_xsltProcessor);
90          m_adaptor.setAuthenticationMethod("none");
91
92          System.out.println("------------------------------------------- done --------------------------------------------");
93
94          System.out.println("starting the adpator and then checking all is active");
95          m_adaptor.start();
96
97          if (m_adaptor.isActive())
98          {
99             System.out.println("Adaptor is active");
100             System.out.println("The name of the processor: " + m_adaptor.getProcessor().getName());
101          }
102
103          System.out.println("------------------------------------------- done --------------------------------------------");
104          System.out.println("Press enter to register relationService");
105          waitForEnterPressed();
106       }
107       catch (Exception JavaDoc ex)
108       {
109          ex.printStackTrace();
110       }
111    }
112
113    public void buildMBeans()
114    {
115       try
116       {
117          System.out.println("Building and registering the relationService and 6 MBeans");
118          String JavaDoc m_relationServiceClassName = "javax.management.relation.RelationService";
119          m_relationServiceObjectName = new ObjectName JavaDoc("relations:type=" + m_relationServiceClassName + "_1");
120          Object JavaDoc[] params = {new Boolean JavaDoc(true)};
121          String JavaDoc[] signature = {"boolean"};
122          m_server.createMBean(m_relationServiceClassName, m_relationServiceObjectName, null, params, signature);
123
124          // create a proxy
125
m_proxy = (RelationServiceMBean JavaDoc)MBeanServerInvocationHandler.newProxyInstance(m_server, m_relationServiceObjectName, RelationServiceMBean JavaDoc.class, false);
126
127          System.out.println("Press ENTER to register 6 MBeans");
128          waitForEnterPressed();
129          String JavaDoc mbeanClassName = "mx4j.examples.tools.adaptor.http.SimpleStandard";
130          ObjectName JavaDoc mbeanObjectName1 = new ObjectName JavaDoc("domain:type=SimpleStandard_1");
131          ObjectName JavaDoc mbeanObjectName2 = new ObjectName JavaDoc("domain:type=SimpleStandard_2");
132          ObjectName JavaDoc mbeanObjectName3 = new ObjectName JavaDoc("domain:type=SimpleStandard_3");
133          ObjectName JavaDoc mbeanObjectName4 = new ObjectName JavaDoc("domain:type=SimpleStandard_4");
134          ObjectName JavaDoc mbeanObjectName5 = new ObjectName JavaDoc("domain:type=SimpleStandard_5");
135          ObjectName JavaDoc mbeanObjectName6 = new ObjectName JavaDoc("domain:type=SimpleStandard_6");
136
137          m_server.createMBean(mbeanClassName, mbeanObjectName1, null);
138          m_server.createMBean(mbeanClassName, mbeanObjectName2, null);
139          m_server.createMBean(mbeanClassName, mbeanObjectName3, null);
140          m_server.createMBean(mbeanClassName, mbeanObjectName4, null);
141          m_server.createMBean(mbeanClassName, mbeanObjectName5, null);
142          m_server.createMBean(mbeanClassName, mbeanObjectName6, null);
143          System.out.println("------------------------------------------- done --------------------------------------------");
144
145          System.out.println("Creating RoleInfos for RelationType");
146          RoleInfo JavaDoc[] roleInfos = new RoleInfo JavaDoc[2];
147          String JavaDoc roleName1 = "primary";
148          roleInfos[0] = new RoleInfo JavaDoc(roleName1, "mx4j.examples.tools.adaptor.http.SimpleStandard", true, true, 1, 1, null);
149
150          String JavaDoc roleName2 = "secondary";
151          roleInfos[1] = new RoleInfo JavaDoc(roleName2, "mx4j.examples.tools.adaptor.http.SimpleStandard", true, true, 0, -1, null);
152
153          // create a relation type with those role infos
154
String JavaDoc relationTypeName = "Building_relation_view1";
155          m_proxy.createRelationType(relationTypeName, roleInfos);
156
157          // creating more relationTypes to test
158
String JavaDoc relationTypeName2 = "Testing_2";
159          m_proxy.createRelationType(relationTypeName2, roleInfos);
160
161          System.out.println("Creating relationIds for relationTypeName: " + relationTypeName);
162          System.out.println("First create the roles...");
163
164          ArrayList JavaDoc roleValue1 = new ArrayList JavaDoc();
165          roleValue1.add(mbeanObjectName1);
166
167          Role JavaDoc role1 = new Role JavaDoc(roleName1, roleValue1);
168
169          ArrayList JavaDoc roleValue2 = new ArrayList JavaDoc();
170          roleValue2.add(mbeanObjectName2);
171          roleValue2.add(mbeanObjectName3);
172          roleValue2.add(mbeanObjectName4);
173
174          Role JavaDoc role2 = new Role JavaDoc(roleName2, roleValue2);
175          RoleList JavaDoc roleList1 = new RoleList JavaDoc();
176          roleList1.add(role1);
177          roleList1.add(role2);
178
179          /// testing form here
180
ArrayList JavaDoc role5Value = new ArrayList JavaDoc();
181          role5Value.add(mbeanObjectName2);
182
183          Role JavaDoc role5 = new Role JavaDoc(roleName1, role5Value);
184          ArrayList JavaDoc roleValue5 = new ArrayList JavaDoc();
185          roleValue5.add(mbeanObjectName4);
186
187          Role JavaDoc role6 = new Role JavaDoc(roleName2, roleValue5);
188          RoleList JavaDoc roleList5 = new RoleList JavaDoc();
189          roleList5.add(role5);
190          roleList5.add(role6);
191
192          System.out.println("------------------------------------------- done --------------------------------------------");
193          System.out.println("Now create relations with ids:::");
194
195          String JavaDoc relationId1 = "relationId_1";
196          m_proxy.createRelation(relationId1, relationTypeName, roleList1);
197
198          String JavaDoc relationId2 = "relationId_2";
199          m_proxy.createRelation(relationId2, relationTypeName, roleList5);
200
201          String JavaDoc relationId3 = "relationId_3";
202          m_proxy.createRelation(relationId3, relationTypeName, roleList1);
203          System.out.println("------------------------------------------- done --------------------------------------------");
204
205          System.out.println("creating relationIds for relationtypeName: " + relationTypeName2);
206
207          String JavaDoc relationId4 = "relationId_number2_1";
208          m_proxy.createRelation(relationId4, relationTypeName2, roleList1);
209
210          String JavaDoc relationId5 = "relationId_number2_2";
211          m_proxy.createRelation(relationId5, relationTypeName2, roleList1);
212
213          String JavaDoc relationId6 = "relationId_number2_3";
214          m_proxy.createRelation(relationId6, relationTypeName2, roleList1);
215          System.out.println("------------------------------------------- done --------------------------------------------");
216          waitForEnterPressed();
217
218          System.out.println("create a relation MBean and add it in the Relation Service");
219          String JavaDoc relMBeanClassName = "mx4j.examples.tools.adaptor.http.SimpleRelationTestSupport";
220          String JavaDoc relationId7 = "relationId_relationMBean_1";
221          ObjectName JavaDoc relMBeanObjName1 = new ObjectName JavaDoc("relationType:name=RelationTypeSupportInstance");
222          m_server.createMBean(relMBeanClassName, relMBeanObjName1, null,
223                               new Object JavaDoc[]{relationId7, m_relationServiceObjectName, relationTypeName2, roleList1},
224                               new String JavaDoc[]{"java.lang.String", "javax.management.ObjectName", "java.lang.String", "javax.management.relation.RoleList"});
225          m_proxy.addRelation(relMBeanObjName1);
226          System.out.println("------------------------------------------- done --------------------------------------------");
227          waitForEnterPressed();
228          RoleResult JavaDoc result = m_proxy.getAllRoles(relationId1);
229          Iterator JavaDoc i = result.getRoles().iterator();
230          while (i.hasNext())
231          {
232             Role JavaDoc r = (Role JavaDoc)i.next();
233             List JavaDoc l = r.getRoleValue();
234             Iterator JavaDoc j = l.iterator();
235             while (j.hasNext())
236             {
237                ObjectName JavaDoc objName = (ObjectName JavaDoc)j.next();
238                System.out.println("ObjectName: " + objName.getCanonicalName() + " for relationId: " + relationId1);
239             }
240          }
241
242          RoleResult JavaDoc result2 = m_proxy.getAllRoles(relationId2);
243          Iterator JavaDoc i2 = result2.getRoles().iterator();
244          while (i2.hasNext())
245          {
246             Role JavaDoc r = (Role JavaDoc)i2.next();
247             List JavaDoc l = r.getRoleValue();
248             Iterator JavaDoc j = l.iterator();
249             while (j.hasNext())
250             {
251                ObjectName JavaDoc objName = (ObjectName JavaDoc)j.next();
252                System.out.println("ObjectName: " + objName.getCanonicalName() + " for relationId: " + relationId2);
253             }
254          }
255
256          System.out.println("getting all relationIds");
257          List JavaDoc li = m_proxy.getAllRelationIds();
258          System.out.println("allrelationIds list: " + li.toString());
259
260          System.out.println("You can view the adaptor at url http://......");
261          System.out.println(">>>>>>>>>>>>>>>> PRESS ENTER TO END THE DEMO <<<<<<<<<<<<<<<<<<<<");
262
263          waitForEnterPressed();
264       }
265       catch (Exception JavaDoc ex)
266       {
267          ex.printStackTrace();
268       }
269    }
270
271    public void endTests()
272    {
273       try
274       {
275          m_adaptor.stop();
276          Set JavaDoc mbeanSet = m_server.queryMBeans(null, Query.initialSubString(Query.classattr(), Query.value("test*")));
277          for (Iterator JavaDoc i = mbeanSet.iterator(); i.hasNext();)
278          {
279             m_server.unregisterMBean(((ObjectInstance JavaDoc)i.next()).getObjectName());
280          }
281          // release the relationService
282
m_server.unregisterMBean(m_relationServiceObjectName);
283          m_server.unregisterMBean(processorName);
284          m_server.unregisterMBean(httpAdaptorObjectName);
285          // release the MBeanServer
286
MBeanServerFactory.releaseMBeanServer(m_server);
287          System.exit(0);
288       }
289       catch (Exception JavaDoc ex)
290       {
291          ex.printStackTrace();
292          System.exit(1);
293       }
294    }
295
296    public static void main(String JavaDoc[] args)
297    {
298       RelationServiceAdaptor bnb = new RelationServiceAdaptor();
299       bnb.startTests(args);
300       bnb.buildMBeans();
301       bnb.endTests();
302    }
303
304    private static void waitForEnterPressed()
305    {
306       try
307       {
308          boolean done = false;
309          while (!done)
310          {
311             char ch = (char)System.in.read();
312             if (ch < 0 || ch == '\n')
313             {
314                done = true;
315             }
316          }
317       }
318       catch (IOException JavaDoc ex)
319       {
320          ex.printStackTrace();
321       }
322       return;
323    }
324 }
Popular Tags