KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > kernel > admin > DistributedJMXServerFactoryImpl


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: DistributedJMXServerManager.java 16:34:49 ddesjardins $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.kernel.admin;
23
24 import java.util.HashMap JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Properties JavaDoc;
27
28 import javax.management.remote.JMXConnector JavaDoc;
29 import javax.management.remote.JMXConnectorFactory JavaDoc;
30 import javax.management.remote.JMXServiceURL JavaDoc;
31 import javax.naming.Context JavaDoc;
32 import javax.naming.InitialContext JavaDoc;
33 import javax.naming.NamingException JavaDoc;
34 import javax.naming.NotContextException JavaDoc;
35
36 import org.objectweb.fractal.api.control.IllegalLifeCycleException;
37 import org.objectweb.fractal.fraclet.annotation.FractalComponent;
38 import org.objectweb.fractal.fraclet.annotation.Interface;
39 import org.objectweb.fractal.fraclet.annotation.LifeCycle;
40 import org.objectweb.fractal.fraclet.annotation.LifeCycleType;
41 import org.objectweb.fractal.fraclet.annotation.Provides;
42 import org.objectweb.petals.PetalsException;
43 import org.objectweb.petals.util.JNDIUtil;
44 import org.objectweb.petals.util.LoggingUtil;
45 import org.objectweb.petals.util.PropertyUtil;
46
47 /**
48  * This class is a manager of JMX server. It is used to get a connection to a
49  * JMX Server on a distant container.
50  *
51  * @author ddesjardins - eBMWebsourcing
52  */

53 @FractalComponent
54 @Provides(interfaces=@Interface(name="service",signature=org.objectweb.petals.kernel.admin.DistributedJMXServerFactory.class))
55 public class DistributedJMXServerFactoryImpl
56     implements DistributedJMXServerFactory {
57
58     /**
59      * Name of the container context
60      */

61     private static final String JavaDoc CONTAINERS_REF = "containers";
62
63     /**
64      * Information of the current container
65      */

66     protected ContainerInformation containerInformation;
67
68     /**
69      * Container context
70      */

71     protected Context JavaDoc containersContext;
72
73     /**
74      * Logger
75      */

76     protected LoggingUtil log;
77
78     /**
79      * Root context
80      */

81     protected InitialContext JavaDoc rootContext;
82
83     /**
84      * Create a
85      *
86      * @param containerName
87      * @return
88      * @throws PetalsException
89      */

90     public DistributedJMXServer createDistributedJMXServer(String JavaDoc containerName)
91         throws PetalsException {
92         log.start();
93         DistributedJMXServer distributedJMXServer = null;
94         try {
95             ContainerInformation containerInformation = (ContainerInformation) containersContext
96                 .lookup(containerName);
97             if (containerInformation != null) {
98                 JMXConnector JavaDoc connector = null;
99                 Properties JavaDoc env = System.getProperties();
100                 env.put("java.naming.factory.initial",
101                     "com.sun.jndi.rmi.registry.RegistryContextFactory");
102                 Map JavaDoc<String JavaDoc, Object JavaDoc> args = new HashMap JavaDoc<String JavaDoc, Object JavaDoc>();
103                 String JavaDoc username = containerInformation.getJmxLogin();
104                 String JavaDoc password = containerInformation.getJmxPassword();
105                 if (!"".equals(username)) {
106                     if (password == null) {
107                         password = "";
108                     }
109                     String JavaDoc[] credentials = new String JavaDoc[] {username, password};
110                     args.put("jmx.remote.credentials", credentials);
111                 }
112                 String JavaDoc urlStr = "service:jmx:rmi:///jndi/rmi://"
113                     + containerInformation.getHost() + ":"
114                     + containerInformation.getJmxPort()
115                     + "/management/rmi-jmx-connector";
116                 JMXServiceURL JavaDoc url;
117
118                 url = new JMXServiceURL JavaDoc(urlStr);
119                 connector = JMXConnectorFactory.newJMXConnector(url, args);
120
121                 distributedJMXServer = new DistributedJMXServer(connector);
122             }
123         } catch (Exception JavaDoc e) {
124             throw new PetalsException(
125                 "Error while trying to connect to JMX server", e);
126         }
127         log.end();
128         return distributedJMXServer;
129     }
130
131     /**
132      * @see DistributedJMXServerFactory#isContainerStarted(String)
133      */

134     public boolean isContainerStarted(String JavaDoc containerName) {
135         log.start();
136         if (containerName == null) {
137             throw new IllegalArgumentException JavaDoc(
138                 "The container name must be non null");
139         }
140         boolean isStarted = false;
141         if (JNDIUtil.isBound(rootContext, CONTAINERS_REF, containerName)) {
142             try {
143                 ContainerInformation containerInformation = (ContainerInformation) containersContext
144                     .lookup(containerName);
145                 isStarted = containerInformation.isStarted();
146             } catch (NamingException JavaDoc e) {
147                 // Do nothing
148
}
149         }
150         log.end();
151         return isStarted;
152     }
153
154     @LifeCycle(on=LifeCycleType.START)
155     public void start() throws IllegalLifeCycleException {
156         log=new LoggingUtil(null);
157         log.start();
158         try {
159             initContext();
160             
161         } catch (PetalsException e) {
162             log.error("Error starting JMX server", e);
163         }
164         log.end();
165     }
166
167     @LifeCycle(on=LifeCycleType.STOP)
168     public void stop() throws IllegalLifeCycleException {
169         log.call();
170     }
171
172     /**
173      * Initialize the containers context
174      *
175      * @throws PetalsException
176      */

177     protected void initContext() throws PetalsException {
178         try {
179             rootContext = new InitialContext JavaDoc(PropertyUtil
180                 .retrieveJNDIProperties());
181             if (!JNDIUtil.isBound(rootContext, "/", CONTAINERS_REF)) {
182                 containersContext = rootContext
183                     .createSubcontext(CONTAINERS_REF);
184             } else {
185                 containersContext = (Context JavaDoc) rootContext
186                     .lookup(CONTAINERS_REF);
187             }
188             if (containersContext == null) {
189                 throw new NotContextException JavaDoc();
190             }
191         } catch (Exception JavaDoc e1) {
192             throw new PetalsException(
193                 "Problem while trying to initialize the container context", e1);
194         }
195     }
196
197 }
198
Popular Tags