KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > kernel > mx4j > PetalsCommandProcessor


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 EBM Websourcing, http://www.ebmwebsourcing.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id: PetalsCommandProcessor.java 11:28:17 AM ddesjardins $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.kernel.mx4j;
23
24 import java.io.IOException JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.List JavaDoc;
27
28 import javax.management.JMException JavaDoc;
29 import javax.management.MBeanServer JavaDoc;
30 import javax.xml.parsers.DocumentBuilder JavaDoc;
31
32 import mx4j.tools.adaptor.http.HttpCommandProcessor;
33 import mx4j.tools.adaptor.http.HttpInputStream;
34 import mx4j.tools.adaptor.http.ServerCommandProcessor;
35
36 import org.w3c.dom.Document JavaDoc;
37 import org.w3c.dom.Element JavaDoc;
38 import org.w3c.dom.Node JavaDoc;
39 import org.w3c.dom.NodeList JavaDoc;
40
41 /**
42  * Petals command processor
43  *
44  * @author ddesjardins - eBMWebsourcing
45  */

46 public class PetalsCommandProcessor implements HttpCommandProcessor {
47
48     /**
49      * Document builder
50      */

51     protected DocumentBuilder JavaDoc documentBuilder;
52
53     /**
54      * ServerCommandProcessor used to process the request
55      */

56     protected ServerCommandProcessor serverCommandProcessor = new ServerCommandProcessor();
57
58     /**
59      * @see HttpCommandProcessor#executeRequest(HttpInputStream)
60      */

61     public Document JavaDoc executeRequest(HttpInputStream httpInputStream)
62         throws IOException JavaDoc, JMException JavaDoc {
63         Document JavaDoc document = preProcess(httpInputStream);
64         Document JavaDoc outDocument = documentBuilder.newDocument();
65         Element JavaDoc petalsNode = outDocument.createElement("petals");
66         outDocument.appendChild(petalsNode);
67         NodeList JavaDoc mbeans = document.getDocumentElement().getChildNodes();
68         Element JavaDoc servicesNode = outDocument.createElement("services");
69         Element JavaDoc composNode = outDocument.createElement("components");
70         List JavaDoc<Node JavaDoc> compos = new ArrayList JavaDoc<Node JavaDoc>();
71         List JavaDoc<Node JavaDoc> services = new ArrayList JavaDoc<Node JavaDoc>();
72         for (int i = 0; i < mbeans.getLength(); i++) {
73             Element JavaDoc mbean = (Element JavaDoc) mbeans.item(i);
74             String JavaDoc objectName = mbean.getAttribute("objectname");
75             if (objectName.indexOf("type=engine") > -1) {
76                 // The mbean is a component
77
compos.add(mbean);
78             } else if (objectName.indexOf("type=binding") > -1) {
79                 // The mbean is a component
80
compos.add(mbean);
81             } else if (objectName.indexOf("type=installer") > -1) {
82                 // The mbean is a component
83
compos.add(mbean);
84             } else if (objectName.indexOf("type=service") > -1) {
85                 // The mbean is a service
86
services.add(mbean);
87             }
88         }
89         for (Node JavaDoc node : services) {
90             Node JavaDoc tmpNode = outDocument.adoptNode(node);
91             servicesNode.appendChild(tmpNode);
92         }
93         for (Node JavaDoc node : compos) {
94             Node JavaDoc tmpNode = outDocument.adoptNode(node);
95             composNode.appendChild(tmpNode);
96         }
97         petalsNode.appendChild(servicesNode);
98         petalsNode.appendChild(composNode);
99         return outDocument;
100     }
101
102     /**
103      * @see HttpCommandProcessor#setDocumentBuilder(DocumentBuilder)
104      */

105     public void setDocumentBuilder(DocumentBuilder JavaDoc documentBuilder) {
106         this.documentBuilder = documentBuilder;
107         serverCommandProcessor.setDocumentBuilder(documentBuilder);
108     }
109
110     /**
111      * @see HttpCommandProcessor#setMBeanServer(MBeanServer)
112      */

113     public void setMBeanServer(MBeanServer JavaDoc mBeanServer) {
114         serverCommandProcessor.setMBeanServer(mBeanServer);
115     }
116
117     /**
118      * Preprocess an request by the server command processor
119      *
120      * @param httpInputStream
121      * request to process
122      * @return Document
123      * @throws IOException
124      * @throws JMException
125      */

126     protected Document JavaDoc preProcess(HttpInputStream httpInputStream)
127         throws IOException JavaDoc, JMException JavaDoc {
128         return serverCommandProcessor.executeRequest(httpInputStream);
129     }
130
131 }
132
Popular Tags