diff options
Diffstat (limited to 'sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/scope')
8 files changed, 1004 insertions, 0 deletions
diff --git a/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/scope/AbstractScopeContainer.java b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/scope/AbstractScopeContainer.java new file mode 100644 index 0000000000..7fa8765426 --- /dev/null +++ b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/scope/AbstractScopeContainer.java @@ -0,0 +1,265 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.component.scope; + + +import java.net.URI; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +import org.osoa.sca.annotations.Destroy; +import org.osoa.sca.annotations.Init; +import org.osoa.sca.annotations.Reference; + +import org.apache.tuscany.spi.AbstractLifecycle; +import org.apache.tuscany.spi.component.AtomicComponent; +import org.apache.tuscany.spi.component.GroupInitializationException; +import org.apache.tuscany.spi.component.InstanceWrapper; +import org.apache.tuscany.spi.component.PersistenceException; +import org.apache.tuscany.spi.component.ScopeContainer; +import org.apache.tuscany.spi.component.ScopeContainerMonitor; +import org.apache.tuscany.spi.component.ScopeRegistry; +import org.apache.tuscany.spi.component.TargetDestructionException; +import org.apache.tuscany.spi.component.TargetResolutionException; +import org.apache.tuscany.spi.event.Event; +import org.apache.tuscany.spi.model.Scope; + +/** + * Implements functionality common to scope contexts. + * + * @version $Rev$ $Date$ + */ +public abstract class AbstractScopeContainer<KEY> extends AbstractLifecycle + implements ScopeContainer<KEY> { + + private static final Comparator<AtomicComponent<?>> COMPARATOR = new Comparator<AtomicComponent<?>>() { + public int compare(AtomicComponent<?> o1, AtomicComponent<?> o2) { + return o1.getInitLevel() - o2.getInitLevel(); + } + }; + + private final Scope scope; + protected final ScopeContainerMonitor monitor; + + protected final Map<AtomicComponent<?>, URI> componentGroups = + new ConcurrentHashMap<AtomicComponent<?>, URI>(); + + protected final Map<KEY, URI> contextGroups = new ConcurrentHashMap<KEY, URI>(); + + // the queue of components to eagerly initialize in each group + protected final Map<URI, List<AtomicComponent<?>>> initQueues = + new HashMap<URI, List<AtomicComponent<?>>>(); + + // the queue of instanceWrappers to destroy, in the order that their instances were created + protected final Map<KEY, List<InstanceWrapper<?>>> destroyQueues = + new ConcurrentHashMap<KEY, List<InstanceWrapper<?>>>(); + + public AbstractScopeContainer(Scope scope, ScopeContainerMonitor monitor) { + this.scope = scope; + this.monitor = monitor; + } + + public Scope getScope() { + return scope; + } + + @Reference + public void setScopeRegistry(ScopeRegistry scopeRegistry) { + scopeRegistry.register(this); + } + + @Init + public synchronized void start() { + int lifecycleState = getLifecycleState(); + if (lifecycleState != UNINITIALIZED && lifecycleState != STOPPED) { + throw new IllegalStateException("Scope must be in UNINITIALIZED or STOPPED state [" + lifecycleState + "]"); + } + setLifecycleState(RUNNING); + } + + @Destroy + public synchronized void stop() { + int lifecycleState = getLifecycleState(); + if (lifecycleState != RUNNING) { + throw new IllegalStateException("Scope in wrong state [" + lifecycleState + "]"); + } + setLifecycleState(STOPPED); + componentGroups.clear(); + contextGroups.clear(); + synchronized (initQueues) { + initQueues.clear(); + } + destroyQueues.clear(); + } + + protected void checkInit() { + if (getLifecycleState() != RUNNING) { + throw new IllegalStateException("Scope container not running [" + getLifecycleState() + "]"); + } + } + + public void onEvent(Event event) { + } + + public <T> void register(AtomicComponent<T> component, URI groupId) { + checkInit(); + if (component.isEagerInit()) { + componentGroups.put(component, groupId); + synchronized (initQueues) { + List<AtomicComponent<?>> initQueue = initQueues.get(groupId); + if (initQueue == null) { + initQueue = new ArrayList<AtomicComponent<?>>(); + initQueues.put(groupId, initQueue); + } + // FIXME it would be more efficient to binary search and then insert + initQueue.add(component); + Collections.sort(initQueue, COMPARATOR); + } + } + } + + public <T> void unregister(AtomicComponent<T> component) { + if (component.isEagerInit()) { + URI groupId = componentGroups.remove(component); + synchronized (initQueues) { + List<AtomicComponent<?>> initQueue = initQueues.get(groupId); + initQueue.remove(component); + if (initQueue.isEmpty()) { + initQueues.remove(groupId); + } + } + } + } + + public void startContext(KEY contextId, URI groupId) throws GroupInitializationException { + assert !contextGroups.containsKey(contextId); + contextGroups.put(contextId, groupId); + destroyQueues.put(contextId, new ArrayList<InstanceWrapper<?>>()); + + // get and clone initialization queue + List<AtomicComponent<?>> initQueue; + synchronized (initQueues) { + initQueue = initQueues.get(groupId); + if (initQueue != null) { + initQueue = new ArrayList<AtomicComponent<?>>(initQueue); + } + } + if (initQueue != null) { + initializeComponents(contextId, initQueue); + } + } + + public void stopContext(KEY contextId) { + assert contextGroups.containsKey(contextId); + shutdownComponents(destroyQueues.get(contextId)); + contextGroups.remove(contextId); + destroyQueues.remove(contextId); + } + + public <T> InstanceWrapper<T> getWrapper(AtomicComponent<T> component, KEY contextId) + throws TargetResolutionException { + return null; + } + + public <T> InstanceWrapper<T> getAssociatedWrapper(AtomicComponent<T> component, KEY contextId) + throws TargetResolutionException { + return null; + } + + public <T> void returnWrapper(AtomicComponent<T> component, InstanceWrapper<T> wrapper, KEY contextId) + throws TargetDestructionException { + } + + public <T> void remove(AtomicComponent<T> component) throws PersistenceException { + throw new UnsupportedOperationException("Scope does not support persistence"); + } + + /** + * Initialise an ordered list of components. + * The list is traversed in order and the getWrapper() method called for each to + * associate an instance with the supplied context. + * + * @param contextId the contextId to associated with the component instances + * @param components the components to be initialized + * @throws GroupInitializationException if one or more components threw an exception during initialization + */ + protected void initializeComponents(KEY contextId, List<AtomicComponent<?>> components) + throws GroupInitializationException { + List<Exception> causes = null; + for (AtomicComponent<?> component : components) { + try { + getWrapper(component, contextId); + + } catch (Exception e) { + if (causes == null) { + causes = new ArrayList<Exception>(); + } + causes.add(e); + } + } + if (causes != null) { + throw new GroupInitializationException(String.valueOf(contextId), causes); + } + } + + /** + * Shut down an ordered list of instances. + * The list passed to this method is treated as a live, mutable list + * so any instances added to this list as shutdown is occuring will also be shut down. + * + * @param instances the list of instances to shutdown + */ + protected void shutdownComponents(List<InstanceWrapper<?>> instances) { + while (true) { + InstanceWrapper<?> toDestroy; + synchronized (instances) { + if (instances.size() == 0) { + return; + } + toDestroy = instances.remove(instances.size() - 1); + } + try { + toDestroy.stop(); + } catch (TargetDestructionException e) { + // log the error from destroy but continue + monitor.destructionError(e); + } + } + } + + public String toString() { + return "In state [" + super.toString() + ']'; + } + + /** + * Creates a new physical instance of a component, wrapped in an InstanceWrapper. + * + * @param component the component whose instance should be created + * @return a wrapped instance that has been injected but not yet started + * @throws TargetResolutionException if there was a problem creating the instance + */ + protected <T> InstanceWrapper<T> createInstance(AtomicComponent<T> component) throws TargetResolutionException { + return component.createInstanceWrapper(); + } +} diff --git a/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/scope/CompositeScopeContainer.java b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/scope/CompositeScopeContainer.java new file mode 100644 index 0000000000..1a5dbcdcd4 --- /dev/null +++ b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/scope/CompositeScopeContainer.java @@ -0,0 +1,114 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.component.scope; + +import java.net.URI; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +import org.osoa.sca.annotations.EagerInit; +import org.osoa.sca.annotations.Service; + +import org.apache.tuscany.api.annotation.Monitor; +import org.apache.tuscany.spi.component.AtomicComponent; +import org.apache.tuscany.spi.component.InstanceWrapper; +import org.apache.tuscany.spi.component.ScopeContainerMonitor; +import org.apache.tuscany.spi.component.TargetDestructionException; +import org.apache.tuscany.spi.component.TargetInitializationException; +import org.apache.tuscany.spi.component.TargetNotFoundException; +import org.apache.tuscany.spi.component.TargetResolutionException; +import org.apache.tuscany.spi.component.ScopeContainer; +import org.apache.tuscany.spi.model.Scope; + +/** + * A scope context which manages atomic component instances keyed by composite + * + * @version $Rev$ $Date$ + */ +@EagerInit +@Service(ScopeContainer.class) +public class CompositeScopeContainer<KEY> extends AbstractScopeContainer<KEY> { + private static final InstanceWrapper<Object> EMPTY = new InstanceWrapper<Object>() { + public Object getInstance() { + return null; + } + + public boolean isStarted() { + return true; + } + + public void start() throws TargetInitializationException { + + } + + public void stop() throws TargetDestructionException { + + } + }; + + // there is one instance per component so we can index directly + private final Map<AtomicComponent<?>, InstanceWrapper<?>> instanceWrappers = + new ConcurrentHashMap<AtomicComponent<?>, InstanceWrapper<?>>(); + + public CompositeScopeContainer(@Monitor ScopeContainerMonitor monitor) { + super(Scope.COMPOSITE, monitor); + } + + public <T> void register(AtomicComponent<T> component, URI groupId) { + super.register(component, groupId); + instanceWrappers.put(component, EMPTY); + } + + public <T> void unregister(AtomicComponent<T> component) { + // FIXME should this component be destroyed already or do we need to stop it? + instanceWrappers.remove(component); + super.unregister(component); + } + + public synchronized void stop() { + super.stop(); + instanceWrappers.clear(); + } + + public <T> InstanceWrapper<T> getWrapper(AtomicComponent<T> component, KEY contextId) + throws TargetResolutionException { + assert instanceWrappers.containsKey(component); + @SuppressWarnings("unchecked") + InstanceWrapper<T> wrapper = (InstanceWrapper<T>) instanceWrappers.get(component); + if (wrapper == EMPTY) { + // FIXME is there a potential race condition here that may result in two instances being created + wrapper = createInstance(component); + instanceWrappers.put(component, wrapper); + wrapper.start(); + destroyQueues.get(contextId).add(wrapper); + } + return wrapper; + } + + public <T> InstanceWrapper<T> getAssociatedWrapper(AtomicComponent<T> component, KEY contextId) + throws TargetResolutionException { + assert instanceWrappers.containsKey(component); + @SuppressWarnings("unchecked") + InstanceWrapper<T> wrapper = (InstanceWrapper<T>) instanceWrappers.get(component); + if (wrapper == EMPTY) { + throw new TargetNotFoundException(component.getUri().toString()); + } + return wrapper; + } +} diff --git a/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/scope/ConversationalScopeContainer.java b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/scope/ConversationalScopeContainer.java new file mode 100644 index 0000000000..2d9d3d938c --- /dev/null +++ b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/scope/ConversationalScopeContainer.java @@ -0,0 +1,190 @@ +/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.component.scope;
+
+import java.net.URI;
+
+import org.apache.tuscany.spi.component.AtomicComponent;
+import org.apache.tuscany.spi.component.InstanceWrapper;
+import org.apache.tuscany.spi.component.PersistenceException;
+import org.apache.tuscany.spi.component.ScopeContainer;
+import org.apache.tuscany.spi.component.ScopeContainerMonitor;
+import org.apache.tuscany.spi.component.TargetDestructionException;
+import org.apache.tuscany.spi.component.TargetResolutionException;
+import org.apache.tuscany.spi.component.WorkContext;
+import org.apache.tuscany.spi.event.Event;
+import org.apache.tuscany.spi.event.RuntimeEventListener;
+import org.apache.tuscany.spi.model.Scope;
+import org.apache.tuscany.spi.services.store.Store;
+import org.apache.tuscany.spi.services.store.StoreExpirationEvent;
+import org.apache.tuscany.spi.services.store.StoreReadException;
+import org.apache.tuscany.spi.services.store.StoreWriteException;
+
+/**
+ * A scope context which manages atomic component instances keyed on a conversation session
+ *
+ * @version $Rev: 452655 $ $Date: 2006-10-03 18:09:02 -0400 (Tue, 03 Oct 2006) $
+ */
+public class ConversationalScopeContainer extends AbstractScopeContainer implements ScopeContainer {
+ private final WorkContext workContext;
+ private final Store nonDurableStore;
+
+ public ConversationalScopeContainer(Store store, WorkContext workContext, final ScopeContainerMonitor monitor) {
+ super(Scope.CONVERSATION, monitor);
+ this.workContext = workContext;
+ this.nonDurableStore = store;
+ if (store != null) {
+ store.addListener(new ExpirationListener(monitor));
+ }
+ }
+
+ public void onEvent(Event event) {
+ checkInit();
+ }
+
+ public synchronized void start() {
+ if (lifecycleState != UNINITIALIZED && lifecycleState != STOPPED) {
+ throw new IllegalStateException("Scope must be in UNINITIALIZED or STOPPED state [" + lifecycleState + "]");
+ }
+ lifecycleState = RUNNING;
+ }
+
+ public synchronized void stop() {
+ lifecycleState = STOPPED;
+ }
+
+ public void register(AtomicComponent component, URI groupId) {
+ super.register(component, groupId);
+ component.addListener(this);
+ }
+
+ public void unregister(AtomicComponent component) {
+ // FIXME should all the instances associated with this component be remove already
+ component.removeListener(this);
+ super.unregister(component);
+ }
+
+ public void persistNew(AtomicComponent component, String id, Object instance, long expiration)
+ throws PersistenceException {
+ try {
+ nonDurableStore.insertRecord(component, id, instance, expiration);
+ } catch (StoreWriteException e) {
+ throw new PersistenceException(e);
+ }
+ }
+
+ public void persist(AtomicComponent component, String id, Object instance, long expiration)
+ throws PersistenceException {
+ try {
+ nonDurableStore.updateRecord(component, id, instance, expiration);
+ } catch (StoreWriteException e) {
+ throw new PersistenceException(e);
+ }
+ }
+
+ public void remove(AtomicComponent component) throws PersistenceException {
+ String conversationId = getConversationId();
+ try {
+ workContext.setCurrentAtomicComponent(component);
+ // FIXME this should be an InstanceWrapper and shouldn't we stop it?
+ Object instance = nonDurableStore.readRecord(component, conversationId);
+ if (instance != null) {
+ nonDurableStore.removeRecord(component, conversationId);
+ }
+ } catch (StoreReadException e) {
+ throw new PersistenceException(e);
+ } catch (StoreWriteException e) {
+ throw new PersistenceException(e);
+ }
+ }
+
+ protected InstanceWrapper getInstanceWrapper(AtomicComponent component, boolean create)
+ throws TargetResolutionException {
+ String conversationId = getConversationId();
+ try {
+ workContext.setCurrentAtomicComponent(component);
+ InstanceWrapper wrapper = (InstanceWrapper) nonDurableStore.readRecord(component, conversationId);
+ if (wrapper != null) {
+ if (component.getMaxIdleTime() > 0) {
+ // update expiration
+ long expire = System.currentTimeMillis() + component.getMaxIdleTime();
+ nonDurableStore.updateRecord(component, conversationId, wrapper, expire);
+ }
+ } else if (create) {
+ // FIXME should the store really be persisting the wrappers
+ wrapper = component.createInstanceWrapper();
+ wrapper.start();
+ long expire = calculateExpiration(component);
+ nonDurableStore.insertRecord(component, conversationId, wrapper, expire);
+ }
+ return wrapper;
+ } catch (StoreReadException e) {
+ throw new TargetResolutionException("Error retrieving target instance", e);
+ } catch (StoreWriteException e) {
+ throw new TargetResolutionException("Error persisting target instance", e);
+ } finally {
+ workContext.setCurrentAtomicComponent(null);
+ }
+ }
+
+ /**
+ * Returns the conversation id associated with the current invocation context
+ * @return the conversation id
+ */
+ private String getConversationId() {
+ String conversationId = (String) workContext.getIdentifier(Scope.CONVERSATION);
+ assert conversationId != null;
+ return conversationId;
+ }
+
+ private long calculateExpiration(AtomicComponent component) {
+ if (component.getMaxAge() > 0) {
+ long now = System.currentTimeMillis();
+ return now + component.getMaxAge();
+ } else if (component.getMaxIdleTime() > 0) {
+ long now = System.currentTimeMillis();
+ return now + component.getMaxIdleTime();
+ } else {
+ return Store.DEFAULT_EXPIRATION_OFFSET;
+ }
+ }
+
+ /**
+ * Receives expiration events from the store and notifies the corresponding atomic component
+ */
+ private static class ExpirationListener implements RuntimeEventListener {
+ private final ScopeContainerMonitor monitor;
+
+ public ExpirationListener(ScopeContainerMonitor monitor) {
+ this.monitor = monitor;
+ }
+
+ public void onEvent(Event event) {
+ if (event instanceof StoreExpirationEvent) {
+ StoreExpirationEvent expiration = (StoreExpirationEvent) event;
+ InstanceWrapper wrapper = (InstanceWrapper) expiration.getInstance();
+ try {
+ wrapper.stop();
+ } catch (TargetDestructionException e) {
+ monitor.destructionError(e);
+ }
+ }
+ }
+ }
+}
diff --git a/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/scope/HttpSessionScopeContainer.java b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/scope/HttpSessionScopeContainer.java new file mode 100644 index 0000000000..5a398e921e --- /dev/null +++ b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/scope/HttpSessionScopeContainer.java @@ -0,0 +1,141 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.component.scope; + +import java.util.ArrayList; +import java.util.List; +import java.util.ListIterator; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +import org.apache.tuscany.spi.component.AtomicComponent; +import org.apache.tuscany.spi.component.ScopeContainerMonitor; +import org.apache.tuscany.spi.component.TargetDestructionException; +import org.apache.tuscany.spi.component.TargetResolutionException; +import org.apache.tuscany.spi.component.WorkContext; +import org.apache.tuscany.spi.component.InstanceWrapper; +import org.apache.tuscany.spi.event.Event; +import org.apache.tuscany.spi.model.Scope; + +import org.apache.tuscany.core.component.event.HttpSessionEnd; + +/** + * A scope context which manages atomic component instances keyed on HTTP session + * + * @version $Rev$ $Date$ + */ +public class HttpSessionScopeContainer extends AbstractScopeContainer { + private final WorkContext workContext; + private final Map<AtomicComponent, Map<Object, InstanceWrapper>> contexts; + private final Map<Object, List<InstanceWrapper>> destroyQueues; + + public HttpSessionScopeContainer(WorkContext workContext, ScopeContainerMonitor monitor) { + super(Scope.SESSION, monitor); + this.workContext = workContext; + contexts = new ConcurrentHashMap<AtomicComponent, Map<Object, InstanceWrapper>>(); + destroyQueues = new ConcurrentHashMap<Object, List<InstanceWrapper>>(); + } + + public void onEvent(Event event) { + checkInit(); + if (event instanceof HttpSessionEnd) { + Object key = ((HttpSessionEnd) event).getId(); + shutdownInstances(key); + workContext.clearIdentifier(key); + } + } + + public synchronized void start() { + if (lifecycleState != UNINITIALIZED && lifecycleState != STOPPED) { + throw new IllegalStateException("Scope must be in UNINITIALIZED or STOPPED state [" + lifecycleState + "]"); + } + lifecycleState = RUNNING; + } + + public synchronized void stop() { + contexts.clear(); + synchronized (destroyQueues) { + destroyQueues.clear(); + } + lifecycleState = STOPPED; + } + + public void register(AtomicComponent component, Object groupId) { + contexts.put(component, new ConcurrentHashMap<Object, InstanceWrapper>()); + component.addListener(this); + } + + public void unregister(AtomicComponent component) { + // FIXME should all the instances associated with this component be destroyed already + contexts.remove(component); + component.removeListener(this); + super.unregister(component); + } + + protected InstanceWrapper getInstanceWrapper(AtomicComponent component, boolean create) + throws TargetResolutionException { + Object key = workContext.getIdentifier(Scope.SESSION); + assert key != null : "HTTP session key not bound in work context"; + return getInstance(component, key, create); + } + + private InstanceWrapper getInstance(AtomicComponent component, Object key, boolean create) + throws TargetResolutionException { + Map<Object, InstanceWrapper> wrappers = contexts.get(component); + InstanceWrapper ctx = wrappers.get(key); + if (ctx == null && !create) { + return null; + } + if (ctx == null) { + ctx = component.createInstanceWrapper(); + ctx.start(); + wrappers.put(key, ctx); + List<InstanceWrapper> destroyQueue = destroyQueues.get(key); + if (destroyQueue == null) { + destroyQueue = new ArrayList<InstanceWrapper>(); + destroyQueues.put(key, destroyQueue); + } + synchronized (destroyQueue) { + destroyQueue.add(ctx); + } + } + return ctx; + + } + + private void shutdownInstances(Object key) { + List<InstanceWrapper> destroyQueue = destroyQueues.remove(key); + if (destroyQueue != null) { + for (Map<Object, InstanceWrapper> map : contexts.values()) { + map.remove(key); + } + ListIterator<InstanceWrapper> iter = destroyQueue.listIterator(destroyQueue.size()); + synchronized (destroyQueue) { + while (iter.hasPrevious()) { + try { + iter.previous().stop(); + } catch (TargetDestructionException e) { + monitor.destructionError(e); + } + } + } + } + } + +} diff --git a/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/scope/InstanceWrapperBase.java b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/scope/InstanceWrapperBase.java new file mode 100644 index 0000000000..def862fa7c --- /dev/null +++ b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/scope/InstanceWrapperBase.java @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.component.scope; + +import org.apache.tuscany.spi.component.TargetDestructionException; +import org.apache.tuscany.spi.component.TargetInitializationException; +import org.apache.tuscany.spi.component.InstanceWrapper; + +/** + * @version $Rev$ $Date$ + */ +public class InstanceWrapperBase<T> implements InstanceWrapper<T> { + protected final T instance; + private boolean started; + + public InstanceWrapperBase(T instance) { + assert instance != null; + this.instance = instance; + } + + public T getInstance() { + assert started; + return instance; + } + + public boolean isStarted() { + return started; + } + + public void start() throws TargetInitializationException { + started = true; + } + + public void stop() throws TargetDestructionException { + started = false; + } +} diff --git a/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/scope/RequestScopeContainer.java b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/scope/RequestScopeContainer.java new file mode 100644 index 0000000000..d40277dc23 --- /dev/null +++ b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/scope/RequestScopeContainer.java @@ -0,0 +1,130 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.component.scope; + +import java.net.URI; +import java.util.ArrayList; +import java.util.List; +import java.util.ListIterator; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +import org.apache.tuscany.spi.component.AtomicComponent; +import org.apache.tuscany.spi.component.ScopeContainerMonitor; +import org.apache.tuscany.spi.component.TargetDestructionException; +import org.apache.tuscany.spi.component.TargetResolutionException; +import org.apache.tuscany.spi.component.InstanceWrapper; +import org.apache.tuscany.spi.event.Event; +import org.apache.tuscany.spi.model.Scope; + +import org.apache.tuscany.core.component.event.RequestEnd; + +/** + * A scope context which manages atomic component instances keyed on the current request context + * + * @version $Rev$ $Date$ + */ +public class RequestScopeContainer extends AbstractScopeContainer { + private final Map<AtomicComponent, Map<Thread, InstanceWrapper>> contexts; + private final Map<Thread, List<InstanceWrapper>> destroyQueues; + + public RequestScopeContainer(ScopeContainerMonitor monitor) { + super(Scope.REQUEST, monitor); + contexts = new ConcurrentHashMap<AtomicComponent, Map<Thread, InstanceWrapper>>(); + destroyQueues = new ConcurrentHashMap<Thread, List<InstanceWrapper>>(); + } + + public void onEvent(Event event) { + checkInit(); + if (event instanceof RequestEnd) { + shutdownInstances(Thread.currentThread()); + } + } + + public synchronized void start() { + if (lifecycleState != UNINITIALIZED && lifecycleState != STOPPED) { + throw new IllegalStateException("Scope must be in UNINITIALIZED or STOPPED state [" + lifecycleState + "]"); + } + lifecycleState = RUNNING; + } + + public synchronized void stop() { + contexts.clear(); + synchronized (destroyQueues) { + destroyQueues.clear(); + } + lifecycleState = STOPPED; + } + + public void register(AtomicComponent component, URI groupId) { + super.register(component, groupId); + contexts.put(component, new ConcurrentHashMap<Thread, InstanceWrapper>()); + } + + public void unregister(AtomicComponent component) { + // FIXME should all the instances associated with this component be destroyed already + contexts.remove(component); + super.unregister(component); + } + + protected InstanceWrapper getInstanceWrapper(AtomicComponent component, boolean create) + throws TargetResolutionException { + Map<Thread, InstanceWrapper> instanceContextMap = contexts.get(component); + assert instanceContextMap != null : "Atomic component not registered"; + InstanceWrapper ctx = instanceContextMap.get(Thread.currentThread()); + if (ctx == null && !create) { + return null; + } + if (ctx == null) { + ctx = component.createInstanceWrapper(); + ctx.start(); + instanceContextMap.put(Thread.currentThread(), ctx); + List<InstanceWrapper> destroyQueue = destroyQueues.get(Thread.currentThread()); + if (destroyQueue == null) { + destroyQueue = new ArrayList<InstanceWrapper>(); + destroyQueues.put(Thread.currentThread(), destroyQueue); + } + synchronized (destroyQueue) { + destroyQueue.add(ctx); + } + } + return ctx; + } + + private void shutdownInstances(Thread key) { + List<InstanceWrapper> destroyQueue = destroyQueues.remove(key); + if (destroyQueue != null && destroyQueue.size() > 0) { + Thread thread = Thread.currentThread(); + for (Map<Thread, InstanceWrapper> map : contexts.values()) { + map.remove(thread); + } + ListIterator<InstanceWrapper> iter = destroyQueue.listIterator(destroyQueue.size()); + synchronized (destroyQueue) { + while (iter.hasPrevious()) { + try { + iter.previous().stop(); + } catch (TargetDestructionException e) { + monitor.destructionError(e); + } + } + } + } + } + +} diff --git a/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/scope/ScopeRegistryImpl.java b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/scope/ScopeRegistryImpl.java new file mode 100644 index 0000000000..bcf05982eb --- /dev/null +++ b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/scope/ScopeRegistryImpl.java @@ -0,0 +1,49 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.component.scope; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +import org.apache.tuscany.spi.ObjectFactory; +import org.apache.tuscany.spi.component.ScopeContainer; +import org.apache.tuscany.spi.component.ScopeRegistry; +import org.apache.tuscany.spi.model.Scope; + +/** + * The default implementation of a scope registry + * + * @version $Rev$ $Date$ + */ +public class ScopeRegistryImpl implements ScopeRegistry { + private final Map<Scope, ScopeContainer> scopeCache = + new ConcurrentHashMap<Scope, ScopeContainer>(); + private final Map<Scope, ObjectFactory<? extends ScopeContainer>> factoryCache = + new ConcurrentHashMap<Scope, ObjectFactory<? extends ScopeContainer>>(); + + public void register(ScopeContainer container) { + scopeCache.put(container.getScope(), container); + } + + public ScopeContainer getScopeContainer(Scope scope) { + return scopeCache.get(scope); + } + + +} diff --git a/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/scope/StatelessScopeContainer.java b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/scope/StatelessScopeContainer.java new file mode 100644 index 0000000000..0e1d5cab6a --- /dev/null +++ b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/scope/StatelessScopeContainer.java @@ -0,0 +1,62 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.component.scope; + +import org.osoa.sca.annotations.EagerInit; +import org.osoa.sca.annotations.Service; + +import org.apache.tuscany.spi.component.AtomicComponent; +import org.apache.tuscany.spi.component.InstanceWrapper; +import org.apache.tuscany.spi.component.ScopeContainerMonitor; +import org.apache.tuscany.spi.component.TargetDestructionException; +import org.apache.tuscany.spi.component.TargetResolutionException; +import org.apache.tuscany.spi.component.ScopeContainer; +import org.apache.tuscany.spi.model.Scope; +import org.apache.tuscany.api.annotation.Monitor; + +/** + * A scope context which manages stateless atomic component instances in a non-pooled fashion. + * + * @version $Rev$ $Date$ + */ +@EagerInit +@Service(ScopeContainer.class) +public class StatelessScopeContainer<KEY> extends AbstractScopeContainer<KEY> { + + public StatelessScopeContainer(@Monitor ScopeContainerMonitor monitor) { + super(Scope.STATELESS, monitor); + } + + public <T> InstanceWrapper<T> getWrapper(AtomicComponent<T> component, KEY contextId) + throws TargetResolutionException { + InstanceWrapper<T> ctx = createInstance(component); + ctx.start(); + return ctx; + } + + public <T> InstanceWrapper<T> getAssociatedWrapper(AtomicComponent<T> component, KEY contextId) + throws TargetResolutionException { + throw new UnsupportedOperationException(); + } + + public <T> void returnWrapper(AtomicComponent<T> component, InstanceWrapper<T> wrapper, KEY contextId) + throws TargetDestructionException { + wrapper.stop(); + } +} |