diff options
Diffstat (limited to 'tags/java-stable-20060304/sca/core/src/main/java/org/apache/tuscany/core/context/scope')
8 files changed, 0 insertions, 1266 deletions
diff --git a/tags/java-stable-20060304/sca/core/src/main/java/org/apache/tuscany/core/context/scope/AbstractScopeContext.java b/tags/java-stable-20060304/sca/core/src/main/java/org/apache/tuscany/core/context/scope/AbstractScopeContext.java deleted file mode 100644 index 449af4bdf8..0000000000 --- a/tags/java-stable-20060304/sca/core/src/main/java/org/apache/tuscany/core/context/scope/AbstractScopeContext.java +++ /dev/null @@ -1,158 +0,0 @@ -/** - * - * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.tuscany.core.context.scope; - -import java.util.List; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -import org.apache.tuscany.core.builder.RuntimeConfiguration; -import org.apache.tuscany.core.context.AbstractContext; -import org.apache.tuscany.core.context.InstanceContext; -import org.apache.tuscany.core.context.QualifiedName; -import org.apache.tuscany.core.context.EventContext; -import org.apache.tuscany.core.context.Context; -import org.apache.tuscany.core.context.ScopeContext; -import org.apache.tuscany.core.context.TargetException; - -/** - * Implements functionality common to scope contexts. - * <p> - * <b>NB: </b>Minimal synchronization is performed, particularly for initializing and destroying scopes, and it is - * assumed the scope container will block requests until these operations have completed. - * - * @version $Rev$ $Date$ - */ -public abstract class AbstractScopeContext extends AbstractContext implements ScopeContext{ - // ---------------------------------- - // Fields - // ---------------------------------- - - // The collection of runtime configurations for the scope - protected Map<String, RuntimeConfiguration<InstanceContext>> runtimeConfigurations = new ConcurrentHashMap(); - - // The event context the scope container is associated with - protected EventContext eventContext; - - // ---------------------------------- - // Constructors - // ---------------------------------- - - public AbstractScopeContext(EventContext eventContext) { - assert (eventContext != null) : "Event context was null"; - this.eventContext = eventContext; - } - - // ---------------------------------- - // Lifecycle methods - // --------------------------_-------- - - public synchronized void start() { - } - - public synchronized void stop() { - } - - - // ---------------------------------- - // Scope methods - // ---------------------------------- - - public void registerConfigurations(List<RuntimeConfiguration<InstanceContext>> configurations) { - for (RuntimeConfiguration<InstanceContext> configuration : configurations) { - runtimeConfigurations.put(configuration.getName(), configuration); - } - } - - public Object getInstance(QualifiedName qName) throws TargetException { - Object instance = null; - InstanceContext context = getContext(qName.getPartName()); - if (context == null) { - TargetException e = new TargetException("Target not found"); - e.setIdentifier(qName.getQualifiedName()); - throw e; - } - return context.getInstance(qName); - } - - public Object getInstance(QualifiedName qName, boolean notify) throws TargetException { - return getInstance(qName); - } - - //---------------------------------- - // InstanceContext methods - //---------------------------------- - - public Object getImplementationInstance() throws TargetException{ - return this; - } - - public Object getImplementationInstance(boolean notify) throws TargetException{ - return this; - } - - // ---------------------------------- - // Protected methods - // ---------------------------------- - - protected EventContext getEventContext() { - return eventContext; - } - - /** - * Notfies instances that are associated with a context and configured to receive callbacks that the context is - * being destroyed in reverse order - * - * @param key the context key - */ - protected void notifyInstanceShutdown(Object key) { - InstanceContext[] contexts = getShutdownContexts(key); - if ((contexts == null) || (contexts.length < 1)) { - return; - } - // shutdown destroyable instances in reverse instantiation order - for (int i = contexts.length - 1; i >= 0; i--) { - InstanceContext context = contexts[i]; - - if (context.getLifecycleState() == Context.RUNNING) { - synchronized (context) { - context.setLifecycleState(Context.STOPPING); - removeContextByKey(context.getName(), key); - try { - context.stop(); - } catch (TargetException e) { - // TODO send a monitoring event - // log.error("Error releasing instance [" + context.getName() + "]",e); - } - } - } - } - } - - protected void checkInit() { - if (lifecycleState != RUNNING) { - throw new IllegalStateException("Scope not running [" + lifecycleState + "]"); - } - } - - /** - * Returns an array of contexts that need to be notified of scope shutdown. The array must be in the order in which - * component contexts were created - */ - protected abstract InstanceContext[] getShutdownContexts(Object key); - -} diff --git a/tags/java-stable-20060304/sca/core/src/main/java/org/apache/tuscany/core/context/scope/AbstractScopeStrategy.java b/tags/java-stable-20060304/sca/core/src/main/java/org/apache/tuscany/core/context/scope/AbstractScopeStrategy.java deleted file mode 100644 index f89d09196d..0000000000 --- a/tags/java-stable-20060304/sca/core/src/main/java/org/apache/tuscany/core/context/scope/AbstractScopeStrategy.java +++ /dev/null @@ -1,67 +0,0 @@ -/** - * - * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on - * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ -package org.apache.tuscany.core.context.scope; - -import org.apache.tuscany.core.context.ScopeStrategy; -import org.apache.tuscany.model.assembly.Scope; - -/** - * Implements basic scope strategy functionality - * - * @version $Rev$ $Date$ - */ -public abstract class AbstractScopeStrategy implements ScopeStrategy { - - public AbstractScopeStrategy() { - } - - /** - * Determines legal scope references according to standard SCA scope rules - * - * @param pReferrer the scope of the component making the reference - * @param pReferee the scope of the component being referred to - */ - public boolean downScopeReference(Scope pReferrer, Scope pReferee) { - if (pReferrer == Scope.UNDEFINED || pReferee == Scope.UNDEFINED) { - return false; - } - if (pReferee == pReferrer){ - return false; - }else if(pReferrer == Scope.INSTANCE){ - return false; - }else if(pReferee == Scope.INSTANCE){ - return true; - }else if (pReferrer == Scope.REQUEST && pReferee == Scope.SESSION){ - return false; - }else if (pReferrer == Scope.REQUEST && pReferee == Scope.MODULE){ - return false; -// }else if (pReferrer == Scope.SESSION && pReferee == Scope.REQUEST){ -// return true; - }else if (pReferrer == Scope.SESSION && pReferee == Scope.MODULE){ - return false; -// }else if (pReferrer == Scope.MODULE){ -// return true; - }else{ - return true; - } - //FIXME Jim this does not work with enumerations, what does it mean to have a scope <0? -// } else if ((pReferrer < 0) || (pReferee < 0)) { -// return false; -// } -// -// return (pReferrer > pReferee); -// return pReferrer != pReferee; - } - -} diff --git a/tags/java-stable-20060304/sca/core/src/main/java/org/apache/tuscany/core/context/scope/AggregateScopeContext.java b/tags/java-stable-20060304/sca/core/src/main/java/org/apache/tuscany/core/context/scope/AggregateScopeContext.java deleted file mode 100644 index 19f554a625..0000000000 --- a/tags/java-stable-20060304/sca/core/src/main/java/org/apache/tuscany/core/context/scope/AggregateScopeContext.java +++ /dev/null @@ -1,184 +0,0 @@ -/** - * - * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.tuscany.core.context.scope; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -import org.apache.tuscany.core.builder.RuntimeConfiguration; -import org.apache.tuscany.core.context.AbstractContext; -import org.apache.tuscany.core.context.AggregateContext; -import org.apache.tuscany.core.context.EventContext; -import org.apache.tuscany.core.context.EventException; -import org.apache.tuscany.core.context.InstanceContext; -import org.apache.tuscany.core.context.QualifiedName; -import org.apache.tuscany.core.context.ScopeContext; -import org.apache.tuscany.core.context.ScopeInitializationException; -import org.apache.tuscany.core.context.ScopeRuntimeException; -import org.apache.tuscany.core.context.TargetException; - -/** - * Manages the lifecycle of aggregate component contexts, i.e. contexts which contain child contexts - * - * @see org.apache.tuscany.core.context.AggregateContext - * @version $Rev$ $Date$ - */ -public class AggregateScopeContext extends AbstractContext implements ScopeContext { - - // ---------------------------------- - // Fields - // ---------------------------------- - - private EventContext eventContext; - - private List<RuntimeConfiguration<InstanceContext>> configs = new ArrayList(); - - // Aggregate component contexts in this scope keyed by name - private Map<String, AggregateContext> contexts = new ConcurrentHashMap(); - - // indicates if a module start event has been previously propagated so child contexts added after can be notified - private boolean moduleScopeStarted; - - // ---------------------------------- - // Constructors - // ---------------------------------- - - public AggregateScopeContext(EventContext eventContext) { - assert (eventContext != null) : "Event context was null"; - this.eventContext = eventContext; - name = "Aggregate Scope"; - } - - // ---------------------------------- - // Lifecycle methods - // ---------------------------------- - - public void start() throws ScopeInitializationException { - for (RuntimeConfiguration<InstanceContext> configuration : configs) { - InstanceContext context = configuration.createInstanceContext(); - if (!(context instanceof AggregateContext)) { - ScopeInitializationException e = new ScopeInitializationException("Context not an aggregate type"); - e.addContextName(context.getName()); - throw e; - } - AggregateContext aggregateCtx = (AggregateContext) context; - aggregateCtx.start(); - contexts.put(aggregateCtx.getName(), aggregateCtx); - } - lifecycleState = RUNNING; - } - - public void stop() throws ScopeRuntimeException { - for (AggregateContext context : contexts.values()) { - context.stop(); - } - } - - // ---------------------------------- - // Methods - // ---------------------------------- - - public void registerConfigurations(List<RuntimeConfiguration<InstanceContext>> configurations) { - this.configs = configurations; - } - - public void registerConfiguration(RuntimeConfiguration<InstanceContext> configuration) { - assert (configuration != null) : "Configuration was null"; - configs.add(configuration); - if (lifecycleState == RUNNING) { - InstanceContext context = configuration.createInstanceContext(); - if (!(context instanceof AggregateContext)) { - ScopeInitializationException e = new ScopeInitializationException("Context not an aggregate type"); - e.setIdentifier(context.getName()); - throw e; - } - AggregateContext aggregateCtx = (AggregateContext) context; - aggregateCtx.start(); - if (moduleScopeStarted) { - aggregateCtx.fireEvent(EventContext.MODULE_START, null); - } - contexts.put(aggregateCtx.getName(), aggregateCtx); - } - } - - public boolean isCacheable() { - return false; - } - - public Object getInstance(QualifiedName qName) throws TargetException { - Object instance = null; - InstanceContext context = getContext(qName.getPartName()); - if (context == null) { - TargetException e = new TargetException("Component not found"); - e.setIdentifier(qName.getQualifiedName()); - throw e; - } - return context.getInstance(qName); - } - - public Object getInstance(QualifiedName qName, boolean notify) throws TargetException { - return getInstance(qName); - } - - public InstanceContext getContext(String ctxName) { - checkInit(); - return contexts.get(ctxName); - } - - public InstanceContext getContextByKey(String ctxName, Object key) { - return getContext(ctxName); - } - - public void removeContext(String ctxName) throws ScopeRuntimeException { - InstanceContext context = contexts.remove(ctxName); - if (context != null) { - context.stop(); - } - } - - public void removeContextByKey(String ctxName, Object key) throws ScopeRuntimeException { - } - - public void onEvent(int type, Object message) throws EventException { - if (type == EventContext.MODULE_START) { - // track module starting so that aggregate contexts registered after the event are notified properly - moduleScopeStarted = true; - } else if (type == EventContext.MODULE_STOP) { - moduleScopeStarted = false; - } - // propagate events to child contexts - for (AggregateContext context : contexts.values()) { - context.fireEvent(type, message); - } - } - - public Object getImplementationInstance() throws TargetException{ - return this; - } - - public Object getImplementationInstance(boolean notify) throws TargetException{ - return this; - } - - private void checkInit() { - if (lifecycleState != RUNNING) { - throw new IllegalStateException("Scope not running [" + lifecycleState + "]"); - } - } -} diff --git a/tags/java-stable-20060304/sca/core/src/main/java/org/apache/tuscany/core/context/scope/DefaultScopeStrategy.java b/tags/java-stable-20060304/sca/core/src/main/java/org/apache/tuscany/core/context/scope/DefaultScopeStrategy.java deleted file mode 100644 index 509eb7941f..0000000000 --- a/tags/java-stable-20060304/sca/core/src/main/java/org/apache/tuscany/core/context/scope/DefaultScopeStrategy.java +++ /dev/null @@ -1,52 +0,0 @@ -/** - * - * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.tuscany.core.context.scope; - -import java.util.HashMap; -import java.util.Map; - -import org.apache.tuscany.core.context.EventContext; -import org.apache.tuscany.core.context.ScopeContext; -import org.apache.tuscany.model.assembly.Scope; - -/** - * Implements a {@link org.apache.tuscany.core.context.ScopeStrategy} for the default module scopes: stateless, request, session, - * and module. - * - * @version $Rev$ $Date$ - */ -public class DefaultScopeStrategy extends AbstractScopeStrategy { - - public DefaultScopeStrategy() { - } - - public Map<Scope,ScopeContext> createScopes(EventContext eventContext) { - ScopeContext moduleScope = new ModuleScopeContext(eventContext); - ScopeContext sessionScope = new HttpSessionScopeContext(eventContext); - ScopeContext requestScope = new RequestScopeContext(eventContext); - ScopeContext statelessScope = new StatelessScopeContext(eventContext); - ScopeContext aggregrateScope = new AggregateScopeContext(eventContext); - Map<Scope,ScopeContext> scopes = new HashMap(); - scopes.put(Scope.MODULE,moduleScope); - scopes.put(Scope.SESSION,sessionScope); - scopes.put(Scope.REQUEST,requestScope); - scopes.put(Scope.INSTANCE,statelessScope); - scopes.put(Scope.AGGREGATE,aggregrateScope); - return scopes; - } - -} diff --git a/tags/java-stable-20060304/sca/core/src/main/java/org/apache/tuscany/core/context/scope/HttpSessionScopeContext.java b/tags/java-stable-20060304/sca/core/src/main/java/org/apache/tuscany/core/context/scope/HttpSessionScopeContext.java deleted file mode 100644 index e1fcc4ab70..0000000000 --- a/tags/java-stable-20060304/sca/core/src/main/java/org/apache/tuscany/core/context/scope/HttpSessionScopeContext.java +++ /dev/null @@ -1,254 +0,0 @@ -/** - * - * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.tuscany.core.context.scope; - -import java.util.Map; -import java.util.Queue; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentLinkedQueue; - -import org.apache.tuscany.core.builder.RuntimeConfiguration; -import org.apache.tuscany.core.context.Context; -import org.apache.tuscany.core.context.CoreRuntimeException; -import org.apache.tuscany.core.context.EventContext; -import org.apache.tuscany.core.context.InstanceContext; -import org.apache.tuscany.core.context.LifecycleEventListener; -import org.apache.tuscany.core.context.RuntimeEventListener; -import org.apache.tuscany.core.context.ScopeRuntimeException; -import org.apache.tuscany.core.context.SimpleComponentContext; - -/** - * An implementation of an HTTP session-scoped component container where each HTTP session is mapped to a context in the scope - * - * @version $Rev$ $Date$ - */ -public class HttpSessionScopeContext extends AbstractScopeContext implements RuntimeEventListener, LifecycleEventListener { - - // The collection of service component contexts keyed by session - private Map<Object, Map<String, InstanceContext>> contexts; - - // Stores ordered lists of contexts to shutdown keyed by session - private Map<Object, Queue<InstanceContext>> destroyableContexts; - - // ---------------------------------- - // Constructors - // ---------------------------------- - - public HttpSessionScopeContext(EventContext eventContext) { - super(eventContext); - setName("Http Session Scope"); - } - - // ---------------------------------- - // Lifecycle methods - // ---------------------------------- - - public synchronized void start() { - if (lifecycleState != UNINITIALIZED) { - throw new IllegalStateException("Scope container must be in UNINITIALIZED state"); - } - super.start(); - contexts = new ConcurrentHashMap(); - destroyableContexts = new ConcurrentHashMap(); - lifecycleState = RUNNING; - } - - public synchronized void stop() { - if (lifecycleState != RUNNING) { - throw new IllegalStateException("Scope container in wrong state"); - } - super.stop(); - contexts = null; - contexts = null; - destroyableContexts = null; - lifecycleState = STOPPED; - } - - // ---------------------------------- - // Listener methods - // ---------------------------------- - - public void onEvent(int type, Object key) { - checkInit(); - if (key == null) { - return; - } - if (type == EventContext.SESSION_END) { - notifyInstanceShutdown(key); - destroyComponentContext(key); - } - } - - // ---------------------------------- - // Scope methods - // ---------------------------------- - - public boolean isCacheable() { - return true; - } - - public void registerConfiguration(RuntimeConfiguration<InstanceContext> configuration) { - runtimeConfigurations.put(configuration.getName(), configuration); - } - - public InstanceContext getContext(String ctxName) { - checkInit(); - if (ctxName == null) { - return null; - } - // try{ - Map<String, InstanceContext> ctxs = getSessionContext(); - if (ctxs == null) { - return null; - } - InstanceContext ctx = ctxs.get(ctxName); - if (ctx == null) { - // the configuration was added after the session had started, so create a context now and start it - RuntimeConfiguration<InstanceContext> configuration = runtimeConfigurations.get(ctxName); - if (configuration != null) { - ctx = configuration.createInstanceContext(); - ctx.addContextListener(this); - ctx.start(); - ctxs.put(ctx.getName(), ctx); - } - } - return ctx; - } - - public InstanceContext getContextByKey(String ctxName, Object key) { - checkInit(); - if (key == null && ctxName == null) { - return null; - } - Map components = (Map) contexts.get(key); - if (components == null) { - return null; - } - return (InstanceContext) components.get(ctxName); - } - - public void removeContext(String ctxName) { - checkInit(); - Object key = getEventContext().getIdentifier(EventContext.HTTP_SESSION); - removeContextByKey(ctxName, key); - } - - public void removeContextByKey(String ctxName, Object key) { - checkInit(); - if (key == null || ctxName == null) { - return; - } - Map components = (Map) contexts.get(key); - if (components == null) { - return; - } - components.remove(ctxName); - Map definitions = contexts.get(key); - InstanceContext meta = (InstanceContext) definitions.get(ctxName); - destroyableContexts.get(key).remove(meta); - definitions.remove(ctxName); - } - - public void onInstanceCreate(Context context) throws ScopeRuntimeException { - checkInit(); - if (context instanceof SimpleComponentContext) { - // if destroyable, queue the context to have its component implementation instance released - if (((SimpleComponentContext) context).isDestroyable()) { - Object key = getEventContext().getIdentifier(EventContext.HTTP_SESSION); - Queue comps = destroyableContexts.get(key); - if (comps == null) { - ScopeRuntimeException e = new ScopeRuntimeException("Shutdown queue not found for key"); - e.setIdentifier(key.toString()); - throw e; - } - comps.add(context); - } - } - } - - /** - * Returns an array of {@link SimpleComponentContext}s representing components that need to be notified of scope shutdown or - * null if none found. - */ - protected InstanceContext[] getShutdownContexts(Object key) { - /* - * This method will be called from the Listener which is associated with a different thread than the request. So, just - * grab the key directly - */ - Queue queue = destroyableContexts.get(key); - if (queue != null) { - // create 0-length array since Queue.size() has O(n) traversal - return (InstanceContext[]) queue.toArray(new InstanceContext[0]); - } else { - return null; - } - } - - // ---------------------------------- - // Private methods - // ---------------------------------- - - /** - * Returns and, if necessary, creates a context for the current sesion - */ - private Map<String, InstanceContext> getSessionContext() throws CoreRuntimeException { - Object key = getEventContext().getIdentifier(EventContext.HTTP_SESSION); - if (key == null) { - throw new ScopeRuntimeException("Session key not set in request context"); - } - Map m = contexts.get(key); - if (m != null) { - return m; // already created, return - } - Map<String, InstanceContext> sessionContext = new ConcurrentHashMap(runtimeConfigurations.size()); - for (RuntimeConfiguration<InstanceContext> config : runtimeConfigurations.values()) { - InstanceContext context = null; - context = config.createInstanceContext(); - context.addContextListener(this); - context.start(); - sessionContext.put(context.getName(), context); - } - - Queue shutdownQueue = new ConcurrentLinkedQueue(); - contexts.put(key, sessionContext); - destroyableContexts.put(key, shutdownQueue); - // initialize eager components. Note this cannot be done when we initially create each context since a component may - // contain a forward reference to a component which has not been instantiated - for (InstanceContext context : sessionContext.values()) { - if (context instanceof SimpleComponentContext) { - SimpleComponentContext simpleCtx = (SimpleComponentContext) context; - if (simpleCtx.isEagerInit()) { - // Get the instance and perform manual shutdown registration to avoid a map lookup - context.getInstance(null, false); - if (simpleCtx.isDestroyable()) { - shutdownQueue.add(context); - } - } - } - } - return sessionContext; - } - - /** - * Removes the components associated with an expiring context - */ - private void destroyComponentContext(Object key) { - contexts.remove(key); - destroyableContexts.remove(key); - } - -}
\ No newline at end of file diff --git a/tags/java-stable-20060304/sca/core/src/main/java/org/apache/tuscany/core/context/scope/ModuleScopeContext.java b/tags/java-stable-20060304/sca/core/src/main/java/org/apache/tuscany/core/context/scope/ModuleScopeContext.java deleted file mode 100644 index 00f2747fae..0000000000 --- a/tags/java-stable-20060304/sca/core/src/main/java/org/apache/tuscany/core/context/scope/ModuleScopeContext.java +++ /dev/null @@ -1,182 +0,0 @@ -/** - * - * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.tuscany.core.context.scope; - -import java.util.Map; -import java.util.Queue; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentLinkedQueue; - -import org.apache.tuscany.core.builder.RuntimeConfiguration; -import org.apache.tuscany.core.context.Context; -import org.apache.tuscany.core.context.CoreRuntimeException; -import org.apache.tuscany.core.context.EventContext; -import org.apache.tuscany.core.context.InstanceContext; -import org.apache.tuscany.core.context.LifecycleEventListener; -import org.apache.tuscany.core.context.RuntimeEventListener; -import org.apache.tuscany.core.context.SimpleComponentContext; - -/** - * Manages component contexts whose implementations are module scoped - * - * @version $Rev$ $Date$ - */ -public class ModuleScopeContext extends AbstractScopeContext implements RuntimeEventListener, LifecycleEventListener { - - // ---------------------------------- - // Fields - // ---------------------------------- - - // Component contexts in this scope keyed by name - private Map<String, InstanceContext> componentContexts; - - private Queue<SimpleComponentContext> destroyableContexts; - - // ---------------------------------- - // Constructor - // ---------------------------------- - - public ModuleScopeContext(EventContext eventContext) { - super(eventContext); - setName("Module Scope"); - } - - // ---------------------------------- - // Listener methods - // ---------------------------------- - - public void onEvent(int type, Object key) { - if (type == EventContext.MODULE_START) { - lifecycleState = RUNNING; - initComponentContexts(); - } else if (type == EventContext.MODULE_STOP) { - notifyInstanceShutdown(key); - } - } - - // ---------------------------------- - // Lifecycle methods - // ---------------------------------- - - public synchronized void start() { - if (lifecycleState != UNINITIALIZED) { - throw new IllegalStateException("Scope must be in UNINITIALIZED state [" + lifecycleState + "]"); - } - } - - public synchronized void stop() { - if (lifecycleState != RUNNING) { - throw new IllegalStateException("Scope in wrong state [" + lifecycleState + "]"); - } - super.stop(); - componentContexts = null; - destroyableContexts = null; - lifecycleState = STOPPED; - } - - // ---------------------------------- - // Methods - // ---------------------------------- - - public boolean isCacheable() { - return true; - } - - public void registerConfiguration(RuntimeConfiguration<InstanceContext> configuration) { - runtimeConfigurations.put(configuration.getName(), configuration); - if (lifecycleState == RUNNING) { - componentContexts.put(configuration.getName(), configuration.createInstanceContext()); - } - } - - public InstanceContext getContext(String ctxName) { - checkInit(); - return componentContexts.get(ctxName); - } - - public InstanceContext getContextByKey(String ctxName, Object key) { - checkInit(); - return componentContexts.get(ctxName); - } - - public void removeContext(String ctxName) { - checkInit(); - Object component = componentContexts.remove(ctxName); - if (component != null) { - destroyableContexts.remove(component); - } - } - - public void removeContextByKey(String ctxName, Object key) { - checkInit(); - removeContext(ctxName); - } - - public void onInstanceCreate(Context context) { - checkInit(); - if (context instanceof SimpleComponentContext) { - SimpleComponentContext serviceContext = (SimpleComponentContext) context; - // Queue the context to have its implementation instance released if destroyable - if (serviceContext.isDestroyable()) { - destroyableContexts.add(serviceContext); - } - } - } - - /** - * Returns an array of {@link SimpleComponentContext}s representing components that need to be notified of scope shutdown. - */ - protected InstanceContext[] getShutdownContexts(Object key) { - if (destroyableContexts != null) { - // create 0-length array since Queue.size() has O(n) traversal - return (InstanceContext[]) destroyableContexts.toArray(new InstanceContext[0]); - } else { - return null; - } - } - - // ---------------------------------- - // Private methods - // ---------------------------------- - - private synchronized void initComponentContexts() throws CoreRuntimeException { - if (componentContexts == null) { - componentContexts = new ConcurrentHashMap(); - destroyableContexts = new ConcurrentLinkedQueue(); - for (RuntimeConfiguration<InstanceContext> config : runtimeConfigurations.values()) { - InstanceContext context = config.createInstanceContext(); - context.addContextListener(this); - context.start(); - componentContexts.put(context.getName(), context); - } - // Initialize eager contexts. Note this cannot be done when we initially create each context since a component may - // contain a forward reference to a component which has not been instantiated - for (InstanceContext context : componentContexts.values()) { - if (context instanceof SimpleComponentContext) { - SimpleComponentContext simpleCtx = (SimpleComponentContext) context; - if (simpleCtx.isEagerInit()) { - // perform silent creation and manual shutdown registration - simpleCtx.getInstance(null, false); - if (simpleCtx.isDestroyable()) { - destroyableContexts.add(simpleCtx); - } - } - } - } - } - } -}
\ No newline at end of file diff --git a/tags/java-stable-20060304/sca/core/src/main/java/org/apache/tuscany/core/context/scope/RequestScopeContext.java b/tags/java-stable-20060304/sca/core/src/main/java/org/apache/tuscany/core/context/scope/RequestScopeContext.java deleted file mode 100644 index deed3e2dee..0000000000 --- a/tags/java-stable-20060304/sca/core/src/main/java/org/apache/tuscany/core/context/scope/RequestScopeContext.java +++ /dev/null @@ -1,224 +0,0 @@ -/** - * - * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.tuscany.core.context.scope; - -import java.util.Map; -import java.util.Queue; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentLinkedQueue; - -import org.apache.tuscany.core.builder.RuntimeConfiguration; -import org.apache.tuscany.core.context.InstanceContext; -import org.apache.tuscany.core.context.Context; -import org.apache.tuscany.core.context.LifecycleEventListener; -import org.apache.tuscany.core.context.CoreRuntimeException; -import org.apache.tuscany.core.context.EventContext; -import org.apache.tuscany.core.context.RuntimeEventListener; -import org.apache.tuscany.core.context.SimpleComponentContext; - -/** - * An implementation of a request-scoped component container. - * - * @version $Rev$ $Date$ - */ -public class RequestScopeContext extends AbstractScopeContext implements RuntimeEventListener, LifecycleEventListener { - - // ---------------------------------- - // Fields - // ---------------------------------- - - // A collection of service component contexts keyed by thread. Note this could have been implemented with a ThreadLocal but - // using a Map allows finer-grained concurrency. - private Map<Object, Map<String, InstanceContext>> contextMap; - - // stores ordered lists of contexts to shutdown for each thread. - private Map<Object, Queue> destroyComponents; - - // ---------------------------------- - // Constructor - // ---------------------------------- - - public RequestScopeContext(EventContext eventContext) { - super(eventContext); - setName("Request Scope"); - } - - // ---------------------------------- - // Listener methods - // ---------------------------------- - - public void onEvent(int type, Object key) { - checkInit(); - /* clean up current context for pooled threads */ - if (type == EventContext.REQUEST_END) { - getEventContext().clearIdentifier(EventContext.HTTP_SESSION); - notifyInstanceShutdown(Thread.currentThread()); - destroyContext(); - } - } - - // ---------------------------------- - // Lifecycle methods - // ---------------------------------- - - public synchronized void start() { - if (lifecycleState != UNINITIALIZED) { - throw new IllegalStateException("Scope must be in UNINITIALIZED state [" + lifecycleState + "]"); - } - super.start(); - contextMap = new ConcurrentHashMap(); - destroyComponents = new ConcurrentHashMap(); - lifecycleState = RUNNING; - - } - - public synchronized void stop() { - if (lifecycleState != RUNNING) { - throw new IllegalStateException("Scope in wrong state [" + lifecycleState + "]"); - } - super.stop(); - contextMap = null; - destroyComponents = null; - lifecycleState = STOPPED; - } - - // ---------------------------------- - // Methods - // ---------------------------------- - - public boolean isCacheable() { - return true; - } - - public void registerConfiguration(RuntimeConfiguration<InstanceContext> configuration) { - runtimeConfigurations.put(configuration.getName(), configuration); - } - - public InstanceContext getContext(String ctxName) { - checkInit(); - Map<String, InstanceContext> contexts = getComponentContexts(); - InstanceContext ctx = contexts.get(ctxName); - if (ctx == null){ - // check to see if the configuration was added after the request was started - RuntimeConfiguration<InstanceContext> configuration = runtimeConfigurations.get(ctxName); - if (configuration != null) { - ctx = configuration.createInstanceContext(); - ctx.addContextListener(this); - ctx.start(); - contexts.put(ctx.getName(), ctx); - } - } - return ctx; - } - - public InstanceContext getContextByKey(String ctxName, Object key) { - checkInit(); - if (key == null) { - return null; - } - Map<String, InstanceContext> components = (Map) contextMap.get(key); - if (components == null) { - return null; - } - return components.get(ctxName); - } - - public void removeContext(String ctxName) { - checkInit(); - removeContextByKey(ctxName, Thread.currentThread()); - } - - public void removeContextByKey(String ctxName, Object key) { - checkInit(); - if (key == null || ctxName == null) { - return; - } - Map components = (Map) contextMap.get(key); - if (components == null) { - return; - } - components.remove(ctxName); - Map<String, InstanceContext> contexts = (Map) contextMap.get(key); - // no synchronization for the following two operations since the request - // context will not be shutdown before the second call is processed - InstanceContext context = contexts.get(ctxName); - destroyComponents.get(key).remove(context); - } - - public void onInstanceCreate(Context context) { - checkInit(); - if (context instanceof SimpleComponentContext) { - // Queue the context to have its implementation instance released if destroyable - if (((SimpleComponentContext) context).isDestroyable()) { - Queue collection = destroyComponents.get(Thread.currentThread()); - collection.add(context); - } - } - } - - /** - * Returns an array of {@link SimpleComponentContext}s representing components that need to be notified of scope shutdown. - */ - protected InstanceContext[] getShutdownContexts(Object key) { - checkInit(); - Queue queue = destroyComponents.get(Thread.currentThread()); - if (queue != null) { - // create 0-length array since Queue.size() has O(n) traversal - return (InstanceContext[]) queue.toArray(new InstanceContext[0]); - } else { - return null; - } - } - - // ---------------------------------- - // Private methods - // ---------------------------------- - - private void destroyContext() { - // TODO uninitialize all request-scoped components - contextMap.remove(Thread.currentThread()); - destroyComponents.remove(Thread.currentThread()); - } - - /** - * Initializes ServiceComponentContexts for the current request. - * <p> - * TODO This eagerly creates all component contexts, even if the component is never accessed during the request. This method - * should be profiled to determine if lazy initialization is more performant - * <p> - * TODO Eager initialization is not performed for request-scoped components - */ - - private Map<String, InstanceContext> getComponentContexts() throws CoreRuntimeException { - Map contexts = (Map) contextMap.get(Thread.currentThread()); - if (contexts == null) { - contexts = new ConcurrentHashMap(); - Queue shutdownQueue = new ConcurrentLinkedQueue(); - for (RuntimeConfiguration<InstanceContext> config : runtimeConfigurations.values()) { - InstanceContext context = null; - context = config.createInstanceContext(); - context.addContextListener(this); - context.start(); - contexts.put(context.getName(), context); - } - contextMap.put(Thread.currentThread(), contexts); - destroyComponents.put(Thread.currentThread(), shutdownQueue); - } - return contexts; - } - -}
\ No newline at end of file diff --git a/tags/java-stable-20060304/sca/core/src/main/java/org/apache/tuscany/core/context/scope/StatelessScopeContext.java b/tags/java-stable-20060304/sca/core/src/main/java/org/apache/tuscany/core/context/scope/StatelessScopeContext.java deleted file mode 100644 index 8b12f8b183..0000000000 --- a/tags/java-stable-20060304/sca/core/src/main/java/org/apache/tuscany/core/context/scope/StatelessScopeContext.java +++ /dev/null @@ -1,145 +0,0 @@ -/** - * - * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.tuscany.core.context.scope; - -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -import org.apache.tuscany.core.builder.RuntimeConfiguration; -import org.apache.tuscany.core.context.InstanceContext; -import org.apache.tuscany.core.context.Context; -import org.apache.tuscany.core.context.LifecycleEventListener; -import org.apache.tuscany.core.context.CoreRuntimeException; -import org.apache.tuscany.core.context.EventContext; -import org.apache.tuscany.core.context.RuntimeEventListener; - -/** - * A container that manages stateless components. - * - * @version $Rev$ $Date$ - */ -public class StatelessScopeContext extends AbstractScopeContext implements RuntimeEventListener, LifecycleEventListener { - - // ---------------------------------- - // Fields - // ---------------------------------- - - // Component contexts keyed by name - private Map<String, InstanceContext> contextMap; - - // ---------------------------------- - // Constructor - // ---------------------------------- - - public StatelessScopeContext(EventContext eventContext) { - super(eventContext); - setName("Stateless Scope"); - } - - // ---------------------------------- - // Lifecycle methods - // ---------------------------------- - - public synchronized void start() { - if (lifecycleState != UNINITIALIZED) { - throw new IllegalStateException("Scope must be in UNINITIALIZED state [" + lifecycleState + "]"); - } - super.start(); - lifecycleState = RUNNING; - prepare(); - } - - public synchronized void stop() { - if (lifecycleState != RUNNING) { - throw new IllegalStateException("Scope in wrong state [" + lifecycleState + "]"); - } - super.stop(); - contextMap = null; - lifecycleState = STOPPED; - } - - // ---------------------------------- - // Methods - // ---------------------------------- - - public void registerConfiguration(RuntimeConfiguration<InstanceContext> configuration) { - runtimeConfigurations.put(configuration.getName(), configuration); - if (lifecycleState == RUNNING) { - contextMap.put(configuration.getName(), configuration.createInstanceContext()); - } - - } - - public void onEvent(int type, Object key) { - // do nothing - } - - public boolean isCacheable() { - return true; - } - - public InstanceContext getContext(String ctxName) { - return contextMap.get(ctxName); - } - - public InstanceContext getContextByKey(String ctxName, Object key) { - return getContext(ctxName); - } - - public void removeContext(String ctxName) { - removeContextByKey(ctxName, null); - } - - public void removeContextByKey(String ctxName, Object key) { - contextMap.remove(ctxName); - } - - /** - * Always returns null since stateless components cannot be shutdown - */ - protected InstanceContext[] getShutdownContexts(Object key) { - return null; - } - - // ---------------------------------- - // Private methods - // ---------------------------------- - - public void onInstanceCreate(Context component) { - // do nothing - } - - private void prepare() throws CoreRuntimeException { - if (lifecycleState != RUNNING) { - throw new IllegalStateException("Scope not in INITIALIZED state [" + lifecycleState + "]"); - } - if (contextMap == null) { - contextMap = new ConcurrentHashMap(); - for (RuntimeConfiguration<InstanceContext> config : runtimeConfigurations.values()) { - for (int i = 0; i < runtimeConfigurations.size(); i++) { - InstanceContext context = null; - context = config.createInstanceContext(); - context.addContextListener(this); - context.start(); - contextMap.put(context.getName(), context); - } - - } - } - } - -}
\ No newline at end of file |