KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > kernel > admin > service > PetalsAdminService


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: PetalsAdminService.java 16:40:52 alouis $
20  * -------------------------------------------------------------------------
21  */

22
23 package org.objectweb.petals.kernel.admin.service;
24
25 import java.util.Collection JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.Set JavaDoc;
31
32 import javax.naming.Binding JavaDoc;
33 import javax.naming.InitialContext JavaDoc;
34 import javax.naming.NamingEnumeration JavaDoc;
35 import javax.naming.NamingException JavaDoc;
36
37 import org.objectweb.fractal.api.Component;
38 import org.objectweb.fractal.api.control.IllegalLifeCycleException;
39 import org.objectweb.fractal.fraclet.annotation.FractalComponent;
40 import org.objectweb.fractal.fraclet.annotation.Interface;
41 import org.objectweb.fractal.fraclet.annotation.LifeCycle;
42 import org.objectweb.fractal.fraclet.annotation.LifeCycleType;
43 import org.objectweb.fractal.fraclet.annotation.Monolog;
44 import org.objectweb.fractal.fraclet.annotation.Provides;
45 import org.objectweb.fractal.fraclet.annotation.Requires;
46 import org.objectweb.util.monolog.api.Logger;
47
48 import org.objectweb.petals.jbi.management.deployment.DeploymentService;
49 import org.objectweb.petals.jbi.management.service.InstallationService;
50 import org.objectweb.petals.jbi.registry.AbstractEndpoint;
51 import org.objectweb.petals.jbi.registry.Registry;
52 import org.objectweb.petals.jbi.registry.RegistryException;
53 import org.objectweb.petals.jbi.transport.Transporter;
54 import org.objectweb.petals.kernel.admin.ContainerInformation;
55
56
57 import org.objectweb.petals.monitoring.ExchangeStateReport;
58 import org.objectweb.petals.monitoring.RouterMonitor;
59 import org.objectweb.petals.util.JNDIUtil;
60 import org.objectweb.petals.util.LoggingUtil;
61 import org.objectweb.petals.util.PropertyUtil;
62
63 /**
64  * This class is used for Petals global Administration
65  *
66  * @author alouis - eBMWebsourcing
67  */

68 @FractalComponent
69 @Provides(interfaces = @Interface(name = "service", signature = org.objectweb.petals.kernel.admin.service.PetalsAdminServiceMBean.class))
70 public class PetalsAdminService implements PetalsAdminServiceMBean {
71
72     protected static final String JavaDoc CONTAINERS_REF = "containers";
73
74     protected static final String JavaDoc ENDPOINTS_REF = "endpoints";
75
76     protected boolean monitoring=true;
77     /**
78      * Logger wrapper
79      */

80     protected LoggingUtil log;
81
82     /**
83      * Monolog
84      */

85     @Monolog(name = "logger")
86     protected Logger logger;
87
88     /**
89      * JBI :: JMX Deployment
90      */

91     @Requires(name = "deployment", signature = org.objectweb.petals.jbi.management.deployment.DeploymentService.class)
92     protected DeploymentService deployment;
93
94     /**
95      * JBI :: JMX Installation
96      */

97     @Requires(name = "installation", signature = org.objectweb.petals.jbi.management.service.InstallationService.class)
98     protected InstallationService installation;
99
100     @Requires(name = "transporter", signature = org.objectweb.petals.jbi.transport.Transporter.class)
101     protected Transporter transporter;
102
103     @Requires(name = "registry", signature = org.objectweb.petals.jbi.registry.Registry.class)
104     protected Registry registry;
105
106     @Requires(name = "router-monitor", signature = org.objectweb.petals.monitoring.RouterMonitor.class)
107     protected RouterMonitor routerMonitor;
108
109
110     protected Component rootComponent;
111
112     /**
113      * uninstalled serviceassemblies, components and sharelibraries endpoints
114      * are removed the container is removed from the global configuration
115      *
116      * @throws Exception
117      */

118     public void shutdownContainer() throws Exception JavaDoc {
119         log.start();
120         log
121         .info("Shutdown the server. It will be removed from the Petals network.");
122         try {
123             log.info(" 1/4 Undeploy Service Assemblies.");
124             deployment.shutdown();
125         } catch (Exception JavaDoc e) {
126             log.error("Can not shutdown the DeploymentService.", e);
127         }
128         try {
129             log.info(" 2/4 Uninstall Shared Libraries and Components.");
130             installation.shutdown();
131         } catch (Exception JavaDoc e) {
132             log.error("Can not shutdown the InstallationService.", e);
133         }
134         try {
135             log
136             .info(" 3/4 Shutdown the Transporter (remove the server from the global configuration).");
137             transporter.shutdown();
138         } catch (Exception JavaDoc e) {
139             log.error("Can not shutdown the Transporter.", e);
140         }
141
142         log.info(" 4/4 Stopping the server...");
143         System.exit(0);
144         log.end();
145     }
146
147     public void stopContainer() throws Exception JavaDoc {
148         log.start();
149         log.info("Stopping the server...");
150         System.exit(0);
151         log.end();
152     }
153
154     /**
155      * Retrieve the collection of all the started servers configuration from
156      * this domain.
157      *
158      * @return Collection of server information ContainerInformation
159      */

160     public Collection JavaDoc<Map JavaDoc<String JavaDoc, String JavaDoc>> getAllServersConfiguration() {
161         return retrieveAllServersConfiguration(false);
162     }
163
164     /**
165      * Retrieve the collection of all the started servers configuration from
166      * this domain.
167      *
168      * @return Collection of server information ContainerInformation
169      */

170     public Collection JavaDoc<Map JavaDoc<String JavaDoc, String JavaDoc>> getAllStartedServersConfiguration() {
171         return retrieveAllServersConfiguration(true);
172     }
173
174     /**
175      * Retrieve the collection of all the started servers configuration from
176      * this domain.
177      *
178      * @param startedOnly
179      * return only started servers information
180      * @return Collection of server information ContainerInformation
181      */

182     protected Collection JavaDoc<Map JavaDoc<String JavaDoc, String JavaDoc>> retrieveAllServersConfiguration(
183         boolean startedOnly) {
184
185         log.start();
186         Set JavaDoc<Map JavaDoc<String JavaDoc, String JavaDoc>> containers = new HashSet JavaDoc<Map JavaDoc<String JavaDoc, String JavaDoc>>();
187
188         try {
189             InitialContext JavaDoc rootContext = new InitialContext JavaDoc(PropertyUtil
190                 .retrieveJNDIProperties());
191
192             if (JNDIUtil.isBound(rootContext, "/", CONTAINERS_REF)) {
193
194                 NamingEnumeration JavaDoc<Binding JavaDoc> enums = rootContext
195                 .listBindings(CONTAINERS_REF);
196
197                 while (enums.hasMoreElements()) {
198
199                     Binding JavaDoc binding = (Binding JavaDoc) enums.next();
200                     ContainerInformation containerInformation = (ContainerInformation) binding
201                     .getObject();
202
203                     if (!startedOnly
204                             || (startedOnly && containerInformation.isStarted())) {
205
206                         containers
207                         .add(createConfigurationMap(containerInformation));
208                     }
209                 }
210             }
211         } catch (NamingException JavaDoc e) {
212             log
213             .debug("Problem while trying to retrieve all the containers information : context may not be initialized");
214         }
215         log.end();
216         return containers;
217     }
218
219     /**
220      * Create a Map from a ContainerInformation object
221      *
222      * @param info
223      * @return a Map of the server properties
224      */

225     protected Map JavaDoc<String JavaDoc, String JavaDoc> createConfigurationMap(
226         ContainerInformation info) {
227         Map JavaDoc<String JavaDoc, String JavaDoc> map = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
228         if (info != null) {
229             map.put(CONF_HOST, info.getHost());
230             map.put(CONF_HTML_PORT, info.getHtmlPort());
231             map.put(CONF_JMX_LOGIN, info.getJmxLogin());
232             map.put(CONF_JMX_PASSWORD, info.getJmxPassword());
233             map.put(CONF_JMX_PORT, info.getJmxPort());
234             map.put(CONF_JNDI_FACTORY, info.getJndiFactory());
235             map.put(CONF_JNDI_PORT, info.getJndiPort());
236             map.put(CONF_JORAM_DOMAIN, info.getJoramDomain());
237             map.put(CONF_JORAM_DOMAIN_PORT, info.getJoramDomainPort());
238             map.put(CONF_JORAM_ID, info.getJoramId());
239             map.put(CONF_JORAM_LOGIN, info.getJoramLogin());
240             map.put(CONF_JORAM_PASSWORD, info.getJoramPassword());
241             map.put(CONF_JORAM_TCP_PORT, info.getJoramTCPPort());
242             map.put(CONF_NAME, info.getName());
243             map
244             .put(
245                 CONF_STATE,
246                 (info.getState() == ContainerInformation.ContainerState.STARTED) ? "started"
247                     : "stopped");
248             map.put(CONF_UID, "" + info.getUid());
249         }
250         return map;
251     }
252
253     /**
254      * Remove the specified server from the network configuration. <br>
255      * The server is unbound, but not shutdown. All ServiceEndpoints activated
256      * on this server are unbound. <br>
257      * <b>Be very careful when using this operation, the network configuration
258      * may become unstable.</b><br>
259      * TODO JORAM configuration not updated
260      * @param serverName
261      * @throws Exception
262      * The server is not found in the network configuration, some
263      * Endpoints have not been removed, or problem while accessing
264      * the network configuration
265      */

266     public void removeServerFromNetwork(String JavaDoc serverName) throws Exception JavaDoc {
267
268         log.start();
269
270         InitialContext JavaDoc rootContext;
271         try {
272             rootContext = new InitialContext JavaDoc(PropertyUtil
273                 .retrieveJNDIProperties());
274         } catch (NamingException JavaDoc e) {
275             throw new Exception JavaDoc("Network configuration can not be accessed.", e);
276         }
277
278         try {
279             Collection JavaDoc<AbstractEndpoint> eps = getServiceEndpointForContainer(serverName);
280
281             rootContext.unbind(CONTAINERS_REF + "/" + serverName);
282
283             for (AbstractEndpoint endpoint : eps) {
284                 registry.deregisterInternalEndpoint(endpoint);
285             }
286
287         } catch (NamingException JavaDoc e) {
288             throw new Exception JavaDoc(
289                 "Server not found in the network configuration.", e);
290         } catch (RegistryException e) {
291             throw new Exception JavaDoc("A ServiceEndpoint failed to be removed.", e);
292         }
293
294         log.end();
295     }
296
297     /**
298      * Return all the registered endpoints for the specified container
299      *
300      * @param containerName
301      * @return a collection of AbstractEndpoint. Not null, may be empty
302      * @throws NamingException
303      * problem accessing JNDI
304      */

305     protected Collection JavaDoc<AbstractEndpoint> getServiceEndpointForContainer(
306         String JavaDoc containerName) throws NamingException JavaDoc {
307
308         log.start();
309
310         InitialContext JavaDoc rootContext = new InitialContext JavaDoc(PropertyUtil
311             .retrieveJNDIProperties());
312
313         Collection JavaDoc<AbstractEndpoint> eps = new HashSet JavaDoc<AbstractEndpoint>();
314
315         if (JNDIUtil.isBound(rootContext, "/", ENDPOINTS_REF)) {
316
317             NamingEnumeration JavaDoc<Binding JavaDoc> enums = rootContext
318             .listBindings(ENDPOINTS_REF);
319
320             while (enums.hasMoreElements()) {
321
322                 Binding JavaDoc binding = (Binding JavaDoc) enums.next();
323                 AbstractEndpoint abstractEndpoint = (AbstractEndpoint) binding
324                 .getObject();
325
326                 if (!abstractEndpoint.getContainerName().equals(containerName)) {
327                     eps.add(abstractEndpoint);
328                 }
329             }
330         }
331         log.end();
332
333         return eps;
334     }
335
336
337     public boolean getMonitoring(){
338         return routerMonitor.isMonitoring();
339     }
340     public void setMonitoring(boolean activate) throws Exception JavaDoc{
341         routerMonitor.setMonitoring(activate);
342     }
343
344     /**
345      * Return information about messages exchanges since the specified date.<br>
346      * Information are sort by exchange-start-date.
347      * Information is returned in a bulk mode, with no specific Java Object. <br>
348      * - Each element of the list represents an exchange report.<br>
349      * &nbsp;&nbsp;- an exchange report is a <em>List(Map(String,Object))</em> of the different captures of the exchange<br>
350      * &nbsp;&nbsp;&nbsp;&nbsp;- a capture is a Map of <em>Object</em> objects, following the structure:<br>
351      * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "id" <em>String</em> the id of the exchange<br>
352      * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "time" <em>long</em> the date of the capture<br>
353      * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "component" <em>String</em> the component that interact on this exchange<br>
354      * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "role" <em>String</em> the role of the component : [consumer|provider]<br>
355      * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "type" <em>String</em> the message type : [in|out|fault|ANY]<br>
356      * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "status"<em>String</em> the message status : [active|done|error]<br>
357      *
358      * @param sinceTime <em>long</em> representation of the date, in the standard <em>long<em> format
359      * @return the raw reports. not null, may be empty
360      */

361     public List JavaDoc<List JavaDoc<Map JavaDoc<String JavaDoc,Object JavaDoc>>> bulkData(long sinceTime){
362         log.start();
363         List JavaDoc<ExchangeStateReport> reports = routerMonitor.getReports(sinceTime);
364         List JavaDoc<List JavaDoc<Map JavaDoc<String JavaDoc,Object JavaDoc>>> bulkDatas = MonitoringService.bulkData(reports);
365         log.end();
366         return bulkDatas;
367     }
368
369
370     // ----------------------------------------------------------------------
371
// Fractal LifecycleController implementation
372
// ----------------------------------------------------------------------
373

374     /**
375      * @see org.objectweb.fractal.api.control.LifeCycleController#startFc()
376      */

377     @LifeCycle(on = LifeCycleType.START)
378     public void start() throws IllegalLifeCycleException {
379         log = new LoggingUtil(logger);
380     }
381
382 }
383
Popular Tags