From 6fadbc9cd1fcf03d0b3630f772d91df149b70428 Mon Sep 17 00:00:00 2001 From: lresende Date: Wed, 11 Nov 2009 23:07:19 +0000 Subject: Moving 1.x branches git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@835123 13f79535-47bb-0310-9956-ffa450edef68 --- .../sca/host/embedded/SCADomainBeanTestCase.java | 65 +++++++ .../sca/host/embedded/SCADomainTestCase.java | 64 +++++++ .../embedded/impl/DefaultSCADomainTestCase.java | 67 +++++++ .../embedded/impl/EmbeddedSCADomainTestCase.java | 199 +++++++++++++++++++++ .../sca/host/embedded/impl/TestModelResolver.java | 104 +++++++++++ .../src/test/java/test/crud/CRUD.java | 56 ++++++ .../test/java/test/crud/CRUDImplementation.java | 51 ++++++ .../java/test/crud/CRUDImplementationFactory.java | 37 ++++ .../crud/DefaultCRUDImplementationFactory.java | 47 +++++ .../java/test/crud/backend/ResourceManager.java | 92 ++++++++++ .../test/crud/impl/CRUDImplementationImpl.java | 118 ++++++++++++ .../crud/impl/CRUDImplementationProcessor.java | 102 +++++++++++ .../java/test/crud/module/CRUDModuleActivator.java | 69 +++++++ .../crud/provider/CRUDImplementationProvider.java | 73 ++++++++ .../CRUDImplementationProviderFactory.java | 49 +++++ .../test/java/test/crud/provider/CRUDInvoker.java | 80 +++++++++ 16 files changed, 1273 insertions(+) create mode 100644 sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java/org/apache/tuscany/sca/host/embedded/SCADomainBeanTestCase.java create mode 100644 sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java/org/apache/tuscany/sca/host/embedded/SCADomainTestCase.java create mode 100644 sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java/org/apache/tuscany/sca/host/embedded/impl/DefaultSCADomainTestCase.java create mode 100644 sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java/org/apache/tuscany/sca/host/embedded/impl/EmbeddedSCADomainTestCase.java create mode 100644 sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java/org/apache/tuscany/sca/host/embedded/impl/TestModelResolver.java create mode 100644 sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java/test/crud/CRUD.java create mode 100644 sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java/test/crud/CRUDImplementation.java create mode 100644 sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java/test/crud/CRUDImplementationFactory.java create mode 100644 sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java/test/crud/DefaultCRUDImplementationFactory.java create mode 100644 sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java/test/crud/backend/ResourceManager.java create mode 100644 sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java/test/crud/impl/CRUDImplementationImpl.java create mode 100644 sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java/test/crud/impl/CRUDImplementationProcessor.java create mode 100644 sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java/test/crud/module/CRUDModuleActivator.java create mode 100644 sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java/test/crud/provider/CRUDImplementationProvider.java create mode 100644 sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java/test/crud/provider/CRUDImplementationProviderFactory.java create mode 100644 sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java/test/crud/provider/CRUDInvoker.java (limited to 'sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java') diff --git a/sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java/org/apache/tuscany/sca/host/embedded/SCADomainBeanTestCase.java b/sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java/org/apache/tuscany/sca/host/embedded/SCADomainBeanTestCase.java new file mode 100644 index 0000000000..c47ea2ab7a --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java/org/apache/tuscany/sca/host/embedded/SCADomainBeanTestCase.java @@ -0,0 +1,65 @@ +/* + * 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.host.embedded; + +import junit.framework.TestCase; + +import org.osoa.sca.ServiceReference; + +import test.crud.CRUD; + + +/** + * @version $Rev$ $Date$ + */ +public class SCADomainBeanTestCase extends TestCase { + + private SCADomainBean domain; + + @Override + protected void setUp() throws Exception { + domain = new SCADomainBean(); + domain.setDeployableComposites("crud.composite"); + } + + public void testStart() throws Exception { + ServiceReference serviceReference = domain.getServiceReference(CRUD.class, "CRUDServiceComponent"); + assertNotNull(serviceReference); + CRUD service = serviceReference.getService(); + String id = service.create("ABC"); + Object result = service.retrieve(id); + assertEquals("ABC", result); + service.update(id, "EFG"); + result = service.retrieve(id); + assertEquals("EFG", result); + service.delete(id); + result = service.retrieve(id); + assertNull(result); + } + + /** + * @throws java.lang.Exception + */ + @Override + protected void tearDown() throws Exception { + domain.close(); + } + +} diff --git a/sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java/org/apache/tuscany/sca/host/embedded/SCADomainTestCase.java b/sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java/org/apache/tuscany/sca/host/embedded/SCADomainTestCase.java new file mode 100644 index 0000000000..896a34515c --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java/org/apache/tuscany/sca/host/embedded/SCADomainTestCase.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.host.embedded; + +import junit.framework.TestCase; + +import org.osoa.sca.ServiceReference; + +import test.crud.CRUD; + + +/** + * @version $Rev$ $Date$ + */ +public class SCADomainTestCase extends TestCase { + + private SCADomain domain; + + @Override + protected void setUp() throws Exception { + domain = SCADomain.newInstance("crud.composite"); + } + + public void testStart() throws Exception { + ServiceReference serviceReference = domain.getServiceReference(CRUD.class, "CRUDServiceComponent"); + assertNotNull(serviceReference); + CRUD service = serviceReference.getService(); + String id = service.create("ABC"); + Object result = service.retrieve(id); + assertEquals("ABC", result); + service.update(id, "EFG"); + result = service.retrieve(id); + assertEquals("EFG", result); + service.delete(id); + result = service.retrieve(id); + assertNull(result); + } + + /** + * @throws java.lang.Exception + */ + @Override + protected void tearDown() throws Exception { + domain.close(); + } + +} diff --git a/sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java/org/apache/tuscany/sca/host/embedded/impl/DefaultSCADomainTestCase.java b/sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java/org/apache/tuscany/sca/host/embedded/impl/DefaultSCADomainTestCase.java new file mode 100644 index 0000000000..b8626b00d6 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java/org/apache/tuscany/sca/host/embedded/impl/DefaultSCADomainTestCase.java @@ -0,0 +1,67 @@ +/* + * 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.host.embedded.impl; + +import junit.framework.TestCase; + +import org.apache.tuscany.sca.host.embedded.management.ComponentManager; + +import test.crud.CRUD; + +/** + * @version $Rev$ $Date$ + */ +public class DefaultSCADomainTestCase extends TestCase { + private DefaultSCADomain domain; + + /** + * @throws java.lang.Exception + */ + @Override + protected void setUp() throws Exception { + domain = new DefaultSCADomain(getClass().getClassLoader(), getClass().getClassLoader(), + "http://localhost", ".", "crud.composite"); + } + + public void testStart() throws Exception { + CRUD service = domain.getService(CRUD.class, "CRUDServiceComponent"); + assertNotNull(service); + } + + public void testComponentManager() throws Exception { + ComponentManager componentManager = domain.getComponentManager(); + assertEquals(1, componentManager.getComponentNames().size()); + assertEquals("CRUDServiceComponent", componentManager.getComponentNames().iterator().next()); + assertNotNull(componentManager.getComponent("CRUDServiceComponent")); + + assertTrue(componentManager.isComponentStarted("CRUDServiceComponent")); + componentManager.stopComponent("CRUDServiceComponent"); + assertFalse(componentManager.isComponentStarted("CRUDServiceComponent")); + } + + /** + * @throws java.lang.Exception + */ + @Override + protected void tearDown() throws Exception { + domain.close(); + } + +} diff --git a/sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java/org/apache/tuscany/sca/host/embedded/impl/EmbeddedSCADomainTestCase.java b/sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java/org/apache/tuscany/sca/host/embedded/impl/EmbeddedSCADomainTestCase.java new file mode 100644 index 0000000000..fc11337426 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java/org/apache/tuscany/sca/host/embedded/impl/EmbeddedSCADomainTestCase.java @@ -0,0 +1,199 @@ +/* + * 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.host.embedded.impl; + +import java.net.URL; + +import javax.xml.namespace.QName; + +import junit.framework.TestCase; + +import org.apache.tuscany.sca.assembly.Component; +import org.apache.tuscany.sca.assembly.Composite; +import org.apache.tuscany.sca.contribution.Contribution; +import org.apache.tuscany.sca.contribution.service.ContributionService; +import org.apache.tuscany.sca.host.embedded.management.ComponentListener; +import org.apache.tuscany.sca.host.embedded.management.ComponentManager; + +import test.crud.CRUD; + + +/** + * @version $Rev$ $Date$ + */ +public class EmbeddedSCADomainTestCase extends TestCase { + private EmbeddedSCADomain domain; + + /** + * @throws java.lang.Exception + */ + @Override + protected void setUp() throws Exception { + + // Create a test embedded SCA domain + domain = new EmbeddedSCADomain(getClass().getClassLoader(), "http://localhost"); + } + + public void testDomain() throws Exception { + // Start the domain + domain.start(); + + // Determine my class loader and my test SCA contribution location + ClassLoader myClassLoader = getClass().getClassLoader(); + String url = myClassLoader.getResource("test.txt").toString(); + url = url.substring(0, url.length()-8); + + // Contribute the SCA contribution + TestModelResolver myResolver = new TestModelResolver(myClassLoader); + ContributionService contributionService = domain.getContributionService(); + Contribution contribution = contributionService.contribute("http://test/contribution", new URL(url), myResolver, false); + assertNotNull(contribution); + + // Decide which SCA composite I want to deploy + Composite myComposite = myResolver.getComposite(new QName("http://sample/crud", "crud")); + + // Add the deployable composite to the domain + domain.getDomainComposite().getIncludes().add(myComposite); + domain.getCompositeBuilder().build(myComposite); + + // Start the composite + domain.getCompositeActivator().activate(myComposite); + domain.getCompositeActivator().start(myComposite); + + // At this point the domain contains my contribution, my composite and + // it's started, my application code can start using it + + // Get the CRUDServiceComponent service + CRUD service = domain.getService(CRUD.class, "CRUDServiceComponent"); + + // Invoke the service + String id = service.create("ABC"); + Object result = service.retrieve(id); + assertEquals("ABC", result); + service.update(id, "EFG"); + result = service.retrieve(id); + assertEquals("EFG", result); + service.delete(id); + result = service.retrieve(id); + assertNull(result); + + // Stop my composite + domain.getCompositeActivator().stop(myComposite); + domain.getCompositeActivator().deactivate(myComposite); + + // Remove my composite + domain.getDomainComposite().getIncludes().remove(myComposite); + + // Remove my contribution + contributionService.remove("http://test/contribution"); + + // Stop the domain + domain.stop(); + } + + public void testComponentManager() throws Exception { + // Start the domain + domain.start(); + + // Determine my class loader and my test SCA contribution location + ClassLoader myClassLoader = getClass().getClassLoader(); + String url = myClassLoader.getResource("test.txt").toString(); + url = url.substring(0, url.length()-8); + + // Contribute the SCA contribution + TestModelResolver myResolver = new TestModelResolver(myClassLoader); + ContributionService contributionService = domain.getContributionService(); + Contribution contribution = contributionService.contribute("http://test/contribution", new URL(url), myResolver, false); + assertNotNull(contribution); + + // Decide which SCA composite I want to deploy + Composite myComposite = myResolver.getComposite(new QName("http://sample/crud", "crud")); + + // Add the deployable composite to the domain + domain.getDomainComposite().getIncludes().add(myComposite); + domain.getCompositeBuilder().build(myComposite); + + // Start the composite + domain.getCompositeActivator().activate(myComposite); + domain.getCompositeActivator().start(myComposite); + + // At this point the domain contains my contribution, my composite and + // it's started, my application code can start using it + + ComponentManager componentManager = domain.getComponentManager(); + assertEquals(1, componentManager.getComponentNames().size()); + assertEquals("CRUDServiceComponent", componentManager.getComponentNames().iterator().next()); + + Component component = componentManager.getComponent("CRUDServiceComponent"); + assertNotNull(component); + assertEquals("CRUDServiceComponent", component.getName()); + + MyComponentListener cl = new MyComponentListener(); + componentManager.addComponentListener(cl); + + assertTrue(componentManager.isComponentStarted("CRUDServiceComponent")); + + assertFalse(cl.stopCalled); + componentManager.stopComponent("CRUDServiceComponent"); + assertTrue(cl.stopCalled); + assertFalse(componentManager.isComponentStarted("CRUDServiceComponent")); + + assertFalse(cl.startCalled); + componentManager.startComponent("CRUDServiceComponent"); + assertTrue(cl.startCalled); + assertTrue(componentManager.isComponentStarted("CRUDServiceComponent")); + + // Stop my composite + domain.getCompositeActivator().stop(myComposite); + domain.getCompositeActivator().deactivate(myComposite); + + // Remove my composite + domain.getDomainComposite().getIncludes().remove(myComposite); + + // Remove my contribution + contributionService.remove("http://test/contribution"); + + // Stop the domain + domain.stop(); + } + + class MyComponentListener implements ComponentListener { + boolean startCalled; + boolean stopCalled; + + public void componentStarted(String componentName) { + startCalled = true; + } + + public void componentStopped(String componentName) { + stopCalled = true; + } + + } + + /** + * @throws java.lang.Exception + */ + @Override + protected void tearDown() throws Exception { + domain.close(); + } + +} diff --git a/sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java/org/apache/tuscany/sca/host/embedded/impl/TestModelResolver.java b/sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java/org/apache/tuscany/sca/host/embedded/impl/TestModelResolver.java new file mode 100644 index 0000000000..6969cba054 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java/org/apache/tuscany/sca/host/embedded/impl/TestModelResolver.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.host.embedded.impl; + +import java.lang.ref.WeakReference; +import java.util.HashMap; +import java.util.Map; + +import javax.xml.namespace.QName; + +import org.apache.tuscany.sca.assembly.Composite; +import org.apache.tuscany.sca.contribution.resolver.ClassReference; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; + + +/** + * A test model resolver, based on a map. + * + * @version $Rev$ $Date$ + */ +public class TestModelResolver implements ModelResolver { + private static final long serialVersionUID = -7826976465762296634L; + + private Map map = new HashMap(); + private WeakReference classLoader; + + private Map composites = new HashMap(); + + public TestModelResolver(ClassLoader classLoader) { + this.classLoader = new WeakReference(classLoader); + } + + public T resolveModel(Class modelClass, T unresolved) { + Object resolved = map.get(unresolved); + if (resolved != null) { + + // Return the resolved object + return modelClass.cast(resolved); + + } else if (unresolved instanceof ClassReference) { + + // Load a class on demand + ClassReference classReference = (ClassReference)unresolved; + Class clazz; + try { + clazz = Class.forName(classReference.getClassName(), true, classLoader.get()); + } catch (ClassNotFoundException e) { + + // Return the unresolved object + return unresolved; + } + + // Store a new ClassReference wrappering the loaded class + resolved = new ClassReference(clazz); + map.put(resolved, resolved); + + // Return the resolved ClassReference + return modelClass.cast(resolved); + + } else { + + // Return the unresolved object + return unresolved; + } + } + + public void addModel(Object resolved) { + map.put(resolved, resolved); + if (resolved instanceof Composite) { + Composite composite = (Composite)resolved; + composites.put(composite.getName(), composite); + } + } + + public Object removeModel(Object resolved) { + if (resolved instanceof Composite) { + Composite composite = (Composite)resolved; + composites.remove(composite.getName()); + } + return map.remove(resolved); + } + + public Composite getComposite(QName qname) { + return composites.get(qname); + } + +} diff --git a/sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java/test/crud/CRUD.java b/sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java/test/crud/CRUD.java new file mode 100644 index 0000000000..7f652ff489 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java/test/crud/CRUD.java @@ -0,0 +1,56 @@ +/* + * 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 test.crud; + +/** + * The service interface of the single CRUD service provided by CRUD components. + * + * @version $Rev$ $Date$ + */ +public interface CRUD { + + /** + * Create a new resource. + * @param resource + * @return + */ + String create(Object resource); + + /** + * Retrieve a resource. + * @param id + * @return + */ + Object retrieve(String id); + + /** + * Update a resource. + * @param id + * @param resource + * @return + */ + Object update(String id, Object resource); + + /** + * Delete a resource. + * @param id + */ + void delete(String id); + +} diff --git a/sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java/test/crud/CRUDImplementation.java b/sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java/test/crud/CRUDImplementation.java new file mode 100644 index 0000000000..29f620c78d --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java/test/crud/CRUDImplementation.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 test.crud; + +import org.apache.tuscany.sca.assembly.Implementation; + +/** + * The model representing a sample CRUD implementation in an SCA assembly model. + * The sample CRUD implementation is not a full blown implementation, it only + * supports a subset of what a component implementation can support: - a single + * fixed service (as opposed to a list of services typed by different + * interfaces) - a directory attribute used to specify where a CRUD component is + * going to persist resources - no references or properties - no policy intents + * or policy sets + * + * @version $$Rev$$ $$Date: 2007-04-23 19:18:54 -0700 (Mon, 23 Apr + * 2007) $$ + */ +public interface CRUDImplementation extends Implementation { + + /** + * Returns the directory used by CRUD implementations to persist resources. + * + * @return the directory used to persist resources + */ + public String getDirectory(); + + /** + * Sets the directory used by CRUD implementations to persist resources. + * + * @param directory the directory used to persist resources + */ + public void setDirectory(String directory); + +} diff --git a/sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java/test/crud/CRUDImplementationFactory.java b/sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java/test/crud/CRUDImplementationFactory.java new file mode 100644 index 0000000000..7890391ed5 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java/test/crud/CRUDImplementationFactory.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 test.crud; + + +/** + * A factory for the sample CRUD implementation model. + * + * @version $Rev$ $Date$ + */ +public interface CRUDImplementationFactory { + + /** + * Creates a new CRUD implementation. + * + * @return + */ + CRUDImplementation createCRUDImplementation(); + +} diff --git a/sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java/test/crud/DefaultCRUDImplementationFactory.java b/sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java/test/crud/DefaultCRUDImplementationFactory.java new file mode 100644 index 0000000000..17df409c8c --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java/test/crud/DefaultCRUDImplementationFactory.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 test.crud; + +import org.apache.tuscany.sca.assembly.AssemblyFactory; +import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceFactory; + +import test.crud.impl.CRUDImplementationImpl; + +/** + * A default factory for the CRUD implementation model. + * + * @version $Rev$ $Date$ + */ +public class DefaultCRUDImplementationFactory implements CRUDImplementationFactory { + + private AssemblyFactory assemblyFactory; + private JavaInterfaceFactory javaFactory; + + public DefaultCRUDImplementationFactory(AssemblyFactory assemblyFactory, + JavaInterfaceFactory javaFactory) { + this.assemblyFactory = assemblyFactory; + this.javaFactory = javaFactory; + } + + public CRUDImplementation createCRUDImplementation() { + return new CRUDImplementationImpl(assemblyFactory, javaFactory); + } + +} diff --git a/sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java/test/crud/backend/ResourceManager.java b/sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java/test/crud/backend/ResourceManager.java new file mode 100644 index 0000000000..f5914be043 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java/test/crud/backend/ResourceManager.java @@ -0,0 +1,92 @@ +/* + * 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 test.crud.backend; + +import java.util.HashMap; +import java.util.Map; + +/** + * A fake resource manager implementation used as a backend by the sample + * CRUD component implementation. + * + * @version $Rev$ $Date$ + */ +public class ResourceManager { + private static int counter; + private static final Map store = new HashMap(); + private String directory; + + /** + * Constructs a new resource manager. + * + * @param directory the directory where to persist resources + */ + public ResourceManager(String directory) { + super(); + this.directory = directory; + } + + /** + * Creates a new resource. + * + * @param resource + * @return + */ + public String createResource(Object resource) { + System.out.println("create(" + resource + ") in " + directory); + String key = String.valueOf(counter++); + store.put(key, resource); + return key; + } + + /** + * Deletes a resource. + * + * @param id + */ + public void deleteResource(String id) { + System.out.println("delete(" + id + ")"); + store.remove(id); + } + + /** + * Retrieves a resource. + * + * @param id + * @return + */ + public Object retrieveResource(String id) { + System.out.println("retrieve(" + id + ")"); + return store.get(id); + } + + /** + * Updates a resource. + * + * @param id + * @param resource + * @return + */ + public Object updateResource(String id, Object resource) { + System.out.println("update(" + id + ")"); + return store.put(id, resource); + } + +} diff --git a/sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java/test/crud/impl/CRUDImplementationImpl.java b/sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java/test/crud/impl/CRUDImplementationImpl.java new file mode 100644 index 0000000000..2060432b1b --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java/test/crud/impl/CRUDImplementationImpl.java @@ -0,0 +1,118 @@ +/* + * 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 test.crud.impl; + +import java.util.Collections; +import java.util.List; + +import org.apache.tuscany.sca.assembly.AssemblyFactory; +import org.apache.tuscany.sca.assembly.ConstrainingType; +import org.apache.tuscany.sca.assembly.Property; +import org.apache.tuscany.sca.assembly.Reference; +import org.apache.tuscany.sca.assembly.Service; +import org.apache.tuscany.sca.interfacedef.InvalidInterfaceException; +import org.apache.tuscany.sca.interfacedef.java.JavaInterface; +import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceContract; +import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceFactory; + +import test.crud.CRUD; +import test.crud.CRUDImplementation; + +/** + * The model representing a sample CRUD implementation in an SCA assembly model. + * + * @version $$Rev$$ $$Date: 2007-04-23 19:18:54 -0700 (Mon, 23 Apr + * 2007) $$ + */ +public class CRUDImplementationImpl implements CRUDImplementation { + + private Service crudService; + private String directory; + + /** + * Constructs a new CRUD implementation. + */ + public CRUDImplementationImpl(AssemblyFactory assemblyFactory, + JavaInterfaceFactory javaFactory) { + + // CRUD implementation always provide a single service exposing + // the CRUD interface, and have no references and properties + crudService = assemblyFactory.createService(); + crudService.setName("CRUD"); + JavaInterface javaInterface; + try { + javaInterface = javaFactory.createJavaInterface(CRUD.class); + } catch (InvalidInterfaceException e) { + throw new IllegalArgumentException(e); + } + JavaInterfaceContract interfaceContract = javaFactory.createJavaInterfaceContract(); + interfaceContract.setInterface(javaInterface); + crudService.setInterfaceContract(interfaceContract); + } + + public String getDirectory() { + return directory; + } + + public void setDirectory(String directory) { + this.directory = directory; + } + + public ConstrainingType getConstrainingType() { + // The sample CRUD implementation does not support constrainingTypes + return null; + } + + public List getProperties() { + // The sample CRUD implementation does not support properties + return Collections.emptyList(); + } + + public List getServices() { + // The sample CRUD implementation provides a single fixed CRUD service + return Collections.singletonList(crudService); + } + + public List getReferences() { + // The sample CRUD implementation does not support properties + return Collections.emptyList(); + } + + public String getURI() { + // The sample CRUD implementation does not have a URI + return null; + } + + public void setConstrainingType(ConstrainingType constrainingType) { + // The sample CRUD implementation does not support constrainingTypes + } + + public void setURI(String uri) { + // The sample CRUD implementation does not have a URI + } + + public boolean isUnresolved() { + // The sample CRUD implementation is always resolved + return false; + } + + public void setUnresolved(boolean unresolved) { + // The sample CRUD implementation is always resolved + } +} diff --git a/sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java/test/crud/impl/CRUDImplementationProcessor.java b/sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java/test/crud/impl/CRUDImplementationProcessor.java new file mode 100644 index 0000000000..4222154ee5 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java/test/crud/impl/CRUDImplementationProcessor.java @@ -0,0 +1,102 @@ +/* + * 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 test.crud.impl; + +import static javax.xml.stream.XMLStreamConstants.END_ELEMENT; + +import javax.xml.namespace.QName; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; +import javax.xml.stream.XMLStreamWriter; + +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; +import org.apache.tuscany.sca.contribution.service.ContributionReadException; +import org.apache.tuscany.sca.contribution.service.ContributionResolveException; +import org.apache.tuscany.sca.contribution.service.ContributionWriteException; + +import test.crud.CRUDImplementation; +import test.crud.CRUDImplementationFactory; + + +/** + * Implements a STAX artifact processor for CRUD implementations. + * + * The artifact processor is responsible for processing + * elements in SCA assembly XML composite files and populating the CRUD + * implementation model, resolving its references to other artifacts in the SCA + * contribution, and optionally write the model back to SCA assembly XML. + * + * @version $Rev$ $Date$ + */ +public class CRUDImplementationProcessor implements StAXArtifactProcessor { + private static final QName IMPLEMENTATION_CRUD = new QName("http://crud", "implementation.crud"); + + private CRUDImplementationFactory crudFactory; + + public CRUDImplementationProcessor(CRUDImplementationFactory crudFactory) { + this.crudFactory = crudFactory; + } + + public QName getArtifactType() { + // Returns the qname of the XML element processed by this processor + return IMPLEMENTATION_CRUD; + } + + public Class getModelType() { + // Returns the type of model processed by this processor + return CRUDImplementation.class; + } + + public CRUDImplementation read(XMLStreamReader reader) throws ContributionReadException, XMLStreamException { + + // Read an element + + // Read the directory attribute. This is where the sample + // CRUD implementation will persist resources. + String directory = reader.getAttributeValue(null, "directory"); + + // Create an initialize the CRUD implementation model + CRUDImplementation implementation = crudFactory.createCRUDImplementation(); + implementation.setDirectory(directory); + + // Skip to end element + while (reader.hasNext()) { + if (reader.next() == END_ELEMENT && IMPLEMENTATION_CRUD.equals(reader.getName())) { + break; + } + } + + return implementation; + } + + public void resolve(CRUDImplementation impl, ModelResolver resolver) throws ContributionResolveException { + } + + public void write(CRUDImplementation implementation, XMLStreamWriter writer) throws ContributionWriteException, XMLStreamException { + + writer.writeStartElement(IMPLEMENTATION_CRUD.getNamespaceURI(), IMPLEMENTATION_CRUD.getLocalPart()); + + if (implementation != null) { + writer.writeAttribute("directory", implementation.getDirectory()); + } + + writer.writeEndElement(); + } +} diff --git a/sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java/test/crud/module/CRUDModuleActivator.java b/sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java/test/crud/module/CRUDModuleActivator.java new file mode 100644 index 0000000000..00a1da9217 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java/test/crud/module/CRUDModuleActivator.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 test.crud.module; + +import org.apache.tuscany.sca.assembly.AssemblyFactory; +import org.apache.tuscany.sca.contribution.ModelFactoryExtensionPoint; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessorExtensionPoint; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.ModuleActivator; +import org.apache.tuscany.sca.interfacedef.java.DefaultJavaInterfaceFactory; +import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceFactory; +import org.apache.tuscany.sca.provider.ProviderFactoryExtensionPoint; + +import test.crud.CRUDImplementationFactory; +import test.crud.DefaultCRUDImplementationFactory; +import test.crud.impl.CRUDImplementationProcessor; +import test.crud.provider.CRUDImplementationProviderFactory; + + +/** + * Implements a module activator for the CRUD implementation extension module. + * The module activator is responsible for contributing the CRUD implementation + * extensions and plugging them in the extension points defined by the Tuscany + * runtime. + * + * @version $Rev$ $Date$ + */ +public class CRUDModuleActivator implements ModuleActivator { + + public void start(ExtensionPointRegistry registry) { + + // Create the CRUD implementation factory + ModelFactoryExtensionPoint factories = registry.getExtensionPoint(ModelFactoryExtensionPoint.class); + AssemblyFactory assemblyFactory = factories.getFactory(AssemblyFactory.class); + JavaInterfaceFactory javaFactory = new DefaultJavaInterfaceFactory(); + CRUDImplementationFactory crudFactory = new DefaultCRUDImplementationFactory(assemblyFactory, javaFactory); + factories.addFactory(crudFactory); + + // Add the CRUD implementation extension to the StAXArtifactProcessor + // extension point + StAXArtifactProcessorExtensionPoint processors = registry.getExtensionPoint(StAXArtifactProcessorExtensionPoint.class); + CRUDImplementationProcessor implementationArtifactProcessor = new CRUDImplementationProcessor(crudFactory); + processors.addArtifactProcessor(implementationArtifactProcessor); + + // Add the CRUD provider factory to the ProviderFactory extension point + ProviderFactoryExtensionPoint providerFactories = registry.getExtensionPoint(ProviderFactoryExtensionPoint.class); + providerFactories.addProviderFactory(new CRUDImplementationProviderFactory()); + } + + public void stop(ExtensionPointRegistry registry) { + } +} diff --git a/sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java/test/crud/provider/CRUDImplementationProvider.java b/sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java/test/crud/provider/CRUDImplementationProvider.java new file mode 100644 index 0000000000..6d663c0979 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java/test/crud/provider/CRUDImplementationProvider.java @@ -0,0 +1,73 @@ +/* + * 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 test.crud.provider; + +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.invocation.Invoker; +import org.apache.tuscany.sca.provider.ImplementationProvider; +import org.apache.tuscany.sca.runtime.RuntimeComponent; +import org.apache.tuscany.sca.runtime.RuntimeComponentService; + +import test.crud.CRUDImplementation; +import test.crud.backend.ResourceManager; + + +/** + * The model representing a sample CRUD implementation in an SCA assembly model. + * The sample CRUD implementation is not a full blown implementation, it only + * supports a subset of what a component implementation can support: - a single + * fixed service (as opposed to a list of services typed by different + * interfaces) - a directory attribute used to specify where a CRUD component is + * going to persist resources - no references or properties - no policy intents + * or policy sets + * + * @version $$Rev$$ $$Date: 2007-04-23 19:18:54 -0700 (Mon, 23 Apr + * 2007) $$ + */ +public class CRUDImplementationProvider implements ImplementationProvider { + + private RuntimeComponent component; + private CRUDImplementation implementation; + + /** + * Constructs a new CRUD implementation. + */ + public CRUDImplementationProvider(RuntimeComponent component, CRUDImplementation implementation) { + this.component = component; + this.implementation = implementation; + } + + public Invoker createInvoker(RuntimeComponentService service, Operation operation) { + CRUDInvoker invoker = new CRUDInvoker(operation, new ResourceManager(implementation.getDirectory())); + return invoker; + } + + public boolean supportsOneWayInvocation() { + return false; + } + + public void start() { + System.out.println("Starting " + component.getName()); + } + + public void stop() { + System.out.println("Stopping " + component.getName()); + } + +} diff --git a/sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java/test/crud/provider/CRUDImplementationProviderFactory.java b/sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java/test/crud/provider/CRUDImplementationProviderFactory.java new file mode 100644 index 0000000000..290716ed7f --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java/test/crud/provider/CRUDImplementationProviderFactory.java @@ -0,0 +1,49 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package test.crud.provider; + +import org.apache.tuscany.sca.provider.ImplementationProvider; +import org.apache.tuscany.sca.provider.ImplementationProviderFactory; +import org.apache.tuscany.sca.runtime.RuntimeComponent; + +import test.crud.CRUDImplementation; + + +/** + * The model representing a sample CRUD implementation in an SCA assembly model. + * + * @version $$Rev$$ $$Date: 2007-04-23 19:18:54 -0700 (Mon, 23 Apr + * 2007) $$ + */ +public class CRUDImplementationProviderFactory implements ImplementationProviderFactory { + + /** + * Constructs a new CRUD implementation. + */ + public CRUDImplementationProviderFactory() { + } + + public ImplementationProvider createImplementationProvider(RuntimeComponent component, CRUDImplementation implementation) { + return new CRUDImplementationProvider(component, implementation); + } + + public Class getModelType() { + return CRUDImplementation.class; + } +} diff --git a/sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java/test/crud/provider/CRUDInvoker.java b/sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java/test/crud/provider/CRUDInvoker.java new file mode 100644 index 0000000000..8f32089e9c --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.0.1/modules/host-embedded/src/test/java/test/crud/provider/CRUDInvoker.java @@ -0,0 +1,80 @@ +/* + * 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 test.crud.provider; + +import java.lang.reflect.InvocationTargetException; + +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.invocation.Invoker; +import org.apache.tuscany.sca.invocation.Message; + +import test.crud.backend.ResourceManager; + + +/** + * Implements a target invoker for CRUD component implementations. + * + * The target invoker is responsible for dispatching invocations to the particular + * component implementation logic. In this example we are simply delegating the + * CRUD operation invocations to the corresponding methods on our fake + * resource manager. + * + * @version $Rev$ $Date$ + */ +public class CRUDInvoker implements Invoker { + private Operation operation; + private ResourceManager resourceManager; + + public CRUDInvoker(Operation operation, ResourceManager resourceManager) { + this.operation = operation; + this.resourceManager = resourceManager; + } + + public Message invoke(Message msg) { + try { + Object[] args = msg.getBody(); + Object resp = doTheWork(args); + msg.setBody(resp); + } catch (InvocationTargetException e) { + msg.setFaultBody(e.getCause()); + } + return msg; + } + + public Object doTheWork(Object[] args) throws InvocationTargetException { + if (operation.getName().equals("create")) { + return resourceManager.createResource(args[0]); + + } else if (operation.getName().equals("retrieve")) { + return resourceManager.retrieveResource((String)args[0]); + + } else if (operation.getName().equals("update")) { + return resourceManager.updateResource((String)args[0], args[1]); + + } else if (operation.getName().equals("delete")) { + resourceManager.deleteResource((String)args[0]); + return null; + + } else { + return null; + } + } + +} -- cgit v1.2.3