1 22 package org.objectweb.petals.jbi.component.lifecycle; 23 24 import java.net.URI ; 25 import java.util.Collections ; 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 import java.util.Map ; 29 30 import javax.jbi.JBIException; 31 import javax.management.AttributeChangeNotification ; 32 import javax.management.Notification ; 33 import javax.management.ObjectName ; 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 49 public class ServiceAssemblyLifeCycle extends LifeCycleAbstract { 50 51 private URI installationRoot; 52 53 private SystemState recoverySrv; 54 55 private RepositoryService repositoryService; 56 57 private ServiceAssembly serviceAssembly; 58 59 private Map <String , ServiceUnitLifeCycle> serviceUnitsLifeCycles; 60 61 73 public ServiceAssemblyLifeCycle(ObjectName mbeanName, LoggingUtil log, 74 ServiceAssembly serviceAssembly, 75 RepositoryService repositoryService, SystemState recoverySrv, 76 URI 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 <String , ServiceUnitLifeCycle>()); 84 } 85 86 89 @Override 90 public void doInit() throws JBIException { 91 92 } 93 94 @Override 95 public void doShutdown() throws JBIException { 96 Map <String , ServiceUnitLifeCycle> sus = this 97 .getServiceUnitsLifeCycles(); 98 99 synchronized (sus) { 100 for (Iterator iter = sus.values().iterator(); iter.hasNext();) { 101 ServiceUnitLifeCycle lifeCycle = (ServiceUnitLifeCycle) iter 102 .next(); 103 lifeCycle.shutDown(); 104 } 105 } 106 } 107 108 @Override 109 public void doStart() throws JBIException { 110 Map <String , ServiceUnitLifeCycle> sus = this 111 .getServiceUnitsLifeCycles(); 112 113 synchronized (sus) { 114 for (Iterator iter = sus.values().iterator(); iter.hasNext();) { 115 ServiceUnitLifeCycle lifeCycle = (ServiceUnitLifeCycle) iter 116 .next(); 117 lifeCycle.start(); 118 } 119 } 120 } 121 122 @Override 123 public void doStop() throws JBIException { 124 Map <String , ServiceUnitLifeCycle> sus = this 125 .getServiceUnitsLifeCycles(); 126 127 synchronized (sus) { 128 for (Iterator iter = sus.values().iterator(); iter.hasNext();) { 129 ServiceUnitLifeCycle lifeCycle = (ServiceUnitLifeCycle) iter 130 .next(); 131 lifeCycle.stop(); 132 } 133 } 134 } 135 136 public URI getInstallationRoot() { 137 return installationRoot; 138 } 139 140 public ServiceAssembly getServiceAssembly() { 141 return serviceAssembly; 142 } 143 144 149 public Map <String , ServiceUnitLifeCycle> getServiceUnitsLifeCycles() { 150 return serviceUnitsLifeCycles; 151 } 152 153 157 167 public void registerSUIntoSA(String suName, 168 ServiceUnitLifeCycle serviceUnitLifeCycle) throws JBIException { 169 Map <String , 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 187 public void unregisterSUFromSA(String suName) { 188 Map <String , ServiceUnitLifeCycle> sus = getServiceUnitsLifeCycles(); 189 synchronized (sus) { 190 sus.remove(suName); 191 } 192 } 193 194 201 public void removePackage() throws PetalsException { 202 repositoryService.removeServiceAssemblyPackage(serviceAssembly 203 .getIdentification().getName()); 204 } 205 206 @Override 207 public synchronized void setState(String state) throws JBIException { 208 super.setState(state); 209 String saName = this.serviceAssembly.getIdentification().getName(); 210 try { 211 recoverySrv.updateServiceAssemblyState(saName, state); 212 } catch (Exception e) { 213 String 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 222 @Override 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 re) { 235 setState(UNKNOWN); 236 237 String 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 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 263 public synchronized void stateChanged(StateChangedEvent event) { 264 log.call(); 265 Notification n = new AttributeChangeNotification (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 275 public synchronized void stateChangeFailed(StateChangeFailedEvent event) { 276 log.call(); 277 Notification n = new AttributeChangeNotification (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 288 @Override 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 re) { 303 setState(UNKNOWN); 304 305 String 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 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 |