001: /*
002: * $Id: PooledJavaComponent.java 11379 2008-03-17 02:46:56Z 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.Component;
015: import org.mule.api.component.LifecycleAdapter;
016: import org.mule.api.lifecycle.InitialisationCallback;
017: import org.mule.api.lifecycle.InitialisationException;
018: import org.mule.api.lifecycle.LifecycleTransitionResult;
019: import org.mule.api.model.EntryPointResolverSet;
020: import org.mule.api.object.ObjectFactory;
021: import org.mule.api.routing.NestedRouterCollection;
022: import org.mule.config.PoolingProfile;
023: import org.mule.util.pool.DefaultLifecycleEnabledObjectPool;
024: import org.mule.util.pool.LifecyleEnabledObjectPool;
025: import org.mule.util.pool.ObjectPool;
026:
027: /**
028: * <code>PooledJavaComponent</code> implements pooling.
029: */
030: public class PooledJavaComponent extends AbstractJavaComponent {
031:
032: protected PoolingProfile poolingProfile;
033: protected LifecyleEnabledObjectPool lifecycleAdapterPool;
034:
035: public PooledJavaComponent() {
036: super ();
037: }
038:
039: public PooledJavaComponent(ObjectFactory objectFactory) {
040: this (objectFactory, null);
041: }
042:
043: public PooledJavaComponent(ObjectFactory objectFactory,
044: PoolingProfile poolingProfile) {
045: super (objectFactory);
046: this .poolingProfile = poolingProfile;
047: }
048:
049: public PooledJavaComponent(ObjectFactory objectFactory,
050: PoolingProfile poolingProfile,
051: EntryPointResolverSet entryPointResolverSet,
052: NestedRouterCollection nestedRouterCollection) {
053: super (objectFactory, entryPointResolverSet,
054: nestedRouterCollection);
055: this .poolingProfile = poolingProfile;
056: }
057:
058: protected LifecycleAdapter borrowComponentLifecycleAdaptor()
059: throws Exception {
060: return (LifecycleAdapter) lifecycleAdapterPool.borrowObject();
061: }
062:
063: protected void returnComponentLifecycleAdaptor(
064: LifecycleAdapter lifecycleAdapter) {
065: lifecycleAdapterPool.returnObject(lifecycleAdapter);
066: }
067:
068: protected void doStart() throws MuleException {
069: super .doStart();
070: // Wrap pool's objectFactory with a LifeCycleAdaptor factory so we pool
071: // LifeCycleAdaptor's and not just pojo instances.
072: lifecycleAdapterPool = new DefaultLifecycleEnabledObjectPool(
073: new LifeCycleAdaptorFactory(), poolingProfile);
074: lifecycleAdapterPool.initialise();
075: lifecycleAdapterPool.start();
076: }
077:
078: protected void doStop() throws MuleException {
079: super .doStop();
080: if (lifecycleAdapterPool != null) {
081: lifecycleAdapterPool.stop();
082: lifecycleAdapterPool.close();
083: lifecycleAdapterPool = null;
084: }
085: }
086:
087: public void setPoolingProfile(PoolingProfile poolingProfile) {
088: // TODO What happens if this is set while component is started? Should we i)
089: // do nothing ii) issue warning iii) stop/start the pool
090: // (!!) iv) throw exception?
091: this .poolingProfile = poolingProfile;
092: }
093:
094: public PoolingProfile getPoolingProfile() {
095: return poolingProfile;
096: }
097:
098: /**
099: * <code>LifeCycleAdaptorFactory</code> wraps the Component' s
100: * {@link ObjectFactory}. The LifeCycleAdaptorFactory "getInstance()" method
101: * creates a new {@link LifecycleAdapter} wrapping the object instance obtained
102: * for the component instance {@link ObjectFactory} set on the {@link Component}.<br/>
103: * This allows us to keep {@link LifecycleAdapter} creation in the Component and
104: * out of the {@link DefaultLifecyleEnabledObjectPool} and to use the generic
105: * {@link ObjectPool} interface.
106: */
107: protected class LifeCycleAdaptorFactory implements ObjectFactory {
108:
109: public Object getInstance() throws Exception {
110: return createLifeCycleAdaptor();
111: }
112:
113: public Class getObjectClass() {
114: return LifecycleAdapter.class;
115: }
116:
117: public LifecycleTransitionResult initialise()
118: throws InitialisationException {
119: return objectFactory.initialise();
120: }
121:
122: public void dispose() {
123: objectFactory.dispose();
124: }
125:
126: public void addObjectInitialisationCallback(
127: InitialisationCallback callback) {
128: objectFactory.addObjectInitialisationCallback(callback);
129:
130: }
131:
132: public boolean isSingleton() {
133: return false;
134: }
135: }
136:
137: }
|