diff options
author | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2009-11-11 23:06:50 +0000 |
---|---|---|
committer | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2009-11-11 23:06:50 +0000 |
commit | 0f3f9b59b310833f31ba234ee4aefa808649833c (patch) | |
tree | 4f8b3354beb0643151424eb978eece9e55038c90 /sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca | |
parent | f860c2f35b2f94e379d2ff7d5c13f54cd4a3132a (diff) |
Moving 1.x branches
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@835120 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca')
83 files changed, 6573 insertions, 0 deletions
diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/component/CallableReferenceImpl.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/component/CallableReferenceImpl.java new file mode 100644 index 0000000000..f0f0049056 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/component/CallableReferenceImpl.java @@ -0,0 +1,59 @@ +/* + * 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.sca.core.component; + +import org.apache.tuscany.sca.factory.ObjectFactory; +import org.osoa.sca.CallableReference; +import org.osoa.sca.Conversation; + +/** + * 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/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/component/CallbackReferenceImpl.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/component/CallbackReferenceImpl.java new file mode 100644 index 0000000000..45a48730d3 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/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.sca.core.component; + +import org.apache.tuscany.sca.factory.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/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/component/ComponentContextImpl.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/component/ComponentContextImpl.java new file mode 100644 index 0000000000..abcd17f505 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/component/ComponentContextImpl.java @@ -0,0 +1,93 @@ +/* + * 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.sca.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; + +/** + * 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 (RuntimeException 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 (RuntimeException e) { + throw new ServiceRuntimeException(e.getMessage(), e); + } + } + + public <B> B getService(Class<B> businessInterface, String referenceName) { + try { + return component.getService(businessInterface, referenceName); + } catch (RuntimeException e) { + throw new ServiceRuntimeException(e.getMessage(), e); + } + } + + public <B> ServiceReference<B> getServiceReference(Class<B> businessInterface, String referenceName) { + try { + return component.getServiceReference(businessInterface, referenceName); + } catch (RuntimeException e) { + throw new ServiceRuntimeException(e.getMessage(), e); + } + } + + public <B> B getProperty(Class<B> type, String propertyName) { + try { + return component.getProperty(type, propertyName); + } catch (RuntimeException e) { + throw new ServiceRuntimeException(e.getMessage(), e); + } + } + + public <B> ServiceReference<B> createSelfReference(Class<B> businessInterface) { + return getServiceReference(businessInterface, "$self$."); + // 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 getServiceReference(businessInterface, "$self$."+serviceName); + } + + public RequestContext getRequestContext() { + return null; + } +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/component/ComponentContextProvider.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/component/ComponentContextProvider.java new file mode 100644 index 0000000000..3999d0d5b4 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/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.sca.core.component; + +import java.net.URI; + +import org.osoa.sca.CallableReference; +import org.osoa.sca.ComponentContext; +import org.osoa.sca.ServiceReference; + +/** + * 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/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/component/RequestContextImpl.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/component/RequestContextImpl.java new file mode 100644 index 0000000000..3a219b9cd9 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/component/RequestContextImpl.java @@ -0,0 +1,54 @@ +/* + * 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.sca.core.component; + +import javax.security.auth.Subject; + +import org.osoa.sca.CallableReference; +import org.osoa.sca.RequestContext; +import org.osoa.sca.ServiceReference; + +/** + * @version $Rev$ $Date$ + */ +public class RequestContextImpl implements RequestContext { + + public RequestContextImpl() { + } + + public Subject getSecuritySubject() { + throw new UnsupportedOperationException(); + } + + public String getServiceName() { + throw new UnsupportedOperationException(); + } + + public <B> ServiceReference<B> getServiceReference() { + throw new UnsupportedOperationException(); + } + + public <CB> CB getCallback() { + throw new UnsupportedOperationException(); + } + + public <CB> CallableReference<CB> getCallbackReference() { + throw new UnsupportedOperationException(); + } +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/component/ServiceReferenceImpl.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/component/ServiceReferenceImpl.java new file mode 100644 index 0000000000..e268f5750f --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/component/ServiceReferenceImpl.java @@ -0,0 +1,51 @@ +/* + * 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.sca.core.component; + +import org.apache.tuscany.sca.factory.ObjectFactory; +import org.osoa.sca.ServiceReference; + +/** + * 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/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/event/BaseEventPublisher.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/event/BaseEventPublisher.java new file mode 100644 index 0000000000..517e5579a4 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/event/BaseEventPublisher.java @@ -0,0 +1,89 @@ +/* + * 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.sca.core.event; + +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.CopyOnWriteArrayList; + +import org.apache.tuscany.sca.event.Event; +import org.apache.tuscany.sca.event.EventFilter; +import org.apache.tuscany.sca.event.EventPublisher; +import org.apache.tuscany.sca.event.RuntimeEventListener; +import org.apache.tuscany.sca.event.TrueFilter; + +/** + * Base implementation of an <code>EventPublisher</code> + * + * @version $Rev$ $Date$ + */ +public abstract class BaseEventPublisher implements EventPublisher { + protected static final EventFilter TRUE_FILTER = new TrueFilter(); + protected Map<EventFilter, List<RuntimeEventListener>> listeners; + + public void addListener(RuntimeEventListener listener) { + addListener(TRUE_FILTER, listener); + } + + public void removeListener(RuntimeEventListener listener) { + assert listener != null : "Listener cannot be null"; + synchronized (getListeners()) { + for (List<RuntimeEventListener> currentList : getListeners().values()) { + for (RuntimeEventListener current : currentList) { + if (current == listener) { + currentList.remove(current); + return; + } + } + } + } + } + + public void addListener(EventFilter filter, RuntimeEventListener listener) { + assert listener != null : "Listener cannot be null"; + synchronized (getListeners()) { + List<RuntimeEventListener> list = getListeners().get(filter); + if (list == null) { + list = new CopyOnWriteArrayList<RuntimeEventListener>(); + listeners.put(filter, list); + } + list.add(listener); + } + } + + public void publish(Event event) { + assert event != null : "Event object was null"; + for (Map.Entry<EventFilter, List<RuntimeEventListener>> entry : getListeners().entrySet()) { + if (entry.getKey().match(event)) { + for (RuntimeEventListener listener : entry.getValue()) { + listener.onEvent(event); + } + } + } + } + + protected Map<EventFilter, List<RuntimeEventListener>> getListeners() { + if (listeners == null) { + listeners = new ConcurrentHashMap<EventFilter, List<RuntimeEventListener>>(); + } + return listeners; + } + +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/event/ComponentStart.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/event/ComponentStart.java new file mode 100644 index 0000000000..8e90c953e5 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/event/ComponentStart.java @@ -0,0 +1,54 @@ +/* + * 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.sca.core.event; + +import java.net.URI; + +import org.apache.tuscany.sca.event.Event; + +/** + * Propagated when a component starts + * + * @version $$Rev$$ $$Date$$ + */ +public class ComponentStart implements Event { + + private Object source; + 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) { + this.source = source; + this.uri = componentURI; + } + + public URI getComponentURI() { + return uri; + } + + public Object getSource() { + return source; + } + +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/event/ComponentStop.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/event/ComponentStop.java new file mode 100644 index 0000000000..d879182e14 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/event/ComponentStop.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.sca.core.event; + +import java.net.URI; + +import org.apache.tuscany.sca.event.Event; + +/** + * Propagated when a component stops + * + * @version $$Rev$$ $$Date$$ + */ +public class ComponentStop implements Event { + + private Object source; + 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) { + this.source = source; + this.uri = componentUri; + } + + public URI getComponentURI() { + return uri; + } + + public Object getSource() { + return source; + } +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/event/ConversationEnd.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/event/ConversationEnd.java new file mode 100644 index 0000000000..cd14710880 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/event/ConversationEnd.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.sca.core.event; + +import org.apache.tuscany.sca.event.Event; + + + +/** + * Propagated when a conversation is expired + * + * @version $$Rev$$ $$Date$$ + */ +public class ConversationEnd implements Event { + + private Object source; + private Object id; + + /** + * Creates a new event + * + * @param source the source of the event + * @param id the id of the conversation being ended + */ + public ConversationEnd(Object source, Object id) { + this.source = source; + this.id = id; + } + + public Object getSource() { + return source; + } + + public Object getConversationID() { + return id; + } +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/event/ConversationStart.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/event/ConversationStart.java new file mode 100644 index 0000000000..a4cc49204e --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/event/ConversationStart.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.sca.core.event; + +import org.apache.tuscany.sca.event.Event; + + +/** + * Propagated when a conversation has started + * + * @version $$Rev$$ $$Date$$ + */ +public class ConversationStart implements Event { + + private Object source; + private Object id; + + /** + * Creates a new event + * + * @param source the source of the event + * @param id the id of the conversation being started + */ + public ConversationStart(Object source, Object id) { + this.source = source; + this.id = id; + } + + public Object getSource() { + return source; + } + + public Object getConversationID() { + return id; + } +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/event/HttpSessionEnd.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/event/HttpSessionEnd.java new file mode 100644 index 0000000000..4c3fd8b397 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/event/HttpSessionEnd.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.sca.core.event; + +import org.apache.tuscany.sca.event.Event; + + + +/** + * Propagated when an HTTP-based session is expired + * + * @version $$Rev$$ $$Date$$ + */ +public class HttpSessionEnd implements Event { + + private Object source; + private Object id; + + /** + * 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) { + this.source = source; + this.id = id; + } + + public Object getSource() { + return source; + } + + public Object getSessionID() { + return id; + } +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/event/HttpSessionStart.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/event/HttpSessionStart.java new file mode 100644 index 0000000000..67b922b99c --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/event/HttpSessionStart.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.sca.core.event; + +import org.apache.tuscany.sca.event.Event; + + +/** + * Propagated when an HTTP-based session has started + * + * @version $$Rev$$ $$Date$$ + */ +public class HttpSessionStart implements Event { + + private Object source; + private Object id; + + /** + * Creates a new event + * + * @param source the source of the event + * @param id the id of the HTTP session being started + */ + public HttpSessionStart(Object source, Object id) { + this.source = source; + this.id = id; + } + + public Object getSource() { + return source; + } + + public Object getSessionID() { + return id; + } +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/event/RequestEnd.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/event/RequestEnd.java new file mode 100644 index 0000000000..8b5c512cf2 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/event/RequestEnd.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.sca.core.event; + +import org.apache.tuscany.sca.event.Event; + +/** + * Propagated when a request completes or is ended + * + * @version $$Rev$$ $$Date$$ + */ +public class RequestEnd implements Event { + + private Object source; + + /** + * Creates a new event + * + * @param source the source of the event + */ + public RequestEnd(Object source) { + this.source = source; + } + + public Object getSource() { + return source; + } + +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/event/RequestStart.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/event/RequestStart.java new file mode 100644 index 0000000000..974563b57c --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/event/RequestStart.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.sca.core.event; + +import org.apache.tuscany.sca.event.Event; + +/** + * Propagated when a request is started in the runtime + * + * @version $$Rev$$ $$Date$$ + */ +public class RequestStart implements Event { + + private Object source; + + /** + * Creates a new event + * + * @param source the source of the event + */ + public RequestStart(Object source) { + this.source = source; + } + + public Object getSource() { + return source; + } + +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/AbstractInvocationHandler.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/AbstractInvocationHandler.java new file mode 100644 index 0000000000..6c1f38e4c2 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/AbstractInvocationHandler.java @@ -0,0 +1,104 @@ +/* + * 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.sca.core.invocation; + +import java.util.UUID; + +import org.apache.tuscany.sca.interfacedef.ConversationSequence; +import org.apache.tuscany.sca.interfacedef.Interface; +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.invocation.InvocationChain; +import org.apache.tuscany.sca.invocation.Invoker; +import org.apache.tuscany.sca.invocation.Message; +import org.apache.tuscany.sca.invocation.MessageFactory; +import org.apache.tuscany.sca.runtime.RuntimeWire; + +/** + * Base class for performing invocations on a wire. Subclasses are responsible for retrieving and supplying the + * appropriate chain, target invoker, and invocation arguments. + * + * @version $Rev$ $Date$ + */ +public abstract class AbstractInvocationHandler { + protected boolean conversational; + private boolean conversationStarted; + private MessageFactory messageFactory; + + protected AbstractInvocationHandler(MessageFactory messageFactory, boolean conversational) { + this.conversational = conversational; + this.messageFactory = messageFactory; + } + + protected Object invoke(InvocationChain chain, Object[] args, RuntimeWire wire) throws Throwable { + + Message msgContext = ThreadMessageContext.getMessageContext(); + Message msg = messageFactory.createMessage(); + if (conversational) { + Object id = msgContext.getConversationID(); + if (id == null) { + String convIdFromThread = createConversationID(); + msg.setConversationID(convIdFromThread); + } + } + + Invoker headInvoker = chain.getHeadInvoker(); + msg.setCorrelationID(msgContext.getCorrelationID()); + Operation operation = chain.getTargetOperation(); + Interface contract = operation.getInterface(); + if (contract != null && contract.isConversational()) { + ConversationSequence sequence = chain.getTargetOperation().getConversationSequence(); + if (sequence == ConversationSequence.CONVERSATION_END) { + msg.setConversationSequence(ConversationSequence.CONVERSATION_END); + conversationStarted = false; + } else if (sequence == ConversationSequence.CONVERSATION_CONTINUE) { + if (conversationStarted) { + msg.setConversationSequence(ConversationSequence.CONVERSATION_CONTINUE); + } else { + conversationStarted = true; + msg.setConversationSequence(ConversationSequence.CONVERSATION_START); + } + } + } + msg.setBody(args); + msg.setFrom(wire.getSource()); + msg.setTo(wire.getTarget()); + ThreadMessageContext.setMessageContext(msg); + try { + // dispatch the wire down the chain and get the response + Message resp = headInvoker.invoke(msg); + Object body = resp.getBody(); + if (resp.isFault()) { + throw (Throwable)body; + } + return body; + } finally { + ThreadMessageContext.setMessageContext(msgContext); + } + } + + /** + * Creates a new conversational id + * + * @return the conversational id + */ + private String createConversationID() { + return UUID.randomUUID().toString(); + } + +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/CallbackInterfaceInterceptor.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/CallbackInterfaceInterceptor.java new file mode 100644 index 0000000000..219dc55536 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/CallbackInterfaceInterceptor.java @@ -0,0 +1,57 @@ +/* + * 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.sca.core.invocation; + +import org.apache.tuscany.sca.invocation.Interceptor; +import org.apache.tuscany.sca.invocation.Invoker; +import org.apache.tuscany.sca.invocation.Message; +import org.osoa.sca.NoRegisteredCallbackException; + +/** + * An interceptor applied to the forward direction of a wire that ensures the callback target implements the required + * service contract. This is required as callback targets may be set dynamically by service implementations. + * + * @version $Rev$ $Date$ + */ +public class CallbackInterfaceInterceptor implements Interceptor { + private boolean invokingServiceImplements; + private Invoker next; + + public CallbackInterfaceInterceptor(boolean invokingServiceImplements) { + this.invokingServiceImplements = invokingServiceImplements; + } + + public Message invoke(Message msg) { + // TODO check in the context if a callback object is set, if so invoke next since the setCallback will + // perform the check + if (!invokingServiceImplements) { + throw new NoRegisteredCallbackException("Callback target does not implement the callback interface"); + } + return next.invoke(msg); + } + + public void setNext(Invoker next) { + this.next = next; + } + + public Invoker getNext() { + return next; + } + +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/CallbackWireObjectFactory.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/CallbackWireObjectFactory.java new file mode 100644 index 0000000000..dae4ed708b --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/CallbackWireObjectFactory.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.sca.core.invocation; + +import java.util.List; + +import org.apache.tuscany.sca.factory.ObjectCreationException; +import org.apache.tuscany.sca.factory.ObjectFactory; +import org.apache.tuscany.sca.runtime.RuntimeWire; + +/** + * Returns proxy instance for a wire callback + * + * @version $Rev$ $Date$ + */ +public class CallbackWireObjectFactory implements ObjectFactory { + private ProxyFactory proxyFactory; + private Class<?> interfaze; + private List<RuntimeWire> wires; + + public CallbackWireObjectFactory(Class<?> interfaze, ProxyFactory proxyService, List<RuntimeWire> wires) { + this.interfaze = interfaze; + this.proxyFactory = proxyService; + this.wires = wires; + } + + public Object getInstance() throws ObjectCreationException { + return proxyFactory.createCallbackProxy(interfaze, wires); + } + +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/ExtensibleWireProcessor.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/ExtensibleWireProcessor.java new file mode 100644 index 0000000000..934f2f7aa0 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/ExtensibleWireProcessor.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.sca.core.invocation; + +import org.apache.tuscany.sca.runtime.RuntimeWire; +import org.apache.tuscany.sca.runtime.RuntimeWireProcessor; +import org.apache.tuscany.sca.runtime.RuntimeWireProcessorExtensionPoint; + +/** + * The default implementation of an extensible <code>WireProcessor</code> + * + * @version $Rev$ $Date$ + */ +public class ExtensibleWireProcessor implements RuntimeWireProcessor { + + private RuntimeWireProcessorExtensionPoint processors; + + public ExtensibleWireProcessor(RuntimeWireProcessorExtensionPoint processors) { + this.processors = processors; + } + + public void process(RuntimeWire wire) { + for (RuntimeWireProcessor processor : processors.getWireProcessors()) { + processor.process(wire); + } + } + +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/InvocationChainImpl.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/InvocationChainImpl.java new file mode 100644 index 0000000000..e04148e374 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/InvocationChainImpl.java @@ -0,0 +1,126 @@ +/* + * 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.sca.core.invocation; + +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.invocation.Interceptor; +import org.apache.tuscany.sca.invocation.InvocationChain; +import org.apache.tuscany.sca.invocation.Invoker; + +/** + * Default implementation of an invocation chain + * + * @version $Rev$ $Date$ + */ +public class InvocationChainImpl implements InvocationChain { + private Operation sourceOperation; + private Operation targetOperation; + private Invoker invokerChainHead; + private Invoker invokerChainTail; + + public InvocationChainImpl(Operation operation) { + assert operation != null; + this.targetOperation = operation; + this.sourceOperation = operation; + } + + public InvocationChainImpl(Operation sourceOperation, Operation targetOperation) { + assert sourceOperation != null; + assert targetOperation != null; + this.targetOperation = targetOperation; + this.sourceOperation = sourceOperation; + } + + public Operation getTargetOperation() { + return targetOperation; + } + + public void addInterceptor(Interceptor interceptor) { + if (invokerChainHead == null) { + invokerChainHead = interceptor; + } else { + if (invokerChainHead instanceof Interceptor) { + ((Interceptor)invokerChainTail).setNext(interceptor); + } + } + invokerChainTail = interceptor; + } + + public void addInvoker(Invoker invoker) { + if (invokerChainHead == null) { + invokerChainHead = invoker; + } else { + if (invokerChainTail instanceof Interceptor) { + ((Interceptor)invokerChainTail).setNext(invoker); + } + } + invokerChainTail = invoker; + } + + public Invoker getHeadInvoker() { + return invokerChainHead; + } + + public Invoker getTailInvoker() { + return invokerChainTail; + } + + /** + * @return the sourceOperation + */ + public Operation getSourceOperation() { + return sourceOperation; + } + + /** + * @param sourceOperation the sourceOperation to set + */ + public void setSourceOperation(Operation sourceOperation) { + this.sourceOperation = sourceOperation; + } + + public void addInterceptor(int index, Interceptor interceptor) { + int i = 0; + Invoker next = invokerChainHead; + Invoker prev = null; + while (next != null && i < index) { + prev = next; + if (next instanceof Interceptor) { + next = ((Interceptor)next).getNext(); + i++; + } else { + throw new ArrayIndexOutOfBoundsException(index); + } + } + if (i == index) { + if (prev != null) { + ((Interceptor)prev).setNext(interceptor); + } else { + invokerChainHead = interceptor; + } + interceptor.setNext(next); + if (next == null) { + invokerChainTail = interceptor; + } + } else { + throw new ArrayIndexOutOfBoundsException(index); + } + } + +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/JDKCallbackInvocationHandler.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/JDKCallbackInvocationHandler.java new file mode 100644 index 0000000000..5a028e6e22 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/JDKCallbackInvocationHandler.java @@ -0,0 +1,95 @@ +/* + * 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.sca.core.invocation; + +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.IdentityHashMap; +import java.util.List; +import java.util.Map; + +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.interfacedef.java.impl.JavaInterfaceUtil; +import org.apache.tuscany.sca.invocation.InvocationChain; +import org.apache.tuscany.sca.invocation.MessageFactory; +import org.apache.tuscany.sca.runtime.EndpointReference; +import org.apache.tuscany.sca.runtime.RuntimeWire; +import org.osoa.sca.NoRegisteredCallbackException; + +/** + * Responsible for dispatching to a callback through a wire. <p/> TODO cache + * target invoker + * + * @version $Rev$ $Date$ + */ +public class JDKCallbackInvocationHandler extends AbstractInvocationHandler implements InvocationHandler { + private static final long serialVersionUID = -3350283555825935609L; + private transient Map<String, RuntimeWire> wires; + + /** + * Constructor used for deserialization only + */ + public JDKCallbackInvocationHandler(MessageFactory messageFactory) { + super(messageFactory, false); + wires = new HashMap<String, RuntimeWire>(); + } + + public JDKCallbackInvocationHandler(MessageFactory messageFactory, List<RuntimeWire> wireList) { + super(messageFactory, false); + this.wires = new HashMap<String, RuntimeWire>(); + for (RuntimeWire wire : wireList) { + wires.put(wire.getSource().getURI(), wire); + } + } + + @SuppressWarnings( {"unchecked"}) + public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { + if (method.getParameterTypes().length == 0 && "toString".equals(method.getName())) { + return "[Proxy - " + Integer.toHexString(hashCode()) + "]"; + } else if (method.getDeclaringClass().equals(Object.class) && "equals".equals(method.getName())) { + // TODO implement + throw new UnsupportedOperationException(); + } else if (Object.class.equals(method.getDeclaringClass()) && "hashCode".equals(method.getName())) { + return hashCode(); + // TODO beter hash algorithm + } + EndpointReference from = ThreadMessageContext.getMessageContext().getFrom(); + RuntimeWire wire = wires.get(from.getURI()); + assert wire != null; + List<InvocationChain> chains = wire.getCallbackInvocationChains(); + IdentityHashMap<Operation, InvocationChain> map = new IdentityHashMap<Operation, InvocationChain>(); + for (InvocationChain chain : chains) { + map.put(chain.getTargetOperation(), chain); + } + Operation operation = JavaInterfaceUtil.findOperation(method, map.keySet()); + InvocationChain chain = map.get(operation); + try { + return invoke(chain, args, wire); + } catch (InvocationTargetException e) { + Throwable t = e.getCause(); + if (t instanceof NoRegisteredCallbackException) { + throw t; + } + throw e; + } + } + +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/JDKInvocationHandler.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/JDKInvocationHandler.java new file mode 100644 index 0000000000..756e5c545f --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/JDKInvocationHandler.java @@ -0,0 +1,115 @@ +/* + * 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.sca.core.invocation; + +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; +import java.lang.reflect.Proxy; +import java.util.List; + +import org.apache.tuscany.sca.interfacedef.DataType; +import org.apache.tuscany.sca.interfacedef.InterfaceContract; +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.invocation.InvocationChain; +import org.apache.tuscany.sca.invocation.MessageFactory; +import org.apache.tuscany.sca.runtime.RuntimeWire; + +/** + * @version $Rev$ $Date$ + */ +public class JDKInvocationHandler extends AbstractInvocationHandler implements InvocationHandler { + // private Class<?> proxyInterface; + private RuntimeWire wire; + + public JDKInvocationHandler(MessageFactory messageFactory, Class<?> proxyInterface, RuntimeWire wire) { + super(messageFactory, false); + this.wire = wire; + init(proxyInterface, wire); + } + + private void init(Class<?> interfaze, RuntimeWire wire) { + InterfaceContract contract = wire.getSource().getInterfaceContract(); + this.conversational = contract.getInterface().isConversational(); + } + + public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { + if (method.getParameterTypes().length == 0 && "toString".equals(method.getName())) { + return "[Proxy - " + Integer.toHexString(hashCode()) + "]"; + } else if (method.getDeclaringClass().equals(Object.class) && "equals".equals(method.getName())) { + Object obj = args[0]; + if (obj == null) { + return false; + } + if (!Proxy.isProxyClass(obj.getClass())) { + return false; + } + return equals(Proxy.getInvocationHandler(obj)); + } else if (Object.class.equals(method.getDeclaringClass()) && "hashCode".equals(method.getName())) { + return hashCode(); + } + InvocationChain chain = getInvocationChain(method); + if (chain == null) { + throw new IllegalArgumentException("No matching opeeration is found: " + method); + } + + // send the invocation down the wire + Object result = invoke(chain, args, wire); + + return result; + } + + /** + * Determines if the given operation matches the given method + * + * @return true if the operation matches, false if does not + */ + private static boolean match(Operation operation, Method method) { + Class<?>[] params = method.getParameterTypes(); + DataType<List<DataType>> inputType = operation.getInputType(); + List<DataType> types = inputType.getLogical(); + boolean matched = true; + if (types.size() == params.length && method.getName().equals(operation.getName())) { + for (int i = 0; i < params.length; i++) { + Class<?> clazz = params[i]; + if (!clazz.equals(operation.getInputType().getLogical().get(i).getPhysical())) { + matched = false; + } + } + } else { + matched = false; + } + return matched; + + } + + private InvocationChain getInvocationChain(Method method) { + for (InvocationChain chain : wire.getInvocationChains()) { + Operation operation = chain.getSourceOperation(); + if (operation.isDynamic()) { + operation.setName(method.getName()); + return chain; + } else if (match(operation, method)) { + return chain; + } + } + return null; + } + +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/JDKProxyService.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/JDKProxyService.java new file mode 100644 index 0000000000..371f9bedb8 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/JDKProxyService.java @@ -0,0 +1,78 @@ +/* + * 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.sca.core.invocation; + +import java.lang.reflect.Proxy; +import java.util.List; + +import org.apache.tuscany.sca.interfacedef.InterfaceContractMapper; +import org.apache.tuscany.sca.interfacedef.impl.InterfaceContractMapperImpl; +import org.apache.tuscany.sca.invocation.MessageFactory; +import org.apache.tuscany.sca.runtime.RuntimeWire; +import org.osoa.sca.CallableReference; + +/** + * the default implementation of a wire service that uses JDK dynamic proxies + * + * @version $$Rev$$ $$Date: 2007-04-11 18:59:43 -0700 (Wed, 11 Apr + * 2007) $$ + */ +public class JDKProxyService implements ProxyFactory { + protected InterfaceContractMapper contractMapper; + private MessageFactory messageFactory; + + public JDKProxyService() { + this(new MessageFactoryImpl(), new InterfaceContractMapperImpl()); + } + + public JDKProxyService(MessageFactory messageFactory, InterfaceContractMapper mapper) { + this.contractMapper = mapper; + this.messageFactory = messageFactory; + } + + public <T> T createProxy(Class<T> interfaze, RuntimeWire wire) throws ProxyCreationException { + assert interfaze != null; + assert wire != null; + JDKInvocationHandler handler = new JDKInvocationHandler(messageFactory, interfaze, wire); + ClassLoader cl = interfaze.getClassLoader(); + return interfaze.cast(Proxy.newProxyInstance(cl, new Class[] {interfaze}, handler)); + } + + public Object createCallbackProxy(Class<?> interfaze, List<RuntimeWire> wires) throws ProxyCreationException { + ClassLoader cl = interfaze.getClassLoader(); + JDKCallbackInvocationHandler handler = new JDKCallbackInvocationHandler(messageFactory, wires); + return interfaze.cast(Proxy.newProxyInstance(cl, new Class[] {interfaze}, handler)); + } + + public <B, R extends CallableReference<B>> R cast(B target) throws IllegalArgumentException { + throw new UnsupportedOperationException(); + + // InvocationHandler handler = Proxy.getInvocationHandler(target); + // if (handler instanceof JDKInvocationHandler) { + // // TODO return a ServiceReference + // throw new UnsupportedOperationException(); + // } else if (handler instanceof JDKCallbackInvocationHandler) { + // // TODO return a CallbackReference + // throw new UnsupportedOperationException(); + // } else { + // throw new IllegalArgumentException("Not a Tuscany SCA proxy"); + // } + + } +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/MessageFactoryImpl.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/MessageFactoryImpl.java new file mode 100644 index 0000000000..6e182a6a14 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/MessageFactoryImpl.java @@ -0,0 +1,36 @@ +/* + * 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.sca.core.invocation; + +import org.apache.tuscany.sca.invocation.Message; +import org.apache.tuscany.sca.invocation.MessageFactory; + +/** + * Implementation of MessageFactory. + * + * @version $Rev$ $Date$ + */ +public class MessageFactoryImpl implements MessageFactory { + + public Message createMessage() { + return new MessageImpl(); + } + +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/MessageImpl.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/MessageImpl.java new file mode 100644 index 0000000000..a1b2963cf5 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/MessageImpl.java @@ -0,0 +1,117 @@ +/* + * 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.sca.core.invocation; + +import org.apache.tuscany.sca.interfacedef.ConversationSequence; +import org.apache.tuscany.sca.invocation.Message; +import org.apache.tuscany.sca.runtime.EndpointReference; + +/** + * The default implementation of a message flowed through a wire during an invocation + * + * @version $Rev $Date + */ +public class MessageImpl implements Message { + private Object body; + private Object messageID; + private Object correlationID; + private boolean isFault; + private ConversationSequence conversationSequence; + private String conversationId; + + private EndpointReference from; + private EndpointReference to; + + public MessageImpl(String conversationId, ConversationSequence conversationSequence, Object body) { + this.conversationId = conversationId; + this.conversationSequence = conversationSequence; + this.body = body; + } + + public MessageImpl() { + } + + @SuppressWarnings("unchecked") + public <T> T getBody() { + return (T) body; + } + + public <T> void setBody(T body) { + this.isFault = false; + this.body = body; + } + + public String getConversationID() { + return conversationId; + } + + public void setConversationID(String conversationId) { + this.conversationId = conversationId; + } + + public ConversationSequence getConversationSequence() { + return conversationSequence; + } + + public void setConversationSequence(ConversationSequence conversationSequence) { + this.conversationSequence = conversationSequence; + } + + public Object getMessageID() { + return messageID; + } + + public void setMessageID(Object messageId) { + this.messageID = messageId; + } + + public Object getCorrelationID() { + return correlationID; + } + + public void setCorrelationID(Object correlationId) { + this.correlationID = correlationId; + } + + public boolean isFault() { + return isFault; + } + + public void setFaultBody(Object fault) { + this.isFault = true; + this.body = fault; + } + + public EndpointReference getFrom() { + return from; + } + + public void setFrom(EndpointReference from) { + this.from = from; + } + + public EndpointReference getTo() { + return to; + } + + public void setTo(EndpointReference to) { + this.to = to; + } + +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/NoMethodForOperationException.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/NoMethodForOperationException.java new file mode 100644 index 0000000000..cbeedc086d --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/NoMethodForOperationException.java @@ -0,0 +1,43 @@ +/* + * 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.sca.core.invocation; + + +/** + * Thrown when an {@link org.apache.tuscany.sca.factory.model.Operation} cannot be mapped to a method on an interface + * @version $Rev$ $Date$ + */ +public class NoMethodForOperationException extends ProxyCreationException { + private static final long serialVersionUID = 5116536602309483679L; + + public NoMethodForOperationException() { + } + + public NoMethodForOperationException(String message) { + super(message); + } + + public NoMethodForOperationException(String message, Throwable cause) { + super(message, cause); + } + + public NoMethodForOperationException(Throwable cause) { + super(cause); + } +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/NonBlockingInterceptor.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/NonBlockingInterceptor.java new file mode 100644 index 0000000000..be6f364271 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/NonBlockingInterceptor.java @@ -0,0 +1,174 @@ +/* + * 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.sca.core.invocation; + +import java.util.LinkedList; + +import org.apache.tuscany.sca.interfacedef.ConversationSequence; +import org.apache.tuscany.sca.invocation.Interceptor; +import org.apache.tuscany.sca.invocation.Invoker; +import org.apache.tuscany.sca.invocation.Message; +import org.apache.tuscany.sca.runtime.EndpointReference; +import org.apache.tuscany.sca.runtime.RuntimeWire; +import org.apache.tuscany.sca.work.WorkScheduler; +import org.osoa.sca.ServiceRuntimeException; + +/** + * Adds non-blocking behavior to an invocation chain + * + * @version $$Rev$$ $$Date$$ + */ +public class NonBlockingInterceptor implements Interceptor { + + private static final Message RESPONSE = new ImmutableMessage(); + + private WorkScheduler workScheduler; + private Invoker next; + + public NonBlockingInterceptor(WorkScheduler workScheduler) { + this.workScheduler = workScheduler; + } + + public NonBlockingInterceptor(WorkScheduler workScheduler, Interceptor next) { + this.workScheduler = workScheduler; + this.next = next; + } + + public Message invoke(final Message msg) { + // Retrieve conversation id to transfer to new thread + // Notice that we cannot clear the conversation id from the current thread + final String conversationID = ThreadMessageContext.getMessageContext().getConversationID(); + // Schedule the invocation of the next interceptor in a new Work instance + try { + workScheduler.scheduleWork(new Runnable() { + public void run() { + msg.setCorrelationID(null); + // if we got a conversation id, transfer it to new thread + if (conversationID != null) { + msg.setConversationID(conversationID); + } + Message context = ThreadMessageContext.setMessageContext(msg); + try { + next.invoke(msg); + } finally { + ThreadMessageContext.setMessageContext(context); + } + } + }); + } catch (Exception e) { + throw new ServiceRuntimeException(e); + } + return RESPONSE; + } + + public Invoker getNext() { + return next; + } + + public void setNext(Invoker next) { + this.next = next; + } + + /** + * A dummy message passed back on an invocation + */ + private static class ImmutableMessage implements Message { + + public String getConversationID() { + return null; + } + + public RuntimeWire getWire() { + return null; + } + + public void setConversationID(String conversationId) { + throw new UnsupportedOperationException(); + } + + public void setWire(RuntimeWire wire) { + throw new UnsupportedOperationException(); + } + + @SuppressWarnings("unchecked") + public Object getBody() { + return null; + } + + public void setBody(Object body) { + if (body != null) { + throw new UnsupportedOperationException(); + } + } + + public void setCallbackWires(LinkedList<RuntimeWire> wires) { + + } + + public Object getMessageID() { + return null; + } + + public void setMessageID(Object messageId) { + throw new UnsupportedOperationException(); + } + + public Object getCorrelationID() { + return null; + } + + public void setCorrelationID(Object correlationId) { + throw new UnsupportedOperationException(); + } + + public boolean isFault() { + return false; + } + + public void setFaultBody(Object fault) { + throw new UnsupportedOperationException(); + } + + public void setConversationSequence(ConversationSequence sequence) { + throw new UnsupportedOperationException(); + } + + public ConversationSequence getConversationSequence() { + return null; + } + + public EndpointReference getFrom() { + return null; + } + + public EndpointReference getTo() { + return null; + } + + public void setFrom(EndpointReference from) { + throw new UnsupportedOperationException(); + } + + public void setTo(EndpointReference to) { + throw new UnsupportedOperationException(); + } + + } + +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/ProxyCreationException.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/ProxyCreationException.java new file mode 100644 index 0000000000..4900ae9a7e --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/ProxyCreationException.java @@ -0,0 +1,48 @@ +/* + * 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.sca.core.invocation; + +import org.apache.tuscany.sca.factory.ObjectCreationException; + + +/** + * Denotes an error creating a proxy + * + * @version $$Rev$$ $$Date$$ + */ +public class ProxyCreationException extends ObjectCreationException { + private static final long serialVersionUID = 8002454344828513781L; + + public ProxyCreationException() { + super(); + } + + public ProxyCreationException(String message, Throwable cause) { + super(message, cause); + } + + public ProxyCreationException(String message) { + super(message); + } + + public ProxyCreationException(Throwable cause) { + super(cause); + } + +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/ProxyFactory.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/ProxyFactory.java new file mode 100644 index 0000000000..381844ffc5 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/ProxyFactory.java @@ -0,0 +1,61 @@ +/* + * 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.sca.core.invocation; + +import java.util.List; + +import org.apache.tuscany.sca.runtime.RuntimeWire; +import org.osoa.sca.CallableReference; + +/** + * Creates proxies that implement Java interfaces and invocation handlers for fronting wires + * + * @version $$Rev$$ $$Date$$ + */ + +public interface ProxyFactory { + + /** + * Creates a Java proxy for the given wire + * + * @param interfaze the interface the proxy implements + * @param wire the wire to proxy @return the proxy + * @throws ProxyCreationException + */ + <T> T createProxy(Class<T> interfaze, RuntimeWire wire) throws ProxyCreationException; + + /** + * Creates a Java proxy for the service contract callback + * + * @param interfaze the interface the proxy should implement + * @return the proxy + * @throws ProxyCreationException + */ + Object createCallbackProxy(Class<?> interfaze, List<RuntimeWire> wires) throws ProxyCreationException; + + /** + * Cast a proxy to a CallableReference. + * + * @param target a proxy generated by this implementation + * @return a CallableReference (or subclass) equivalent to this prozy + * @throws IllegalArgumentException if the object supplied is not a proxy + */ + <B, R extends CallableReference<B>> R cast(B target) throws IllegalArgumentException; + +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/ThreadMessageContext.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/ThreadMessageContext.java new file mode 100644 index 0000000000..52bea91341 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/ThreadMessageContext.java @@ -0,0 +1,68 @@ +/* + * 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.sca.core.invocation; + +import org.apache.tuscany.sca.invocation.Message; + +/** + * Class for tunneling a WorkContext through the invocation of a user class. + * + * @version $Rev$ $Date$ + */ +public final class ThreadMessageContext { + + private static final ThreadLocal<Message> CONTEXT = new ThreadLocal<Message>() { + protected synchronized Message initialValue() { + return new MessageImpl(); + } + }; + + private ThreadMessageContext() { + } + + /** + * Set the WorkContext for the current thread. + * The current work context is returned and must be restored after the invocation is complete. + * Typical usage would be: + * <pre> + * WorkContext old = PojoWorkContextTunnel.setThreadWorkContext(newContext); + * try { + * ... invoke user code ... + * } finally { + * PojoWorkContextTunnel.setThreadWorkContext(old); + * } + * </pre> + * @param context + * @return the current work context for the thread; this must be restored after the invocation is made + */ + public static Message setMessageContext(Message context) { + Message old = CONTEXT.get(); + CONTEXT.set(context); + return old; + } + + /** + * Returns the WorkContext for the current thread. + * + * @return the WorkContext for the current thread + */ + public static Message getMessageContext() { + return CONTEXT.get(); + } +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/WireObjectFactory.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/WireObjectFactory.java new file mode 100644 index 0000000000..e6e41e8f85 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/WireObjectFactory.java @@ -0,0 +1,55 @@ +/* + * 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.sca.core.invocation; + +import org.apache.tuscany.sca.factory.ObjectCreationException; +import org.apache.tuscany.sca.factory.ObjectFactory; +import org.apache.tuscany.sca.runtime.RuntimeWire; + +/** + * Uses a wire to return an object instance + * + * @version $Rev$ $Date$ + */ +public class WireObjectFactory<T> implements ObjectFactory<T> { + private Class<T> interfaze; + private RuntimeWire wire; + private ProxyFactory proxyService; + private boolean optimizable; + + /** + * Constructor. + * + * @param interfaze the interface to inject on the client + * @param wire the backing wire + * @param proxyService the wire service to create the proxy + * @throws NoMethodForOperationException + */ + public WireObjectFactory(Class<T> interfaze, RuntimeWire wire, ProxyFactory proxyService) + throws NoMethodForOperationException { + this.interfaze = interfaze; + this.wire = wire; + this.proxyService = proxyService; + } + + public T getInstance() throws ObjectCreationException { + return interfaze.cast(proxyService.createProxy(interfaze, wire)); + } + +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/runtime/ActivationException.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/runtime/ActivationException.java new file mode 100644 index 0000000000..987a8288b0 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/runtime/ActivationException.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.sca.core.runtime; + + +/** + * Denotes an error starting the runtime + * + * @version $Rev$ $Date$ + */ +public class ActivationException extends Exception { + private static final long serialVersionUID = 8612661660934426123L; + + public ActivationException(String message) { + super(message); + } + + public ActivationException(Throwable cause) { + super(cause); + } +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/runtime/CompositeActivator.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/runtime/CompositeActivator.java new file mode 100644 index 0000000000..fc8a8f0a15 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/runtime/CompositeActivator.java @@ -0,0 +1,70 @@ +/* + * 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.sca.core.runtime; + +import org.apache.tuscany.sca.assembly.Component; +import org.apache.tuscany.sca.assembly.Composite; + +/** + * Start/stop a composite + * + * @version $Rev$ $Date$ + */ +public interface CompositeActivator { + + /** + * Activate a composite + * @param composite + */ + void activate(Composite composite) throws ActivationException; + + /** + * Stop a composite + * @param composite + */ + void deactivate(Composite composite) throws ActivationException; + + /** + * Start a composite + * @deprecated + * @param composite + */ + void start(Composite composite) throws ActivationException; + + /** + * Stop a composite + * @deprecated + * @param composite + */ + void stop(Composite composite) throws ActivationException; + + /** + * Start a component + * @param component + */ + void start(Component component) throws ActivationException; + + /** + * Stop a composite + * @param composite + */ + void stop(Component component) throws ActivationException; + +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/runtime/CompositeActivatorImpl.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/runtime/CompositeActivatorImpl.java new file mode 100644 index 0000000000..9f5d2ab63d --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/runtime/CompositeActivatorImpl.java @@ -0,0 +1,655 @@ +/* + * 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.sca.core.runtime; + +import org.apache.tuscany.sca.assembly.AssemblyFactory; +import org.apache.tuscany.sca.assembly.Binding; +import org.apache.tuscany.sca.assembly.Component; +import org.apache.tuscany.sca.assembly.ComponentReference; +import org.apache.tuscany.sca.assembly.ComponentService; +import org.apache.tuscany.sca.assembly.Composite; +import org.apache.tuscany.sca.assembly.Implementation; +import org.apache.tuscany.sca.assembly.SCABinding; +import org.apache.tuscany.sca.assembly.SCABindingFactory; +import org.apache.tuscany.sca.assembly.builder.CompositeBuilderException; +import org.apache.tuscany.sca.assembly.builder.CompositeBuilderMonitor; +import org.apache.tuscany.sca.assembly.builder.Problem; +import org.apache.tuscany.sca.assembly.builder.impl.CompositeBuilderImpl; +import org.apache.tuscany.sca.core.invocation.InvocationChainImpl; +import org.apache.tuscany.sca.core.invocation.NonBlockingInterceptor; +import org.apache.tuscany.sca.interfacedef.IncompatibleInterfaceContractException; +import org.apache.tuscany.sca.interfacedef.InterfaceContract; +import org.apache.tuscany.sca.interfacedef.InterfaceContractMapper; +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.invocation.InvocationChain; +import org.apache.tuscany.sca.invocation.Invoker; +import org.apache.tuscany.sca.provider.BindingProviderFactory; +import org.apache.tuscany.sca.provider.ImplementationProvider; +import org.apache.tuscany.sca.provider.ImplementationProviderFactory; +import org.apache.tuscany.sca.provider.ProviderFactoryExtensionPoint; +import org.apache.tuscany.sca.provider.ReferenceBindingProvider; +import org.apache.tuscany.sca.provider.ServiceBindingProvider; +import org.apache.tuscany.sca.runtime.EndpointReference; +import org.apache.tuscany.sca.runtime.RuntimeComponent; +import org.apache.tuscany.sca.runtime.RuntimeComponentReference; +import org.apache.tuscany.sca.runtime.RuntimeComponentService; +import org.apache.tuscany.sca.runtime.RuntimeWire; +import org.apache.tuscany.sca.runtime.RuntimeWireProcessor; +import org.apache.tuscany.sca.scope.ScopeRegistry; +import org.apache.tuscany.sca.scope.ScopedRuntimeComponent; +import org.apache.tuscany.sca.work.WorkScheduler; + +/** + * @version $Rev$ $Date$ + */ +public class CompositeActivatorImpl implements CompositeActivator { + + private final AssemblyFactory assemblyFactory; + private final SCABindingFactory scaBindingFactory; + private final InterfaceContractMapper interfaceContractMapper; + private final ScopeRegistry scopeRegistry; + private final WorkScheduler workScheduler; + private final RuntimeWireProcessor wireProcessor; + private final ProviderFactoryExtensionPoint providerFactories; + + /** + * @param assemblyFactory + * @param interfaceContractMapper + * @param workContext + * @param workScheduler + * @param wirePostProcessorRegistry + */ + public CompositeActivatorImpl(AssemblyFactory assemblyFactory, + SCABindingFactory scaBindingFactory, + InterfaceContractMapper interfaceContractMapper, + ScopeRegistry scopeRegistry, + WorkScheduler workScheduler, + RuntimeWireProcessor wireProcessor, + ProviderFactoryExtensionPoint providerFactories) { + super(); + this.assemblyFactory = assemblyFactory; + this.scaBindingFactory = scaBindingFactory; + this.interfaceContractMapper = interfaceContractMapper; + this.scopeRegistry = scopeRegistry; + this.workScheduler = workScheduler; + this.wireProcessor = wireProcessor; + this.providerFactories = providerFactories; + } + + /** + * Configure a composite + * + * @param composite + * @throws IncompatibleInterfaceContractException + */ + @SuppressWarnings("unchecked") + protected void configureComposite(Composite composite) throws IncompatibleInterfaceContractException { + for (Component component : composite.getComponents()) { + + for (ComponentService service : component.getServices()) { + for (Binding binding : service.getBindings()) { + BindingProviderFactory providerFactory = (BindingProviderFactory)providerFactories + .getProviderFactory(binding.getClass()); + if (providerFactory != null) { + ServiceBindingProvider bindingProvider = providerFactory + .createServiceBindingProvider((RuntimeComponent)component, + (RuntimeComponentService)service, + binding); + if (bindingProvider != null) { + ((RuntimeComponentService)service).setBindingProvider(binding, bindingProvider); + } + } else { + throw new IllegalStateException("Provider factory not found for class: " + binding.getClass() + .getName()); + } + } + } + for (ComponentReference reference : component.getReferences()) { + for (Binding binding : reference.getBindings()) { + BindingProviderFactory providerFactory = (BindingProviderFactory)providerFactories + .getProviderFactory(binding.getClass()); + if (providerFactory != null) { + ReferenceBindingProvider bindingProvider = providerFactory + .createReferenceBindingProvider((RuntimeComponent)component, + (RuntimeComponentReference)reference, + binding); + if (bindingProvider != null) { + ((RuntimeComponentReference)reference).setBindingProvider(binding, bindingProvider); + } + } else { + throw new IllegalStateException("Provider factory not found for class: " + binding.getClass() + .getName()); + } + } + } + + Implementation implementation = component.getImplementation(); + if (implementation instanceof Composite) { + configureComposite((Composite)implementation); + } else if (implementation != null) { + ImplementationProviderFactory providerFactory = (ImplementationProviderFactory)providerFactories + .getProviderFactory(implementation.getClass()); + if (providerFactory != null) { + ImplementationProvider implementationProvider = providerFactory + .createImplementationProvider((RuntimeComponent)component, implementation); + if (implementationProvider != null) { + ((RuntimeComponent)component).setImplementationProvider(implementationProvider); + } + } else { + throw new IllegalStateException("Provider factory not found for class: " + implementation + .getClass().getName()); + } + setScopeContainer(component); + } + } + } + + /** + * Start a composite + */ + protected void startComposite(Composite composite) { + for (Component component : composite.getComponents()) { + startComponent(component); + } + } + + /** + * Stop a composite + */ + public void stopComposite(Composite composite) { + for (Component component : composite.getComponents()) { + stopComponent(component); + + } + + } + + /** + * Start a component + */ + public void startComponent(Component component) { + + for (ComponentService service : component.getServices()) { + for (Binding binding : service.getBindings()) { + ServiceBindingProvider bindingProvider = ((RuntimeComponentService)service) + .getBindingProvider(binding); + if (bindingProvider != null) { + bindingProvider.start(); + } + } + } + for (ComponentReference reference : component.getReferences()) { + for (Binding binding : reference.getBindings()) { + ReferenceBindingProvider bindingProvider = ((RuntimeComponentReference)reference) + .getBindingProvider(binding); + if (bindingProvider != null) { + try { + bindingProvider.start(); + } catch (RuntimeException e) { + // TODO: [rfeng] Ignore the self reference if a runtime exception happens + if (!reference.getName().startsWith("$self$.")) { + throw e; + } + } + } + } + } + + Implementation implementation = component.getImplementation(); + if (implementation instanceof Composite) { + startComposite((Composite)implementation); + } else { + ImplementationProvider implementationProvider = ((RuntimeComponent)component) + .getImplementationProvider(); + if (implementationProvider != null) { + implementationProvider.start(); + } + } + + if (component instanceof ScopedRuntimeComponent) { + ScopedRuntimeComponent runtimeComponent = (ScopedRuntimeComponent)component; + if (runtimeComponent.getScopeContainer() != null) { + runtimeComponent.getScopeContainer().start(); + } + } + + } + + /** + * Stop a component + */ + public void stopComponent(Component component) { + for (ComponentService service : component.getServices()) { + for (Binding binding : service.getBindings()) { + ServiceBindingProvider bindingProvider = ((RuntimeComponentService)service) + .getBindingProvider(binding); + if (bindingProvider != null) { + bindingProvider.stop(); + } + } + } + for (ComponentReference reference : component.getReferences()) { + for (Binding binding : reference.getBindings()) { + ReferenceBindingProvider bindingProvider = ((RuntimeComponentReference)reference) + .getBindingProvider(binding); + if (bindingProvider != null) { + bindingProvider.stop(); + } + } + } + Implementation implementation = component.getImplementation(); + if (implementation instanceof Composite) { + stopComposite((Composite)implementation); + } else { + ImplementationProvider implementationProvider = ((RuntimeComponent)component) + .getImplementationProvider(); + if (implementationProvider != null) { + implementationProvider.stop(); + } + } + + if (component instanceof ScopedRuntimeComponent) { + ScopedRuntimeComponent runtimeComponent = (ScopedRuntimeComponent)component; + if (runtimeComponent.getScopeContainer() != null) { + runtimeComponent.getScopeContainer().stop(); + } + } + + } + + + /** + * Create runtime wires for the composite + * + * @param composite + * @throws IncompatibleInterfaceContractException + */ + protected void createRuntimeWires(Composite composite) throws IncompatibleInterfaceContractException { + for (Component component : composite.getComponents()) { + Implementation implementation = component.getImplementation(); + if (implementation instanceof Composite) { + // Recursively create runtime wires + createRuntimeWires((Composite)implementation); + } else { + // Create outbound wires for the component references + for (ComponentReference reference : component.getReferences()) { + for (Binding binding : reference.getBindings()) { + createWires(component, reference, binding); + } + } + // Create inbound wires for the component services + for (ComponentService service : component.getServices()) { + for (Binding binding : service.getBindings()) { + createWires(component, service, binding); + } + } + } + } + } + + /** + * Get the effective interface contract for a reference binding + * + * @param reference + * @param binding + * @return + */ + private InterfaceContract getInterfaceContract(ComponentReference reference, Binding binding) { + InterfaceContract interfaceContract = reference.getInterfaceContract(); + ReferenceBindingProvider provider = ((RuntimeComponentReference)reference).getBindingProvider(binding); + if (provider != null) { + InterfaceContract bindingContract = provider.getBindingInterfaceContract(); + if (bindingContract != null) { + interfaceContract = bindingContract; + } + } + return interfaceContract; + } + + /** + * Create the runtime wires for a reference binding + * + * @param component + * @param reference + * @param binding + */ + private void createWires(Component component, ComponentReference reference, Binding binding) { + if (!(reference instanceof RuntimeComponentReference)) { + return; + } + RuntimeComponentReference runtimeRef = (RuntimeComponentReference)reference; + InterfaceContract bindingContract = getInterfaceContract(reference, binding); + + if (!(binding instanceof SCABinding)) { + InterfaceContract sourceContract = reference.getInterfaceContract(); + + // Component Reference --> External Service + EndpointReference wireSource = new EndpointReferenceImpl((RuntimeComponent)component, + (RuntimeComponentReference)reference, binding, + sourceContract); + + EndpointReference wireTarget = new EndpointReferenceImpl(null, null, binding, bindingContract); + RuntimeWire wire = new RuntimeWireImpl(wireSource, wireTarget); + + for (Operation operation : sourceContract.getInterface().getOperations()) { + Operation targetOperation = interfaceContractMapper.map(bindingContract.getInterface(), operation); + InvocationChain chain = new InvocationChainImpl(operation, targetOperation); + if (operation.isNonBlocking()) { + chain.addInterceptor(new NonBlockingInterceptor(workScheduler)); + } + addBindingInterceptor(component, reference, binding, chain, operation, false); + wire.getInvocationChains().add(chain); + } + if (sourceContract.getCallbackInterface() != null) { + for (Operation operation : sourceContract.getCallbackInterface().getOperations()) { + Operation targetOperation = interfaceContractMapper.map(bindingContract.getCallbackInterface(), + operation); + InvocationChain chain = new InvocationChainImpl(operation, targetOperation); + if (operation.isNonBlocking()) { + chain.addInterceptor(new NonBlockingInterceptor(workScheduler)); + } + addBindingInterceptor(component, reference, binding, chain, operation, true); + wire.getCallbackInvocationChains().add(chain); + } + } + runtimeRef.getRuntimeWires().add(wire); + wireProcessor.process(wire); + // TODO: For non-SCA binding, how do we deal with targets? For now, assuming targets only apply to SCABinding + return; + } + for (ComponentService service : reference.getTargets()) { + Component target = null; + SCABinding scaBinding = service.getBinding(SCABinding.class); + if (scaBinding != null) { + target = scaBinding.getComponent(); + } + + // FIXME: [rfeng] Ignore unresolved services + if (service.isUnresolved()) { + continue; + } + + // FIXME: [rfeng] We might need a better way to get the impl interface contract + InterfaceContract targetContract = service.getService().getInterfaceContract(); + + EndpointReference wireSource = new EndpointReferenceImpl((RuntimeComponent)component, + (RuntimeComponentReference)reference, binding, + bindingContract); + + EndpointReference wireTarget = new EndpointReferenceImpl((RuntimeComponent)target, + (RuntimeComponentService)service, binding, + targetContract); + + RuntimeWire wire = new RuntimeWireImpl(wireSource, wireTarget); + + for (Operation operation : bindingContract.getInterface().getOperations()) { + Operation targetOperation = interfaceContractMapper.map(targetContract.getInterface(), operation); + InvocationChain chain = new InvocationChainImpl(operation, targetOperation); + if (operation.isNonBlocking()) { + chain.addInterceptor(new NonBlockingInterceptor(workScheduler)); + } + addBindingInterceptor(component, reference, binding, chain, operation, false); + if (target != null) { + addImplementationInterceptor(target, service, chain, targetOperation, false); + } + wire.getInvocationChains().add(chain); + } + if (bindingContract.getCallbackInterface() != null) { + if (reference.getName().startsWith("$self$.")) { + // No callback is needed + continue; + } + for (Operation operation : bindingContract.getCallbackInterface().getOperations()) { + Operation targetOperation = interfaceContractMapper.map(targetContract.getCallbackInterface(), + operation); + InvocationChain chain = new InvocationChainImpl(operation, targetOperation); + if (operation.isNonBlocking()) { + chain.addInterceptor(new NonBlockingInterceptor(workScheduler)); + } + addBindingInterceptor(component, reference, binding, chain, operation, true); + addImplementationInterceptor(component, null, chain, targetOperation, true); + wire.getCallbackInvocationChains().add(chain); + } + } + + runtimeRef.getRuntimeWires().add(wire); + if (!wire.getCallbackInvocationChains().isEmpty()) { + if (wire.getTarget().getContract() != null) { + ((RuntimeComponentService) wire.getTarget().getContract()).getCallbackWires().add(wire); + } + } + wireProcessor.process(wire); + } + } + + /** + * Get the effective interface contract for the service binding + * + * @param service + * @param binding + * @return + */ + private InterfaceContract getInterfaceContract(ComponentService service, Binding binding) { + InterfaceContract interfaceContract = service.getInterfaceContract(); + + ServiceBindingProvider provider = ((RuntimeComponentService)service).getBindingProvider(binding); + if (provider != null) { + InterfaceContract bindingContract = provider.getBindingInterfaceContract(); + if (bindingContract != null) { + interfaceContract = bindingContract; + } + } + return interfaceContract; + } + + /** + * Create runtime wires for a service binding + * + * @param component + * @param service + * @param binding + */ + private void createWires(Component component, ComponentService service, Binding binding) { + if (!(service instanceof RuntimeComponentService)) { + return; + } + RuntimeComponentService runtimeService = (RuntimeComponentService)service; + + // FIXME: [rfeng] We might need a better way to get the impl interface contract + InterfaceContract targetContract = service.getService().getInterfaceContract(); + + InterfaceContract sourceContract = getInterfaceContract(service, binding); + + EndpointReference wireSource = new EndpointReferenceImpl(null, null, binding, sourceContract); + + EndpointReference wireTarget = new EndpointReferenceImpl((RuntimeComponent)component, + (RuntimeComponentService)service, binding, + targetContract); + + RuntimeWire wire = new RuntimeWireImpl(wireSource, wireTarget); + + for (Operation operation : sourceContract.getInterface().getOperations()) { + Operation targetOperation = interfaceContractMapper.map(targetContract.getInterface(), operation); + InvocationChain chain = new InvocationChainImpl(operation, targetOperation); + /* lresende */ + if (operation.isNonBlocking()) { + chain.addInterceptor(new NonBlockingInterceptor(workScheduler)); + } + + addImplementationInterceptor(component, service, chain, targetOperation, false); + wire.getInvocationChains().add(chain); + } + // if (sourceContract.getCallbackInterface() != null) { + // for (Operation operation : + // sourceContract.getCallbackInterface().getOperations()) { + // Operation targetOperation = + // interfaceContractMapper.map(targetContract.getCallbackInterface(), + // operation); + // InvocationChain chain = new InvocationChainImpl(operation, + // targetOperation); + // if (operation.isNonBlocking()) { + // chain.addInterceptor(new NonBlockingInterceptor(workScheduler, + // workContext)); + // } + // addImplementationInterceptor(component, service, chain, operation, + // true); + // wire.getCallbackInvocationChains().add(chain); + // } + // } + + runtimeService.getRuntimeWires().add(wire); + wireProcessor.process(wire); + } + + /** + * Add the interceptor for a component implementation + * + * @param component + * @param service + * @param chain + * @param operation + * @param isCallback + */ + private void addImplementationInterceptor(Component component, + ComponentService service, + InvocationChain chain, + Operation operation, + boolean isCallback) { + ImplementationProvider provider = ((RuntimeComponent)component).getImplementationProvider(); + if (provider != null) { + Invoker invoker = null; + if (!isCallback) { + invoker = provider.createInvoker((RuntimeComponentService)service, operation); + } else { + invoker = provider.createCallbackInvoker(operation); + } + chain.addInvoker(invoker); + } + } + + /** + * Add the interceptor for a binding + * + * @param component + * @param reference + * @param binding + * @param chain + * @param operation + * @param isCallback + */ + private void addBindingInterceptor(Component component, + ComponentReference reference, + Binding binding, + InvocationChain chain, + Operation operation, + boolean isCallback) { + try { + ReferenceBindingProvider provider = ((RuntimeComponentReference)reference).getBindingProvider(binding); + if (provider != null) { + Invoker invoker = provider.createInvoker(operation, isCallback); + if (invoker != null) { + chain.addInvoker(invoker); + } + } + } catch (RuntimeException e) { + // TODO: [rfeng] Ignore the self reference if a runtime exception happens + if (!reference.getName().startsWith("$self$.")) { + throw e; + } + } + } + + private void setScopeContainer(Component component) { + if (!(component instanceof ScopedRuntimeComponent)) { + return; + } + ScopedRuntimeComponent runtimeComponent = (ScopedRuntimeComponent)component; + runtimeComponent.setScopeContainer(scopeRegistry.getScopeContainer(runtimeComponent)); + } + + protected void buildComposite(Composite composite) throws CompositeBuilderException { + + CompositeBuilderMonitor monitor = new CompositeBuilderMonitor() { + + public void problem(Problem problem) { + // Uncommenting the following two lines can be useful to detect + // and troubleshoot SCA assembly XML composite configuration + // problems. + + System.out.println("Composite assembly problem: " + problem.getMessage()); + } + }; + + CompositeBuilderImpl builder = new CompositeBuilderImpl(assemblyFactory, scaBindingFactory, interfaceContractMapper, monitor); + + builder.build(composite); + + // if (!problems.isEmpty()) { + // throw new VariantRuntimeException(new RuntimeException("Problems in + // the composite...")); + // } + } + + public void activate(Composite composite) throws ActivationException { + try { + buildComposite(composite); + configureComposite(composite); + createRuntimeWires(composite); + } catch (Exception e) { + throw new ActivationException(e); + } + } + + public void deactivate(Composite composite) throws ActivationException { + } + + public void start(Composite composite) throws ActivationException { + try { + startComposite(composite); + } catch (Exception e) { + throw new ActivationException(e); + } + } + + public void stop(Composite composite) throws ActivationException { + try{ + stopComposite(composite); + } catch(Exception e) { + throw new ActivationException(e); + } + } + + public void start(Component component) throws ActivationException { + try { + startComponent(component); + } catch (Exception e) { + throw new ActivationException(e); + } + + } + + public void stop(Component component) throws ActivationException { + try{ + stopComponent(component); + } catch(Exception e) { + throw new ActivationException(e); + } + } +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/runtime/EndpointReferenceImpl.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/runtime/EndpointReferenceImpl.java new file mode 100644 index 0000000000..233f81ccae --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/runtime/EndpointReferenceImpl.java @@ -0,0 +1,128 @@ +/* + * 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.sca.core.runtime; + +import org.apache.tuscany.sca.assembly.Binding; +import org.apache.tuscany.sca.assembly.Contract; +import org.apache.tuscany.sca.interfacedef.InterfaceContract; +import org.apache.tuscany.sca.runtime.EndpointReference; +import org.apache.tuscany.sca.runtime.RuntimeComponent; + +/** + * @version $Rev$ $Date$ + */ +public class EndpointReferenceImpl implements EndpointReference { + private RuntimeComponent component; + private Contract contract; + private Binding binding; + private InterfaceContract interfaceContract; + private String uri; + + /** + * @param component + * @param contract + * @param binding + * @param interfaceContract + */ + public EndpointReferenceImpl(RuntimeComponent component, + Contract contract, + Binding binding, + InterfaceContract interfaceContract) { + super(); + this.component = component; + this.contract = contract; + this.binding = binding; + this.interfaceContract = interfaceContract; + this.uri = binding.getURI(); + } + + /** + * @param uri + */ + public EndpointReferenceImpl(String uri) { + super(); + this.uri = uri; + } + + public Binding getBinding() { + return binding; + } + + public void setBinding(Binding binding) { + this.binding = binding; + } + + public RuntimeComponent getComponent() { + return component; + } + + public void setComponent(RuntimeComponent component) { + this.component = component; + } + + public Contract getContract() { + return contract; + } + + public void setContract(Contract contract) { + this.contract = contract; + } + + public InterfaceContract getInterfaceContract() { + return interfaceContract; + } + + public void setInterfaceContract(InterfaceContract interfaceContract) { + this.interfaceContract = interfaceContract; + } + + public String getURI() { + return uri; + } + + public void setURI(String uri) { + this.uri = uri; + } + + @Override + public int hashCode() { + final int PRIME = 31; + int result = 1; + result = PRIME * result + ((uri == null) ? 0 : uri.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + final EndpointReferenceImpl other = (EndpointReferenceImpl)obj; + if (uri == null) { + if (other.uri != null) + return false; + } else if (!uri.equals(other.uri)) + return false; + return true; + } +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/runtime/RuntimeAssemblyFactory.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/runtime/RuntimeAssemblyFactory.java new file mode 100644 index 0000000000..ec0cdadde6 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/runtime/RuntimeAssemblyFactory.java @@ -0,0 +1,61 @@ +/* + * 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.sca.core.runtime; + +import org.apache.tuscany.sca.assembly.AssemblyFactory; +import org.apache.tuscany.sca.assembly.Component; +import org.apache.tuscany.sca.assembly.ComponentReference; +import org.apache.tuscany.sca.assembly.ComponentService; +import org.apache.tuscany.sca.assembly.DefaultAssemblyFactory; +import org.apache.tuscany.sca.core.invocation.ProxyFactory; +import org.apache.tuscany.sca.interfacedef.InterfaceContractMapper; + +/** + * @version $Rev$ $Date$ + */ +public class RuntimeAssemblyFactory extends DefaultAssemblyFactory implements AssemblyFactory { + private final ProxyFactory proxyFactory; + private final InterfaceContractMapper interfaceContractMapper; + + /** + * @param proxyFactory + */ + public RuntimeAssemblyFactory(InterfaceContractMapper interfaceContractMapper, ProxyFactory proxyFactory) { + super(); + this.proxyFactory = proxyFactory; + this.interfaceContractMapper = interfaceContractMapper; + } + + @Override + public Component createComponent() { + return new RuntimeComponentImpl(proxyFactory); + } + + @Override + public ComponentReference createComponentReference() { + return new RuntimeComponentReferenceImpl(interfaceContractMapper); + } + + @Override + public ComponentService createComponentService() { + return new RuntimeComponentServiceImpl(interfaceContractMapper); + } + +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/runtime/RuntimeComponentImpl.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/runtime/RuntimeComponentImpl.java new file mode 100644 index 0000000000..aacce904b5 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/runtime/RuntimeComponentImpl.java @@ -0,0 +1,128 @@ +/* + * 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.sca.core.runtime; + +import java.util.List; + +import org.apache.tuscany.sca.assembly.ComponentReference; +import org.apache.tuscany.sca.assembly.Property; +import org.apache.tuscany.sca.assembly.impl.ComponentImpl; +import org.apache.tuscany.sca.core.component.ServiceReferenceImpl; +import org.apache.tuscany.sca.core.invocation.ProxyFactory; +import org.apache.tuscany.sca.core.invocation.WireObjectFactory; +import org.apache.tuscany.sca.provider.ImplementationProvider; +import org.apache.tuscany.sca.runtime.RuntimeComponent; +import org.apache.tuscany.sca.runtime.RuntimeComponentReference; +import org.apache.tuscany.sca.runtime.RuntimeWire; +import org.apache.tuscany.sca.scope.ScopeContainer; +import org.apache.tuscany.sca.scope.ScopedRuntimeComponent; +import org.osoa.sca.CallableReference; +import org.osoa.sca.RequestContext; +import org.osoa.sca.ServiceReference; + +/** + * @version $Rev$ $Date$ + */ +public class RuntimeComponentImpl extends ComponentImpl implements RuntimeComponent, ScopedRuntimeComponent { + public static final String SELF_REFERENCE_PREFIX = "$self$."; + protected ImplementationProvider implementationProvider; + protected ProxyFactory proxyService; + protected ScopeContainer scopeContainer; + + /** + * @param proxyService + */ + public RuntimeComponentImpl(ProxyFactory proxyService) { + super(); + this.proxyService = proxyService; + } + + public <B> ServiceReference<B> createSelfReference(Class<B> businessInterface) { + return getServiceReference(businessInterface, SELF_REFERENCE_PREFIX); + } + + public <B> ServiceReference<B> createSelfReference(Class<B> businessInterface, String serviceName) { + return getServiceReference(businessInterface, SELF_REFERENCE_PREFIX + serviceName); + } + + public <B> B getProperty(Class<B> type, String propertyName) { + for (Property p : getProperties()) { + if (p.getName().equals(propertyName)) { + // FIXME: Need to use the property object factory to create the + // instance + return null; + } + } + return null; + } + + public RequestContext getRequestContext() { + return null; + } + + public <B> B getService(Class<B> businessInterface, String referenceName) { + List<ComponentReference> refs = getReferences(); + for (ComponentReference ref : refs) { + if (ref.getName().equals(referenceName)) { + RuntimeComponentReference attachPoint = (RuntimeComponentReference)ref; + RuntimeWire wire = attachPoint.getRuntimeWires().get(0); + return proxyService.createProxy(businessInterface, wire); + } + } + return null; + } + + public <B> ServiceReference<B> getServiceReference(Class<B> businessInterface, String referenceName) { + List<ComponentReference> references = getReferences(); + for (ComponentReference reference : references) { + if (reference.getName().equals(referenceName) || referenceName.equals("$self$.") + && reference.getName().startsWith(referenceName)) { + RuntimeComponentReference attachPoint = (RuntimeComponentReference)reference; + RuntimeWire wire = attachPoint.getRuntimeWires().get(0); + WireObjectFactory<B> factory = new WireObjectFactory<B>(businessInterface, wire, proxyService); + return new ServiceReferenceImpl<B>(businessInterface, factory); + } + } + return null; + + } + + @SuppressWarnings("unchecked") + public <B, R extends CallableReference<B>> R cast(B target) throws IllegalArgumentException { + Object ref = proxyService.cast(target); + return (R)ref; + } + + public ImplementationProvider getImplementationProvider() { + return implementationProvider; + } + + public void setImplementationProvider(ImplementationProvider provider) { + this.implementationProvider = provider; + } + + public ScopeContainer getScopeContainer() { + return scopeContainer; + } + + public void setScopeContainer(ScopeContainer scopeContainer) { + this.scopeContainer = scopeContainer; + } +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/runtime/RuntimeComponentReferenceImpl.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/runtime/RuntimeComponentReferenceImpl.java new file mode 100644 index 0000000000..128ae29b81 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/runtime/RuntimeComponentReferenceImpl.java @@ -0,0 +1,86 @@ +/* + * 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.sca.core.runtime; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.tuscany.sca.assembly.Binding; +import org.apache.tuscany.sca.assembly.impl.ComponentReferenceImpl; +import org.apache.tuscany.sca.interfacedef.InterfaceContractMapper; +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.invocation.InvocationChain; +import org.apache.tuscany.sca.invocation.Invoker; +import org.apache.tuscany.sca.provider.ReferenceBindingProvider; +import org.apache.tuscany.sca.runtime.RuntimeComponentReference; +import org.apache.tuscany.sca.runtime.RuntimeWire; + +public class RuntimeComponentReferenceImpl extends ComponentReferenceImpl implements RuntimeComponentReference { + private List<RuntimeWire> wires = new ArrayList<RuntimeWire>(); + private Map<Binding, ReferenceBindingProvider> bindingProviders = new HashMap<Binding, ReferenceBindingProvider>(); + private InterfaceContractMapper mapper; + + public RuntimeComponentReferenceImpl(InterfaceContractMapper mapper) { + super(); + this.mapper = mapper; + } + + public void addRuntimeWire(RuntimeWire wire) { + wires.add(wire); + } + + public List<RuntimeWire> getRuntimeWires() { + return wires; + } + + public RuntimeWire getRuntimeWire(Binding binding) { + for (RuntimeWire wire : wires) { + if (wire.getSource().getBinding() == binding) { + return wire; + } + } + return null; + } + + public ReferenceBindingProvider getBindingProvider(Binding binding) { + return bindingProviders.get(binding); + } + + public void setBindingProvider(Binding binding, ReferenceBindingProvider bindingProvider) { + bindingProviders.put(binding, bindingProvider); + } + + public Invoker getInvoker(Binding binding, Operation operation) { + RuntimeWire wire = getRuntimeWire(binding); + if (wire == null) { + return null; + } + for (InvocationChain chain : wire.getInvocationChains()) { + Operation op = chain.getSourceOperation(); + if (mapper.isCompatible(operation, op, op.getInterface().isRemotable())) { + return chain.getHeadInvoker(); + } + } + return null; + } + +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/runtime/RuntimeComponentServiceImpl.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/runtime/RuntimeComponentServiceImpl.java new file mode 100644 index 0000000000..0bb8e2c457 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/runtime/RuntimeComponentServiceImpl.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.sca.core.runtime; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.tuscany.sca.assembly.Binding; +import org.apache.tuscany.sca.assembly.impl.ComponentServiceImpl; +import org.apache.tuscany.sca.interfacedef.InterfaceContractMapper; +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.invocation.InvocationChain; +import org.apache.tuscany.sca.invocation.Invoker; +import org.apache.tuscany.sca.provider.ServiceBindingProvider; +import org.apache.tuscany.sca.runtime.RuntimeComponentService; +import org.apache.tuscany.sca.runtime.RuntimeWire; + +public class RuntimeComponentServiceImpl extends ComponentServiceImpl implements RuntimeComponentService { + private InterfaceContractMapper mapper; + + public RuntimeComponentServiceImpl(InterfaceContractMapper mapper) { + super(); + this.mapper = mapper; + } + + private List<RuntimeWire> wires = new ArrayList<RuntimeWire>(); + private List<RuntimeWire> callbackWires = new ArrayList<RuntimeWire>(); + private Map<Binding, ServiceBindingProvider> bindingProviders = new HashMap<Binding, ServiceBindingProvider>(); + + public void addRuntimeWire(RuntimeWire wire) { + wires.add(wire); + } + + public List<RuntimeWire> getRuntimeWires() { + return wires; + } + + public RuntimeWire getRuntimeWire(Binding binding) { + for (RuntimeWire wire : wires) { + if (wire.getTarget().getBinding() == binding) { + return wire; + } + } + return null; + } + + public List<RuntimeWire> getCallbackWires() { + return callbackWires; + } + + public void addCallbackWire(RuntimeWire callbackWire) { + this.callbackWires.add(callbackWire); + } + + public ServiceBindingProvider getBindingProvider(Binding binding) { + return bindingProviders.get(binding); + } + + public void setBindingProvider(Binding binding, ServiceBindingProvider bindingProvider) { + bindingProviders.put(binding, bindingProvider); + } + + public Invoker getInvoker(Binding binding, Operation operation) { + RuntimeWire wire = getRuntimeWire(binding); + if (wire == null) { + return null; + } + for (InvocationChain chain : wire.getInvocationChains()) { + Operation op = chain.getTargetOperation(); + if (mapper.isCompatible(operation, op, op.getInterface().isRemotable())) { + return chain.getHeadInvoker(); + } + } + return null; + } + + public Invoker getCallbackInvoker(Binding binding, Operation operation) { + for (RuntimeWire wire : callbackWires) { + if (wire.getTarget().getBinding() == binding) { + for (InvocationChain chain : wire.getCallbackInvocationChains()) { + Operation op = chain.getSourceOperation(); + if (mapper.isCompatible(operation, op, op.getInterface().isRemotable())) { + return chain.getHeadInvoker(); + } + } + } + } + return null; + } +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/runtime/RuntimeSCABindingProvider.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/runtime/RuntimeSCABindingProvider.java new file mode 100644 index 0000000000..ad055731f3 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/runtime/RuntimeSCABindingProvider.java @@ -0,0 +1,55 @@ +/* + * 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.sca.core.runtime; + +import org.apache.tuscany.sca.assembly.SCABinding; +import org.apache.tuscany.sca.interfacedef.InterfaceContract; +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.invocation.Invoker; +import org.apache.tuscany.sca.provider.ReferenceBindingProvider; +import org.apache.tuscany.sca.runtime.RuntimeComponent; +import org.apache.tuscany.sca.runtime.RuntimeComponentReference; + +/** + * @version $Rev$ $Date$ + */ +public class RuntimeSCABindingProvider implements ReferenceBindingProvider { + + private RuntimeComponentReference reference; + + public RuntimeSCABindingProvider(RuntimeComponent component, RuntimeComponentReference reference, SCABinding binding) { + this.reference = reference; + } + + public InterfaceContract getBindingInterfaceContract() { + return reference.getInterfaceContract(); + } + + public Invoker createInvoker(Operation operation, boolean isCallback) { + return null; + } + + public void start() { + } + + public void stop() { + } + +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/runtime/RuntimeSCABindingProviderFactory.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/runtime/RuntimeSCABindingProviderFactory.java new file mode 100644 index 0000000000..06b7b3d40e --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/runtime/RuntimeSCABindingProviderFactory.java @@ -0,0 +1,51 @@ +/* + * 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.sca.core.runtime; + +import org.apache.tuscany.sca.assembly.SCABinding; +import org.apache.tuscany.sca.provider.BindingProviderFactory; +import org.apache.tuscany.sca.provider.ReferenceBindingProvider; +import org.apache.tuscany.sca.provider.ServiceBindingProvider; +import org.apache.tuscany.sca.runtime.RuntimeComponent; +import org.apache.tuscany.sca.runtime.RuntimeComponentReference; +import org.apache.tuscany.sca.runtime.RuntimeComponentService; + +/** + * @version $Rev$ $Date$ + */ +public class RuntimeSCABindingProviderFactory implements BindingProviderFactory<SCABinding> { + + public ReferenceBindingProvider createReferenceBindingProvider(RuntimeComponent component, + RuntimeComponentReference reference, + SCABinding binding) { + return new RuntimeSCABindingProvider(component, reference, binding); + } + + public ServiceBindingProvider createServiceBindingProvider(RuntimeComponent component, + RuntimeComponentService service, + SCABinding binding) { + return null; + } + + public Class<SCABinding> getModelType() { + return SCABinding.class; + } + +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/runtime/RuntimeWireImpl.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/runtime/RuntimeWireImpl.java new file mode 100644 index 0000000000..cdf7ec01c0 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/runtime/RuntimeWireImpl.java @@ -0,0 +1,66 @@ +/* + * 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.sca.core.runtime; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.tuscany.sca.invocation.InvocationChain; +import org.apache.tuscany.sca.runtime.EndpointReference; +import org.apache.tuscany.sca.runtime.RuntimeWire; + +/** + * @version $Rev$ $Date$ + */ +public class RuntimeWireImpl implements RuntimeWire { + private EndpointReference wireSource; + private EndpointReference wireTarget; + + private final List<InvocationChain> chains = new ArrayList<InvocationChain>(); + private final List<InvocationChain> callbackChains = new ArrayList<InvocationChain>(); + + /** + * @param source + * @param target + */ + public RuntimeWireImpl(EndpointReference source, + EndpointReference target) { + super(); + this.wireSource = source; + this.wireTarget = target; + } + + public List<InvocationChain> getCallbackInvocationChains() { + return callbackChains; + } + + public List<InvocationChain> getInvocationChains() { + return chains; + } + + public EndpointReference getSource() { + return wireSource; + } + + public EndpointReference getTarget() { + return wireTarget; + } + +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/scope/AbstractScopeContainer.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/scope/AbstractScopeContainer.java new file mode 100644 index 0000000000..1d776b729c --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/scope/AbstractScopeContainer.java @@ -0,0 +1,146 @@ +/* + * 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.sca.core.scope; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +import org.apache.tuscany.sca.event.Event; +import org.apache.tuscany.sca.provider.ImplementationProvider; +import org.apache.tuscany.sca.runtime.RuntimeComponent; +import org.apache.tuscany.sca.scope.AbstractLifecycle; +import org.apache.tuscany.sca.scope.InstanceWrapper; +import org.apache.tuscany.sca.scope.PersistenceException; +import org.apache.tuscany.sca.scope.Scope; +import org.apache.tuscany.sca.scope.ScopeContainer; +import org.apache.tuscany.sca.scope.ScopedImplementationProvider; +import org.apache.tuscany.sca.scope.TargetDestructionException; +import org.apache.tuscany.sca.scope.TargetResolutionException; + +/** + * Implements functionality common to scope contexts. + * + * @version $Rev$ $Date$ + */ +public abstract class AbstractScopeContainer<KEY> extends AbstractLifecycle implements ScopeContainer<KEY> { + protected Map<KEY, InstanceWrapper<?>> wrappers = new ConcurrentHashMap<KEY, InstanceWrapper<?>>(); + protected final Scope scope; + + protected RuntimeComponent component; + + public AbstractScopeContainer(Scope scope, RuntimeComponent component) { + this.scope = scope; + this.component = component; + } + + protected void checkInit() { + if (getLifecycleState() != RUNNING) { + throw new IllegalStateException("Scope container not running [" + getLifecycleState() + "]"); + } + } + + /** + * 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 InstanceWrapper createInstanceWrapper() throws TargetResolutionException { + ImplementationProvider implementationProvider = component.getImplementationProvider(); + if (implementationProvider instanceof ScopedImplementationProvider) { + return ((ScopedImplementationProvider)implementationProvider).createInstanceWrapper(); + } + return null; + } + + public InstanceWrapper getAssociatedWrapper(KEY contextId) throws TargetResolutionException { + return null; + } + + public Scope getScope() { + return scope; + } + + public InstanceWrapper getWrapper(KEY contextId) throws TargetResolutionException { + return wrappers.get(contextId); + } + + public void onEvent(Event event) { + } + + protected boolean isEagerInit() { + ImplementationProvider implementationProvider = ((RuntimeComponent)component).getImplementationProvider(); + if (implementationProvider instanceof ScopedImplementationProvider) { + return ((ScopedImplementationProvider)implementationProvider).isEagerInit(); + } + return false; + } + + public void remove() throws PersistenceException { + throw new UnsupportedOperationException("Scope does not support persistence"); + } + + public void returnWrapper(InstanceWrapper wrapper, KEY contextId) throws TargetDestructionException { + } + + 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); + } + + public void startContext(KEY contextId) { + if(isEagerInit()) { + try { + getWrapper(contextId); + } catch (TargetResolutionException e) { + // + } + } + } + + public synchronized void stop() { + int lifecycleState = getLifecycleState(); + if (lifecycleState != RUNNING) { + throw new IllegalStateException("Scope in wrong state [" + lifecycleState + "]"); + } + setLifecycleState(STOPPED); + } + + public void stopContext(KEY contextId) { + wrappers.remove(contextId); + } + + public String toString() { + return "In state [" + super.toString() + ']'; + } + + public RuntimeComponent getComponent() { + return component; + } + + public void setComponent(RuntimeComponent component) { + this.component = component; + } +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/scope/CompositeScopeContainer.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/scope/CompositeScopeContainer.java new file mode 100644 index 0000000000..520d3ab48c --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/scope/CompositeScopeContainer.java @@ -0,0 +1,70 @@ +/* + * 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.sca.core.scope; + +import org.apache.tuscany.sca.runtime.RuntimeComponent; +import org.apache.tuscany.sca.scope.InstanceWrapper; +import org.apache.tuscany.sca.scope.Scope; +import org.apache.tuscany.sca.scope.TargetNotFoundException; +import org.apache.tuscany.sca.scope.TargetResolutionException; + +/** + * A scope context which manages atomic component instances keyed by composite + * + * @version $Rev$ $Date$ + */ +public class CompositeScopeContainer<KEY> extends AbstractScopeContainer<KEY> { + private InstanceWrapper<?> wrapper; + + public CompositeScopeContainer(RuntimeComponent component) { + super(Scope.COMPOSITE, component); + } + + public synchronized void stop() { + super.stop(); + wrapper = null; + } + + public InstanceWrapper getWrapper(KEY contextId) throws TargetResolutionException { + if (wrapper == null) { + wrapper = createInstanceWrapper(); + wrapper.start(); + } + return wrapper; + } + + public InstanceWrapper getAssociatedWrapper(KEY contextId) throws TargetResolutionException { + if (wrapper == null) { + throw new TargetNotFoundException(component.getURI()); + } + return wrapper; + } + + @Override + public synchronized void start() { + super.start(); + if(isEagerInit()) { + try { + getWrapper(null); + } catch (TargetResolutionException e) { + throw new IllegalStateException(e); + } + } + } +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/scope/CompositeScopeContainerFactory.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/scope/CompositeScopeContainerFactory.java new file mode 100644 index 0000000000..7078903b8b --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/scope/CompositeScopeContainerFactory.java @@ -0,0 +1,40 @@ +/* + * 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.sca.core.scope; + +import org.apache.tuscany.sca.runtime.RuntimeComponent; +import org.apache.tuscany.sca.scope.Scope; +import org.apache.tuscany.sca.scope.ScopeContainer; +import org.apache.tuscany.sca.scope.ScopeContainerFactory; + +/** + * @version $Rev$ $Date$ + */ +public class CompositeScopeContainerFactory implements ScopeContainerFactory { + + public ScopeContainer createScopeContainer(RuntimeComponent component) { + return new CompositeScopeContainer(component); + } + + public Scope getScope() { + return Scope.COMPOSITE; + } + +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/scope/ConversationalScopeContainer.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/scope/ConversationalScopeContainer.java new file mode 100644 index 0000000000..61f7d51c29 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/scope/ConversationalScopeContainer.java @@ -0,0 +1,182 @@ +/*
+ * 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.sca.core.scope;
+
+import org.apache.tuscany.sca.core.invocation.ThreadMessageContext;
+import org.apache.tuscany.sca.event.Event;
+import org.apache.tuscany.sca.event.RuntimeEventListener;
+import org.apache.tuscany.sca.runtime.RuntimeComponent;
+import org.apache.tuscany.sca.scope.InstanceWrapper;
+import org.apache.tuscany.sca.scope.PersistenceException;
+import org.apache.tuscany.sca.scope.Scope;
+import org.apache.tuscany.sca.scope.ScopeContainer;
+import org.apache.tuscany.sca.scope.TargetDestructionException;
+import org.apache.tuscany.sca.scope.TargetResolutionException;
+import org.apache.tuscany.sca.store.Store;
+import org.apache.tuscany.sca.store.StoreExpirationEvent;
+
+/**
+ * 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 Store nonDurableStore;
+
+ public ConversationalScopeContainer(Store store, RuntimeComponent component) {
+ super(Scope.CONVERSATION, component);
+ this.nonDurableStore = store;
+ if (store != null) {
+ store.addListener(new ExpirationListener());
+ }
+ }
+
+ 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 persistNew(RuntimeComponent 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(RuntimeComponent 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(RuntimeComponent 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(RuntimeComponent component, boolean create)
+ throws TargetResolutionException {
+ throw new UnsupportedOperationException("To be implemented");
+ // 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 = ThreadMessageContext.getMessageContext().getConversationID();
+ assert conversationId != null;
+ return conversationId;
+ }
+
+ // private long calculateExpiration(RuntimeComponent 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 {
+
+ public ExpirationListener() {
+ }
+
+ 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/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/scope/ConversationalScopeContainerFactory.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/scope/ConversationalScopeContainerFactory.java new file mode 100644 index 0000000000..4fdda90bc2 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/scope/ConversationalScopeContainerFactory.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.sca.core.scope; + +import org.apache.tuscany.sca.runtime.RuntimeComponent; +import org.apache.tuscany.sca.scope.Scope; +import org.apache.tuscany.sca.scope.ScopeContainer; +import org.apache.tuscany.sca.scope.ScopeContainerFactory; +import org.apache.tuscany.sca.store.Store; + +/** + * @version $Rev$ $Date$ + */ +public class ConversationalScopeContainerFactory implements ScopeContainerFactory { + private Store store; + + public ConversationalScopeContainerFactory(Store store) { + super(); + this.store = store; + } + + public ScopeContainer createScopeContainer(RuntimeComponent component) { + return new ConversationalScopeContainer(store, component); + } + + public Scope getScope() { + return Scope.CONVERSATION; + } + +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/scope/HttpSessionScopeContainer.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/scope/HttpSessionScopeContainer.java new file mode 100644 index 0000000000..bcbf3cdf3c --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/scope/HttpSessionScopeContainer.java @@ -0,0 +1,81 @@ +/* + * 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.sca.core.scope; + +import org.apache.tuscany.sca.core.event.HttpSessionEnd; +import org.apache.tuscany.sca.event.Event; +import org.apache.tuscany.sca.runtime.RuntimeComponent; +import org.apache.tuscany.sca.scope.InstanceWrapper; +import org.apache.tuscany.sca.scope.Scope; +import org.apache.tuscany.sca.scope.TargetResolutionException; + +/** + * A scope context which manages atomic component instances keyed on HTTP + * session + * + * @version $Rev$ $Date$ + */ +public class HttpSessionScopeContainer extends AbstractScopeContainer<Object> { + + public HttpSessionScopeContainer(RuntimeComponent component) { + super(Scope.SESSION, component); + } + + public void onEvent(Event event) { + checkInit(); + if (event instanceof HttpSessionEnd) { + Object key = ((HttpSessionEnd)event).getSessionID(); + // FIXME: Remove the session id + } + } + + 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; + } + + protected InstanceWrapper getInstanceWrapper(boolean create) throws TargetResolutionException { +// Object key = workContext.getIdentifier(Scope.SESSION); + // FIXME: Need to fix this + Object key ="http-session-id"; + assert key != null : "HTTP session key not bound in work context"; + InstanceWrapper ctx = wrappers.get(key); + if (ctx == null && !create) { + return null; + } + if (ctx == null) { + ctx = super.createInstanceWrapper(); + ctx.start(); + wrappers.put(key, ctx); + } + return ctx; + } + + @Override + public InstanceWrapper getWrapper(Object contextId) throws TargetResolutionException { + return getInstanceWrapper(true); + } + +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/scope/HttpSessionScopeContainerFactory.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/scope/HttpSessionScopeContainerFactory.java new file mode 100644 index 0000000000..e275bb56b9 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/scope/HttpSessionScopeContainerFactory.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.sca.core.scope; + +import org.apache.tuscany.sca.runtime.RuntimeComponent; +import org.apache.tuscany.sca.scope.Scope; +import org.apache.tuscany.sca.scope.ScopeContainer; +import org.apache.tuscany.sca.scope.ScopeContainerFactory; + +/** + * @version $Rev$ $Date$ + */ +public class HttpSessionScopeContainerFactory implements ScopeContainerFactory { + + public HttpSessionScopeContainerFactory() { + super(); + } + + public ScopeContainer createScopeContainer(RuntimeComponent component) { + return new HttpSessionScopeContainer(component); + } + + public Scope getScope() { + return Scope.SESSION; + } + +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/scope/RequestScopeContainer.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/scope/RequestScopeContainer.java new file mode 100644 index 0000000000..aaafb4603c --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/scope/RequestScopeContainer.java @@ -0,0 +1,85 @@ +/* + * 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.sca.core.scope; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +import org.apache.tuscany.sca.core.event.RequestEnd; +import org.apache.tuscany.sca.event.Event; +import org.apache.tuscany.sca.runtime.RuntimeComponent; +import org.apache.tuscany.sca.scope.InstanceWrapper; +import org.apache.tuscany.sca.scope.Scope; +import org.apache.tuscany.sca.scope.TargetResolutionException; + +/** + * A scope context which manages atomic component instances keyed on the current + * request context + * + * @version $Rev$ $Date$ + */ +public class RequestScopeContainer extends AbstractScopeContainer<Thread> { + private final Map<Thread, InstanceWrapper> contexts; + + public RequestScopeContainer(RuntimeComponent component) { + super(Scope.REQUEST, component); + contexts = new ConcurrentHashMap<Thread, 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; + } + + protected InstanceWrapper getInstanceWrapper(boolean create) throws TargetResolutionException { + InstanceWrapper ctx = wrappers.get(Thread.currentThread()); + if (ctx == null && !create) { + return null; + } + if (ctx == null) { + ctx = super.createInstanceWrapper(); + ctx.start(); + wrappers.put(Thread.currentThread(), ctx); + } + return ctx; + } + + @Override + public InstanceWrapper getWrapper(Thread contextId) throws TargetResolutionException { + return getInstanceWrapper(true); + } + +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/scope/RequestScopeContainerFactory.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/scope/RequestScopeContainerFactory.java new file mode 100644 index 0000000000..3851637220 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/scope/RequestScopeContainerFactory.java @@ -0,0 +1,40 @@ +/* + * 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.sca.core.scope; + +import org.apache.tuscany.sca.runtime.RuntimeComponent; +import org.apache.tuscany.sca.scope.Scope; +import org.apache.tuscany.sca.scope.ScopeContainer; +import org.apache.tuscany.sca.scope.ScopeContainerFactory; + +/** + * @version $Rev$ $Date$ + */ +public class RequestScopeContainerFactory implements ScopeContainerFactory { + + public ScopeContainer createScopeContainer(RuntimeComponent component) { + return new RequestScopeContainer(component); + } + + public Scope getScope() { + return Scope.REQUEST; + } + +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/scope/ScopeRegistryImpl.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/scope/ScopeRegistryImpl.java new file mode 100644 index 0000000000..774c1edf37 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/scope/ScopeRegistryImpl.java @@ -0,0 +1,68 @@ +/* + * 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.sca.core.scope; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +import org.apache.tuscany.sca.provider.ImplementationProvider; +import org.apache.tuscany.sca.runtime.RuntimeComponent; +import org.apache.tuscany.sca.scope.Scope; +import org.apache.tuscany.sca.scope.ScopeContainer; +import org.apache.tuscany.sca.scope.ScopeContainerFactory; +import org.apache.tuscany.sca.scope.ScopeRegistry; +import org.apache.tuscany.sca.scope.ScopedImplementationProvider; +import org.apache.tuscany.sca.scope.ScopedRuntimeComponent; + +/** + * The default implementation of a scope registry + * + * @version $Rev$ $Date$ + */ +public class ScopeRegistryImpl implements ScopeRegistry { + private final Map<Scope, ScopeContainerFactory> scopeCache = new ConcurrentHashMap<Scope, ScopeContainerFactory>(); + + public void register(ScopeContainerFactory factory) { + scopeCache.put(factory.getScope(), factory); + } + + public ScopeContainer getScopeContainer(RuntimeComponent runtimeComponent) { + if (!(runtimeComponent instanceof ScopedRuntimeComponent)) { + return null; + } + ScopedRuntimeComponent component = (ScopedRuntimeComponent)runtimeComponent; + if (component.getScopeContainer() != null) { + return component.getScopeContainer(); + } + ImplementationProvider implementationProvider = component.getImplementationProvider(); + if (implementationProvider instanceof ScopedImplementationProvider) { + ScopedImplementationProvider provider = (ScopedImplementationProvider)implementationProvider; + Scope scope = provider.getScope(); + if (scope == null) { + scope = Scope.STATELESS; + } + ScopeContainerFactory factory = scopeCache.get(scope); + ScopeContainer container = factory.createScopeContainer(component); + component.setScopeContainer(container); + return container; + } + return null; + } + +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/scope/StatelessScopeContainer.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/scope/StatelessScopeContainer.java new file mode 100644 index 0000000000..dd481fa062 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/scope/StatelessScopeContainer.java @@ -0,0 +1,54 @@ +/* + * 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.sca.core.scope; + +import org.apache.tuscany.sca.runtime.RuntimeComponent; +import org.apache.tuscany.sca.scope.InstanceWrapper; +import org.apache.tuscany.sca.scope.Scope; +import org.apache.tuscany.sca.scope.TargetDestructionException; +import org.apache.tuscany.sca.scope.TargetResolutionException; + +/** + * A scope context which manages stateless atomic component instances in a non-pooled fashion. + * + * @version $Rev$ $Date$ + */ +public class StatelessScopeContainer<KEY> extends AbstractScopeContainer<KEY> { + + public StatelessScopeContainer(RuntimeComponent component) { + super(Scope.STATELESS, component); + } + + public InstanceWrapper getWrapper(KEY contextId) + throws TargetResolutionException { + InstanceWrapper ctx = createInstanceWrapper(); + ctx.start(); + return ctx; + } + + public InstanceWrapper getAssociatedWrapper(KEY contextId) + throws TargetResolutionException { + throw new UnsupportedOperationException(); + } + + public void returnWrapper(InstanceWrapper wrapper, KEY contextId) + throws TargetDestructionException { + wrapper.stop(); + } +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/scope/StatelessScopeContainerFactory.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/scope/StatelessScopeContainerFactory.java new file mode 100644 index 0000000000..e0eff710e9 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/scope/StatelessScopeContainerFactory.java @@ -0,0 +1,40 @@ +/* + * 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.sca.core.scope; + +import org.apache.tuscany.sca.runtime.RuntimeComponent; +import org.apache.tuscany.sca.scope.Scope; +import org.apache.tuscany.sca.scope.ScopeContainer; +import org.apache.tuscany.sca.scope.ScopeContainerFactory; + +/** + * @version $Rev$ $Date$ + */ +public class StatelessScopeContainerFactory implements ScopeContainerFactory { + + public ScopeContainer createScopeContainer(RuntimeComponent component) { + return new StatelessScopeContainer(component); + } + + public Scope getScope() { + return Scope.STATELESS; + } + +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/store/MemoryStore.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/store/MemoryStore.java new file mode 100644 index 0000000000..5b838116c5 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/store/MemoryStore.java @@ -0,0 +1,195 @@ +/* + * 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.sca.core.store; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; + +import org.apache.tuscany.sca.core.event.BaseEventPublisher; +import org.apache.tuscany.sca.runtime.RuntimeComponent; +import org.apache.tuscany.sca.store.DuplicateRecordException; +import org.apache.tuscany.sca.store.RecoveryListener; +import org.apache.tuscany.sca.store.Store; +import org.apache.tuscany.sca.store.StoreExpirationEvent; +import org.apache.tuscany.sca.store.StoreMonitor; +import org.apache.tuscany.sca.store.StoreWriteException; +import org.osoa.sca.annotations.Destroy; +import org.osoa.sca.annotations.EagerInit; +import org.osoa.sca.annotations.Init; +import org.osoa.sca.annotations.Property; +import org.osoa.sca.annotations.Service; + +/** + * Implements a non-durable, non-transactional store using a simple in-memory map + * + * @version $Rev$ $Date$ + */ +@Service(Store.class) +@EagerInit +public class MemoryStore extends BaseEventPublisher implements Store { + private Map<RuntimeComponent, Map<String, Record>> store; + // TODO integrate with a core threading scheme + private ScheduledExecutorService scheduler; + private long reaperInterval = 300000; + private StoreMonitor monitor; + private long defaultExpirationOffset = 600000; // 10 minutes + + public MemoryStore(StoreMonitor monitor) { + this.monitor = monitor; + this.store = new ConcurrentHashMap<RuntimeComponent, Map<String, Record>>(); + this.scheduler = Executors.newSingleThreadScheduledExecutor(); + } + + /** + * Returns the maximum default expiration offset for records in the store + * + * @return the maximum default expiration offset for records in the store + */ + public long getDefaultExpirationOffset() { + return defaultExpirationOffset; + } + + /** + * Sets the maximum default expiration offset for records in the store + */ + @Property + public void setDefaultExpirationOffset(long defaultExpirationOffset) { + this.defaultExpirationOffset = defaultExpirationOffset; + } + + /** + * Sets the interval for expired entry scanning to be performed + */ + @Property + public void setReaperInterval(long reaperInterval) { + this.reaperInterval = reaperInterval; + } + + public long getReaperInterval() { + return reaperInterval; + } + + @Init + public void init() { + scheduler.scheduleWithFixedDelay(new Reaper(), reaperInterval, reaperInterval, TimeUnit.MILLISECONDS); + monitor.start("In-memory store started"); + } + + @Destroy + public void destroy() { + scheduler.shutdown(); + monitor.stop("In-memory store stopped"); + } + + public void insertRecord(RuntimeComponent owner, String id, Object object, long expiration) throws StoreWriteException { + Map<String, Record> map = store.get(owner); + if (map == null) { + map = new ConcurrentHashMap<String, Record>(); + store.put(owner, map); + } + if (map.containsKey(id)) { + throw new DuplicateRecordException("Duplicate record: " + owner.getURI() +" : " + id); + } + map.put(id, new Record(object, expiration)); + } + + public void updateRecord(RuntimeComponent owner, String id, Object object, long expiration) throws StoreWriteException { + Map<String, Record> map = store.get(owner); + if (map == null) { + throw new StoreWriteException("Record not found: " + owner.getURI() +" : " + id); + } + Record record = map.get(id); + if (record == null) { + throw new StoreWriteException("Record not found: " + owner.getURI() +" : " + id); + } + record.data = object; + } + + public Object readRecord(RuntimeComponent owner, String id) { + Map<String, Record> map = store.get(owner); + if (map == null) { + return null; + } + Record record = map.get(id); + if (record != null) { + return record.data; + } + return null; + } + + public void removeRecords() { + store.clear(); + } + + public void removeRecord(RuntimeComponent owner, String id) throws StoreWriteException { + Map<String, Record> map = store.get(owner); + if (map == null) { + throw new StoreWriteException("Owner not found: " + owner.getURI() +" : " + id); + } + if (map.remove(id) == null) { + throw new StoreWriteException("Owner not found: " + owner.getURI() +" : " + id); + } + } + + public void recover(RecoveryListener listener) { + throw new UnsupportedOperationException(); + } + + private class Record { + private Object data; + private long expiration = NEVER; + + public Record(Object data, long expiration) { + this.data = data; + this.expiration = expiration; + } + + public Object getData() { + return data; + } + + public long getExpiration() { + return expiration; + } + } + + private class Reaper implements Runnable { + + public void run() { + long now = System.currentTimeMillis(); + for (Map.Entry<RuntimeComponent, Map<String, Record>> entries : store.entrySet()) { + for (Map.Entry<String, Record> entry : entries.getValue().entrySet()) { + final long expiration = entry.getValue().expiration; + if (expiration != NEVER && now >= expiration) { + RuntimeComponent owner = entries.getKey(); + Object instance = entry.getValue().getData(); + // notify listeners of the expiration + StoreExpirationEvent event = new StoreExpirationEvent(this, owner, instance); + publish(event); + entries.getValue().remove(entry.getKey()); + } + } + } + } + } + +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/util/IOHelper.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/util/IOHelper.java new file mode 100644 index 0000000000..2058397fb0 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/util/IOHelper.java @@ -0,0 +1,182 @@ +/*
+ * 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.sca.core.util;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.JarURLConnection;
+import java.net.URL;
+import java.util.jar.JarFile;
+
+public class IOHelper {
+ /**
+ * The default buffer size to use.
+ */
+ private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
+
+ protected IOHelper() {
+
+ }
+
+ /**
+ * Unconditionally close an <code>InputStream</code>.
+ * <p>
+ * Equivalent to {@link InputStream#close()}, except any exceptions will be ignored.
+ * This is typically used in finally blocks.
+ *
+ * @param input the InputStream to close, may be null or already closed
+ */
+ public static void closeQuietly(InputStream input) {
+ try {
+ if (input != null) {
+ input.close();
+ }
+ } catch (IOException ioe) {
+ // ignore
+ }
+ }
+
+ /**
+ * Unconditionally close an <code>OutputStream</code>.
+ * <p>
+ * Equivalent to {@link OutputStream#close()}, except any exceptions will be ignored.
+ * This is typically used in finally blocks.
+ *
+ * @param output the OutputStream to close, may be null or already closed
+ */
+ public static void closeQuietly(OutputStream output) {
+ try {
+ if (output != null) {
+ output.close();
+ }
+ } catch (IOException ioe) {
+ // ignore
+ }
+ }
+
+ /**
+ * Copy bytes from an <code>InputStream</code> to an
+ * <code>OutputStream</code>.
+ * <p>
+ * This method buffers the input internally, so there is no need to use a
+ * <code>BufferedInputStream</code>.
+ *
+ * @param input the <code>InputStream</code> to read from
+ * @param output the <code>OutputStream</code> to write to
+ * @return the number of bytes copied
+ * @throws NullPointerException if the input or output is null
+ * @throws IOException if an I/O error occurs
+ * @since Commons IO 1.1
+ */
+ public static int copy(InputStream input, OutputStream output) throws IOException {
+ byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
+ int count = 0;
+ int n = 0;
+ while (-1 != (n = input.read(buffer))) { // NOPMD
+ output.write(buffer, 0, n);
+ count += n;
+ }
+ return count;
+ }
+
+ public static InputStream getInputStream(URL url) throws IOException {
+ return new SafeURLInputStream(url);
+ }
+
+ /**
+ * This class is a workaround for URL stream issue as illustrated below.
+ * InputStream is=url.getInputStream(); is.close(); // This line doesn't close
+ * the JAR file if the URL is a jar entry like "jar:file:/a.jar!/my.composite" We
+ * also need to turn off the JarFile cache.
+ *
+ * @see http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4950148
+ *
+ * @version $Rev$ $Date$
+ */
+ public static class SafeURLInputStream extends InputStream {
+ private JarFile jarFile;
+ private InputStream is;
+
+ public SafeURLInputStream(URL url) throws IOException {
+ String protocol = url.getProtocol();
+ if (protocol != null && (protocol.equals("jar"))) {
+ JarURLConnection connection = (JarURLConnection)url.openConnection();
+ // We cannot use cache
+ connection.setUseCaches(false);
+ try {
+ is = connection.getInputStream();
+ } catch (IOException e) {
+ throw e;
+ }
+ jarFile = connection.getJarFile();
+ } else {
+ is = url.openStream();
+ }
+ }
+
+ public SafeURLInputStream(JarURLConnection connection) throws IOException {
+ // We cannot use cache
+ connection.setUseCaches(false);
+ is = connection.getInputStream();
+ jarFile = connection.getJarFile();
+ }
+
+ public int available() throws IOException {
+ return is.available();
+ }
+
+ public void close() throws IOException {
+ is.close();
+ // We need to close the JAR file
+ if (jarFile != null) {
+ jarFile.close();
+ }
+ }
+
+ public synchronized void mark(int readlimit) {
+ is.mark(readlimit);
+ }
+
+ public boolean markSupported() {
+ return is.markSupported();
+ }
+
+ public int read() throws IOException {
+ return is.read();
+ }
+
+ public int read(byte[] b, int off, int len) throws IOException {
+ return is.read(b, off, len);
+ }
+
+ public int read(byte[] b) throws IOException {
+ return is.read(b);
+ }
+
+ public synchronized void reset() throws IOException {
+ is.reset();
+ }
+
+ public long skip(long n) throws IOException {
+ return is.skip(n);
+ }
+ }
+}
\ No newline at end of file diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/work/Jsr237Work.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/work/Jsr237Work.java new file mode 100644 index 0000000000..6fe883157f --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/work/Jsr237Work.java @@ -0,0 +1,63 @@ +/* + * 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.sca.core.work; + +/* + * JCA work wrapper. + */ +public class Jsr237Work<T extends Runnable> implements commonj.work.Work { + + // Work that is being executed. + private T work; + + /* + * Initializes the work instance. + */ + public Jsr237Work(T work) { + this.work = work; + } + + /* + * Returns the completed work. + */ + public T getWork() { + return work; + } + + /* + * Release the work. + */ + public void release() { + } + + /* + * Work attributes are not daemon. + */ + public boolean isDaemon() { + return false; + } + + /* + * Runs the work. + */ + public void run() { + work.run(); + } +}
\ No newline at end of file diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/work/Jsr237WorkScheduler.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/work/Jsr237WorkScheduler.java new file mode 100644 index 0000000000..c8a5dd7afe --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/work/Jsr237WorkScheduler.java @@ -0,0 +1,167 @@ +/* + * 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.sca.core.work; + +import org.apache.tuscany.sca.work.NotificationListener; +import org.apache.tuscany.sca.work.WorkScheduler; +import org.apache.tuscany.sca.work.WorkSchedulerException; + +import commonj.work.WorkEvent; +import commonj.work.WorkException; +import commonj.work.WorkListener; +import commonj.work.WorkManager; +import commonj.work.WorkRejectedException; + +/** + * A work scheduler implementation based on a JSR 237 work manager. + * <p/> + * <p/> + * This needs a JSR 237 work manager implementation available for scheduling work. Instances can be configured with a + * work manager implementation that is injected in. It is the responsibility of the runtime environment to make a work + * manager implementaion available. For example, if the managed environment supports work manager the runtime can use + * the appropriate lookup mechanism to inject the work manager implementation. </p> + */ +public class Jsr237WorkScheduler implements WorkScheduler { + + /** + * Underlying JSR-237 work manager + */ + private WorkManager jsr237WorkManager; + + /** + * Initializes the JSR 237 work manager. + * + * @param jsr237WorkManager JSR 237 work manager. + */ + public Jsr237WorkScheduler(WorkManager jsr237WorkManager) { + if (jsr237WorkManager == null) { + throw new IllegalArgumentException("Work manager cannot be null"); + } + this.jsr237WorkManager = jsr237WorkManager; + } + + /** + * Schedules a unit of work for future execution. The notification listener is used to register interest in + * callbacks regarding the status of the work. + * + * @param work The unit of work that needs to be asynchronously executed. + */ + public <T extends Runnable> void scheduleWork(T work) { + scheduleWork(work, null); + } + + /** + * Schedules a unit of work for future execution. The notification listener is used to register interest in + * callbacks regarding the status of the work. + * + * @param work The unit of work that needs to be asynchronously executed. + * @param listener Notification listener for callbacks. + */ + public <T extends Runnable> void scheduleWork(T work, NotificationListener<T> listener) { + + if (work == null) { + throw new IllegalArgumentException("Work cannot be null"); + } + + Jsr237Work<T> jsr237Work = new Jsr237Work<T>(work); + try { + if (listener == null) { + jsr237WorkManager.schedule(jsr237Work); + } else { + Jsr237WorkListener<T> jsr237WorkListener = new Jsr237WorkListener<T>(listener, work); + jsr237WorkManager.schedule(jsr237Work, jsr237WorkListener); + } + } catch (WorkRejectedException ex) { + if (listener != null) { + listener.workRejected(work); + } else { + throw new WorkSchedulerException(ex); + } + } catch (WorkException ex) { + throw new WorkSchedulerException(ex); + } + + } + + /* + * Worklistener for keeping track of work status callbacks. + * + */ + private class Jsr237WorkListener<T extends Runnable> implements WorkListener { + + // Notification listener + private NotificationListener<T> listener; + + // Work + private T work; + + /* + * Initializes the notification listener. + */ + public Jsr237WorkListener(NotificationListener<T> listener, T work) { + this.listener = listener; + this.work = work; + } + + /* + * Callback when the work is accepted. + */ + public void workAccepted(WorkEvent workEvent) { + T work = getWork(); + listener.workAccepted(work); + } + + /* + * Callback when the work is rejected. + */ + public void workRejected(WorkEvent workEvent) { + T work = getWork(); + listener.workRejected(work); + } + + /* + * Callback when the work is started. + */ + public void workStarted(WorkEvent workEvent) { + T work = getWork(); + listener.workStarted(work); + } + + /* + * Callback when the work is completed. + */ + public void workCompleted(WorkEvent workEvent) { + T work = getWork(); + Exception exception = workEvent.getException(); + if (exception != null) { + listener.workFailed(work, exception); + } else { + listener.workCompleted(work); + } + } + + /* + * Gets the underlying work from the work event. + */ + private T getWork() { + return work; + } + + } +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/work/ThreadPoolWorkManager.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/work/ThreadPoolWorkManager.java new file mode 100644 index 0000000000..0bc6f1b336 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/work/ThreadPoolWorkManager.java @@ -0,0 +1,220 @@ +/* + * 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.sca.core.work; + +import java.rmi.server.UID; +import java.util.Collection; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.RejectedExecutionException; + +import org.osoa.sca.annotations.Destroy; +import org.osoa.sca.annotations.Property; + +import commonj.work.Work; +import commonj.work.WorkEvent; +import commonj.work.WorkException; +import commonj.work.WorkItem; +import commonj.work.WorkListener; +import commonj.work.WorkManager; +import commonj.work.WorkRejectedException; + +/** + * A thread-pool based implementation for the JSR-237 work manager. + * <p/> + * <p/> + * This implementation supports only local work. + * <p/> + * TODO Elaborate the implementation. </p> + */ +public class ThreadPoolWorkManager implements WorkManager { + + // Map of work items currently handled by the work manager + private Map<WorkItemImpl, WorkListener> workItems = new ConcurrentHashMap<WorkItemImpl, WorkListener>(); + + // Thread-pool + private ExecutorService executor; + + /** + * Initializes the thread-pool. + * + * @param threadPoolSize Thread-pool size. + */ + public ThreadPoolWorkManager(@Property(name = "poolSize") int threadPoolSize) { + executor = Executors.newFixedThreadPool(threadPoolSize); + } + + /** + * Schedules a unit of work asynchronously. + * + * @param work Work that needs to be scheduled. + * @return Work Work item representing the asynchronous work + */ + public WorkItem schedule(Work work) throws WorkException { + return schedule(work, null); + } + + /** + * Schedules a unit of work asynchronously. + * + * @param work Work that needs to be scheduled. + * @param workListener Work listener for callbacks. + * @return Work Work item representing the asynchronous work + */ + public WorkItem schedule(Work work, WorkListener workListener) throws WorkRejectedException { + + WorkItemImpl workItem = new WorkItemImpl(new UID().toString(), work); + if (workListener != null) { + workItems.put(workItem, workListener); + } + workAccepted(workItem, work); + if (scheduleWork(work, workItem)) { + return workItem; + } else { + workItem.setStatus(WorkEvent.WORK_REJECTED); + if (workListener != null) { + workListener.workRejected(new WorkEventImpl(workItem)); + } + throw new WorkRejectedException("Unable to schedule work"); + } + } + + /** + * Wait for all the specified units of work to finish. + * + * @param works Units of the work that need to finish. + * @param timeout Timeout for waiting for the units of work to finish. + */ + public boolean waitForAll(Collection works, long timeout) { + throw new UnsupportedOperationException("waitForAll not supported"); + } + + /** + * Wait for any of the specified units of work to finish. + * + * @param works Units of the work that need to finish. + * @param timeout Timeout for waiting for the units of work to finish. + */ + public Collection waitForAny(Collection works, long timeout) { + throw new UnsupportedOperationException("waitForAny not supported"); + } + + /** + * Method provided for subclasses to indicate a work accptance. + * + * @param workItem Work item representing the work that was accepted. + * @param work Work that was accepted. + */ + private void workAccepted(final WorkItemImpl workItem, final Work work) { + WorkListener listener = workItems.get(workItem); + if (listener != null) { + workItem.setStatus(WorkEvent.WORK_ACCEPTED); + WorkEvent event = new WorkEventImpl(workItem); + listener.workAccepted(event); + } + } + + /* + * Method to indicate a work start. + */ + private void workStarted(final WorkItemImpl workItem, final Work work) { + WorkListener listener = workItems.get(workItem); + if (listener != null) { + workItem.setStatus(WorkEvent.WORK_STARTED); + WorkEvent event = new WorkEventImpl(workItem); + listener.workStarted(event); + } + } + + /* + * Method to indicate a work completion. + */ + private void workCompleted(final WorkItemImpl workItem, final Work work) { + workCompleted(workItem, work, null); + } + + /* + * Method to indicate a work completion. + */ + private void workCompleted(final WorkItemImpl workItem, final Work work, final WorkException exception) { + WorkListener listener = workItems.get(workItem); + if (listener != null) { + workItem.setStatus(WorkEvent.WORK_COMPLETED); + workItem.setResult(work); + workItem.setException(exception); + WorkEvent event = new WorkEventImpl(workItem); + listener.workCompleted(event); + workItems.remove(workItem); + } + } + + /* + * Schedules the work using the threadpool. + */ + private boolean scheduleWork(final Work work, final WorkItemImpl workItem) { + try { + executor.execute(new DecoratingWork(workItem, work)); + return true; + } catch (RejectedExecutionException ex) { + return false; + } + } + + /* + * Class that decorates the original worker so that it can get callbacks when work is done. + */ + private final class DecoratingWork implements Runnable { + + // Work item for this work. + private WorkItemImpl workItem; + + // The original work. + private Work decoratedWork; + + /* + * Initializes the work item and underlying work. + */ + private DecoratingWork(final WorkItemImpl workItem, final Work decoratedWork) { + this.workItem = workItem; + this.decoratedWork = decoratedWork; + } + + /* + * Overrides the run method. + */ + public void run() { + workStarted(workItem, decoratedWork); + try { + decoratedWork.run(); + workCompleted(workItem, decoratedWork); + } catch (Throwable th) { + workCompleted(workItem, decoratedWork, new WorkException(th.getMessage(), th)); + } + } + + } + + @Destroy + public void destroy() { + executor.shutdown(); + } + +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/work/WorkEventImpl.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/work/WorkEventImpl.java new file mode 100644 index 0000000000..62ebeea1fb --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/work/WorkEventImpl.java @@ -0,0 +1,74 @@ +/* + * 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.sca.core.work; + + +import commonj.work.WorkEvent; +import commonj.work.WorkException; +import commonj.work.WorkItem; + +/** + * Default immutable implementation of the <code>WorkEvent</code> class. + */ +class WorkEventImpl implements WorkEvent { + + // Work item for this event + private WorkItem workItem; + + // Exception if something has gone wrong + private WorkException exception; + + /** + * Instantiates the event. + * + * @param workItem Work item for this event. + */ + public WorkEventImpl(final WorkItemImpl workItem) { + this.workItem = workItem; + this.exception = workItem.getException(); + } + + /** + * Returns the work type based on whether the work was accepted, started, + * rejected or completed. + * + * @return Work type. + */ + public int getType() { + return workItem.getStatus(); + } + + /** + * Returns the work item associated with this work type. + * + * @return Work item. + */ + public WorkItem getWorkItem() { + return workItem; + } + + /** + * Returns the exception if the work completed with an exception. + * + * @return Work exception. + */ + public WorkException getException() { + return exception; + } +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/work/WorkItemImpl.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/work/WorkItemImpl.java new file mode 100644 index 0000000000..fae3f1aae3 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/core/work/WorkItemImpl.java @@ -0,0 +1,166 @@ +/* + * 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.sca.core.work; + +import commonj.work.Work; +import commonj.work.WorkException; +import commonj.work.WorkItem; + +/** + * An identity based immutable implementation of the <code>WorkItem</code> + * interface. + * + */ +class WorkItemImpl implements WorkItem { + + // Id scoped for the VM + private String id; + + // Status + private int status = -1; + + // Result + private Work result; + + // Original work + private Work originalWork; + + // Exception + private WorkException exception; + + /** + * Instantiates an id for this item. + * + * @param id of this work event. + */ + protected WorkItemImpl(final String id, final Work orginalWork) { + this.id = id; + this.originalWork = orginalWork; + } + + /** + * Returns the id. + * + * @return Id of this item. + */ + public String getId() { + return id; + } + + /** + * Returns the original work. + * + * @return Original work. + */ + public Work getOriginalWork() { + return originalWork; + } + + /** + * Returns the work result if the work completed. + * + * @return Work. + * @throws WorkException If the work completed with an exception. + */ + public Work getResult() throws WorkException { + return result; + } + + /** + * Sets the result. + * + * @param result Result. + */ + protected void setResult(final Work result) { + this.result = result; + } + + /** + * Returns the exception if work completed with an exception. + * + * @return Work exception. + */ + protected WorkException getException() { + return exception; + } + + /** + * Sets the exception. + * + * @param exception Exception. + */ + protected void setException(final WorkException exception) { + this.exception = exception; + } + + /** + * Returns the work type based on whether the work was accepted, started, + * rejected or completed. + * + * @return Work status. + */ + public int getStatus() { + return status; + } + + /** + * Sets the status. + * + * @param status Status. + */ + protected void setStatus(final int status) { + this.status = status; + } + + /** + * @see Object#hashCode() + */ + public int hashCode() { + return id.hashCode(); + } + + /** + * Indicates whether some other object is "equal to" this one. + * + * @param obj Object to be compared. + * @return true if this object is the same as the obj argument; false + * otherwise.. + */ + public boolean equals(final Object obj) { + return (obj != null) && (obj.getClass() == WorkItemImpl.class) && ((WorkItemImpl) obj).id.equals(id); + } + + /** + * Compares this object with the specified object for order. Returns a + * negative integer, zero, or a positive integer as this object is less + * than, equal to, or greater than the specified object. + * + * @param o Object to be compared. + * @return A negative integer, zero, or a positive integer as this object + * is less than, equal to, or greater than the specified object. + * @throws ClassCastException needs better documentation. + */ + public int compareTo(final Object o) { + if (o.getClass() != WorkItemImpl.class) { + throw new ClassCastException(o.getClass().getName()); + } else { + return ((WorkItemImpl) o).getId().compareTo(getId()); + } + } +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/factory/ObjectCreationException.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/factory/ObjectCreationException.java new file mode 100644 index 0000000000..3432c61c76 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/factory/ObjectCreationException.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.sca.factory; + + +/** + * Denotes an error creating a new object instance + * + * @version $Rev$ $Date$ + */ +public class ObjectCreationException extends RuntimeException { + private static final long serialVersionUID = -6423113430265944499L; + + public ObjectCreationException() { + super(); + } + + public ObjectCreationException(String message, Throwable cause) { + super(message, cause); + } + + public ObjectCreationException(String message) { + super(message); + } + + public ObjectCreationException(Throwable cause) { + super(cause); + } + +} + diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/factory/ObjectFactory.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/factory/ObjectFactory.java new file mode 100644 index 0000000000..c5e0dad4e7 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/factory/ObjectFactory.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.sca.factory; + +/** + * Implementations create new instances of a particular type + * + * @version $Rev$ $Date$ + */ +public interface ObjectFactory<T> { + + /** + * Return a instance of the type that this factory creates. + * + * @return a instance from this factory + */ + T getInstance() throws ObjectCreationException; + +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/factory/SingletonObjectFactory.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/factory/SingletonObjectFactory.java new file mode 100644 index 0000000000..074a1121c9 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/factory/SingletonObjectFactory.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.sca.factory; + + +/** + * Implementation of ObjectFactory that returns a single instance, typically an immutable type. + * + * @version $Rev$ $Date$ + */ +public class SingletonObjectFactory<T> implements ObjectFactory<T> { + private final T instance; + + public SingletonObjectFactory(T instance) { + this.instance = instance; + } + + public T getInstance() { + return instance; + } + +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/scope/AbstractLifecycle.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/scope/AbstractLifecycle.java new file mode 100644 index 0000000000..e5f88d91c6 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/scope/AbstractLifecycle.java @@ -0,0 +1,77 @@ +/* + * 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.sca.scope; + +/** + * Base class providing a simple implementation of Lifecycle. + * + * @version $Rev$ $Date$ + */ +public abstract class AbstractLifecycle implements Lifecycle { + protected volatile int lifecycleState = UNINITIALIZED; + + public int getLifecycleState() { + return lifecycleState; + } + + /** + * Set the current state of the Lifecycle. + * + * @param lifecycleState the new state + */ + protected void setLifecycleState(int lifecycleState) { + this.lifecycleState = lifecycleState; + } + + public void start() { + setLifecycleState(RUNNING); + } + + public void stop() { + setLifecycleState(STOPPED); + } + + /** + * Returns the current lifecycle as a String (for example, "RUNNING"). + * + * @return the current lifecycle as a String + */ + public String toString() { + switch (lifecycleState) { + case Lifecycle.CONFIG_ERROR: + return "CONFIG_ERROR"; + case Lifecycle.ERROR: + return "ERROR"; + case Lifecycle.INITIALIZING: + return "INITIALIZING"; + case Lifecycle.INITIALIZED: + return "INITIALIZED"; + case Lifecycle.RUNNING: + return "RUNNING"; + case Lifecycle.STOPPING: + return "STOPPING"; + case Lifecycle.STOPPED: + return "STOPPED"; + case Lifecycle.UNINITIALIZED: + return "UNINITIALIZED"; + default: + return "UNKNOWN"; + } + } +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/scope/CoreRuntimeException.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/scope/CoreRuntimeException.java new file mode 100644 index 0000000000..ac3e51a982 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/scope/CoreRuntimeException.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.sca.scope; + + +/** + * The root exception for the runtime package. Exceptions occurring in the runtime are generally non-recoverable + * + * @version $Rev$ $Date$ + */ +public abstract class CoreRuntimeException extends RuntimeException { + + public CoreRuntimeException() { + super(); + } + + public CoreRuntimeException(String message, Throwable cause) { + super(message, cause); + } + + public CoreRuntimeException(String message) { + super(message); + } + + public CoreRuntimeException(Throwable cause) { + super(cause); + } + +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/scope/InstanceWrapper.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/scope/InstanceWrapper.java new file mode 100644 index 0000000000..a952e2e72b --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/scope/InstanceWrapper.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.sca.scope; + + +/** + * Provides lifecycle management for an implementation instance associated with + * a component for use by the component's associated {@link org.apache.tuscany.sca.scope.ScopeContainer} + * + * @version $Rev$ $Date$ + */ +public interface InstanceWrapper<T> { + + /** + * @return + */ + T getInstance(); + + /** + * @throws TargetInitializationException + */ + void start() throws TargetInitializationException; + + /** + * @throws TargetDestructionException + */ + void stop() throws TargetDestructionException; + +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/scope/Lifecycle.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/scope/Lifecycle.java new file mode 100644 index 0000000000..02075cad01 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/scope/Lifecycle.java @@ -0,0 +1,69 @@ +/* + * 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.sca.scope; + +/** + * Implementations adhere to runtime lifecycle semantics + * + * @version $Rev$ $Date$ + */ +public interface Lifecycle { + /* A configuration error state */ + int CONFIG_ERROR = -1; + /* Has not been initialized */ + int UNINITIALIZED = 0; + /* In the process of being configured and initialized */ + int INITIALIZING = 1; + /* Instantiated and configured */ + int INITIALIZED = 2; + /* Configured and initialized */ + int RUNNING = 4; + /* In the process of being shutdown */ + int STOPPING = 5; + /* Has been shutdown and removed from the composite */ + int STOPPED = 6; + /* In an error state */ + int ERROR = 7; + + /** + * Returns the lifecycle state + * + * @see #UNINITIALIZED + * @see #INITIALIZING + * @see #INITIALIZED + * @see #RUNNING + * @see #STOPPING + * @see #STOPPED + */ + int getLifecycleState(); + + /** + * Starts the Lifecycle. + * + * @throws CoreRuntimeException + */ + void start() throws CoreRuntimeException; + + /** + * Stops the Lifecycle. + * + * @throws CoreRuntimeException + */ + void stop() throws CoreRuntimeException; +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/scope/PersistenceException.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/scope/PersistenceException.java new file mode 100644 index 0000000000..54fba1ee03 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/scope/PersistenceException.java @@ -0,0 +1,43 @@ +/* + * 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.sca.scope; + +/** + * Raised when an error persisting a component implementation instance is encountered + * + * @version $Rev$ $Date$ + */ +public class PersistenceException extends Exception { + private static final long serialVersionUID = -908468170919651248L; + + public PersistenceException() { + } + + public PersistenceException(String message) { + super(message); + } + + public PersistenceException(String message, Throwable cause) { + super(message, cause); + } + + public PersistenceException(Throwable cause) { + super(cause); + } +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/scope/Scope.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/scope/Scope.java new file mode 100644 index 0000000000..9b3f208cb3 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/scope/Scope.java @@ -0,0 +1,63 @@ +/* + * 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.sca.scope; + +/** + * The default implementation scopes supported by assemblies. + * + * @version $Rev$ $Date$ + */ +public class Scope { + public static final Scope STATELESS = new Scope("STATELESS"); + public static final Scope REQUEST = new Scope("REQUEST"); + public static final Scope SESSION = new Scope("SESSION"); + public static final Scope CONVERSATION = new Scope("CONVERSATION"); + public static final Scope COMPOSITE = new Scope("COMPOSITE"); + public static final Scope SYSTEM = new Scope("SYSTEM"); + public static final Scope UNDEFINED = new Scope("UNDEFINED"); + + private String scope; + + public Scope(String scope) { + this.scope = scope.toUpperCase().intern(); + } + + public String getScope() { + return scope; + } + + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + final Scope scope1 = (Scope) o; + return !(scope != null ? scope != scope1.scope.intern() : scope1.scope != null); + } + + public int hashCode() { + return scope != null ? scope.hashCode() : 0; + } + + public String toString() { + return scope; + } +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/scope/ScopeContainer.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/scope/ScopeContainer.java new file mode 100644 index 0000000000..30482fae90 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/scope/ScopeContainer.java @@ -0,0 +1,93 @@ +/* + * 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.sca.scope; + +import org.apache.tuscany.sca.event.RuntimeEventListener; +import org.apache.tuscany.sca.runtime.RuntimeComponent; + + +/** + * Manages the lifecycle and visibility of instances associated with a an {@link RuntimeComponent}. + * + * @version $Rev$ $Date$ + * @param <KEY> the type of IDs that this container uses to identify its contexts. + * For example, for COMPOSITE scope this could be the URI of the composite component, + * or for HTTP Session scope it might be the HTTP session ID. + */ +public interface ScopeContainer<KEY> extends Lifecycle, RuntimeEventListener { + + /** + * Returns the Scope that this container supports. + * + * @return the Scope that this container supports + */ + Scope getScope(); + + /** + * Start a new context with the supplied ID. + * + * @param contextId an ID that uniquely identifies the context. + */ + void startContext(KEY contextId); + + /** + * Stop the context with the supplied ID. + * + * @param contextId an ID that uniquely identifies the context. + */ + void stopContext(KEY contextId); + + /** + * Returns an instance wrapper associated with the current scope context, creating one if necessary + * @param contextId the id for the scope context + * + * @return the wrapper for the target instance + * @throws TargetResolutionException if there was a problem instantiating the target instance + */ + InstanceWrapper getWrapper(KEY contextId) throws TargetResolutionException; + + /** + * Returns an implementation instance associated with the current scope context. + * If no instance is found, a {@link TargetNotFoundException} is thrown. + * @param contextId the id for the scope context + * + * @return the wrapper for the target instance + * @throws TargetResolutionException if there was a problem instantiating the target instance + */ + InstanceWrapper getAssociatedWrapper(KEY contextId) + throws TargetResolutionException; + + /** + * Return a wrapper after use (for example, after invoking the instance). + * @param wrapper the wrapper for the target instance being returned + * @param contextId the id for the scope context + * + * @throws TargetDestructionException if there was a problem returning the target instance + */ + void returnWrapper(InstanceWrapper wrapper, KEY contextId) + throws TargetDestructionException; + + /** + * Removes a component implementation instance associated with the current context from persistent storage + * + * @param component the owning component + * @throws PersistenceException if there was a problem removing the instance + */ + void remove() throws PersistenceException; +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/scope/ScopeContainerFactory.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/scope/ScopeContainerFactory.java new file mode 100644 index 0000000000..b6acae7382 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/scope/ScopeContainerFactory.java @@ -0,0 +1,32 @@ +/* + * 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.sca.scope; + +import org.apache.tuscany.sca.runtime.RuntimeComponent; + +/** + * Factory to create ScopeContainer for components + * + * @version $Rev$ $Date$ + */ +public interface ScopeContainerFactory { + ScopeContainer createScopeContainer(RuntimeComponent component); + Scope getScope(); +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/scope/ScopeIdentifier.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/scope/ScopeIdentifier.java new file mode 100644 index 0000000000..77e7d76842 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/scope/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.sca.scope; + +/** + * 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.sca.factory.component.WorkContext + */ +public interface ScopeIdentifier { + + /** + * Returns the scope id for the request. + */ + Object getIdentifier(); +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/scope/ScopeNotFoundException.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/scope/ScopeNotFoundException.java new file mode 100644 index 0000000000..40a251c6f2 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/scope/ScopeNotFoundException.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.sca.scope; + +/** + * @version $Rev$ $Date$ + */ +public class ScopeNotFoundException extends Exception { + + private static final long serialVersionUID = -3447306764458851441L; + + public ScopeNotFoundException(String scope) { + super("Scope not found: " + scope); + } +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/scope/ScopeRegistry.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/scope/ScopeRegistry.java new file mode 100644 index 0000000000..f3319ee093 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/scope/ScopeRegistry.java @@ -0,0 +1,43 @@ +/* + * 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.sca.scope; + +import org.apache.tuscany.sca.runtime.RuntimeComponent; + + +/** + * Manages {@link ScopeContainer}s in the runtime + * + * @version $$Rev$$ $$Date$$ + */ +public interface ScopeRegistry { + + /** + * Returns the scope container for the given scope or null if one not found + * + * @param scope the scope + * @return the scope container for the given scope or null if one not found + */ + ScopeContainer getScopeContainer(RuntimeComponent component); + + /** + * @param factory + */ + void register(ScopeContainerFactory factory); +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/scope/ScopedImplementationProvider.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/scope/ScopedImplementationProvider.java new file mode 100644 index 0000000000..8b9c50b070 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/scope/ScopedImplementationProvider.java @@ -0,0 +1,64 @@ +/* + * 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.sca.scope; + +import org.apache.tuscany.sca.provider.ImplementationProvider; + +/** + * A component implementation can implement this interface to provide scope + * management for the components + * + * @version $Rev$ $Date$ + */ +public interface ScopedImplementationProvider extends ImplementationProvider { + /** + * Get the scope for the component implementation + * + * @return The scope for the component implementation, if null is returned, + * STATELESS will be used + */ + Scope getScope(); + + /** + * Indicate if the component needs to be eagerly initialized + * + * @return true if the component is marked to be eagerly initialized, false + * otherwise + */ + boolean isEagerInit(); + + /** + * @return the maxAge + */ + long getMaxAge(); + + /** + * @return the maxIdleTime + */ + long getMaxIdleTime(); + + /** + * Create a wrapper for the component instance for the scope management + * + * @return A wrapper for the component instance + */ + InstanceWrapper createInstanceWrapper(); + +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/scope/ScopedRuntimeComponent.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/scope/ScopedRuntimeComponent.java new file mode 100644 index 0000000000..6c33683bde --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/scope/ScopedRuntimeComponent.java @@ -0,0 +1,40 @@ +/* + * 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.sca.scope; + +import org.apache.tuscany.sca.runtime.RuntimeComponent; + +/** + * Scoped runtime component + * + * @version $Rev$ $Date$ + */ +public interface ScopedRuntimeComponent extends RuntimeComponent { + /** + * Set the associated scope container + * @param scopeContainer + */ + void setScopeContainer(ScopeContainer scopeContainer); + /** + * Get the assoicated scope container + * @return + */ + ScopeContainer getScopeContainer(); +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/scope/TargetDestructionException.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/scope/TargetDestructionException.java new file mode 100644 index 0000000000..6b251295f5 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/scope/TargetDestructionException.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.sca.scope; + +/** + * Denotes an error destroying a target + * + * @version $Rev$ $Date$ + */ +public class TargetDestructionException extends TargetResolutionException { + private static final long serialVersionUID = -6126684147851674709L; + + public TargetDestructionException() { + super(); + } + + public TargetDestructionException(String message, Throwable cause) { + super(message, cause); + } + + public TargetDestructionException(String message) { + super(message); + } + + public TargetDestructionException(Throwable cause) { + super(cause); + } +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/scope/TargetInitializationException.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/scope/TargetInitializationException.java new file mode 100644 index 0000000000..17cd21967f --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/scope/TargetInitializationException.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.sca.scope; + +/** + * Denotes an error initializing a target + * + * @version $Rev$ $Date$ + */ +public class TargetInitializationException extends TargetResolutionException { + private static final long serialVersionUID = -6228778208649752698L; + + public TargetInitializationException() { + super(); + } + + public TargetInitializationException(String message, Throwable cause) { + super(message, cause); + } + + public TargetInitializationException(String message) { + super(message); + } + + public TargetInitializationException(Throwable cause) { + super(cause); + } +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/scope/TargetInvocationException.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/scope/TargetInvocationException.java new file mode 100644 index 0000000000..f9bfa0a788 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/scope/TargetInvocationException.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.sca.scope; + +/** + * Raised when an error is encountered during a target invocation + * + * @version $Rev$ $Date$ + */ +public class TargetInvocationException extends Exception { + + private static final long serialVersionUID = -6553427708442761743L; + + public TargetInvocationException() { + super(); + } + + public TargetInvocationException(String message, Throwable cause) { + super(message, cause); + } + + public TargetInvocationException(String message) { + super(message); + } + + public TargetInvocationException(Throwable cause) { + super(cause); + } +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/scope/TargetInvokerCreationException.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/scope/TargetInvokerCreationException.java new file mode 100644 index 0000000000..a20d030ee8 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/scope/TargetInvokerCreationException.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.sca.scope; + +/** + * Denotes an error creating a target invoker + * + * @version $Rev$ $Date$ + * @Deprecated + */ +public abstract class TargetInvokerCreationException extends Exception { + + public TargetInvokerCreationException() { + super(); + } + + public TargetInvokerCreationException(String message, Throwable cause) { + super(message, cause); + } + + public TargetInvokerCreationException(String message) { + super(message); + } + + public TargetInvokerCreationException(Throwable cause) { + super(cause); + } +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/scope/TargetNotFoundException.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/scope/TargetNotFoundException.java new file mode 100644 index 0000000000..4dd63f58f0 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/scope/TargetNotFoundException.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.sca.scope; + +/** + * Thrown when a target of an operation cannot be found + * + * @version $$Rev$$ $$Date$$ + */ +public class TargetNotFoundException extends TargetResolutionException { + private static final long serialVersionUID = 5541830480658471186L; + + public TargetNotFoundException() { + super(); + } + + public TargetNotFoundException(String message, Throwable cause) { + super(message, cause); + } + + public TargetNotFoundException(String message) { + super(message); + } + + public TargetNotFoundException(Throwable cause) { + super(cause); + } +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/scope/TargetResolutionException.java b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/scope/TargetResolutionException.java new file mode 100644 index 0000000000..284ab8b6c7 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/core/src/main/java/org/apache/tuscany/sca/scope/TargetResolutionException.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.sca.scope; + +/** + * Denotes an error retrieving a target instance + * + * @version $Rev$ $Date$ + */ +public class TargetResolutionException extends Exception { + private static final long serialVersionUID = 2912513650522019405L; + + public TargetResolutionException() { + super(); + } + + public TargetResolutionException(String message, Throwable cause) { + super(message, cause); + } + + public TargetResolutionException(String message) { + super(message); + } + + public TargetResolutionException(Throwable cause) { + super(cause); + } +} |