001: /*
002: * $Id: DefaultJavaComponent.java 11382 2008-03-17 11:55:54Z dfeist $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010:
011: package org.mule.component;
012:
013: import org.mule.api.MuleException;
014: import org.mule.api.component.JavaComponent;
015: import org.mule.api.component.LifecycleAdapter;
016: import org.mule.api.lifecycle.InitialisationException;
017: import org.mule.api.model.EntryPointResolverSet;
018: import org.mule.api.model.ModelException;
019: import org.mule.api.object.ObjectFactory;
020: import org.mule.api.routing.NestedRouterCollection;
021: import org.mule.config.i18n.CoreMessages;
022: import org.mule.config.i18n.MessageFactory;
023:
024: /**
025: * Default implementation of {@link JavaComponent}. Component lifecycle is
026: * propagated to the component object instance via the {@link LifecycleAdapter}.
027: */
028: public class DefaultJavaComponent extends AbstractJavaComponent {
029:
030: protected LifecycleAdapter singletonComponentLifecycleAdapter;
031:
032: public DefaultJavaComponent() {
033: // For spring only
034: super ();
035: }
036:
037: public DefaultJavaComponent(ObjectFactory objectFactory) {
038: super (objectFactory);
039: }
040:
041: public DefaultJavaComponent(ObjectFactory objectFactory,
042: EntryPointResolverSet entryPointResolverSet,
043: NestedRouterCollection nestedRouterCollection) {
044: super (objectFactory, entryPointResolverSet,
045: nestedRouterCollection);
046: }
047:
048: protected void doStart() throws MuleException {
049: super .doStart();
050:
051: // If this component is using a SingletonObjectFactory we should create
052: // LifecycleAdaptor wrapper just once now and not on each event. This also
053: // allows start/stop life-cycle methods to be propagated to singleton
054: // component instances.
055: if (objectFactory != null && objectFactory.isSingleton()) {
056: // On first call, create and initialise singleton instance
057: try {
058: if (singletonComponentLifecycleAdapter == null) {
059: singletonComponentLifecycleAdapter = createLifeCycleAdaptor();
060: }
061: } catch (Exception e) {
062: throw new InitialisationException(
063: MessageFactory
064: .createStaticMessage("Unable to create instance of POJO service"),
065: e, this );
066:
067: }
068: // On all calls, start if not started.
069: if (!singletonComponentLifecycleAdapter.isStarted()) {
070: try {
071: singletonComponentLifecycleAdapter.start();
072: } catch (Exception e) {
073: throw new ModelException(CoreMessages
074: .failedToStart("Service '"
075: + service.getName() + "'"), e);
076: }
077: }
078: }
079: }
080:
081: protected void doStop() throws MuleException {
082: super .doStop();
083: // It only makes sense to propagate this life-cycle to singleton component
084: // implementations
085: if (singletonComponentLifecycleAdapter != null) {
086: if (singletonComponentLifecycleAdapter.isStarted()) {
087: try {
088: singletonComponentLifecycleAdapter.stop();
089: } catch (Exception e) {
090: throw new ModelException(CoreMessages
091: .failedToStop("Service '"
092: + service.getName() + "'"), e);
093: }
094: }
095: }
096: }
097:
098: protected void doDispose() {
099: super .doDispose();
100: // It only makes sense to propagating this life-cycle to singleton component
101: // implementations
102: if (singletonComponentLifecycleAdapter != null) {
103: singletonComponentLifecycleAdapter.dispose();
104: }
105: }
106:
107: protected LifecycleAdapter borrowComponentLifecycleAdaptor()
108: throws Exception {
109: LifecycleAdapter componentLifecycleAdapter;
110: if (singletonComponentLifecycleAdapter != null) {
111: componentLifecycleAdapter = singletonComponentLifecycleAdapter;
112: } else {
113: componentLifecycleAdapter = createLifeCycleAdaptor();
114: componentLifecycleAdapter.start();
115: }
116: return componentLifecycleAdapter;
117: }
118:
119: protected void returnComponentLifecycleAdaptor(
120: LifecycleAdapter lifecycleAdapter) throws Exception {
121: if (singletonComponentLifecycleAdapter == null
122: && lifecycleAdapter != null) {
123: lifecycleAdapter.stop();
124: lifecycleAdapter.dispose();
125: lifecycleAdapter = null;
126: }
127: }
128:
129: }
|