001: /*
002: * $Id: AbstractModel.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.model;
012:
013: import org.mule.api.MuleException;
014: import org.mule.api.MuleContext;
015: import org.mule.api.component.LifecycleAdapterFactory;
016: import org.mule.api.context.notification.ServerNotification;
017: import org.mule.api.lifecycle.InitialisationException;
018: import org.mule.api.lifecycle.LifecycleTransitionResult;
019: import org.mule.api.model.EntryPointResolver;
020: import org.mule.api.model.EntryPointResolverSet;
021: import org.mule.api.model.Model;
022: import org.mule.component.DefaultLifecycleAdapterFactory;
023: import org.mule.context.notification.ModelNotification;
024: import org.mule.model.resolvers.DefaultEntryPointResolverSet;
025: import org.mule.model.resolvers.LegacyEntryPointResolverSet;
026: import org.mule.service.DefaultServiceExceptionStrategy;
027:
028: import java.beans.ExceptionListener;
029: import java.util.Collection;
030: import java.util.Iterator;
031:
032: import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicBoolean;
033:
034: import org.apache.commons.logging.Log;
035: import org.apache.commons.logging.LogFactory;
036:
037: /**
038: * <code>MuleModel</code> is the default implementation of the Model. The model
039: * encapsulates and manages the runtime behaviour of a Mule Server instance. It is
040: * responsible for maintaining the UMOs instances and their configuration.
041: */
042: public abstract class AbstractModel implements Model {
043:
044: public static final String DEFAULT_MODEL_NAME = "main";
045:
046: private String name = DEFAULT_MODEL_NAME;
047: private EntryPointResolverSet entryPointResolverSet = null; // values are supplied below as required
048: private LifecycleAdapterFactory lifecycleAdapterFactory = new DefaultLifecycleAdapterFactory();
049: private AtomicBoolean initialised = new AtomicBoolean(false);
050: private AtomicBoolean started = new AtomicBoolean(false);
051: private ExceptionListener exceptionListener = new DefaultServiceExceptionStrategy();
052:
053: protected transient Log logger = LogFactory.getLog(getClass());
054: protected MuleContext muleContext;
055:
056: /*
057: * (non-Javadoc)
058: *
059: * @see org.mule.api.UMOModel#getName()
060: */
061: public String getName() {
062: return name;
063: }
064:
065: /*
066: * (non-Javadoc)
067: *
068: * @see org.mule.api.UMOModel#setName(java.lang.String)
069: */
070: public void setName(String name) {
071: this .name = name;
072: }
073:
074: /*
075: * (non-Javadoc)
076: *
077: * @see org.mule.api.model.Model#getEntryPointResolver()
078: */
079: public EntryPointResolverSet getEntryPointResolverSet() {
080: if (null == entryPointResolverSet) {
081: entryPointResolverSet = new LegacyEntryPointResolverSet();
082: }
083: return entryPointResolverSet;
084: }
085:
086: /*
087: * (non-Javadoc)
088: *
089: * @see org.mule.api.model.Model#setEntryPointResolver(org.mule.api.model.EntryPointResolver)
090: */
091: public void setEntryPointResolverSet(
092: EntryPointResolverSet entryPointResolverSet) {
093: this .entryPointResolverSet = entryPointResolverSet;
094: }
095:
096: /**
097: * This allows us to configure entry point resolvers incrementally
098: *
099: * @param entryPointResolvers Resolvers to add
100: */
101: public void setEntryPointResolvers(Collection entryPointResolvers) {
102: if (null == entryPointResolverSet) {
103: entryPointResolverSet = new DefaultEntryPointResolverSet();
104: }
105: for (Iterator resolvers = entryPointResolvers.iterator(); resolvers
106: .hasNext();) {
107: entryPointResolverSet
108: .addEntryPointResolver((EntryPointResolver) resolvers
109: .next());
110: }
111: }
112:
113: /*
114: * (non-Javadoc)
115: *
116: * @see org.mule.api.model.Model#getLifecycleAdapterFactory()
117: */
118: public LifecycleAdapterFactory getLifecycleAdapterFactory() {
119: return lifecycleAdapterFactory;
120: }
121:
122: /*
123: * (non-Javadoc)
124: *
125: * @see org.mule.api.model.Model#setLifecycleAdapterFactory(org.mule.api.lifecycle.LifecycleAdapterFactory)
126: */
127: public void setLifecycleAdapterFactory(
128: LifecycleAdapterFactory lifecycleAdapterFactory) {
129: this .lifecycleAdapterFactory = lifecycleAdapterFactory;
130: }
131:
132: /** Destroys any current components */
133: public void dispose() {
134: fireNotification(new ModelNotification(this ,
135: ModelNotification.MODEL_DISPOSING));
136: fireNotification(new ModelNotification(this ,
137: ModelNotification.MODEL_DISPOSED));
138: }
139:
140: /**
141: * Stops any registered components
142: *
143: * @throws MuleException if a Service fails tcomponent
144: */
145: public LifecycleTransitionResult stop() throws MuleException {
146: fireNotification(new ModelNotification(this ,
147: ModelNotification.MODEL_STOPPING));
148: started.set(false);
149: fireNotification(new ModelNotification(this ,
150: ModelNotification.MODEL_STOPPED));
151: return LifecycleTransitionResult.OK;
152: }
153:
154: /**
155: * Starts all registered components
156: *
157: * @throws MuleException if any of the components fail to start
158: */
159: public LifecycleTransitionResult start() throws MuleException {
160: if (!initialised.get()) {
161: throw new IllegalStateException("Not Initialised");
162: }
163:
164: if (!started.get()) {
165: fireNotification(new ModelNotification(this ,
166: ModelNotification.MODEL_STARTING));
167: started.set(true);
168: fireNotification(new ModelNotification(this ,
169: ModelNotification.MODEL_STARTED));
170: } else {
171: logger.debug("Model already started");
172: }
173: return LifecycleTransitionResult.OK;
174: }
175:
176: public LifecycleTransitionResult initialise()
177: throws InitialisationException {
178: if (!initialised.get()) {
179: fireNotification(new ModelNotification(this ,
180: ModelNotification.MODEL_INITIALISING));
181: initialised.set(true);
182: fireNotification(new ModelNotification(this ,
183: ModelNotification.MODEL_INITIALISED));
184: } else {
185: logger.debug("Model already initialised");
186: }
187: return LifecycleTransitionResult.OK;
188: }
189:
190: public ExceptionListener getExceptionListener() {
191: return exceptionListener;
192: }
193:
194: public void setExceptionListener(ExceptionListener exceptionListener) {
195: this .exceptionListener = exceptionListener;
196: }
197:
198: void fireNotification(ServerNotification notification) {
199: if (muleContext != null) {
200: muleContext.fireNotification(notification);
201: } else if (logger.isDebugEnabled()) {
202: logger
203: .debug("MuleContext is not yet available for firing notifications, ignoring event: "
204: + notification);
205: }
206: }
207:
208: public void setMuleContext(MuleContext context) {
209: this.muleContext = context;
210: }
211:
212: }
|