KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 Fossil E-Commerce, http://www.fossilec.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: MX4JHtmlAdaptor.java 454 2006-05-23 13:51:29Z ddesjardins $
20  * -------------------------------------------------------------------------
21  */

22
23 package org.objectweb.petals.kernel.mx4j;
24
25 import javax.management.Attribute JavaDoc;
26 import javax.management.InstanceNotFoundException JavaDoc;
27 import javax.management.MalformedObjectNameException JavaDoc;
28 import javax.management.ObjectName JavaDoc;
29
30 import mx4j.tools.adaptor.http.HttpAdaptor;
31 import mx4j.tools.adaptor.http.HttpAdaptorMBean;
32 import mx4j.tools.adaptor.http.XSLTProcessor;
33
34 import org.objectweb.fractal.api.control.LifeCycleController;
35 import org.objectweb.fractal.fraclet.annotation.FractalComponent;
36 import org.objectweb.fractal.fraclet.annotation.LifeCycle;
37 import org.objectweb.fractal.fraclet.annotation.LifeCycleType;
38 import org.objectweb.fractal.fraclet.annotation.Monolog;
39 import org.objectweb.fractal.fraclet.annotation.Requires;
40 import org.objectweb.fractal.jmx.agent.AdminAttributes;
41 import org.objectweb.petals.util.LoggingUtil;
42 import org.objectweb.petals.util.SystemUtil;
43 import org.objectweb.util.monolog.api.Logger;
44
45 /**
46  * This is the Fractal JMX adaptor for use with MX4J tools.
47  *
48  * @version $Rev: 454 $ $Date: 2006-05-23 15:51:29 +0200 (mar., 23 mai 2006) $
49  * @since Petals 1.0
50  * @author <a HREF="mailto:rmarins@fossilec.com">Rafael Marins</a>
51  * @author ddesjardins - eBMWebsourcing
52  * @author Anass OUAZZANI - eBMWebsourcing
53  */

54 @FractalComponent
55 public class MX4JHtmlAdaptor {
56
57     private static final String JavaDoc ADAPTOR = "MX4JHtmlAdaptor";
58
59     /**
60      * HTTP Adaptor
61      */

62     protected HttpAdaptorMBean adaptor;
63
64     /**
65      * AdminAttributes
66      */

67     @Requires(name="adminAtt",signature=org.objectweb.fractal.jmx.agent.AdminAttributes.class)
68     protected AdminAttributes adminAttributes;
69
70     /**
71      * Logger
72      */

73     protected LoggingUtil log;
74
75     /**
76      * Logger
77      */

78     @Monolog(name="logger")
79     protected Logger logger;
80
81     private ObjectName JavaDoc oName;
82
83     /**
84      * @see LifeCycleController#startFc()
85      */

86     @LifeCycle(on=LifeCycleType.START)
87     public void start() {
88         log=new LoggingUtil(null);
89         log.start();
90         try {
91             // Retreive the port and host properties
92
int port = Integer.parseInt(SystemUtil.getHtmlPort());
93             // TODO : configure fine grained JMW access for security
94
// String host = SystemUtil.getHost();
95
if (adaptor != null && adaptor.isActive()
96                     && adaptor.getPort() == port) {
97                 return;
98             }
99             // unregister & stops
100
if (adaptor != null) {
101                 adaptor.stop();
102                 try {
103                     adminAttributes.getRawMBeanServer().unregisterMBean(
104                             getObjectName());
105                 } catch (InstanceNotFoundException JavaDoc ignore) {
106                     // Do nothing
107
}
108             }
109             adaptor = new HttpAdaptor();
110             adaptor.setPort(port);
111             adaptor.setHost("0.0.0.0");
112
113             adaptor.addCommandProcessor("petals", new PetalsCommandProcessor());
114             
115
116             adminAttributes.getRawMBeanServer().registerMBean(adaptor,
117                     getObjectName());
118
119             ObjectName JavaDoc processorName = new ObjectName JavaDoc(
120                     "Server:name=XSLTProcessor");
121             XSLTProcessor processor = new XSLTProcessor();
122
123             processor.setDefaultPage("petals");
124             processor
125                     .setPathInJar("org/objectweb/petals/kernel/fractal/mx4j/xsl");
126
127             processor.setUseCache(false);
128             adminAttributes.getRawMBeanServer().registerMBean(processor,
129                     processorName);
130             adminAttributes.getRawMBeanServer().setAttribute(getObjectName(),
131                     new Attribute JavaDoc("ProcessorName", processorName));
132
133             adaptor.start();
134         } catch (Exception JavaDoc e) {
135             log.error("Fail to start HTML adaptor", e);
136         }
137         log.end();
138     }
139
140     /**
141      * @see LifeCycleController#stopFc()
142      */

143     @LifeCycle(on=LifeCycleType.STOP)
144     public void stop() {
145         log.start();
146         if (adaptor != null) {
147             adaptor.stop();
148             try {
149                 adminAttributes.getRawMBeanServer().unregisterMBean(
150                         getObjectName());
151             } catch (Exception JavaDoc ignore) {
152                 log.warning("Error during MBean unregistering: "
153                         + ignore.getMessage(), ignore);
154             }
155         }
156         log.end();
157     }
158
159     // -------------------------------------------------------------------------
160
// Private implementation methods
161
// -------------------------------------------------------------------------
162

163     private ObjectName JavaDoc getObjectName() throws MalformedObjectNameException JavaDoc {
164         if (oName == null) {
165             String JavaDoc id = '@' + Integer.toHexString(this.hashCode());
166             oName = new ObjectName JavaDoc(ADAPTOR + id + ":type=html,port="
167                     + adaptor.getPort());
168         }
169         return oName;
170     }
171 }
172
Popular Tags