diff options
Diffstat (limited to 'tags/java-M1-final/java/sca/core/src/main/java/org/apache/tuscany/core/context/scope')
8 files changed, 0 insertions, 1161 deletions
diff --git a/tags/java-M1-final/java/sca/core/src/main/java/org/apache/tuscany/core/context/scope/AbstractScopeContext.java b/tags/java-M1-final/java/sca/core/src/main/java/org/apache/tuscany/core/context/scope/AbstractScopeContext.java deleted file mode 100644 index 7eed472101..0000000000 --- a/tags/java-M1-final/java/sca/core/src/main/java/org/apache/tuscany/core/context/scope/AbstractScopeContext.java +++ /dev/null @@ -1,74 +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.ContextFactory; -import org.apache.tuscany.core.context.Context; -import org.apache.tuscany.core.context.EventContext; -import org.apache.tuscany.core.context.QualifiedName; -import org.apache.tuscany.core.context.ScopeContext; -import org.apache.tuscany.core.context.TargetException; -import org.apache.tuscany.core.context.impl.AbstractLifecycle; - -/** - * Implements functionality common to scope contexts. - * - * @version $Rev$ $Date$ - */ -public abstract class AbstractScopeContext extends AbstractLifecycle implements ScopeContext { - - // The collection of runtime configurations for the scope - protected Map<String, ContextFactory<Context>> contextFactories = new ConcurrentHashMap<String, ContextFactory<Context>>(); - - // The event context the scope container is associated with - protected EventContext eventContext; - - public AbstractScopeContext(EventContext eventContext) { - assert (eventContext != null) : "Event context was null"; - this.eventContext = eventContext; - } - - public void registerFactories(List<ContextFactory<Context>> configurations) { - for (ContextFactory<Context> configuration : configurations) { - contextFactories.put(configuration.getName(), configuration); - } - } - - public Object getInstance(QualifiedName qName) throws TargetException { - Context context = getContext(qName.getPartName()); - if (context == null) { - TargetException e = new TargetException("Target not found"); - e.setIdentifier(qName.getQualifiedName()); - throw e; - } - return context.getInstance(qName); - } - - protected void checkInit() { - if (getLifecycleState() != RUNNING) { - throw new IllegalStateException("Scope not running [" + getLifecycleState() + "]"); - } - } - - protected EventContext getEventContext() { - return eventContext; - } -} diff --git a/tags/java-M1-final/java/sca/core/src/main/java/org/apache/tuscany/core/context/scope/AbstractScopeStrategy.java b/tags/java-M1-final/java/sca/core/src/main/java/org/apache/tuscany/core/context/scope/AbstractScopeStrategy.java deleted file mode 100644 index f89d09196d..0000000000 --- a/tags/java-M1-final/java/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-M1-final/java/sca/core/src/main/java/org/apache/tuscany/core/context/scope/CompositeScopeContext.java b/tags/java-M1-final/java/sca/core/src/main/java/org/apache/tuscany/core/context/scope/CompositeScopeContext.java deleted file mode 100644 index 7468ff9dd6..0000000000 --- a/tags/java-M1-final/java/sca/core/src/main/java/org/apache/tuscany/core/context/scope/CompositeScopeContext.java +++ /dev/null @@ -1,156 +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.ContextFactory; -import org.apache.tuscany.core.context.CompositeContext; -import org.apache.tuscany.core.context.Context; -import org.apache.tuscany.core.context.EventContext; -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; -import org.apache.tuscany.core.context.event.Event; -import org.apache.tuscany.core.context.event.ModuleStart; -import org.apache.tuscany.core.context.event.ModuleStop; -import org.apache.tuscany.core.context.impl.AbstractLifecycle; - -/** - * Manages the lifecycle of composite component contexts, i.e. contexts which contain child contexts - * - * @see org.apache.tuscany.core.context.CompositeContext - * @version $Rev$ $Date$ - */ -public class CompositeScopeContext extends AbstractLifecycle implements ScopeContext { - - private List<ContextFactory<Context>> configs = new ArrayList<ContextFactory<Context>>(); - - // Composite component contexts in this scope keyed by name - private Map<String, CompositeContext> contexts = new ConcurrentHashMap<String, CompositeContext>(); - - // indicates if a module start event has been previously propagated so child contexts added after can be notified - private boolean moduleScopeStarted; - - public CompositeScopeContext(EventContext eventContext) { - assert (eventContext != null) : "Event context was null"; - setName("Composite Scope"); - } - - public void start() throws ScopeInitializationException { - for (ContextFactory<Context> configuration : configs) { - Context context = configuration.createContext(); - if (!(context instanceof CompositeContext)) { - ScopeInitializationException e = new ScopeInitializationException("Context not an composite type"); - e.addContextName(context.getName()); - throw e; - } - CompositeContext compositeCtx = (CompositeContext) context; - compositeCtx.start(); - contexts.put(compositeCtx.getName(), compositeCtx); - } - lifecycleState = RUNNING; - } - - public void stop() throws ScopeRuntimeException { - for (CompositeContext context : contexts.values()) { - context.stop(); - } - } - - public void registerFactories(List<ContextFactory<Context>> configurations) { - this.configs = configurations; - } - - public void registerFactory(ContextFactory<Context> configuration) { - assert (configuration != null) : "Configuration was null"; - configs.add(configuration); - if (getLifecycleState() == RUNNING) { - Context context = configuration.createContext(); - if (!(context instanceof CompositeContext)) { - ScopeInitializationException e = new ScopeInitializationException("Context not an composite type"); - e.setIdentifier(context.getName()); - throw e; - } - CompositeContext compositeCtx = (CompositeContext) context; - compositeCtx.start(); - if (moduleScopeStarted) { - compositeCtx.publish(new ModuleStart(this)); - } - contexts.put(compositeCtx.getName(), compositeCtx); - } - } - - public boolean isCacheable() { - return false; - } - - public Object getInstance(QualifiedName qName) throws TargetException { - Context 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 Context getContext(String ctxName) { - checkInit(); - return contexts.get(ctxName); - } - - public Context getContextByKey(String ctxName, Object key) { - return getContext(ctxName); - } - - public void removeContext(String ctxName) throws ScopeRuntimeException { - Context context = contexts.remove(ctxName); - if (context != null) { - context.stop(); - } - } - - public void removeContextByKey(String ctxName, Object key) throws ScopeRuntimeException { - throw new UnsupportedOperationException(); - } - - public void onEvent(Event event){ - if (event instanceof ModuleStart) { - // track module starting so that composite contexts registered after the event are notified properly - moduleScopeStarted = true; - } else if (event instanceof ModuleStop) { - moduleScopeStarted = false; - publish(event); - } - // propagate events to child contexts - for (CompositeContext context : contexts.values()) { - context.publish(event); - } - } - - private void checkInit() { - if (getLifecycleState()!= RUNNING) { - throw new IllegalStateException("Scope not running [" + getLifecycleState() + "]"); - } - } -} diff --git a/tags/java-M1-final/java/sca/core/src/main/java/org/apache/tuscany/core/context/scope/DefaultScopeStrategy.java b/tags/java-M1-final/java/sca/core/src/main/java/org/apache/tuscany/core/context/scope/DefaultScopeStrategy.java deleted file mode 100644 index 1f386fd35f..0000000000 --- a/tags/java-M1-final/java/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 org.apache.tuscany.core.context.EventContext; -import org.apache.tuscany.core.context.ScopeContext; -import org.apache.tuscany.model.assembly.Scope; - -import java.util.HashMap; -import java.util.Map; - -/** - * 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> getScopeContexts(EventContext eventContext) { - ScopeContext moduleScope = new ModuleScopeContext(eventContext); - ScopeContext sessionScope = new SessionScopeContext(eventContext); - ScopeContext requestScope = new RequestScopeContext(eventContext); - ScopeContext statelessScope = new StatelessScopeContext(eventContext); - ScopeContext aggregrateScope = new CompositeScopeContext(eventContext); - Map<Scope,ScopeContext> scopes = new HashMap<Scope,ScopeContext>(); - 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-M1-final/java/sca/core/src/main/java/org/apache/tuscany/core/context/scope/ModuleScopeContext.java b/tags/java-M1-final/java/sca/core/src/main/java/org/apache/tuscany/core/context/scope/ModuleScopeContext.java deleted file mode 100644 index dbb342bb5a..0000000000 --- a/tags/java-M1-final/java/sca/core/src/main/java/org/apache/tuscany/core/context/scope/ModuleScopeContext.java +++ /dev/null @@ -1,194 +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.builder.ContextFactory; -import org.apache.tuscany.core.context.AtomicContext; -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.TargetException; -import org.apache.tuscany.core.context.Lifecycle; -import org.apache.tuscany.core.context.event.InstanceCreated; -import org.apache.tuscany.core.context.event.Event; -import org.apache.tuscany.core.context.event.ModuleStart; -import org.apache.tuscany.core.context.event.ModuleStop; - -import java.util.Map; -import java.util.List; -import java.util.ArrayList; -import java.util.ListIterator; -import java.util.concurrent.ConcurrentHashMap; - -/** - * Manages contexts whose implementations are module scoped. This scope contexts eagerly starts contexts when - * a {@link ModuleStart} event is received. If a contained context has an implementation marked to eagerly initialized, - * the an instance will be created at that time as well. Contained contexts are shutdown when a {@link ModuleStop} - * event is received in reverse order to which their implementation instances were created. - * - * @version $Rev$ $Date$ - */ -public class ModuleScopeContext extends AbstractScopeContext { - - // Component contexts in this scope keyed by name - private Map<String, Context> contexts; - - // the queue of contexts to destroy, in the order that their instances were created - private List<Context> destroyQueue; - - public ModuleScopeContext(EventContext eventContext) { - super(eventContext); - setName("Module Scope"); - } - - public void onEvent(Event event) { - if (event instanceof ModuleStart) { - lifecycleState = RUNNING; - initComponentContexts(); - } else if (event instanceof ModuleStop) { - shutdownContexts(); - } else if (event instanceof InstanceCreated) { - checkInit(); - if (event.getSource() instanceof Context) { - Context context = (Context) event.getSource(); - // Queue the context to have its implementation instance released if destroyable - destroyQueue.add(context); - } - } - } - - 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 + "]"); - } - contexts = null; - destroyQueue = null; - lifecycleState = STOPPED; - } - - public boolean isCacheable() { - return true; - } - - public void registerFactory(ContextFactory<Context> configuration) { - contextFactories.put(configuration.getName(), configuration); - if (lifecycleState == RUNNING) { - contexts.put(configuration.getName(), configuration.createContext()); - } - } - - public Context getContext(String ctxName) { - checkInit(); - initComponentContexts(); - return contexts.get(ctxName); - } - - public Context getContextByKey(String ctxName, Object key) { - checkInit(); - initComponentContexts(); - return contexts.get(ctxName); - } - - public void removeContext(String ctxName) { - checkInit(); - if (contexts == null){ - return; - } - Context context = contexts.remove(ctxName); - if (context != null) { - destroyQueue.remove(context); - } - } - - public void removeContextByKey(String ctxName, Object key){ - removeContext(ctxName); - } - - /** - * Notifies contexts of a shutdown in reverse order to which they were started - */ - private synchronized void shutdownContexts() { - if (destroyQueue == null || destroyQueue.size() == 0) { - return; - } - // shutdown destroyable instances in reverse instantiation order - ListIterator<Context> iter = destroyQueue.listIterator(destroyQueue.size()); - while(iter.hasPrevious()){ - Lifecycle context = iter.previous(); - if (context.getLifecycleState() == RUNNING) { - try { - if (context instanceof AtomicContext){ - ((AtomicContext)context).destroy(); - } - } catch (TargetException e) { - // TODO send a monitoring event - } - } - } - if (contexts == null){ - return; - } - for(Lifecycle context: contexts.values()) { - try { - if (context.getLifecycleState() == RUNNING) { - context.stop(); - } - } catch (CoreRuntimeException e){ - // TODO send monitoring event - } - } - contexts = null; - destroyQueue = null; - } - - /** - * Creates and starts components contexts in the module scope. Implementations marked to eagerly initialize will - * also be notified to do so. - * - * @throws CoreRuntimeException - */ - private synchronized void initComponentContexts() throws CoreRuntimeException { - if (contexts == null) { - contexts = new ConcurrentHashMap<String, Context>(); - destroyQueue = new ArrayList<Context>(); - for (ContextFactory<Context> config : contextFactories.values()) { - Context context = config.createContext(); - context.start(); - contexts.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 (Context context : contexts.values()) { - if (context instanceof AtomicContext) { - AtomicContext atomic = (AtomicContext) context; - if (atomic.isEagerInit()) { - // perform silent creation and manual shutdown registration - atomic.init(); - destroyQueue.add(context); - } - } - context.addListener(this); - } - } - } -}
\ No newline at end of file diff --git a/tags/java-M1-final/java/sca/core/src/main/java/org/apache/tuscany/core/context/scope/RequestScopeContext.java b/tags/java-M1-final/java/sca/core/src/main/java/org/apache/tuscany/core/context/scope/RequestScopeContext.java deleted file mode 100644 index 67ce176af5..0000000000 --- a/tags/java-M1-final/java/sca/core/src/main/java/org/apache/tuscany/core/context/scope/RequestScopeContext.java +++ /dev/null @@ -1,228 +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.builder.ContextFactory; -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.AtomicContext; -import org.apache.tuscany.core.context.TargetException; -import org.apache.tuscany.core.context.Lifecycle; -import org.apache.tuscany.core.context.event.InstanceCreated; -import org.apache.tuscany.core.context.event.Event; -import org.apache.tuscany.core.context.event.RequestEnd; -import org.apache.tuscany.core.context.event.RequestStart; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.ListIterator; -import java.util.concurrent.ConcurrentHashMap; - -/** - * An implementation of a request-scoped component container. This scope contexts eagerly starts contexts when - * a {@link org.apache.tuscany.core.context.event.RequestStart} event is received. If a contained context has an implementation marked to eagerly initialized, - * the an instance will be created at that time as well. Contained contexts are shutdown when a {@link org.apache.tuscany.core.context.event.RequestEnd} - * event is received in reverse order to which their implementation instances were created. - * - * @version $Rev$ $Date$ - */ -public class RequestScopeContext extends AbstractScopeContext { - - // 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, Context>> contexts; - - // stores ordered lists of contexts to shutdown for each thread. - private Map<Object, List<Context>> destroyQueues; - - public RequestScopeContext(EventContext eventContext) { - super(eventContext); - setName("Request Scope"); - } - - public void onEvent(Event event){ - if (event instanceof RequestStart){ - getContexts(); // eager load - }else if (event instanceof RequestEnd){ - checkInit(); - getEventContext().clearIdentifiers(); // clean up current context for pooled threads - shutdownContexts(); - cleanupRequestContexts(); - }else if (event instanceof InstanceCreated){ - checkInit(); - assert(event.getSource() instanceof Context): "Context must be passed on created event"; - Context context = (Context)event.getSource(); - List<Context> collection = destroyQueues.get(Thread.currentThread()); - collection.add(context); - } - } - - public synchronized void start() { - if (lifecycleState != UNINITIALIZED) { - throw new IllegalStateException("Scope must be in UNINITIALIZED state [" + lifecycleState + "]"); - } - contexts = new ConcurrentHashMap<Object, Map<String, Context>>(); - destroyQueues = new ConcurrentHashMap<Object, List<Context>>(); - lifecycleState = RUNNING; - } - - public synchronized void stop() { - if (lifecycleState != RUNNING) { - throw new IllegalStateException("Scope in wrong state [" + lifecycleState + "]"); - } - contexts = null; - destroyQueues = null; - lifecycleState = STOPPED; - } - - public boolean isCacheable() { - return true; - } - - public void registerFactory(ContextFactory<Context> configuration) { - contextFactories.put(configuration.getName(), configuration); - } - - public Context getContext(String ctxName) { - checkInit(); - Map<String, Context> contexts = getContexts(); - Context ctx = contexts.get(ctxName); - if (ctx == null){ - // check to see if the configuration was added after the request was started - ContextFactory<Context> configuration = contextFactories.get(ctxName); - if (configuration != null) { - ctx = configuration.createContext(); - //ctx.addListener(this); - ctx.start(); - contexts.put(ctx.getName(), ctx); - } - } - return ctx; - } - - public Context getContextByKey(String ctxName, Object key) { - checkInit(); - if (key == null) { - return null; - } - Map<String, Context> components = contexts.get(key); - if (components == null) { - return null; - } - return components.get(ctxName); - } - - public void removeContext(String ctxName) { - removeContextByKey(ctxName, Thread.currentThread()); - } - - public void removeContextByKey(String ctxName, Object key) { - checkInit(); - if (key == null || ctxName == null) { - return; - } - Map components = contexts.get(key); - if (components == null) { - return; - } - components.remove(ctxName); - } - - - - private void cleanupRequestContexts() { - // TODO uninitialize all request-scoped components - contexts.remove(Thread.currentThread()); - destroyQueues.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, Context> getContexts() throws CoreRuntimeException { - Map<String, Context> requestContexts = this.contexts.get(Thread.currentThread()); - if (requestContexts == null) { - requestContexts = new ConcurrentHashMap<String, Context>(); - List<Context> shutdownQueue = new ArrayList<Context>(); - for (ContextFactory<Context> config : contextFactories.values()) { - Context context = config.createContext(); - context.start(); - requestContexts.put(context.getName(), context); - } - // 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 (Context context : requestContexts.values()) { - if (context instanceof AtomicContext) { - AtomicContext atomic = (AtomicContext) context; - if (atomic.isEagerInit()) { - atomic.init(); // Notify the instance - synchronized(shutdownQueue){ - shutdownQueue.add(context); - } - } - } - context.addListener(this); - } - contexts.put(Thread.currentThread(), requestContexts); - destroyQueues.put(Thread.currentThread(), shutdownQueue); - } - return requestContexts; - } - - private void shutdownContexts() { - List<Context> destroyQueue = destroyQueues.remove(Thread.currentThread()); - if (destroyQueue == null || destroyQueue.size() == 0) { - return; - } - synchronized(destroyQueue){ - // shutdown destroyable instances in reverse instantiation order - ListIterator<Context> iter = destroyQueue.listIterator(destroyQueue.size()); - while(iter.hasPrevious()){ - Lifecycle context = iter.previous(); - if (context.getLifecycleState() == RUNNING) { - try { - if (context instanceof AtomicContext){ - ((AtomicContext)context).destroy(); - } - } catch (TargetException e) { - // TODO send a monitoring event - } - } - } - } - // shutdown contexts - Map<String,Context> currentContexts = contexts.remove(Thread.currentThread()); - if (currentContexts == null){ - return; - } - for (Lifecycle context: currentContexts.values()){ - if (context.getLifecycleState() == RUNNING) { - context.stop(); - } - } - - } - -}
\ No newline at end of file diff --git a/tags/java-M1-final/java/sca/core/src/main/java/org/apache/tuscany/core/context/scope/SessionScopeContext.java b/tags/java-M1-final/java/sca/core/src/main/java/org/apache/tuscany/core/context/scope/SessionScopeContext.java deleted file mode 100644 index c5785d25f7..0000000000 --- a/tags/java-M1-final/java/sca/core/src/main/java/org/apache/tuscany/core/context/scope/SessionScopeContext.java +++ /dev/null @@ -1,258 +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.builder.ContextFactory; -import org.apache.tuscany.core.context.AtomicContext; -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.ScopeRuntimeException; -import org.apache.tuscany.core.context.TargetException; -import org.apache.tuscany.core.context.Lifecycle; -import org.apache.tuscany.core.context.event.Event; -import org.apache.tuscany.core.context.event.HttpSessionEvent; -import org.apache.tuscany.core.context.event.InstanceCreated; -import org.apache.tuscany.core.context.event.SessionEnd; -import org.apache.tuscany.core.context.event.SessionStart; - -import java.util.ArrayList; -import java.util.List; -import java.util.ListIterator; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -/** - * An implementation of an session-scoped component container. This scope contexts eagerly starts contexts when a - * {@link org.apache.tuscany.core.context.event.SessionStart} event is received. If a contained context has an implementation - * marked to eagerly initialized, the an instance will be created at that time as well. Contained contexts are shutdown when a - * {@link org.apache.tuscany.core.context.event.SessionEnd} event is received in reverse order to which their implementation - * instances were created. - * TODO this implementation needs to be made generic so that it supports a range of session types, i.e. not tied to HTTP - * session scope - * - * @version $Rev$ $Date$ - */ -public class SessionScopeContext extends AbstractScopeContext { - - // The collection of service component contexts keyed by session - private Map<Object, Map<String, Context>> contexts; - - // Stores ordered lists of contexts to shutdown keyed by session - private Map<Object, List<Context>> destroyQueues; - - public SessionScopeContext(EventContext eventContext) { - super(eventContext); - setName("Session Scope"); - } - - public synchronized void start() { - if (lifecycleState != UNINITIALIZED) { - throw new IllegalStateException("Scope container must be in UNINITIALIZED state"); - } - contexts = new ConcurrentHashMap<Object, Map<String, Context>>(); - destroyQueues = new ConcurrentHashMap<Object, List<Context>>(); - lifecycleState = RUNNING; - } - - public synchronized void stop() { - if (lifecycleState != RUNNING) { - throw new IllegalStateException("Scope container in wrong state"); - } - contexts = null; - contexts = null; - destroyQueues = null; - lifecycleState = STOPPED; - } - - public void onEvent(Event event) { - if (event instanceof SessionStart) { - checkInit(); - Object key = ((SessionEnd) event).getId(); - getSessionContexts(key); - }else if (event instanceof SessionEnd) { - checkInit(); - Object key = ((SessionEnd) event).getId(); - shutdownContexts(key); - destroyComponentContext(key); - } else if (event instanceof InstanceCreated) { - checkInit(); - Object sessionKey = getEventContext().getIdentifier(HttpSessionEvent.HTTP_IDENTIFIER); - List<Context> shutdownQueue = destroyQueues.get(sessionKey); - Context context = (Context) event.getSource(); - assert(shutdownQueue != null): "Shutdown queue not found for key"; - shutdownQueue.add(context); - } - } - - public boolean isCacheable() { - return true; - } - - public void registerFactory(ContextFactory<Context> configuration) { - contextFactories.put(configuration.getName(), configuration); - } - - public Context getContext(String ctxName) { - assert(ctxName != null): "No context name specified"; - checkInit(); - Map<String, Context> ctxs = getSessionContexts(); - Context context = ctxs.get(ctxName); - if (context == null) { - // the configuration was added after the session had started, so create a context now and start it - ContextFactory<Context> configuration = contextFactories.get(ctxName); - if (configuration != null) { - context = configuration.createContext(); - context.start(); - if (context instanceof AtomicContext) { - ((AtomicContext) context).init(); - } - - ctxs.put(context.getName(), context); - List<Context> shutdownQueue = destroyQueues.get(getEventContext().getIdentifier(HttpSessionEvent.HTTP_IDENTIFIER)); - synchronized (shutdownQueue) { - shutdownQueue.add(context); - } - context.addListener(this); - } - } - return context; - } - - public Context getContextByKey(String ctxName, Object key) { - checkInit(); - assert(ctxName != null): "No context name specified"; - assert(key != null): "No key specified"; - Map ctxs = contexts.get(key); - if (ctxs == null) { - return null; - } - return (Context) ctxs.get(ctxName); - } - - public void removeContext(String ctxName) { - checkInit(); - Object key = getEventContext().getIdentifier(HttpSessionEvent.HTTP_IDENTIFIER); - removeContextByKey(ctxName, key); - } - - public void removeContextByKey(String ctxName, Object key) { - checkInit(); - assert(ctxName != null): "No context name specified"; - assert(key != null): "No key specified"; - Map components = contexts.get(key); - if (components == null) { - return; - } - components.remove(ctxName); - Map<String, Context> definitions = contexts.get(key); - Context ctx = definitions.get(ctxName); - if (ctx != null) { - destroyQueues.get(key).remove(ctx); - } - definitions.remove(ctxName); - } - - /** - * Returns and, if necessary, creates a context for the current sesion - */ - private Map<String, Context> getSessionContexts() throws CoreRuntimeException { - Object key = getEventContext().getIdentifier(HttpSessionEvent.HTTP_IDENTIFIER); - return getSessionContexts(key); - } - - /** - * Returns and, if necessary, creates a context for the given session key - */ - private Map<String, Context> getSessionContexts(Object key) throws CoreRuntimeException { - if (key == null) { - throw new ScopeRuntimeException("Session key not set in request context"); - } - Map<String, Context> m = contexts.get(key); - if (m != null) { - return m; // already created, return - } - Map<String, Context> sessionContext = new ConcurrentHashMap<String, Context>(contextFactories.size()); - for (ContextFactory<Context> config : contextFactories.values()) { - Context context = config.createContext(); - context.start(); - sessionContext.put(context.getName(), context); - } - - List<Context> shutdownQueue = new ArrayList<Context>(); - contexts.put(key, sessionContext); - destroyQueues.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 (Context context : sessionContext.values()) { - if (context instanceof AtomicContext) { - AtomicContext atomic = (AtomicContext) context; - if (atomic.isEagerInit()) { - atomic.init(); // Notify the instance - synchronized (shutdownQueue) { - shutdownQueue.add(context); - } - } - } - context.addListener(this); - } - return sessionContext; - } - - /** - * Removes the components associated with an expiring context - */ - private void destroyComponentContext(Object key) { - contexts.remove(key); - destroyQueues.remove(key); - } - - - private synchronized void shutdownContexts(Object key) { - List<Context> destroyQueue = destroyQueues.remove(key); - if (destroyQueue == null || destroyQueue.size() == 0) { - return; - } - // shutdown destroyable instances in reverse instantiation order - ListIterator<Context> iter = destroyQueue.listIterator(destroyQueue.size()); - synchronized (destroyQueue) { - while (iter.hasPrevious()) { - Lifecycle context = iter.previous(); - if (context.getLifecycleState() == RUNNING) { - try { - if (context instanceof AtomicContext) { - ((AtomicContext) context).destroy(); - } - } catch (TargetException e) { - // TODO send a monitoring event - } - } - } - } - // shutdown contexts - Map<String, Context> currentContexts = contexts.remove(Thread.currentThread()); - if (currentContexts == null) { - return; - } - for (Lifecycle context : currentContexts.values()) { - if (context.getLifecycleState() == RUNNING) { - context.stop(); - } - } - } - -}
\ No newline at end of file diff --git a/tags/java-M1-final/java/sca/core/src/main/java/org/apache/tuscany/core/context/scope/StatelessScopeContext.java b/tags/java-M1-final/java/sca/core/src/main/java/org/apache/tuscany/core/context/scope/StatelessScopeContext.java deleted file mode 100644 index 46bf58b548..0000000000 --- a/tags/java-M1-final/java/sca/core/src/main/java/org/apache/tuscany/core/context/scope/StatelessScopeContext.java +++ /dev/null @@ -1,132 +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.builder.ContextFactory; -import org.apache.tuscany.core.context.EventContext; -import org.apache.tuscany.core.context.Context; -import org.apache.tuscany.core.context.CoreRuntimeException; -import org.apache.tuscany.core.context.Lifecycle; -import org.apache.tuscany.core.context.event.Event; -import org.apache.tuscany.core.context.event.ModuleStop; - -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -/** - * A container that manages stateless components. - * - * @version $Rev$ $Date$ - */ -public class StatelessScopeContext extends AbstractScopeContext { - - // Component contexts keyed by name - private Map<String, Context> contexts; - - public StatelessScopeContext(EventContext eventContext) { - super(eventContext); - setName("Stateless Scope"); - } - - public synchronized void start() { - if (lifecycleState != UNINITIALIZED) { - throw new IllegalStateException("Scope must be in UNINITIALIZED state [" + lifecycleState + "]"); - } - lifecycleState = RUNNING; - } - - public synchronized void stop() { - if (lifecycleState != RUNNING) { - throw new IllegalStateException("Scope in wrong state [" + lifecycleState + "]"); - } - contexts = null; - lifecycleState = STOPPED; - } - - public void registerFactory(ContextFactory<Context> configuration) { - contextFactories.put(configuration.getName(), configuration); - if (contexts != null) { - contexts.put(configuration.getName(), configuration.createContext()); - } - } - - public void onEvent(Event event){ - if (event instanceof ModuleStop) { - shutdownContexts(); - } - } - - public boolean isCacheable() { - return true; - } - - public Context getContext(String ctxName) { - prepare(); - return contexts.get(ctxName); - } - - public Context getContextByKey(String ctxName, Object key) { - return getContext(ctxName); - } - - public void removeContext(String ctxName) { - if (contexts == null){ - return; - } - contexts.remove(ctxName); - } - - public void removeContextByKey(String ctxName, Object key) { - removeContext(ctxName); - } - - private void prepare() throws CoreRuntimeException { - if (lifecycleState != RUNNING) { - throw new IllegalStateException("Scope not in INITIALIZED state [" + lifecycleState + "]"); - } - if (contexts == null) { - contexts = new ConcurrentHashMap<String, Context> (); - for (ContextFactory<Context> config : contextFactories.values()) { - for (int i = 0; i < contextFactories.size(); i++) { - Context context = config.createContext(); - context.start(); - contexts.put(context.getName(), context); - } - - } - } - } - - private void shutdownContexts(){ - if (contexts == null){ - return; - } - for(Lifecycle context: contexts.values()) { - try { - if (context.getLifecycleState() == RUNNING) { - context.stop(); - } - } catch (CoreRuntimeException e){ - // TODO send monitoring event - } - - } - contexts = null; - } - - -}
\ No newline at end of file |