diff options
Diffstat (limited to 'sandbox/SPI/implementation-crud')
11 files changed, 655 insertions, 0 deletions
diff --git a/sandbox/SPI/implementation-crud/pom.xml b/sandbox/SPI/implementation-crud/pom.xml new file mode 100644 index 0000000000..0e78bc4f25 --- /dev/null +++ b/sandbox/SPI/implementation-crud/pom.xml @@ -0,0 +1,51 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + * 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. +--> +<project> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.apache.tuscany.sca</groupId> + <artifactId>tuscany-samples</artifactId> + <version>1.0-incubating-SNAPSHOT</version> + <relativePath>../pom.xml</relativePath> + </parent> + <artifactId>tuscany-sample-implementation-crud</artifactId> + <name>Apache Tuscany CRUD Implementation Extension Sample</name> + + <dependencies> + <dependency> + <groupId>org.osoa</groupId> + <artifactId>sca-api</artifactId> + <version>1.0-incubating-SNAPSHOT</version> + </dependency> + + <dependency> + <groupId>org.apache.tuscany.sca</groupId> + <artifactId>tuscany-implementation-spi</artifactId> + <version>1.0-incubating-SNAPSHOT</version> + </dependency> + + <dependency> + <groupId>org.apache.tuscany.sca</groupId> + <artifactId>tuscany-host-embedded</artifactId> + <version>1.0-incubating-SNAPSHOT</version> + </dependency> + + </dependencies> +</project> diff --git a/sandbox/SPI/implementation-crud/src/main/java/crud/CRUD.java b/sandbox/SPI/implementation-crud/src/main/java/crud/CRUD.java new file mode 100644 index 0000000000..e33396adfc --- /dev/null +++ b/sandbox/SPI/implementation-crud/src/main/java/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 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/sandbox/SPI/implementation-crud/src/main/java/crud/CRUDImplementation.java b/sandbox/SPI/implementation-crud/src/main/java/crud/CRUDImplementation.java new file mode 100644 index 0000000000..1a9e609605 --- /dev/null +++ b/sandbox/SPI/implementation-crud/src/main/java/crud/CRUDImplementation.java @@ -0,0 +1,150 @@ +/* + * 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 crud; + +import java.util.Collections; +import java.util.List; + +import org.apache.tuscany.assembly.ConstrainingType; +import org.apache.tuscany.assembly.Implementation; +import org.apache.tuscany.assembly.Property; +import org.apache.tuscany.assembly.Reference; +import org.apache.tuscany.assembly.Service; +import org.apache.tuscany.assembly.impl.ServiceImpl; +import org.apache.tuscany.interfacedef.java.JavaInterface; +import org.apache.tuscany.interfacedef.java.JavaInterfaceContract; +import org.apache.tuscany.interfacedef.java.impl.JavaInterfaceContractImpl; +import org.apache.tuscany.interfacedef.java.impl.JavaInterfaceImpl; +import org.apache.tuscany.policy.Intent; +import org.apache.tuscany.policy.PolicySet; + +/** + * 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$$ + */ +public class CRUDImplementation implements Implementation { + private Service crudService; + private String directory; + + /** + * Constructs a new CRUD implementation. + */ + public CRUDImplementation() { + + // CRUD implementation always provide a single service exposing + // the CRUD interface, and have no references and properties + crudService = new ServiceImpl(); + crudService.setName("CRUD"); + JavaInterface javaInterface = new JavaInterfaceImpl(); + javaInterface.setJavaClass(CRUD.class); + JavaInterfaceContract interfaceContract = new JavaInterfaceContractImpl(); + interfaceContract.setInterface(javaInterface); + crudService.setInterfaceContract(interfaceContract); + } + + /** + * Returns the directory used by CRUD implementations to persist + * resources. + * + * @return the directory used to persist resources + */ + public String getDirectory() { + return directory; + } + + /** + * Sets the directory used by CRUD implementations to persist + * resources. + * + * @param directory the directory used to persist resources + */ + public void setDirectory(String directory) { + this.directory = directory; + } + + public ConstrainingType getConstrainingType() { + // CRUD implementations do not support constrainingTypes + return null; + } + + public List<Property> getProperties() { + // CRUD implementations do not support properties + return Collections.emptyList(); + } + + public List<Reference> getReferences() { + // CRUD implementations do not support properties + return Collections.emptyList(); + } + + public List<Service> getServices() { + // CRUD implementations provide a single fixed CRUD service + return Collections.singletonList(crudService); + } + + public String getURI() { + // CRUD implementations don't have a URI + return null; + } + + public void setConstrainingType(ConstrainingType constrainingType) { + // CRUD implementations do not support constrainingTypes + } + + public void setURI(String uri) { + // CRUD implementations don't have a URI + } + + public List<PolicySet> getPolicySets() { + // CRUD implementations do not support policy sets + return Collections.emptyList(); + } + + public List<Intent> getRequiredIntents() { + // CRUD implementations do not support intents + return Collections.emptyList(); + } + + public List<Object> getExtensions() { + // CRUD implementations do not support extensions + return Collections.emptyList(); + } + + public boolean isUnresolved() { + // CRUD implementations are always resolved + return false; + } + + public void setUnresolved(boolean unresolved) { + // CRUD implementations are always resolved + } + +} diff --git a/sandbox/SPI/implementation-crud/src/main/java/crud/CRUDImplementationActivator.java b/sandbox/SPI/implementation-crud/src/main/java/crud/CRUDImplementationActivator.java new file mode 100644 index 0000000000..af586ff901 --- /dev/null +++ b/sandbox/SPI/implementation-crud/src/main/java/crud/CRUDImplementationActivator.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 crud; + +import javax.xml.namespace.QName; + +import org.apache.tuscany.assembly.Implementation; +import org.apache.tuscany.spi.implementation.ImplementationActivator; +import org.apache.tuscany.spi.implementation.InvokerFactory; +import org.apache.tuscany.spi.implementation.XMLSCDLProcessor; + +public class CRUDImplementationActivator implements ImplementationActivator { + + private static final QName IMPLEMENTATION_CRUD = new QName("http://crud", "implementation.crud"); + + public QName getModelQName() { + return IMPLEMENTATION_CRUD; + } + + public XMLSCDLProcessor getSCDLProcessor() { + return new CRUDXMLSCDLProcessor(); + } + + public Class getImplementationClass() { + return CRUDImplementation.class; + } + + public InvokerFactory getInvokerFactory(Implementation m) { + return new CRUDInvokerFactory((CRUDImplementation)m); + } + +} diff --git a/sandbox/SPI/implementation-crud/src/main/java/crud/CRUDInvoker.java b/sandbox/SPI/implementation-crud/src/main/java/crud/CRUDInvoker.java new file mode 100644 index 0000000000..c475383a97 --- /dev/null +++ b/sandbox/SPI/implementation-crud/src/main/java/crud/CRUDInvoker.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 crud; + +import org.apache.tuscany.interfacedef.Operation; +import org.apache.tuscany.spi.implementation.Invoker; + +public class CRUDInvoker implements Invoker { + + private ResourceManager resourceManager; + private Operation operation; + + public CRUDInvoker(ResourceManager resourceManager, Operation operation) { + this.resourceManager = resourceManager; + this.operation = operation; + } + + public Object invoke(Object arg0, Object[] args) { + 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; + } + +} diff --git a/sandbox/SPI/implementation-crud/src/main/java/crud/CRUDInvokerFactory.java b/sandbox/SPI/implementation-crud/src/main/java/crud/CRUDInvokerFactory.java new file mode 100644 index 0000000000..f7a53bd972 --- /dev/null +++ b/sandbox/SPI/implementation-crud/src/main/java/crud/CRUDInvokerFactory.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 crud; + +import java.util.Map; + +import org.apache.tuscany.interfacedef.Operation; +import org.apache.tuscany.spi.implementation.InvokerFactory; + +public class CRUDInvokerFactory implements InvokerFactory { + + private ResourceManager resourceManager; + + public CRUDInvokerFactory(CRUDImplementation m) { + resourceManager = new ResourceManager(m.getDirectory()); + } + + public CRUDInvoker createInvoker(Operation operation) { + return new CRUDInvoker(resourceManager, operation); + } + + public Object getInstance(Map<String, Object> arg0, Map<String, Object> arg1) { + return resourceManager; + } + +} diff --git a/sandbox/SPI/implementation-crud/src/main/java/crud/CRUDXMLSCDLProcessor.java b/sandbox/SPI/implementation-crud/src/main/java/crud/CRUDXMLSCDLProcessor.java new file mode 100644 index 0000000000..e29a6e96e9 --- /dev/null +++ b/sandbox/SPI/implementation-crud/src/main/java/crud/CRUDXMLSCDLProcessor.java @@ -0,0 +1,46 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package crud; + +import javax.xml.stream.XMLStreamReader; +import javax.xml.stream.XMLStreamWriter; + +import org.apache.tuscany.assembly.Implementation; +import org.apache.tuscany.spi.implementation.XMLSCDLProcessor; + +public class CRUDXMLSCDLProcessor implements XMLSCDLProcessor { + + public void read(Implementation implementation, XMLStreamReader reader) { + // Read an <implementation.crud> 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 crudImplementation = (CRUDImplementation) implementation; + crudImplementation.setDirectory(directory); + + } + + public void write(Implementation implementation, XMLStreamWriter outputSource) { + } + +} diff --git a/sandbox/SPI/implementation-crud/src/main/java/crud/ResourceManager.java b/sandbox/SPI/implementation-crud/src/main/java/crud/ResourceManager.java new file mode 100644 index 0000000000..97f818b9b1 --- /dev/null +++ b/sandbox/SPI/implementation-crud/src/main/java/crud/ResourceManager.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 crud; + +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 implements CRUD { + private static int counter; + private static final Map<String, Object> store = new HashMap<String, Object>(); + 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); + } + + //FIXME We shouldn't have to implement the CRUD interface here + + public String create(Object resource) { + return createResource(resource); + } + + public Object retrieve(String id) { + return retrieveResource(id); + } + + public Object update(String id, Object resource) { + return updateResource(id, resource); + } + + public void delete(String id) { + deleteResource(id); + } +} diff --git a/sandbox/SPI/implementation-crud/src/main/resources/META-INF/services/org.apache.tuscany.spi.implementation.ImplementationActivator b/sandbox/SPI/implementation-crud/src/main/resources/META-INF/services/org.apache.tuscany.spi.implementation.ImplementationActivator new file mode 100644 index 0000000000..a661737335 --- /dev/null +++ b/sandbox/SPI/implementation-crud/src/main/resources/META-INF/services/org.apache.tuscany.spi.implementation.ImplementationActivator @@ -0,0 +1,3 @@ +# Implementation class for the ImplementationActivator
+crud.CRUDImplementationActivator
+
diff --git a/sandbox/SPI/implementation-crud/src/main/resources/crud.composite b/sandbox/SPI/implementation-crud/src/main/resources/crud.composite new file mode 100644 index 0000000000..18745033bb --- /dev/null +++ b/sandbox/SPI/implementation-crud/src/main/resources/crud.composite @@ -0,0 +1,30 @@ +<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ * 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.
+-->
+<composite xmlns="http://www.osoa.org/xmlns/sca/1.0" + targetNamespace="http://sample/crud" + xmlns:sc="http://sample/crud" + xmlns:c="http://crud" + name="crud">
+
+ <component name="CRUDServiceComponent">
+ <c:implementation.crud directory="tmp" />
+ </component>
+
+</composite>
diff --git a/sandbox/SPI/implementation-crud/src/test/java/crud/CRUDTestCase.java b/sandbox/SPI/implementation-crud/src/test/java/crud/CRUDTestCase.java new file mode 100644 index 0000000000..e628d9ff72 --- /dev/null +++ b/sandbox/SPI/implementation-crud/src/test/java/crud/CRUDTestCase.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 crud; + +import junit.framework.TestCase; + +import org.apache.tuscany.host.embedded.SCARuntime; +import org.osoa.sca.ComponentContext; +import org.osoa.sca.ServiceReference; + +/** + * @version $Rev$ $Date$ + */ +public class CRUDTestCase extends TestCase { + private CRUD crudService; + + /** + * @throws java.lang.Exception + */ + protected void setUp() throws Exception { + SCARuntime.start("crud.composite"); + ComponentContext context = SCARuntime.getComponentContext("CRUDServiceComponent"); + assertNotNull(context); + ServiceReference<CRUD> self = context.createSelfReference(CRUD.class); + crudService = self.getService(); + + } + + /** + * @throws java.lang.Exception + */ + protected void tearDown() throws Exception { + SCARuntime.stop(); + } + + + public void testCRUD() throws Exception { + String id = crudService.create("ABC"); + Object result = crudService.retrieve(id); + assertEquals("ABC", result); + crudService.update(id, "EFG"); + result = crudService.retrieve(id); + assertEquals("EFG", result); + crudService.delete(id); + result = crudService.retrieve(id); + assertNull(result); + } + + +} |