1 10 11 package org.mule.impl.model; 12 13 import java.beans.ExceptionListener ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 import java.util.Map ; 17 18 import org.apache.commons.logging.Log; 19 import org.apache.commons.logging.LogFactory; 20 import org.mule.MuleManager; 21 import org.mule.config.i18n.Message; 22 import org.mule.config.i18n.Messages; 23 import org.mule.impl.DefaultComponentExceptionStrategy; 24 import org.mule.impl.DefaultLifecycleAdapterFactory; 25 import org.mule.impl.ImmutableMuleDescriptor; 26 import org.mule.impl.MuleSession; 27 import org.mule.impl.internal.notifications.ModelNotification; 28 import org.mule.model.DynamicEntryPointResolver; 29 import org.mule.umo.UMOComponent; 30 import org.mule.umo.UMODescriptor; 31 import org.mule.umo.UMOException; 32 import org.mule.umo.UMOSession; 33 import org.mule.umo.lifecycle.Initialisable; 34 import org.mule.umo.lifecycle.InitialisationException; 35 import org.mule.umo.lifecycle.UMOLifecycleAdapterFactory; 36 import org.mule.umo.manager.UMOServerNotification; 37 import org.mule.umo.model.ModelException; 38 import org.mule.umo.model.UMOEntryPointResolver; 39 import org.mule.umo.model.UMOModel; 40 41 import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap; 42 import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentSkipListMap; 43 import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicBoolean; 44 45 50 public abstract class AbstractModel implements UMOModel 51 { 52 55 protected transient Log logger = LogFactory.getLog(getClass()); 56 57 private String name; 58 private UMOEntryPointResolver entryPointResolver; 59 private UMOLifecycleAdapterFactory lifecycleAdapterFactory; 60 61 private Map components; 62 63 66 protected Map descriptors; 67 68 private AtomicBoolean initialised = new AtomicBoolean(false); 69 70 private AtomicBoolean started = new AtomicBoolean(false); 71 72 private ExceptionListener exceptionListener; 73 74 77 public AbstractModel() 78 { 79 entryPointResolver = new DynamicEntryPointResolver(); 82 lifecycleAdapterFactory = new DefaultLifecycleAdapterFactory(); 83 components = new ConcurrentSkipListMap(); 84 descriptors = new ConcurrentHashMap(); 85 exceptionListener = new DefaultComponentExceptionStrategy(); 86 name = "mule"; 87 } 88 89 94 public String getName() 95 { 96 return name; 97 } 98 99 104 public void setName(String name) 105 { 106 this.name = name; 107 } 108 109 114 public UMOEntryPointResolver getEntryPointResolver() 115 { 116 return entryPointResolver; 117 } 118 119 124 public void setEntryPointResolver(UMOEntryPointResolver entryPointResolver) 125 { 126 this.entryPointResolver = entryPointResolver; 127 } 128 129 134 public boolean isComponentRegistered(String name) 135 { 136 return (components.get(name) != null); 137 } 138 139 144 public UMOComponent registerComponent(UMODescriptor descriptor) throws UMOException 145 { 146 if (descriptor == null) 147 { 148 throw new ModelException(new Message(Messages.X_IS_NULL, "UMO Descriptor")); 149 } 150 151 if (initialised.get()) 152 { 153 descriptor.initialise(); 154 } 155 if (descriptor.getExceptionListener() == null) 157 { 158 descriptor.setExceptionListener(exceptionListener); 159 } 160 161 if (descriptors.get(descriptor.getName()) != null) 163 { 164 throw new ModelException(new Message(Messages.DESCRIPTOR_X_ALREADY_EXISTS, descriptor.getName())); 165 } 166 167 UMOComponent component = (UMOComponent)components.get(descriptor.getName()); 168 169 if (component == null) 170 { 171 component = createComponent(descriptor); 172 descriptors.put(descriptor.getName(), descriptor); 173 components.put(descriptor.getName(), component); 174 } 175 176 logger.debug("Added Mule UMO: " + descriptor.getName()); 177 178 if (initialised.get()) 179 { 180 logger.info("Initialising component: " + descriptor.getName()); 181 component.initialise(); 182 } 183 if (started.get()) 184 { 185 logger.info("Starting component: " + descriptor.getName()); 186 component.start(); 187 } 188 return component; 189 } 190 191 public void unregisterComponent(UMODescriptor descriptor) throws UMOException 192 { 193 if (descriptor == null) 194 { 195 throw new ModelException(new Message(Messages.X_IS_NULL, "UMO Descriptor")); 196 } 197 198 if (!isComponentRegistered(descriptor.getName())) 199 { 200 throw new ModelException(new Message(Messages.COMPONENT_X_NOT_REGISTERED, descriptor.getName())); 201 } 202 UMOComponent component = (UMOComponent)components.remove(descriptor.getName()); 203 204 if (component != null) 205 { 206 component.stop(); 207 descriptors.remove(descriptor.getName()); 208 component.dispose(); 209 logger.info("The component: " + descriptor.getName() + " has been unregistered and disposing"); 210 } 211 } 212 213 218 public UMOLifecycleAdapterFactory getLifecycleAdapterFactory() 219 { 220 return lifecycleAdapterFactory; 221 } 222 223 228 public void setLifecycleAdapterFactory(UMOLifecycleAdapterFactory lifecycleAdapterFactory) 229 { 230 this.lifecycleAdapterFactory = lifecycleAdapterFactory; 231 } 232 233 236 public void dispose() 237 { 238 fireNotification(new ModelNotification(this, ModelNotification.MODEL_DISPOSING)); 239 240 for (Iterator i = components.values().iterator(); i.hasNext();) 241 { 242 UMOComponent component = (UMOComponent)i.next(); 243 try 244 { 245 component.dispose(); 246 logger.info(component + " has been destroyed successfully"); 247 } 248 catch (Exception e1) 249 { 250 logger.warn("Failed to dispose component: " + e1.getMessage()); 251 } 252 } 253 254 components.clear(); 255 descriptors.clear(); 256 components = null; 257 descriptors = null; 258 259 fireNotification(new ModelNotification(this, ModelNotification.MODEL_DISPOSED)); 260 } 261 262 268 public UMOSession getComponentSession(String muleName) 269 { 270 UMOComponent component = (UMOComponent)components.get(muleName); 271 if (component == null) 272 { 273 logger.warn("Component: " + muleName + " not found returning null session"); 274 return null; 275 } 276 else 277 { 278 return new MuleSession(component); 279 } 280 } 281 282 287 public void stop() throws UMOException 288 { 289 fireNotification(new ModelNotification(this, ModelNotification.MODEL_STOPPING)); 290 for (Iterator i = components.values().iterator(); i.hasNext();) 291 { 292 UMOComponent component = (UMOComponent)i.next(); 293 component.stop(); 294 logger.info("Component " + component + " has been stopped successfully"); 295 } 296 fireNotification(new ModelNotification(this, ModelNotification.MODEL_STOPPED)); 297 } 298 299 304 public void start() throws UMOException 305 { 306 if (!initialised.get()) 307 { 308 initialise(); 309 } 310 311 if (!started.get()) 312 { 313 fireNotification(new ModelNotification(this, ModelNotification.MODEL_STARTING)); 314 315 for (Iterator i = components.values().iterator(); i.hasNext();) 316 { 317 AbstractComponent component = (AbstractComponent)i.next(); 318 319 if (component.getDescriptor().getInitialState().equals( 320 ImmutableMuleDescriptor.INITIAL_STATE_STARTED)) 321 { 322 component.start(); 323 logger.info("Component " + component + " has been started successfully"); 324 } 325 else if (component.getDescriptor().getInitialState().equals( 326 ImmutableMuleDescriptor.INITIAL_STATE_PAUSED)) 327 { 328 component.start(true); 329 logger.info("Component " + component 330 + " has been started and paused (initial state = 'paused')"); 331 } 332 else 333 { 334 logger.info("Component " + component 335 + " has not been started (initial state = 'stopped')"); 336 } 337 } 338 started.set(true); 339 fireNotification(new ModelNotification(this, ModelNotification.MODEL_STARTED)); 340 } 341 else 342 { 343 logger.debug("Model already started"); 344 } 345 } 346 347 355 public void startComponent(String name) throws UMOException 356 { 357 UMOComponent component = (UMOComponent)components.get(name); 358 if (component == null) 359 { 360 throw new ModelException(new Message(Messages.COMPONENT_X_NOT_REGISTERED, name)); 361 } 362 else 363 { 364 component.start(); 365 logger.info("Mule " + component.toString() + " has been started successfully"); 366 } 367 } 368 369 376 public void stopComponent(String name) throws UMOException 377 { 378 UMOComponent component = (UMOComponent)components.get(name); 379 if (component == null) 380 { 381 throw new ModelException(new Message(Messages.COMPONENT_X_NOT_REGISTERED, name)); 382 } 383 else 384 { 385 component.stop(); 386 logger.info("mule " + name + " has been stopped successfully"); 387 } 388 } 389 390 403 public void pauseComponent(String name) throws UMOException 404 { 405 UMOComponent component = (UMOComponent)components.get(name); 406 407 if (component != null) 408 { 409 component.pause(); 410 logger.info("Mule Component " + name + " has been paused successfully"); 411 } 412 else 413 { 414 throw new ModelException(new Message(Messages.COMPONENT_X_NOT_REGISTERED, name)); 415 } 416 } 417 418 426 public void resumeComponent(String name) throws UMOException 427 { 428 UMOComponent component = (UMOComponent)components.get(name); 429 430 if (component != null) 431 { 432 component.resume(); 433 logger.info("Mule Component " + name + " has been resumed successfully"); 434 } 435 else 436 { 437 throw new ModelException(new Message(Messages.COMPONENT_X_NOT_REGISTERED, name)); 438 } 439 } 440 441 public void setComponents(List descriptors) throws UMOException 442 { 443 for (Iterator iterator = descriptors.iterator(); iterator.hasNext();) 444 { 445 registerComponent((UMODescriptor)iterator.next()); 446 } 447 } 448 449 public void initialise() throws InitialisationException 450 { 451 if (!initialised.get()) 452 { 453 fireNotification(new ModelNotification(this, ModelNotification.MODEL_INITIALISING)); 454 455 if (exceptionListener instanceof Initialisable) 456 { 457 ((Initialisable)exceptionListener).initialise(); 458 } 459 UMOComponent component = null; 460 for (Iterator i = components.values().iterator(); i.hasNext();) 461 { 462 component = (UMOComponent)i.next(); 463 component.initialise(); 464 465 logger.info("Component " + component.getDescriptor().getName() 466 + " has been started successfully"); 467 } 468 initialised.set(true); 469 fireNotification(new ModelNotification(this, ModelNotification.MODEL_INITIALISED)); 470 } 471 else 472 { 473 logger.debug("Model already initialised"); 474 } 475 } 476 477 public ExceptionListener getExceptionListener() 478 { 479 return exceptionListener; 480 } 481 482 public void setExceptionListener(ExceptionListener exceptionListener) 483 { 484 this.exceptionListener = exceptionListener; 485 } 486 487 public UMODescriptor getDescriptor(String name) 488 { 489 return (UMODescriptor)descriptors.get(name); 490 } 491 492 public UMOComponent getComponent(String name) 493 { 494 return (UMOComponent)components.get(name); 495 } 496 497 502 public Iterator getComponentNames() 503 { 504 return components.keySet().iterator(); 505 } 506 507 void fireNotification(UMOServerNotification notification) 508 { 509 MuleManager.getInstance().fireNotification(notification); 510 } 511 512 protected abstract UMOComponent createComponent(UMODescriptor descriptor); 513 } 514 | Popular Tags |