diff options
author | dims <dims@13f79535-47bb-0310-9956-ffa450edef68> | 2008-06-17 00:23:01 +0000 |
---|---|---|
committer | dims <dims@13f79535-47bb-0310-9956-ffa450edef68> | 2008-06-17 00:23:01 +0000 |
commit | bdd0a41aed7edf21ec2a65cfa17a86af2ef8c48a (patch) | |
tree | 38a92061c0793434c4be189f1d70c3458b6bc41d /sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component |
Move Tuscany from Incubator to top level.
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@668359 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component')
34 files changed, 2504 insertions, 0 deletions
diff --git a/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/CallableReferenceImpl.java b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/CallableReferenceImpl.java new file mode 100644 index 0000000000..e674112a89 --- /dev/null +++ b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/CallableReferenceImpl.java @@ -0,0 +1,60 @@ +/* + * 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; + +import org.osoa.sca.CallableReference; +import org.osoa.sca.Conversation; + +import org.apache.tuscany.spi.ObjectFactory; + +/** + * Base class for implementations of service and callback references. + * + * @version $Rev$ $Date$ + * @param <B> the type of the business interface + */ +public abstract class CallableReferenceImpl<B> implements CallableReference<B> { + private final Class<B> businessInterface; + private final ObjectFactory<B> factory; + + protected CallableReferenceImpl(Class<B> businessInterface, ObjectFactory<B> factory) { + this.businessInterface = businessInterface; + this.factory = factory; + } + + public B getService() { + return factory.getInstance(); + } + + public Class<B> getBusinessInterface() { + return businessInterface; + } + + public boolean isConversational() { + return false; + } + + public Conversation getConversation() { + return null; + } + + public Object getCallbackID() { + return null; + } +} diff --git a/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/CallbackReferenceImpl.java b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/CallbackReferenceImpl.java new file mode 100644 index 0000000000..532a8b2bad --- /dev/null +++ b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/CallbackReferenceImpl.java @@ -0,0 +1,33 @@ +/* + * 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; + +import org.apache.tuscany.spi.ObjectFactory; + +/** + * Default implementation of a callback reference. + * + * @version $Rev$ $Date$ + * @param <CB> the type of the business interface + */ +public class CallbackReferenceImpl<CB> extends CallableReferenceImpl<CB> { + public CallbackReferenceImpl(Class<CB> businessInterface, ObjectFactory<CB> factory) { + super(businessInterface, factory); + } +} diff --git a/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/ComponentContextImpl.java b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/ComponentContextImpl.java new file mode 100644 index 0000000000..c8047abdab --- /dev/null +++ b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/ComponentContextImpl.java @@ -0,0 +1,97 @@ +/* + * 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; + +import org.osoa.sca.CallableReference; +import org.osoa.sca.ComponentContext; +import org.osoa.sca.RequestContext; +import org.osoa.sca.ServiceReference; +import org.osoa.sca.ServiceRuntimeException; + +import org.apache.tuscany.api.TuscanyRuntimeException; +import org.apache.tuscany.spi.ObjectFactory; +import org.apache.tuscany.spi.component.AtomicComponent; +import org.apache.tuscany.spi.component.Component; + +/** + * Implementation of ComponentContext that delegates to a ComponentContextProvider. + * + * @version $Rev$ $Date$ + */ +public class ComponentContextImpl implements ComponentContext { + private final ComponentContextProvider component; + + public ComponentContextImpl(ComponentContextProvider component) { + this.component = component; + } + + public String getURI() { + try { + return component.getUri().toString(); + } catch (TuscanyRuntimeException e) { + throw new ServiceRuntimeException(e.getMessage(), e); + } + } + + public <B, R extends CallableReference<B>> R cast(B target) throws IllegalArgumentException { + try { + return (R) component.cast(target); + } catch (TuscanyRuntimeException e) { + throw new ServiceRuntimeException(e.getMessage(), e); + } + } + + public <B> B getService(Class<B> businessInterface, String referenceName) { + try { + return component.getService(businessInterface, referenceName); + } catch (TuscanyRuntimeException e) { + throw new ServiceRuntimeException(e.getMessage(), e); + } + } + + public <B> ServiceReference<B> getServiceReference(Class<B> businessInterface, String referenceName) { + try { + return component.getServiceReference(businessInterface, referenceName); + } catch (TuscanyRuntimeException e) { + throw new ServiceRuntimeException(e.getMessage(), e); + } + } + + public <B> B getProperty(Class<B> type, String propertyName) { + try { + return component.getProperty(type, propertyName); + } catch (TuscanyRuntimeException e) { + throw new ServiceRuntimeException(e.getMessage(), e); + } + } + + public <B> ServiceReference<B> createSelfReference(Class<B> businessInterface) { + // FIXME: How to get the ObjectFactory? + ObjectFactory<B> factory = ((AtomicComponent) component).createObjectFactory(); + return new ServiceReferenceImpl<B>(businessInterface, factory); + } + + public <B> ServiceReference<B> createSelfReference(Class<B> businessInterface, String serviceName) { + return null; + } + + public RequestContext getRequestContext() { + return null; + } +} diff --git a/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/ComponentContextProvider.java b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/ComponentContextProvider.java new file mode 100644 index 0000000000..4237d8b09f --- /dev/null +++ b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/ComponentContextProvider.java @@ -0,0 +1,44 @@ +/* + * 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; + +import java.net.URI; + +import org.osoa.sca.ComponentContext; +import org.osoa.sca.ServiceReference; +import org.osoa.sca.CallableReference; + +/** + * Interface implemented by Component's that want to expose a ComponentContext. + * + * @version $Rev$ $Date$ + */ +public interface ComponentContextProvider { + ComponentContext getComponentContext(); + + URI getUri(); + + <B> B getService(Class<B> businessInterface, String referenceName); + + <B> ServiceReference<B> getServiceReference(Class<B> businessInterface, String referenceName); + + <B> B getProperty(Class<B> type, String propertyName); + + <B, R extends CallableReference<B>> R cast(B target); +} diff --git a/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/ComponentManagerImpl.java b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/ComponentManagerImpl.java new file mode 100644 index 0000000000..76958c88ff --- /dev/null +++ b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/ComponentManagerImpl.java @@ -0,0 +1,109 @@ +/* + * 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; + +import java.net.URI; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +import org.apache.tuscany.spi.component.AtomicComponent; +import org.apache.tuscany.spi.component.Component; +import org.apache.tuscany.spi.component.ComponentManager; +import org.apache.tuscany.spi.component.DuplicateNameException; +import org.apache.tuscany.spi.component.RegistrationException; +import org.apache.tuscany.spi.event.Event; +import org.apache.tuscany.spi.model.ServiceContract; +import org.apache.tuscany.spi.services.management.TuscanyManagementService; +import org.apache.tuscany.spi.idl.java.JavaServiceContract; + +import org.apache.tuscany.core.resolver.AutowireResolver; + +/** + * Default implementation of the component manager + * + * @version $Rev$ $Date$ + */ +public class ComponentManagerImpl implements ComponentManager { + private TuscanyManagementService managementService; + private AutowireResolver resolver; + private Map<URI, Component> components; + + public ComponentManagerImpl() { + components = new ConcurrentHashMap<URI, Component>(); + } + + public ComponentManagerImpl(TuscanyManagementService managementService, AutowireResolver resolver) { + this(); + this.managementService = managementService; + this.resolver = resolver; + } + + public synchronized void register(Component component) throws RegistrationException { + URI uri = component.getUri(); + assert uri != null; + assert !uri.toString().endsWith("/"); + if (components.containsKey(uri)) { + throw new DuplicateNameException(uri.toString()); + } + components.put(uri, component); + + if (managementService != null && component instanceof AtomicComponent) { + // FIXME shouldn't it take the canonical name and also not distinguish atomic components? + managementService.registerComponent(component.getUri().toString(), component); + } + } + + public <S, I extends S> void registerJavaObject(URI uri, JavaServiceContract<S> service, I instance) + throws RegistrationException { + SystemSingletonAtomicComponent<S, I> component = + new SystemSingletonAtomicComponent<S, I>(uri, service, instance); + register(component); + if (resolver != null) { + for (ServiceContract contract : component.getServiceContracts()) { + resolver.addHostUri(contract, uri); + } + } + } + + public <S, I extends S> void registerJavaObject(URI uri, List<JavaServiceContract<?>> services, I instance) + throws RegistrationException { + SystemSingletonAtomicComponent<S, I> component = + new SystemSingletonAtomicComponent<S, I>(uri, services, instance); + register(component); + if (resolver != null) { + for (ServiceContract contract : component.getServiceContracts()) { + resolver.addHostUri(contract, uri); + } + } + } + + public synchronized void unregister(Component component) throws RegistrationException { + URI uri = component.getUri(); + components.remove(uri); + } + + public Component getComponent(URI name) { + return components.get(name); + } + + public void onEvent(Event event) { + throw new UnsupportedOperationException(); + } +} diff --git a/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/ScopeIdentifier.java b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/ScopeIdentifier.java new file mode 100644 index 0000000000..35125b85ef --- /dev/null +++ b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/ScopeIdentifier.java @@ -0,0 +1,35 @@ +/* + * 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; + +/** + * Implementations enable lazy retrieval of a scope id associated with a request, i.e. an id (and presumably a context) + * do not have to be generated if the scope is never accessed. Identifiers are associated with the current request + * thread and keyed on scope type. + * + * @version $Rev$ $Date$ + * @see org.apache.tuscany.spi.component.WorkContext + */ +public interface ScopeIdentifier { + + /** + * Returns the scope id for the request. + */ + Object getIdentifier(); +} diff --git a/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/ServiceReferenceImpl.java b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/ServiceReferenceImpl.java new file mode 100644 index 0000000000..4d57d3efe0 --- /dev/null +++ b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/ServiceReferenceImpl.java @@ -0,0 +1,52 @@ +/* + * 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; + +import org.osoa.sca.ServiceReference; + +import org.apache.tuscany.spi.ObjectFactory; + +/** + * Default implementation of a ServiceReference. + * + * @version $Rev$ $Date$ + * @param <B> the type of the business interface + */ +public class ServiceReferenceImpl<B> extends CallableReferenceImpl<B> implements ServiceReference<B> { + public ServiceReferenceImpl(Class<B> businessInterface, ObjectFactory<B> factory) { + super(businessInterface, factory); + } + + public Object getConversationID() { + return null; + } + + public void setConversationID(Object conversationId) throws IllegalStateException { + } + + public void setCallbackID(Object callbackID) { + } + + public Object getCallback() { + return null; + } + + public void setCallback(Object callback) { + } +} diff --git a/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/SimpleWorkContext.java b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/SimpleWorkContext.java new file mode 100644 index 0000000000..7e978f6f35 --- /dev/null +++ b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/SimpleWorkContext.java @@ -0,0 +1,113 @@ +/* + * 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; + +import java.util.Map; +import java.util.HashMap; +import java.util.List; +import java.util.LinkedList; +import java.util.ArrayList; +import java.net.URI; + +import org.apache.tuscany.spi.component.WorkContext; +import org.apache.tuscany.spi.component.AtomicComponent; +import org.apache.tuscany.spi.wire.Wire; + +/** + * A simple WorkContext implementation that provides basic thread-local support + * for storing work context information. The implementation is <em>not</em> + * thread safe. + * + * @version $Rev$ $Date$ + */ +public class SimpleWorkContext implements WorkContext { + private final Map<Object, Object> identifiers = new HashMap<Object, Object>(); + private final List<String> serviceNameStack = new ArrayList<String>(); + + private LinkedList<URI> callbackUris; + private LinkedList<Wire> callbackWires; + private Object correlationId; + private AtomicComponent currentAtomicComponent; + + public Object getIdentifier(Object type) { + return identifiers.get(type); + } + + public void setIdentifier(Object type, Object identifier) { + identifiers.put(type, identifier); + } + + public void clearIdentifier(Object type) { + identifiers.remove(type); + } + + public void clearIdentifiers() { + identifiers.clear(); + } + + public LinkedList<URI> getCallbackUris() { + return callbackUris; + } + + public void setCallbackUris(LinkedList<URI> uris) { + this.callbackUris = uris; + } + + public LinkedList<Wire> getCallbackWires() { + return callbackWires; + } + + public void setCallbackWires(LinkedList<Wire> wires) { + this.callbackWires = wires; + } + + public Object getCorrelationId() { + return correlationId; + } + + public void setCorrelationId(Object correlationId) { + this.correlationId = correlationId; + } + + public AtomicComponent getCurrentAtomicComponent() { + return currentAtomicComponent; + } + + public void setCurrentAtomicComponent(AtomicComponent currentAtomicComponent) { + this.currentAtomicComponent = currentAtomicComponent; + } + + public String getCurrentServiceName() { + assert !serviceNameStack.isEmpty(); + return serviceNameStack.get(serviceNameStack.size() - 1); + } + + public void pushServiceName(String name) { + serviceNameStack.add(name); + } + + public String popServiceName() { + assert !serviceNameStack.isEmpty(); + return serviceNameStack.remove(serviceNameStack.size() - 1); + } + + public void clearServiceNames() { + serviceNameStack.clear(); + } +} diff --git a/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/SystemSingletonAtomicComponent.java b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/SystemSingletonAtomicComponent.java new file mode 100644 index 0000000000..872853506e --- /dev/null +++ b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/SystemSingletonAtomicComponent.java @@ -0,0 +1,138 @@ +/* + * 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; + +import java.net.URI; +import java.util.ArrayList; +import java.util.List; + +import org.apache.tuscany.core.injection.SingletonObjectFactory; +import org.apache.tuscany.spi.ObjectCreationException; +import org.apache.tuscany.spi.ObjectFactory; +import org.apache.tuscany.spi.component.AtomicComponent; +import org.apache.tuscany.spi.component.InstanceWrapper; +import org.apache.tuscany.spi.component.TargetInvokerCreationException; +import org.apache.tuscany.spi.component.TargetResolutionException; +import org.apache.tuscany.spi.extension.AbstractComponentExtension; +import org.apache.tuscany.spi.idl.java.JavaServiceContract; +import org.apache.tuscany.spi.model.Operation; +import org.apache.tuscany.spi.model.Scope; +import org.apache.tuscany.spi.model.ServiceContract; +import org.apache.tuscany.spi.model.physical.PhysicalOperationDefinition; +import org.apache.tuscany.spi.wire.TargetInvoker; +import org.apache.tuscany.spi.wire.Wire; + +/** + * An {@link org.apache.tuscany.spi.component.AtomicComponent} used when registering objects directly into a composite + * + * @version $$Rev$$ $$Date$$ + */ +public class SystemSingletonAtomicComponent<S, T extends S> extends AbstractComponentExtension + implements AtomicComponent<T> { + private T instance; + private List<ServiceContract> serviceContracts = new ArrayList<ServiceContract>(); + + public SystemSingletonAtomicComponent(URI name, JavaServiceContract<S> contract, T instance) { + super(name); + this.instance = instance; + this.serviceContracts.add(contract); + } + + public SystemSingletonAtomicComponent(URI name, List<JavaServiceContract<?>> services, T instance) { + super(name); + this.instance = instance; + for (ServiceContract<?> contract : services) { + serviceContracts.add(contract); + } + } + + public Scope getScope() { + return Scope.COMPOSITE; + } + + public boolean isEagerInit() { + return false; + } + + public int getInitLevel() { + return 0; + } + + public long getMaxIdleTime() { + return -1; + } + + public long getMaxAge() { + return -1; + } + + public T getTargetInstance() throws TargetResolutionException { + return instance; + } + + public Object createInstance() throws ObjectCreationException { + throw new UnsupportedOperationException(); + } + + public void removeInstance() { + throw new UnsupportedOperationException(); + } + + public InstanceWrapper<T> createInstanceWrapper() throws ObjectCreationException { + throw new UnsupportedOperationException(); + } + + public ObjectFactory<T> createObjectFactory() { + return new SingletonObjectFactory<T>(instance); + } + + public boolean isOptimizable() { + return true; + } + + public void attachWire(Wire wire) { + throw new UnsupportedOperationException(); + } + + public void attachWires(List<Wire> wires) { + throw new UnsupportedOperationException(); + } + + public List<Wire> getWires(String name) { + throw new UnsupportedOperationException(); + } + + public void attachCallbackWire(Wire wire) { + throw new UnsupportedOperationException(); + } + + public TargetInvoker createTargetInvoker(String targetName, Operation operation) { + return null; + } + + public TargetInvoker createTargetInvoker(String targetName, PhysicalOperationDefinition operation) + throws TargetInvokerCreationException { + return null; + } + + public List<ServiceContract> getServiceContracts() { + return serviceContracts; + } + +} diff --git a/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/WorkContextImpl.java b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/WorkContextImpl.java new file mode 100644 index 0000000000..3eb6e9d3eb --- /dev/null +++ b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/WorkContextImpl.java @@ -0,0 +1,214 @@ +/* + * 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; + +import java.net.URI; +import java.util.ArrayList; +import java.util.IdentityHashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +import org.apache.tuscany.spi.component.AtomicComponent; +import org.apache.tuscany.spi.component.WorkContext; +import org.apache.tuscany.spi.wire.Wire; + +/** + * An implementation of an {@link org.apache.tuscany.spi.component.WorkContext} that handles event-to-thread + * associations using an <code>InheritableThreadLocal</code> + * + * @version $Rev$ $Date$ + */ +public class WorkContextImpl implements WorkContext { + private static final Object CORRELATION_ID = new Object(); + private static final Object CALLBACK_URIS = new Object(); + private static final Object CURRENT_ATOMIC = new Object(); + private static final Object CURRENT_SERVICE_NAMES = new Object(); + private static final Object CALLBACK_WIRES = new Object(); + + // [rfeng] We cannot use InheritableThreadLocal for message ids here since it's shared by parent and children + private ThreadLocal<Map<Object, Object>> workContext = new ThreadLocal<Map<Object, Object>>(); + + // [rfeng] Session id requires InheritableThreadLocal + private ThreadLocal<Map<Object, Object>> inheritableContext = new InheritableThreadLocal<Map<Object, Object>>(); + + public WorkContextImpl() { + super(); + } + + public Object getCorrelationId() { + Map<Object, Object> map = workContext.get(); + if (map == null) { + return null; + } + return map.get(CORRELATION_ID); + } + + public void setCorrelationId(Object id) { + Map<Object, Object> map = getWorkContextMap(); + map.put(CORRELATION_ID, id); + } + + public AtomicComponent getCurrentAtomicComponent() { + Map<Object, Object> map = workContext.get(); + if (map == null) { + return null; + } + return (AtomicComponent) map.get(CURRENT_ATOMIC); + } + + public void setCurrentAtomicComponent(AtomicComponent component) { + Map<Object, Object> map = getWorkContextMap(); + map.put(CURRENT_ATOMIC, component); + } + + @SuppressWarnings("unchecked") + public LinkedList<URI> getCallbackUris() { + Map<Object, Object> map = workContext.get(); + if (map == null) { + return null; + } + return (LinkedList<URI>) map.get(CALLBACK_URIS); + } + + public void setCallbackUris(LinkedList<URI> uris) { + Map<Object, Object> map = getWorkContextMap(); + map.put(CALLBACK_URIS, uris); + } + + + @SuppressWarnings({"unchecked"}) + public LinkedList<Wire> getCallbackWires() { + Map<Object, Object> map = workContext.get(); + if (map == null) { + return null; + } + return (LinkedList<Wire>) map.get(CALLBACK_WIRES); + } + + public void setCallbackWires(LinkedList<Wire> wires) { + Map<Object, Object> map = getWorkContextMap(); + map.put(CALLBACK_WIRES, wires); + } + + + public Object getIdentifier(Object type) { + Map<Object, Object> map = inheritableContext.get(); + if (map == null) { + return null; + } + Object currentId = map.get(type); + if (currentId instanceof ScopeIdentifier) { + currentId = ((ScopeIdentifier) currentId).getIdentifier(); + // once we have accessed the id, replace the lazy wrapper + map.put(type, currentId); + } + return currentId; + } + + public void setIdentifier(Object type, Object identifier) { + Map<Object, Object> map = inheritableContext.get(); + if (map == null) { + map = new IdentityHashMap<Object, Object>(); + inheritableContext.set(map); + } + map.put(type, identifier); + } + + public void clearIdentifier(Object type) { + if (type == null) { + return; + } + Map map = inheritableContext.get(); + if (map != null) { + map.remove(type); + } + } + + public void clearIdentifiers() { + inheritableContext.remove(); + } + + @SuppressWarnings({"unchecked"}) + public String popServiceName() { + Map<Object, Object> map = inheritableContext.get(); + if (map == null) { + return null; + } + List<String> stack = (List) map.get(CURRENT_SERVICE_NAMES); + if (stack == null || stack.size() < 1) { + return null; + } + String name = stack.remove(stack.size() - 1); + if (stack.size() == 0) { + // cleanup to avoid leaks + map.remove(CURRENT_SERVICE_NAMES); + } + return name; + } + + @SuppressWarnings({"unchecked"}) + public String getCurrentServiceName() { + Map<Object, Object> map = inheritableContext.get(); + if (map == null) { + return null; + } + List<String> stack = (List) map.get(CURRENT_SERVICE_NAMES); + if (stack == null || stack.size() < 1) { + return null; + } + return stack.get(stack.size() - 1); + } + + @SuppressWarnings({"unchecked"}) + public void pushServiceName(String name) { + Map<Object, Object> map = inheritableContext.get(); + List<String> names; + if (map == null) { + map = new IdentityHashMap<Object, Object>(); + inheritableContext.set(map); + names = new ArrayList<String>(); + map.put(CURRENT_SERVICE_NAMES, names); + } else { + names = (List<String>) map.get(CURRENT_SERVICE_NAMES); + if (names == null) { + names = new ArrayList<String>(); + map.put(CURRENT_SERVICE_NAMES, names); + } + } + names.add(name); + } + + public void clearServiceNames() { + Map<Object, Object> map = inheritableContext.get(); + if (map == null) { + return; + } + map.remove(CURRENT_SERVICE_NAMES); + } + + private Map<Object, Object> getWorkContextMap() { + Map<Object, Object> map = workContext.get(); + if (map == null) { + map = new IdentityHashMap<Object, Object>(); + workContext.set(map); + } + return map; + } +} diff --git a/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/event/AbstractEvent.java b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/event/AbstractEvent.java new file mode 100644 index 0000000000..6d026ca08c --- /dev/null +++ b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/event/AbstractEvent.java @@ -0,0 +1,39 @@ +/* + * 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.event; + +import org.apache.tuscany.spi.event.Event; + +/** + * A basic implementation of a runtime event + * + * @version $$Rev$$ $$Date$$ + */ +public abstract class AbstractEvent implements Event { + protected Object source; + + public AbstractEvent(Object source) { + assert source != null : "Source id was null"; + this.source = source; + } + + public Object getSource() { + return source; + } +} diff --git a/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/event/AbstractRequestEvent.java b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/event/AbstractRequestEvent.java new file mode 100644 index 0000000000..762b917600 --- /dev/null +++ b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/event/AbstractRequestEvent.java @@ -0,0 +1,38 @@ +/* + * 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.event; + +/** + * Base implementation of a request event + * + * @version $$Rev$$ $$Date$$ + */ +public abstract class AbstractRequestEvent extends AbstractEvent implements RequestEvent { + + /** + * Creates a new event + * + * @param source the source of the event + */ + public AbstractRequestEvent(Object source) { + super(source); + } + + +} diff --git a/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/event/ComponentEvent.java b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/event/ComponentEvent.java new file mode 100644 index 0000000000..7de32a56d0 --- /dev/null +++ b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/event/ComponentEvent.java @@ -0,0 +1,34 @@ +/* + * 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.event; + +import java.net.URI; + +import org.apache.tuscany.spi.event.Event; + +/** + * Implemented by runtime events associated with a component, e.g. lifecycle events + * + * @version $$Rev$$ $$Date$$ + */ +public interface ComponentEvent extends Event { + + URI getComponentUri(); + +} diff --git a/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/event/ComponentStart.java b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/event/ComponentStart.java new file mode 100644 index 0000000000..6be1c88d3f --- /dev/null +++ b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/event/ComponentStart.java @@ -0,0 +1,47 @@ +/* + * 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.event; + +import java.net.URI; + +/** + * Propagated when a component starts + * + * @version $$Rev$$ $$Date$$ + */ +public class ComponentStart extends AbstractEvent implements ComponentEvent { + + private URI uri; + + /** + * Creates a component start event + * + * @param source the source of the event + * @param componentURI the uri of the component being started + */ + public ComponentStart(Object source, URI componentURI) { + super(source); + this.uri = componentURI; + } + + public URI getComponentUri() { + return uri; + } + +} diff --git a/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/event/ComponentStop.java b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/event/ComponentStop.java new file mode 100644 index 0000000000..ac0a7fd5c0 --- /dev/null +++ b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/event/ComponentStop.java @@ -0,0 +1,46 @@ +/* + * 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.event; + +import java.net.URI; + +/** + * Propagated when a component stops + * + * @version $$Rev$$ $$Date$$ + */ +public class ComponentStop extends AbstractEvent implements ComponentEvent { + + private URI uri; + + /** + * Creates a component stop event + * + * @param source the source of the event + * @param componentUri the composite component associated the component being stopped + */ + public ComponentStop(Object source, URI componentUri) { + super(source); + this.uri = componentUri; + } + + public URI getComponentUri() { + return uri; + } +} diff --git a/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/event/ConversationEnd.java b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/event/ConversationEnd.java new file mode 100644 index 0000000000..4a51d970c3 --- /dev/null +++ b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/event/ConversationEnd.java @@ -0,0 +1,31 @@ +/*
+ * 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.event;
+
+/**
+ * Propagated when a conversation session is expired
+ *
+ * @version $$Rev: 430937 $$ $$Date: 2006-08-11 21:17:56 -0400 (Fri, 11 Aug 2006) $$
+ */
+public class ConversationEnd extends ConversationalEvent {
+
+ public ConversationEnd(Object source, Object id) {
+ super(source, id);
+ }
+}
diff --git a/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/event/ConversationStart.java b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/event/ConversationStart.java new file mode 100644 index 0000000000..5725369bf6 --- /dev/null +++ b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/event/ConversationStart.java @@ -0,0 +1,31 @@ +/*
+ * 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.event;
+
+/**
+ * Propagated when a conversation session has started
+ *
+ * @version $$Rev: 430937 $$ $$Date: 2006-08-11 21:17:56 -0400 (Fri, 11 Aug 2006) $$
+ */
+public class ConversationStart extends ConversationalEvent {
+
+ public ConversationStart(Object source, Object id) {
+ super(source, id);
+ }
+}
diff --git a/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/event/ConversationalEvent.java b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/event/ConversationalEvent.java new file mode 100644 index 0000000000..a75086e745 --- /dev/null +++ b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/event/ConversationalEvent.java @@ -0,0 +1,39 @@ +/*
+ * 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.event;
+
+/**
+ * A base implementation of conversational session events in the runtime
+ *
+ * @version $$Rev: 430937 $$ $$Date: 2006-08-11 21:17:56 -0400 (Fri, 11 Aug 2006) $$
+ */
+public class ConversationalEvent extends AbstractEvent {
+
+ private Object id;
+
+ public ConversationalEvent(Object source, Object id) {
+ super(source);
+ assert id != null : "Conversation id was null";
+ this.id = id;
+ }
+
+ public Object getId() {
+ return id;
+ }
+}
diff --git a/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/event/HttpRequestEnded.java b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/event/HttpRequestEnded.java new file mode 100644 index 0000000000..01bd769031 --- /dev/null +++ b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/event/HttpRequestEnded.java @@ -0,0 +1,38 @@ +/*
+ * 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.event;
+
+/**
+ * Propagated when an HTTP-based request has ended.
+ *
+ * @version $$Rev: 430937 $$ $$Date: 2006-08-11 21:17:56 -0400 (Fri, 11 Aug 2006) $$
+ */
+public class HttpRequestEnded extends HttpSessionEvent {
+
+ /**
+ * Creates a new event
+ *
+ * @param source the source of the event
+ * @param id the id of the HTTP session being ended
+ */
+ public HttpRequestEnded(Object source, Object id) {
+ super(source, id);
+ }
+
+}
diff --git a/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/event/HttpRequestStart.java b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/event/HttpRequestStart.java new file mode 100644 index 0000000000..9d0ff80dd7 --- /dev/null +++ b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/event/HttpRequestStart.java @@ -0,0 +1,38 @@ +/*
+ * 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.event;
+
+/**
+ * Propagated when an HTTP-based request has started
+ *
+ * @version $$Rev: 430937 $$ $$Date: 2006-08-11 21:17:56 -0400 (Fri, 11 Aug 2006) $$
+ */
+public class HttpRequestStart extends HttpSessionEvent {
+
+ /**
+ * Creates a new event
+ *
+ * @param source the source of the event
+ * @param id the id of the HTTP session being ended
+ */
+ public HttpRequestStart(Object source, Object id) {
+ super(source, id);
+ }
+
+}
diff --git a/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/event/HttpSessionEnd.java b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/event/HttpSessionEnd.java new file mode 100644 index 0000000000..7f2bebe94a --- /dev/null +++ b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/event/HttpSessionEnd.java @@ -0,0 +1,38 @@ +/* + * 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.event; + +/** + * Propagated when an HTTP-based session is expired + * + * @version $$Rev$$ $$Date$$ + */ +public class HttpSessionEnd extends HttpSessionEvent { + + /** + * Creates a new event + * + * @param source the source of the event + * @param id the id of the HTTP session being ended + */ + public HttpSessionEnd(Object source, Object id) { + super(source, id); + } + +} diff --git a/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/event/HttpSessionEvent.java b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/event/HttpSessionEvent.java new file mode 100644 index 0000000000..ed245d0930 --- /dev/null +++ b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/event/HttpSessionEvent.java @@ -0,0 +1,45 @@ +/* + * 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.event; + +/** + * A base implementation of HTTP-based session events in the runtime + * + * @version $$Rev$$ $$Date$$ + */ +public abstract class HttpSessionEvent extends AbstractEvent { + + private Object id; + + public HttpSessionEvent(Object source, Object id) { + super(source); + assert id != null : "Session id was null"; + this.id = id; + } + + + public Object getSource() { + return source; + } + + public Object getId() { + return id; + } + +} diff --git a/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/event/HttpSessionStart.java b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/event/HttpSessionStart.java new file mode 100644 index 0000000000..7f9c0fadea --- /dev/null +++ b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/event/HttpSessionStart.java @@ -0,0 +1,38 @@ +/* + * 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.event; + +/** + * Propagated when an HTTP-based session has started + * + * @version $$Rev$$ $$Date$$ + */ +public class HttpSessionStart extends HttpSessionEvent { + + /** + * Creates a new event + * + * @param source the source of the event + * @param id the id of the HTTP session being ended + */ + public HttpSessionStart(Object source, Object id) { + super(source, id); + } + +} diff --git a/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/event/RequestEnd.java b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/event/RequestEnd.java new file mode 100644 index 0000000000..25856e86f8 --- /dev/null +++ b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/event/RequestEnd.java @@ -0,0 +1,38 @@ +/* + * 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.event; + +/** + * Propagated when a request completes or is ended + * + * @version $$Rev$$ $$Date$$ + */ +public class RequestEnd extends AbstractRequestEvent { + + /** + * Creates a new event + * + * @param source the source of the event + */ + public RequestEnd(Object source) { + super(source); + } + + +} diff --git a/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/event/RequestEvent.java b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/event/RequestEvent.java new file mode 100644 index 0000000000..9a6d767236 --- /dev/null +++ b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/event/RequestEvent.java @@ -0,0 +1,28 @@ +/* + * 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.event; + +/** + * Implemented by runtime events associated request + * + * @version $$Rev$$ $$Date$$ + */ +public interface RequestEvent { + +} diff --git a/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/event/RequestStart.java b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/event/RequestStart.java new file mode 100644 index 0000000000..466f52551a --- /dev/null +++ b/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/component/event/RequestStart.java @@ -0,0 +1,37 @@ +/* + * 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.event; + +/** + * Propagated when a request is started in the runtime + * + * @version $$Rev$$ $$Date$$ + */ +public class RequestStart extends AbstractRequestEvent { + + /** + * Creates a new event + * + * @param source the source of the event + */ + public RequestStart(Object source) { + super(source); + } + +} 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(); + } +} |