KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > jbi > management > service > LifeCycleManagerImpl


1 /**
2  * PETALS: PETALS Services Platform
3  * Copyright (C) 2005 EBM WebSourcing
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 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
18  * USA.
19  *
20  * Initial developer(s): EBM WebSourcing
21  * --------------------------------------------------------------------------
22  * $Id: Manager.java,v 1.2 2005/07/22 10:24:27 alouis Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.petals.jbi.management.service;
27
28 import java.util.Collections JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Hashtable JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.Set JavaDoc;
34 import java.util.Map.Entry;
35
36 import javax.management.MBeanServer JavaDoc;
37 import javax.management.MalformedObjectNameException JavaDoc;
38 import javax.management.ObjectName JavaDoc;
39
40 import org.objectweb.fractal.api.control.IllegalLifeCycleException;
41 import org.objectweb.fractal.fraclet.annotation.FractalComponent;
42 import org.objectweb.fractal.fraclet.annotation.Interface;
43 import org.objectweb.fractal.fraclet.annotation.LifeCycle;
44 import org.objectweb.fractal.fraclet.annotation.LifeCycleType;
45 import org.objectweb.fractal.fraclet.annotation.Monolog;
46 import org.objectweb.fractal.fraclet.annotation.Provides;
47 import org.objectweb.fractal.fraclet.annotation.Requires;
48 import org.objectweb.fractal.jmx.agent.AdminAttributes;
49 import org.objectweb.petals.jbi.component.lifecycle.ComponentLifeCycle;
50 import org.objectweb.petals.jbi.component.lifecycle.Installer;
51 import org.objectweb.petals.jbi.component.lifecycle.LifeCycleAbstract;
52 import org.objectweb.petals.jbi.component.lifecycle.ServiceAssemblyLifeCycle;
53 import org.objectweb.petals.util.LoggingUtil;
54 import org.objectweb.util.monolog.api.Logger;
55
56 /**
57  * <code>Manager</code> is the main class for JMX management. It is the entry
58  * point for register and unregister elements in the JMX environment (elements
59  * are JBI binding/engine components and systemServices components). Related
60  * MBeans are created when registering and unregistering elements.
61  *
62  * <code>Manager</code> owns the JMX management services offered by the JBI
63  * container to administrative clients. Management services are : - AdminService
64  * (referenced in the JMX server as "admin"); - InstallationService (referenced
65  * in the JMX server as "installation"); - DeploymentService (referenced in the
66  * JMX server as "deployment");
67  *
68  * <code>Manager</code> is the central point for accessing the mBeans
69  * associated to the JBI container elements.
70  *
71  * <code>ManagementException</code> exceptions are thrown when problems for
72  * accessing the JMX server occur.
73  *
74  * TODO refactor to get System Services from Fractal, and BC and SE from JMX
75  * TODO set all properties in a single file, and add a FractalAttribute
76  * component to retrieve it (for joram, container name, etc)
77  *
78  * @author Adrien LOUIS - EBM WebSourcing
79  */

80 @FractalComponent
81 @Provides(interfaces=@Interface(name="service",signature=org.objectweb.petals.jbi.management.service.LifeCycleManagerService.class))
82 public class LifeCycleManagerImpl implements LifeCycleManagerService {
83
84     private static final String JavaDoc DEPLOYMENT_NAME = "deployment";
85
86     private static final String JavaDoc INSTALLATION_NAME = "installation";
87
88     private final static String JavaDoc PETALS_DOMAIN = "Petals";
89
90     private final static String JavaDoc TYPE = "type";
91
92     private final static String JavaDoc NAME = "name";
93
94     private final static String JavaDoc SERVICE_TYPE = "service";
95
96     private final static String JavaDoc ADMIN_SERV_NAME = "Admin";
97
98     private final static String JavaDoc INSTALLATION_SERV_NAME = "Installation";
99
100     private final static String JavaDoc DEPLOYMENT_SERV_NAME = "Deployment";
101
102     protected ObjectName JavaDoc addressName;
103
104     protected ObjectName JavaDoc adminName;
105
106     protected Map JavaDoc<ObjectName JavaDoc, ComponentLifeCycle> bindingCompoLifeCycles;
107
108     protected String JavaDoc containerName;
109
110     protected ObjectName JavaDoc deploymentName;
111
112     protected ObjectName JavaDoc directoryName;
113
114     protected Map JavaDoc<ObjectName JavaDoc, ComponentLifeCycle> engineCompoLifeCycles;
115
116     protected ObjectName JavaDoc installationName;
117
118     protected Map JavaDoc<ObjectName JavaDoc, Installer> installers;
119
120     protected ObjectName JavaDoc itineraryName;
121
122     /**
123      * Monolog logger
124      */

125     @Monolog(name="logger")
126     protected Logger logger;
127
128     protected MBeanNamesImpl mBeanNames;
129
130     @Requires(name="adminAtt",signature=org.objectweb.fractal.jmx.agent.AdminAttributes.class)
131     protected AdminAttributes adminAttributes;
132
133     protected Map JavaDoc<String JavaDoc, ServiceAssemblyLifeCycle> serviceAssemblyLifeCycles;
134
135     protected Map JavaDoc<ObjectName JavaDoc, LifeCycleAbstract> systemServices;
136
137     protected ObjectName JavaDoc transporterName;
138
139     /**
140      * Logger wrapper
141      */

142     private LoggingUtil log;
143
144     /**
145      * Default constructor. Creates a <code>Manager</code> instance. JMX
146      * server is created.
147      *
148      */

149     public LifeCycleManagerImpl() {
150
151         mBeanNames = new MBeanNamesImpl("org.objectweb.petals");
152
153         bindingCompoLifeCycles = Collections
154                 .synchronizedMap(new HashMap JavaDoc<ObjectName JavaDoc, ComponentLifeCycle>());
155
156         engineCompoLifeCycles = Collections
157                 .synchronizedMap(new HashMap JavaDoc<ObjectName JavaDoc, ComponentLifeCycle>());
158
159         systemServices = Collections
160                 .synchronizedMap(new HashMap JavaDoc<ObjectName JavaDoc, LifeCycleAbstract>());
161
162         serviceAssemblyLifeCycles = Collections
163                 .synchronizedMap(new HashMap JavaDoc<String JavaDoc, ServiceAssemblyLifeCycle>());
164
165         installers = Collections
166                 .synchronizedMap(new HashMap JavaDoc<ObjectName JavaDoc, Installer>());
167
168         deploymentName = mBeanNames
169                 .createSystemComponentMBeanName(DEPLOYMENT_NAME);
170
171         installationName = mBeanNames
172                 .createSystemComponentMBeanName(INSTALLATION_NAME);
173     }
174
175     // /////////////////////////
176
// /// retrieve platform component MBeanNames
177
// /////////////////////////
178
/**
179      * Retrieve the AdminServieMBean ObjectName.
180      */

181     public ObjectName JavaDoc getAdminServiceMBeanName() throws ManagementException {
182         ObjectName JavaDoc result = null;
183         Set JavaDoc objNames;
184         try {
185             Hashtable JavaDoc<String JavaDoc, String JavaDoc> attributes = new Hashtable JavaDoc<String JavaDoc, String JavaDoc>();
186             attributes.put(NAME, ADMIN_SERV_NAME);
187             attributes.put(TYPE, SERVICE_TYPE);
188             ObjectName JavaDoc objName = new ObjectName JavaDoc(PETALS_DOMAIN, attributes);
189             objNames = adminAttributes.getRawMBeanServer().queryNames(objName,
190                     null);
191         } catch (MalformedObjectNameException JavaDoc e) {
192             String JavaDoc msg = "Error during AdminService ObjName retrieval";
193             log.error(msg, e);
194             throw new ManagementException(msg, e);
195         } catch (NullPointerException JavaDoc e) {
196             String JavaDoc msg = "Error during AdminService ObjName retrieval";
197             log.error(msg, e);
198             throw new ManagementException(msg, e);
199         }
200         if (objNames != null && objNames.size() == 1) {
201             result = (ObjectName JavaDoc) objNames.iterator().next();
202         } else {
203             throw new ManagementException("AdminService Mbean can't be "
204                     + "correctly retrieved");
205         }
206         return result;
207     }
208
209     /**
210      * @see org.objectweb.petals.jbi.management.service.LifeCycleManagerService#getComponentByName(java.lang.String)
211      */

212     public ComponentLifeCycle getComponentByName(String JavaDoc name) {
213         log.call();
214
215         ComponentLifeCycle on = null;
216
217         // search in bindings
218

219         on = getBindingComponentByName(name);
220
221         if (on == null) {
222             // search in engines
223

224             on = getEngineComponentByName(name);
225         }
226         return on;
227     }
228
229     /**
230      * Retrieve the DeploymentService ObjectName. This is used by a
231      * ServiceAssembly LifeCycle. As there is no MBean for the SA LifeCycle, the
232      * DeploymentServiceMbean is used to represent it. All SA notifications are
233      * reported to the DeploymentLifeCycleMB
234      *
235      * @return the jmx name of the deploymentServiceMBean
236      */

237     public ObjectName JavaDoc getDeploymentServiceMBeanName()
238         throws ManagementException {
239         ObjectName JavaDoc result = null;
240         Set JavaDoc objNames;
241         try {
242             Hashtable JavaDoc<String JavaDoc, String JavaDoc> attributes = new Hashtable JavaDoc<String JavaDoc, String JavaDoc>();
243             attributes.put(NAME, DEPLOYMENT_SERV_NAME);
244             attributes.put(TYPE, SERVICE_TYPE);
245             ObjectName JavaDoc objName = new ObjectName JavaDoc(PETALS_DOMAIN, attributes);
246             objNames = adminAttributes.getRawMBeanServer().queryNames(objName,
247                     null);
248         } catch (MalformedObjectNameException JavaDoc e) {
249             String JavaDoc msg = "Error during DeploymentService ObjName retrieval";
250             log.error(msg, e);
251             throw new ManagementException(msg, e);
252         } catch (NullPointerException JavaDoc e) {
253             String JavaDoc msg = "Error during DeploymentService ObjName retrieval";
254             log.error(msg, e);
255             throw new ManagementException(msg, e);
256         }
257         if (objNames != null && objNames.size() == 1) {
258             result = (ObjectName JavaDoc) objNames.iterator().next();
259         } else {
260             throw new ManagementException("DeploymentService Mbean can't be "
261                     + "correctly retrieved");
262         }
263         return result;
264     }
265
266     /**
267      * Retrieve the InstallationService ObjectName.
268      */

269     public ObjectName JavaDoc getInstallationServiceMBeanName()
270         throws ManagementException {
271         ObjectName JavaDoc result = null;
272         Set JavaDoc objNames;
273         try {
274             Hashtable JavaDoc<String JavaDoc, String JavaDoc> attributes = new Hashtable JavaDoc<String JavaDoc, String JavaDoc>();
275             attributes.put(NAME, INSTALLATION_SERV_NAME);
276             attributes.put(TYPE, SERVICE_TYPE);
277             ObjectName JavaDoc objName = new ObjectName JavaDoc(PETALS_DOMAIN, attributes);
278             objNames = adminAttributes.getRawMBeanServer().queryNames(objName,
279                     null);
280         } catch (MalformedObjectNameException JavaDoc e) {
281             String JavaDoc msg = "Error during InstallationService ObjName retrieval";
282             log.error(msg, e);
283             throw new ManagementException(msg, e);
284         } catch (NullPointerException JavaDoc e) {
285             String JavaDoc msg = "Error during InstallationService ObjName retrieval";
286             log.error(msg, e);
287             throw new ManagementException(msg, e);
288         }
289         if (objNames != null && objNames.size() == 1) {
290             result = (ObjectName JavaDoc) objNames.iterator().next();
291         } else {
292             throw new ManagementException("InstallationService Mbean can't be "
293                     + "correctly retrieved");
294         }
295         return result;
296     }
297
298     // ////////////////////////////
299
// manage components : bindings and engines
300
// ////////////////////////////
301

302     /**
303      * Return the component installer associated with this componant name.
304      *
305      * @param name
306      * the component name
307      * @return The related installer, or null
308      */

309     @SuppressWarnings JavaDoc("unchecked")
310     public ObjectName JavaDoc getInstallerByName(String JavaDoc name) {
311         log.call();
312
313         ObjectName JavaDoc result = null;
314
315         Map JavaDoc<ObjectName JavaDoc, Installer> installers = getInstallers();
316
317         synchronized (installers) {
318             for (Iterator JavaDoc iter = installers.entrySet().iterator(); iter
319                     .hasNext()
320                     && result == null;) {
321                 Entry<ObjectName JavaDoc, Installer> entry = (Entry<ObjectName JavaDoc, Installer>) iter
322                         .next();
323                 ObjectName JavaDoc objName = entry.getKey();
324                 Installer installer = entry.getValue();
325
326                 if (installer.getComponentName().equals(name)) {
327                     result = objName;
328                 }
329             }
330         }
331         return result;
332     }
333
334     /**
335      * Return the <code>MBeanNamesImpl</code> element which allows the
336      * creation of mBeans names.
337      *
338      * @return
339      */

340     public MBeanNamesImpl getMBeanNames() {
341         return mBeanNames;
342     }
343
344     /**
345      * Return the JMX server used by the JBI container.
346      *
347      * @return
348      */

349     public MBeanServer JavaDoc getMBeanServer() {
350         return adminAttributes.getRawMBeanServer();
351     }
352
353     /**
354      * Return the service assembly associated with this name.
355      *
356      * @param saName
357      * @return The related ServiceAssemblyLifeCycle, or null
358      */

359     public ServiceAssemblyLifeCycle getServiceAssemblyByName(String JavaDoc saName) {
360         log.call();
361
362         ServiceAssemblyLifeCycle result = null;
363
364         Map JavaDoc<String JavaDoc, ServiceAssemblyLifeCycle> sas = this
365                 .getServiceAssemblyLifeCycles();
366
367         synchronized (sas) {
368             result = sas.get(saName);
369         }
370         return result;
371     }
372
373     /**
374      * Return the synchronized map of binding components.
375      *
376      * @return
377      */

378     public Map JavaDoc<ObjectName JavaDoc, ComponentLifeCycle> getBindingCompoLifeCycles() {
379         return bindingCompoLifeCycles;
380     }
381
382     /**
383      * Return the synchronized map of engine components.
384      *
385      * @return
386      */

387     public Map JavaDoc<ObjectName JavaDoc, ComponentLifeCycle> getEngineCompoLifeCycles() {
388         return engineCompoLifeCycles;
389     }
390
391     /**
392      * Return the synchronized map of installers.
393      *
394      * @return
395      */

396     public Map JavaDoc<ObjectName JavaDoc, Installer> getInstallers() {
397         return installers;
398     }
399
400     /**
401      * Return the synchronized map of deployed service asse mbly.
402      *
403      * @return
404      */

405     public Map JavaDoc<String JavaDoc, ServiceAssemblyLifeCycle> getServiceAssemblyLifeCycles() {
406         return serviceAssemblyLifeCycles;
407     }
408
409     /**
410      * Return the synchronized map of system services components.
411      *
412      * @return
413      */

414     public Map JavaDoc<ObjectName JavaDoc, LifeCycleAbstract> getSystemServices() {
415         return systemServices;
416     }
417
418     /**
419      * Register a mbean in the JMX server representing the specified binding
420      * component.
421      *
422      * @param componentElement
423      * @throws ManagementException
424      */

425     public ObjectName JavaDoc registerBindingComponent(ComponentLifeCycle lifeCycle)
426         throws ManagementException {
427
428         ObjectName JavaDoc name = lifeCycle.getMBeanName();
429
430         Map JavaDoc<ObjectName JavaDoc, ComponentLifeCycle> bindings = getBindingCompoLifeCycles();
431
432         synchronized (bindings) {
433             try {
434                 adminAttributes.getRawMBeanServer().registerMBean(lifeCycle,
435                         name);
436
437                 bindings.put(name, lifeCycle);
438             } catch (Exception JavaDoc e) {
439                 throw new ManagementException(e);
440             }
441         }
442         return name;
443     }
444
445     /**
446      * Register a mbean in the JMX server representing the specified engine
447      * component
448      *
449      * @param componentElement
450      * @throws ManagementException
451      */

452     public ObjectName JavaDoc registerEngineComponent(ComponentLifeCycle lifeCycle)
453         throws ManagementException {
454         ObjectName JavaDoc name = lifeCycle.getMBeanName();
455
456         Map JavaDoc<ObjectName JavaDoc, ComponentLifeCycle> engines = getEngineCompoLifeCycles();
457
458         synchronized (engines) {
459             try {
460                 adminAttributes.getRawMBeanServer().registerMBean(lifeCycle,
461                         name);
462
463                 engines.put(name, lifeCycle);
464             } catch (Exception JavaDoc e) {
465                 throw new ManagementException(e);
466             }
467         }
468         return name;
469     }
470
471     // ////////////////////////////
472
// manage installers
473
// ////////////////////////////
474
/**
475      * Register an installer mbean in the JMX server representing the specified
476      * component installer.
477      *
478      * @param installerMBean
479      * @throws ManagementException
480      */

481     public ObjectName JavaDoc registerInstaller(Installer installerMBean)
482         throws ManagementException {
483
484         ObjectName JavaDoc installerMBeanName = installerMBean.getMBeanName();
485
486         Map JavaDoc<ObjectName JavaDoc, Installer> installers = getInstallers();
487
488         synchronized (installers) {
489             try {
490                 adminAttributes.getRawMBeanServer().registerMBean(
491                         installerMBean, installerMBeanName);
492
493                 installers.put(installerMBeanName, installerMBean);
494             } catch (Exception JavaDoc e) {
495                 throw new ManagementException(e);
496             }
497         }
498         return installerMBeanName;
499     }
500
501     // ////////////////////////////
502
// manage service assemblies
503
// ////////////////////////////
504
/**
505      * Register a ServiceAssemblyLifeCycle into the a synchronized Map<String,ServiceAssemblyLifeCycle>
506      *
507      * @param saName
508      * @param serviceAssemblyLifeCycle
509      */

510     public void registerServiceAssembly(String JavaDoc saName,
511             ServiceAssemblyLifeCycle serviceAssemblyLifeCycle) {
512         Map JavaDoc<String JavaDoc, ServiceAssemblyLifeCycle> sas = getServiceAssemblyLifeCycles();
513         synchronized (sas) {
514             sas.put(saName, serviceAssemblyLifeCycle);
515         }
516     }
517
518     /**
519      * @see org.objectweb.fractal.api.control.LifeCycleController#startFc()
520      */

521     @LifeCycle(on=LifeCycleType.START)
522     public void start() throws IllegalLifeCycleException {
523         log = new LoggingUtil(logger);
524     }
525
526     // ----------------------------------------------------------------------
527
// Fractal BindingController implementation
528
// ----------------------------------------------------------------------
529

530     /**
531      * Unregister the binding component of the JMX server
532      *
533      * @throws ManagementException
534      */

535     public void unregisterBindingComponent(ObjectName JavaDoc name)
536         throws ManagementException {
537         Map JavaDoc bindings = getBindingCompoLifeCycles();
538
539         synchronized (bindings) {
540             try {
541                 adminAttributes.getRawMBeanServer().unregisterMBean(name);
542
543                 bindings.remove(name);
544             } catch (Exception JavaDoc e) {
545                 throw new ManagementException(e);
546             }
547         }
548     }
549
550     /**
551      * Unregister the engine component of the JMX server
552      *
553      * @throws ManagementException
554      */

555     public void unregisterEngineComponent(ObjectName JavaDoc name)
556         throws ManagementException {
557         Map JavaDoc engines = getEngineCompoLifeCycles();
558
559         synchronized (engines) {
560             try {
561                 adminAttributes.getRawMBeanServer().unregisterMBean(name);
562
563                 engines.remove(name);
564             } catch (Exception JavaDoc e) {
565                 throw new ManagementException(e);
566             }
567         }
568     }
569
570     // ----------------------------------------------------------------------
571
// Fractal LifecycleController implementation
572
// ----------------------------------------------------------------------
573

574     /**
575      * Unregister the component installer of the JMX server
576      *
577      * @throws ManagementException
578      */

579     public void unregisterInstaller(ObjectName JavaDoc name) throws ManagementException {
580         Map JavaDoc<ObjectName JavaDoc, Installer> installers = getInstallers();
581
582         synchronized (installers) {
583             try {
584                 adminAttributes.getRawMBeanServer().unregisterMBean(name);
585
586                 installers.remove(name);
587             } catch (Exception JavaDoc e) {
588                 throw new ManagementException(e);
589             }
590         }
591     }
592
593     /**
594      * Unregister the ServiceAssembly life cycle from the sync map
595      *
596      * @throws ManagementException
597      */

598     public void unregisterServiceAssembly(String JavaDoc saName)
599         throws ManagementException {
600         Map JavaDoc sas = getServiceAssemblyLifeCycles();
601
602         synchronized (sas) {
603             try {
604                 sas.remove(saName);
605             } catch (Exception JavaDoc e) {
606                 throw new ManagementException(e);
607             }
608         }
609     }
610
611     /**
612      * Return the binding component associated with this name.
613      *
614      * @param name
615      * @return The related objectName, or null
616      */

617     private ComponentLifeCycle getBindingComponentByName(String JavaDoc name) {
618         log.call();
619
620         ComponentLifeCycle result = null;
621
622         Map JavaDoc bindings = this.getBindingCompoLifeCycles();
623
624         synchronized (bindings) {
625             for (Iterator JavaDoc iter = bindings.values().iterator(); iter.hasNext()
626                     && result == null;) {
627                 ComponentLifeCycle lifeCycle = (ComponentLifeCycle) iter.next();
628
629                 if (lifeCycle.getName().equals(name)) {
630                     result = lifeCycle;
631                 }
632             }
633         }
634         return result;
635     }
636
637     /**
638      * Return the engine component associated with this name.
639      *
640      * @param name
641      * @return The related objectName, or null
642      */

643     private ComponentLifeCycle getEngineComponentByName(String JavaDoc name) {
644         log.call();
645
646         ComponentLifeCycle result = null;
647
648         Map JavaDoc engines = this.getEngineCompoLifeCycles();
649
650         synchronized (engines) {
651             for (Iterator JavaDoc iter = engines.values().iterator(); iter.hasNext()
652                     && result == null;) {
653                 ComponentLifeCycle lifeCycle = (ComponentLifeCycle) iter.next();
654
655                 if (lifeCycle.getName().equals(name)) {
656                     result = lifeCycle;
657                 }
658             }
659         }
660         return result;
661     }
662 }
663
Popular Tags