KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > jmx > comm > RI_HtmlAdaptor


1 /***
2  * Fractal JMX
3  * Copyright (C) 2003 France Telecom R&D
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 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: fractal@objectweb.org
20  */

21 package org.objectweb.fractal.jmx.comm;
22
23 import org.objectweb.fractal.api.control.BindingController;
24 import org.objectweb.fractal.api.control.LifeCycleController;
25
26 import org.objectweb.fractal.jmx.agent.AdminAttributes;
27
28 // JMX
29
import javax.management.ObjectName JavaDoc;
30 import javax.management.JMException JavaDoc;
31 import javax.management.InstanceNotFoundException JavaDoc;
32 import javax.management.JMRuntimeException JavaDoc;
33 // JMX-RI
34
import com.sun.jdmk.comm.HtmlAdaptorServer;
35
36 /**
37  * A component based on the HTML protocol adaptor provided by the JMX<SUP><FONT SIZE=-2>TM</FONT></SUP> 1.2
38  * Reference Implementation.
39  * This adaptor allows an HTML browser to manage all MBeans in the Fractal JMX agent side.
40  * To connect a browser to the agent, open this page in a web browser:
41  * <UL>
42  * </CODE>http://host:port</CODE>
43  * </UL>
44  * where:
45  * <UL>
46  * <LI><I>host</I> is the host name of the machine on which the agent is running (or localhost if the agent is local).
47  * <LI><I>port</I> is the port number used by the HTML server in the agent (default: 8082).
48  * </UL>
49  * This HTML protocol adaptor provides the following main HTML pages for managing MBeans in the agent:
50  * <UL>
51  * <LI><I>Agent View</I>: Provides a list of object names of all the MBeans registered in the agent.
52  * <LI><I>Agent Administration</I>: Registers and unregisters MBeans in the agent.
53  * <LI><I>MBean View</I>: Reads and writes MBean attributes and perform operations on MBeans in the agent.
54  * </UL>
55  *
56  * <p>The binding controller part of this component allows to set the (required) agent component.
57  * The {@link CommunicatorAttributes attribute controller} part allows to set the <I>port</I> number used to
58  * connect a browser to the agent.
59  * The life-cycle controller part registers this component in the agent when it starts,
60  * if it is not already registered or if its <I>port</I> attribute has changed.
61  * The underlying HTML protocol adaptor is implemented as a dynamic MBean,
62  * and can be managed:
63  * <ul>
64  * <li>as described in the JMX<SUP><FONT SIZE=-2>TM</FONT></SUP> 1.2 Reference Implementation documentation.
65  * <br>It is identified in the agent by the {@link ObjectName}: RI_HtmlAdaptor:type=html,port=<I>port</I>
66  * </li>
67  * <li> or indirectly by accessing the MBean representing the {@link CommunicatorAttributes attribute controller}
68  * of this component.
69  * <br>In this case, it is required to stop and (re)start the component to take account a new <I>port</I> number,
70  * and to browse a page with the new <I>port</I>.
71  * </li>
72  * </ul>
73  *
74  *
75  * @version 0.1
76  */

77 public class RI_HtmlAdaptor implements BindingController, LifeCycleController, CommunicatorAttributes {
78     private static final String JavaDoc ADAPTOR = "RI_HtmlAdaptor";
79     private AdminAttributes _adminAtt;
80     private HtmlAdaptorServer _htmlAdaptor = new HtmlAdaptorServer();
81     private int _port = _htmlAdaptor.getPort();
82
83     // --------------------------------------------------------------------------
84
// Implementation of the BindingController interface
85
// --------------------------------------------------------------------------
86
public String JavaDoc[] listFc() {
87         return new String JavaDoc[] { "adminAtt" };
88     }
89
90     public Object JavaDoc lookupFc(final String JavaDoc itfName) {
91         if (itfName.equals("adminAtt"))
92             return _adminAtt;
93         else
94             return null;
95     }
96
97     public void bindFc(final String JavaDoc itfName, final Object JavaDoc itfValue) {
98         if (itfName.equals("adminAtt")) {
99             _adminAtt = (AdminAttributes) itfValue;
100         }
101     }
102
103     public void unbindFc(final String JavaDoc itfName) {
104         if (itfName.equals("adminAtt")) {
105             _adminAtt = null;
106         }
107     }
108
109     // -------------------------------------------------------------------------
110
// implementation of the LifeCycleController interface
111
// -------------------------------------------------------------------------
112
public String JavaDoc getFcState() {
113         return null;
114     }
115
116     public synchronized void startFc() {
117         ObjectName JavaDoc oName;
118         String JavaDoc id = '@' + Integer.toHexString(this.hashCode());
119         try {
120             //System.out.println("->tries to start with port=" + port);
121
if (_htmlAdaptor != null
122                 && _htmlAdaptor.getState() == HtmlAdaptorServer.ONLINE
123                 && _htmlAdaptor.getPort() == _port) {
124                 //System.out.println("<-already started with port=" + port);
125
return;
126             }
127             // unregister & stops
128
if (_htmlAdaptor != null) {
129                 _htmlAdaptor.stop();
130                 oName = new ObjectName JavaDoc(ADAPTOR + id + ":type=html,port=" + _htmlAdaptor.getPort());
131                 try {
132                     _adminAtt.getRawMBeanServer().unregisterMBean(oName);
133                 } catch (InstanceNotFoundException JavaDoc ignore) {}
134             }
135             // registers & starts
136
_htmlAdaptor = new HtmlAdaptorServer(_port);
137             oName = new ObjectName JavaDoc(ADAPTOR + id + ":type=html,port=" + _port);
138             _adminAtt.getRawMBeanServer().registerMBean(_htmlAdaptor, oName);
139             _htmlAdaptor.start();
140             // checks
141
if (!_htmlAdaptor.waitState(HtmlAdaptorServer.ONLINE, 5000))
142                 throw new JMRuntimeException JavaDoc(
143                     "HtmlAdaptorServer failed to start with port="
144                         + _htmlAdaptor.getPort()
145                         + ", current state="
146                         + _htmlAdaptor.getState());
147             //System.out.println("<-started with port=" + htmlAdaptor.getPort());
148
} catch (JMException JavaDoc e) {
149             throw new JMRuntimeException JavaDoc(e.toString());
150         }
151     }
152
153     public void stopFc() {}
154
155     // -------------------------------------------------------------------------
156
// implements Attribute controller
157
// -------------------------------------------------------------------------
158
public int getPort() {
159         return _port;
160     }
161
162     public void setPort(final int port) {
163         this._port = port;
164     }
165 }
166
Popular Tags