KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > jbi > component > lifecycle > ServiceAssemblyLifeCycle


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: InstallationService.java 154 2006-03-27 15:30:10Z ofabre $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.jbi.component.lifecycle;
23
24 import java.net.URI JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import javax.jbi.JBIException;
31 import javax.management.AttributeChangeNotification JavaDoc;
32 import javax.management.Notification JavaDoc;
33 import javax.management.ObjectName JavaDoc;
34
35 import org.objectweb.petals.PetalsException;
36 import org.objectweb.petals.jbi.component.event.StateChangeFailedEvent;
37 import org.objectweb.petals.jbi.component.event.StateChangedEvent;
38 import org.objectweb.petals.jbi.management.systemstate.SystemState;
39 import org.objectweb.petals.repository.RepositoryService;
40 import org.objectweb.petals.tools.jbicommon.descriptor.ServiceAssembly;
41 import org.objectweb.petals.util.LoggingUtil;
42
43 /**
44  * This class manage the life cycle of deployed service assemblies
45  *
46  * @author Olivier Fabre - EBM Websourcing
47  * @author ddesjardins - eBMWebsourcing
48  */

49 public class ServiceAssemblyLifeCycle extends LifeCycleAbstract {
50
51     private URI JavaDoc installationRoot;
52
53     private SystemState recoverySrv;
54
55     private RepositoryService repositoryService;
56
57     private ServiceAssembly serviceAssembly;
58
59     private Map JavaDoc<String JavaDoc, ServiceUnitLifeCycle> serviceUnitsLifeCycles;
60
61     /**
62      *
63      * @param mbeanName
64      * name of the MBean for this LifeCycle. In fact, the
65      * DeploymentServiceMBean name is used as there is no
66      * SALifeCycleMBean
67      * @param log
68      * @param serviceAssembly
69      * @param repositoryService
70      * @param recoverySrv
71      * @param installationRoot
72      */

73     public ServiceAssemblyLifeCycle(ObjectName JavaDoc mbeanName, LoggingUtil log,
74             ServiceAssembly serviceAssembly,
75             RepositoryService repositoryService, SystemState recoverySrv,
76             URI JavaDoc installationRoot) {
77         super(mbeanName, log);
78         this.installationRoot = installationRoot;
79         this.serviceAssembly = serviceAssembly;
80         this.repositoryService = repositoryService;
81         this.recoverySrv = recoverySrv;
82         this.serviceUnitsLifeCycles = Collections
83                 .synchronizedMap(new HashMap JavaDoc<String JavaDoc, ServiceUnitLifeCycle>());
84     }
85
86     /**
87      * Do nothing
88      */

89     @Override JavaDoc
90     public void doInit() throws JBIException {
91
92     }
93
94     @Override JavaDoc
95     public void doShutdown() throws JBIException {
96         Map JavaDoc<String JavaDoc, ServiceUnitLifeCycle> sus = this
97                 .getServiceUnitsLifeCycles();
98
99         synchronized (sus) {
100             for (Iterator JavaDoc iter = sus.values().iterator(); iter.hasNext();) {
101                 ServiceUnitLifeCycle lifeCycle = (ServiceUnitLifeCycle) iter
102                         .next();
103                 lifeCycle.shutDown();
104             }
105         }
106     }
107
108     @Override JavaDoc
109     public void doStart() throws JBIException {
110         Map JavaDoc<String JavaDoc, ServiceUnitLifeCycle> sus = this
111                 .getServiceUnitsLifeCycles();
112
113         synchronized (sus) {
114             for (Iterator JavaDoc iter = sus.values().iterator(); iter.hasNext();) {
115                 ServiceUnitLifeCycle lifeCycle = (ServiceUnitLifeCycle) iter
116                         .next();
117                 lifeCycle.start();
118             }
119         }
120     }
121
122     @Override JavaDoc
123     public void doStop() throws JBIException {
124         Map JavaDoc<String JavaDoc, ServiceUnitLifeCycle> sus = this
125                 .getServiceUnitsLifeCycles();
126
127         synchronized (sus) {
128             for (Iterator JavaDoc iter = sus.values().iterator(); iter.hasNext();) {
129                 ServiceUnitLifeCycle lifeCycle = (ServiceUnitLifeCycle) iter
130                         .next();
131                 lifeCycle.stop();
132             }
133         }
134     }
135
136     public URI JavaDoc getInstallationRoot() {
137         return installationRoot;
138     }
139
140     public ServiceAssembly getServiceAssembly() {
141         return serviceAssembly;
142     }
143
144     /**
145      * Return the synchronized map of deployed service units.
146      *
147      * @return
148      */

149     public Map JavaDoc<String JavaDoc, ServiceUnitLifeCycle> getServiceUnitsLifeCycles() {
150         return serviceUnitsLifeCycles;
151     }
152
153     // ////////////////////////////////////////////
154
// ElementListener implementation
155
// ////////////////////////////////////////////
156

157     /**
158      * Register a ServiceUnitLifeCycle into a synchronized map of the
159      * ServiceAssemblyLifeCycle
160      *
161      * @param suName
162      * name of a service unit
163      * @param serviceUnitLifeCycle
164      * life cycle of the service unit
165      * @throws JBIException
166      */

167     public void registerSUIntoSA(String JavaDoc suName,
168             ServiceUnitLifeCycle serviceUnitLifeCycle) throws JBIException {
169         Map JavaDoc<String JavaDoc, ServiceUnitLifeCycle> sus = getServiceUnitsLifeCycles();
170         synchronized (sus) {
171             if (!sus.containsKey(suName)) {
172                 sus.put(suName, serviceUnitLifeCycle);
173             } else {
174                 throw new JBIException("Duplicate service unit : same name");
175             }
176
177         }
178     }
179
180     /**
181      * Unregister a ServiceUnitLifeCycle from a synchronized map of the
182      * ServiceAssemblyLifeCycle
183      *
184      * @param suName
185      * name of a service unit
186      */

187     public void unregisterSUFromSA(String JavaDoc suName) {
188         Map JavaDoc<String JavaDoc, ServiceUnitLifeCycle> sus = getServiceUnitsLifeCycles();
189         synchronized (sus) {
190             sus.remove(suName);
191         }
192     }
193
194     /**
195      * Remove the package of the service assembly
196      *
197      * @throws PetalsException
198      * if an error occured during package deletion
199      *
200      */

201     public void removePackage() throws PetalsException {
202         repositoryService.removeServiceAssemblyPackage(serviceAssembly
203                 .getIdentification().getName());
204     }
205
206     @Override JavaDoc
207     public synchronized void setState(String JavaDoc state) throws JBIException {
208         super.setState(state);
209         String JavaDoc saName = this.serviceAssembly.getIdentification().getName();
210         try {
211             recoverySrv.updateServiceAssemblyState(saName, state);
212         } catch (Exception JavaDoc e) {
213             String JavaDoc msg = "Service assembly state can't be persisted for recovery";
214             log.error(msg, e);
215             throw new JBIException(msg, e);
216         }
217     }
218
219     /**
220      * No init state for ServiceAssemblyLifeCycle
221      */

222     @Override JavaDoc
223     public void start() throws JBIException {
224         log.start();
225
226         if (SHUTDOWN.equals(state) || STOPPED.equals(state)) {
227             synchronized (activitySynchronizer) {
228                 try {
229                     setState(STARTING);
230
231                     doStart();
232
233                     setState(STARTED);
234                 } catch (Throwable JavaDoc re) {
235                     setState(UNKNOWN);
236
237                     String JavaDoc msg = "A runtime exception occured during the start.";
238
239                     JBIException e = new JBIException(msg, re);
240
241                     log.error(msg, e);
242
243                     stateChangeFailed(new StateChangeFailedEvent(this, e));
244
245                     throw e;
246                 }
247             }
248         } else {
249             String JavaDoc msg = "The Object can not be started in this state.";
250
251             JBIException e = new JBIException(msg);
252
253             log.error(msg, e);
254
255             stateChangeFailed(new StateChangeFailedEvent(this, e));
256
257             throw e;
258         }
259         log.end();
260     }
261
262     @Override JavaDoc
263     public synchronized void stateChanged(StateChangedEvent event) {
264         log.call();
265         Notification JavaDoc n = new AttributeChangeNotification JavaDoc(mBeanName,
266                 sequenceNumber++, System.currentTimeMillis(),
267                 "State changed : " + event.getOldState() + " -> "
268                         + event.getNewState(), "state", "String", event
269                         .getOldState(), event.getNewState());
270
271         notifSupport.sendNotification(n);
272     }
273
274     @Override JavaDoc
275     public synchronized void stateChangeFailed(StateChangeFailedEvent event) {
276         log.call();
277         Notification JavaDoc n = new AttributeChangeNotification JavaDoc(mBeanName,
278                 sequenceNumber++, System.currentTimeMillis(),
279                 "State change failed : " + event.getException().getMessage(),
280                 "exception", "String", null, event.getException().toString());
281
282         notifSupport.sendNotification(n);
283     }
284
285     /**
286      * No init state for ServiceAssemblyLifeCycle
287      */

288     @Override JavaDoc
289     public void stop() throws JBIException {
290         log.start();
291
292         if (STARTED.equals(state)) {
293             synchronized (activitySynchronizer) {
294                 try {
295
296                     setState(STOPPING);
297
298                     doStop();
299
300                     setState(STOPPED);
301
302                 } catch (Throwable JavaDoc re) {
303                     setState(UNKNOWN);
304
305                     String JavaDoc msg = "A runtime exception occured during the stop.";
306
307                     JBIException e = new JBIException(msg, re);
308
309                     log.error(msg, e);
310
311                     stateChangeFailed(new StateChangeFailedEvent(this, e));
312
313                     throw e;
314                 }
315             }
316         } else {
317             String JavaDoc msg = "The Object can not be stoped in this state.";
318
319             JBIException e = new JBIException(msg);
320
321             log.error(msg, e);
322
323             stateChangeFailed(new StateChangeFailedEvent(this, e));
324
325             throw e;
326         }
327         log.end();
328     }
329 }
330
Popular Tags