diff options
Diffstat (limited to 'java/sca/modules/contribution-updater-impl/src')
10 files changed, 945 insertions, 0 deletions
diff --git a/java/sca/modules/contribution-updater-impl/src/main/java/org/apache/tuscany/sca/contribution/updater/impl/ArtifactsFinder.java b/java/sca/modules/contribution-updater-impl/src/main/java/org/apache/tuscany/sca/contribution/updater/impl/ArtifactsFinder.java new file mode 100644 index 0000000000..a23bf6f8cd --- /dev/null +++ b/java/sca/modules/contribution-updater-impl/src/main/java/org/apache/tuscany/sca/contribution/updater/impl/ArtifactsFinder.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.contribution.updater.impl; + +import java.util.List; + +import org.apache.tuscany.sca.assembly.Component; +import org.apache.tuscany.sca.assembly.Composite; +import org.apache.tuscany.sca.contribution.DeployedArtifact; +import org.apache.tuscany.sca.runtime.RuntimeComponent; + +public class ArtifactsFinder { + + public static Composite findComposite(String compositeURI, + List<Composite> artifacts) { + for (Composite composite : artifacts) { + if (composite.getURI().equals(compositeURI)) + return composite; + } + return null; + } + + public static Component findComponent(Composite composite, + String componentName) { + for (Component component : composite.getComponents()) { + if (component.getName().equals(componentName)) { + return component; + + } + + } + return null; + } + +} diff --git a/java/sca/modules/contribution-updater-impl/src/main/java/org/apache/tuscany/sca/contribution/updater/impl/ComponentUpdaterImpl.java b/java/sca/modules/contribution-updater-impl/src/main/java/org/apache/tuscany/sca/contribution/updater/impl/ComponentUpdaterImpl.java new file mode 100644 index 0000000000..a6a6084f5f --- /dev/null +++ b/java/sca/modules/contribution-updater-impl/src/main/java/org/apache/tuscany/sca/contribution/updater/impl/ComponentUpdaterImpl.java @@ -0,0 +1,381 @@ +/* + * 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.contribution.updater.impl; + +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.util.List; + +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.Multiplicity; +import org.apache.tuscany.sca.assembly.Reference; +import org.apache.tuscany.sca.assembly.builder.CompositeBuilder; +import org.apache.tuscany.sca.contribution.Contribution; +import org.apache.tuscany.sca.contribution.DeployedArtifact; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessorExtensionPoint; +import org.apache.tuscany.sca.contribution.service.ContributionResolveException; +import org.apache.tuscany.sca.contribution.service.ContributionService; +import org.apache.tuscany.sca.contribution.updater.ComponentNotFoundException; +import org.apache.tuscany.sca.contribution.updater.ComponentUpdater; +import org.apache.tuscany.sca.contribution.updater.ComponentUpdaterException; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.assembly.CompositeActivator; +import org.apache.tuscany.sca.core.assembly.CompositeActivatorImpl; +import org.apache.tuscany.sca.interfacedef.InterfaceContractMapper; +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 org.apache.tuscany.sca.contribution.updater.impl.ArtifactsFinder; +import org.apache.tuscany.sca.assembly.xml.MetaComponentProcessor; +import org.apache.tuscany.sca.provider.ImplementationProvider; +import org.apache.tuscany.sca.provider.ReferenceBindingProvider; +import org.apache.tuscany.sca.runtime.RuntimeComponent; +import org.apache.tuscany.sca.runtime.RuntimeComponentReference; +import org.apache.tuscany.sca.implementation.java.IntrospectionException; +import org.apache.tuscany.sca.implementation.java.introspect.impl.JavaIntrospectionHelper; + +public class ComponentUpdaterImpl implements ComponentUpdater { + + private String contribURI; + private String compositeURI; + private String componentName; + private JavaInterfaceFactory javaFactory; + private CompositeBuilder compositeBuilder; + private CompositeActivator compositeActivator; + private ContributionService contributionService; + private ExtensionPointRegistry registry; + private AssemblyFactory assemblyFactory; + private InterfaceContractMapper interfaceContractMapper; + private Contribution contrib; + + public ComponentUpdaterImpl(String contribURI, String compositeURI, + String componentName, AssemblyFactory assembly, + JavaInterfaceFactory javaFactory, + CompositeBuilder compositeBuilder, + CompositeActivator compositeActivator, + ContributionService contribService, + ExtensionPointRegistry registry, InterfaceContractMapper mapper) { + this.contribURI = contribURI; + this.compositeURI = compositeURI; + this.componentName = componentName; + this.javaFactory = javaFactory; + this.compositeBuilder = compositeBuilder; + this.compositeActivator = compositeActivator; + this.contributionService = contribService; + this.registry = registry; + this.assemblyFactory = assembly; + this.interfaceContractMapper = mapper; + this.contrib = this.contributionService.getContribution(contribURI); + } + + private void reconcileReference(Reference reference, + ComponentReference targetReference, String componentName) { + + targetReference.setReference(reference); + targetReference.setIsCallback(reference.isCallback()); + targetReference.setMultiplicity(reference.getMultiplicity()); + if (targetReference.getInterfaceContract() != null) { + if (!targetReference.getInterfaceContract().equals( + reference.getInterfaceContract())) { + if (!interfaceContractMapper.isCompatible(reference + .getInterfaceContract(), targetReference + .getInterfaceContract())) { + System.err + .println("Component reference interface incompatible with reference interface: " + + componentName + + "/" + + targetReference.getName()); + } + } + } else { + targetReference.setInterfaceContract(reference + .getInterfaceContract()); + } + if (targetReference.getBindings().isEmpty()) { + targetReference.getBindings().addAll(reference.getBindings()); + } + + // Reconcile callback bindings + if (targetReference.getCallback() == null) { + targetReference.setCallback(reference.getCallback()); + if (targetReference.getCallback() == null) { + targetReference.setCallback(assemblyFactory.createCallback()); + } + + } else if (targetReference.getCallback().getBindings().isEmpty() + && reference.getCallback() != null) { + targetReference.getCallback().getBindings().addAll( + reference.getCallback().getBindings()); + } + Contribution contrib = contributionService.getContribution(contribURI); + List<DeployedArtifact> artifacts = contrib.getArtifacts(); + // RuntimeComponent source = null; + Composite composite = ArtifactsFinder.findComposite(compositeURI, + artifacts); + Component c = ArtifactsFinder.findComponent(composite, componentName); + if (targetReference.getAutowire() == null) { + targetReference.setAutowire(c.getAutowire()); + } + + // Reconcile targets + if (targetReference.getTargets().isEmpty()) { + targetReference.getTargets().addAll(reference.getTargets()); + } + + } + + public org.apache.tuscany.sca.assembly.Reference buildReference( + String name, String className, Class<?> businessInterface, + boolean required) throws Exception { + + Class<?> rawType = contrib.getClassLoader().loadClass(className); + + if (rawType == null) + return null; + + org.apache.tuscany.sca.assembly.Reference reference = assemblyFactory + .createReference(); + JavaInterfaceContract interfaceContract = javaFactory + .createJavaInterfaceContract(); + reference.setInterfaceContract(interfaceContract); + reference.setName(name); + + if (required) { + reference.setMultiplicity(Multiplicity.ONE_ONE); + } else { + reference.setMultiplicity(Multiplicity.ZERO_ONE); + } + Type[] interfaces = rawType.getGenericInterfaces(); + Type genericType = null; + + for (int i = 0; i < interfaces.length; ++i) { + Type tmp = interfaces[i]; + if (interfaces[i] instanceof ParameterizedType) { + ParameterizedType type = (ParameterizedType) tmp; + tmp = type.getRawType(); + } + if (tmp.getClass().getName().equals( + businessInterface.getClass().getName())) { + genericType = tmp; + } + } + if (genericType == null) + throw new ComponentUpdaterException( + "User has specified a wrong businessInterface:" + + businessInterface); + // baseType = JavaIntrospectionHelper.getBusinessInterface(baseType, + // genericType); + /* + * Class<?> baseType = getBaseType(rawType, genericType); if + * (CallableReference.class.isAssignableFrom(baseType)) { if + * (Collection.class.isAssignableFrom(rawType)) { genericType = + * JavaIntrospectionHelper.getParameterType(genericType); } baseType = + * JavaIntrospectionHelper.getBusinessInterface(baseType, genericType); } + */ + try { + + JavaInterface callInterface = javaFactory + .createJavaInterface(JavaIntrospectionHelper + .getErasure(genericType)); + reference.getInterfaceContract().setInterface(callInterface); + if (callInterface.getCallbackClass() != null) { + JavaInterface callbackInterface = javaFactory + .createJavaInterface(callInterface.getCallbackClass()); + reference.getInterfaceContract().setCallbackInterface( + callbackInterface); + } + } catch (InvalidInterfaceException e) { + throw new IntrospectionException(e); + } + return reference; + } + + /* + * private org.apache.tuscany.sca.assembly.Reference + * createReference(JavaElementImpl element, String name) throws + * IntrospectionException { org.apache.tuscany.sca.assembly.Reference + * reference = assemblyFactory.createReference(); JavaInterfaceContract + * interfaceContract = javaFactory.createJavaInterfaceContract(); + * reference.setInterfaceContract(interfaceContract); + * // reference.setMember((Member)element.getAnchor()); boolean required = + * true; Reference ref = element.getAnnotation(Reference.class); if (ref != + * null) { required = ref.required(); } // reference.setRequired(required); + * reference.setName(name); Class<?> rawType = element.getType(); if + * (rawType.isArray() || Collection.class.isAssignableFrom(rawType)) { if + * (required) { reference.setMultiplicity(Multiplicity.ONE_N); } else { + * reference.setMultiplicity(Multiplicity.ZERO_N); } } else { if (required) { + * reference.setMultiplicity(Multiplicity.ONE_ONE); } else { + * reference.setMultiplicity(Multiplicity.ZERO_ONE); } } Type genericType = + * element.getGenericType(); Class<?> baseType = getBaseType(rawType, + * genericType); if (CallableReference.class.isAssignableFrom(baseType)) { + * if (Collection.class.isAssignableFrom(rawType)) { genericType = + * JavaIntrospectionHelper.getParameterType(genericType); } baseType = + * JavaIntrospectionHelper.getBusinessInterface(baseType, genericType); } + * try { JavaInterface callInterface = + * javaFactory.createJavaInterface(baseType); + * reference.getInterfaceContract().setInterface(callInterface); if + * (callInterface.getCallbackClass() != null) { JavaInterface + * callbackInterface = + * javaFactory.createJavaInterface(callInterface.getCallbackClass()); + * reference.getInterfaceContract().setCallbackInterface(callbackInterface); } } + * catch (InvalidInterfaceException e) { throw new + * IntrospectionException(e); } return reference; } + */ + public ComponentReference addReferenceWire(String referenceName, + String className, Class<?> interfaceName, String targetComponent) + throws ComponentUpdaterException, ComponentNotFoundException { + + StAXArtifactProcessorExtensionPoint staxProcessors = registry + .getExtensionPoint(StAXArtifactProcessorExtensionPoint.class); + MetaComponentProcessor processor = (MetaComponentProcessor) staxProcessors + .getProcessor(Component.class); + + Contribution contrib = contributionService.getContribution(contribURI); + + List<DeployedArtifact> artifacts = contrib.getArtifacts(); + RuntimeComponent source = null; + Composite composite = ArtifactsFinder.findComposite(compositeURI, + artifacts); + // TODO error handling + if (composite != null) + source = (RuntimeComponent) ArtifactsFinder.findComponent( + composite, componentName); + else { + throw new ComponentNotFoundException("Not found component " + + componentName + " for update"); + } + if (source != null) { + // Debig this + RuntimeComponentReference targetReference = (RuntimeComponentReference) assemblyFactory + .createComponentReference(); + targetReference.setName(referenceName); + targetReference.setUnresolved(true); + Reference reference = null; + try { + reference = buildReference(referenceName, className, + interfaceName, true); + } catch (Exception e) { + throw new ComponentUpdaterException(e.getMessage()); + } + source.getImplementation().getReferences().add(reference); + + // targetService. + ComponentService targetService = assemblyFactory + .createComponentService(); + targetService.setUnresolved(true); + targetService.setName(targetComponent); + targetReference.getTargets().add(targetService); + + // reconciliate + reconcileReference(reference, targetReference, componentName); + // create component reference for the reference + source.getReferences().add(targetReference); + try { + processor.resolveReference(targetReference, contrib + .getModelResolver()); + } catch (ContributionResolveException e) { + throw new ComponentUpdaterException( + "Contribution Resolving Exception while updating.."); + } + + CompositeActivatorImpl activator = (CompositeActivatorImpl) compositeActivator; + compositeBuilder.attachWire(source, composite, targetReference); + activator.activate(source, targetReference); + synchronized (source) { + activator.configureComponentContext(source); + } + // RuntimeComponentReference runtimeRef = + // ((RuntimeComponentReference)ref); + // runtimeRef.setComponent(component); + for (Binding binding : targetReference.getBindings()) { + ReferenceBindingProvider bindingProvider = targetReference + .getBindingProvider(binding); + if (bindingProvider != null) { + bindingProvider.start(); + } + + } + + ImplementationProvider implementationProvider = source + .getImplementationProvider(); + if (implementationProvider != null) { + /* + * con la reflection cosi tolgo la dipendneza if + * ((implementationProvider.getClass().getName().equals("JavaImplementationProvider")) { + * Clazz cget } implementationProvider.getClass(). if + * (implementationProvider instanceof + * JavaImplementationProvider) { ((JavaImplementationProvider) + * implementationProvider). startReference(targetReference); } + */ + } + return targetReference; + } else { + throw new ComponentNotFoundException("Not found component " + + componentName + " for update"); + } + } + + public ComponentReference removeReferenceWire(String referenceName, + String targetComponent) throws ComponentUpdaterException, + ComponentNotFoundException { + List<DeployedArtifact> artifacts = contributionService.getContribution( + contribURI).getArtifacts(); + RuntimeComponent source = null; + Composite composite = ArtifactsFinder.findComposite(compositeURI, + artifacts); + ComponentReference toRemove = null; + + if (composite != null) { + source = (RuntimeComponent) ArtifactsFinder.findComponent( + composite, this.componentName); + } else { + throw new ComponentNotFoundException("Not found component " + + componentName + " for update"); + } + if ((source != null) && (targetComponent != null)) { + /* source target refenence */ + + List<ComponentReference> references = source.getReferences(); + for (ComponentReference ref : references) { + if (ref.getName().equals(targetComponent)) { + toRemove = ref; + break; + } + } + if (toRemove != null) { + CompositeActivatorImpl activator = (CompositeActivatorImpl) compositeActivator; + activator.stop(source, toRemove); + activator.deactivate(source, + (RuntimeComponentReference) toRemove); + compositeBuilder.detachWire(composite, source, toRemove); + } else { + throw new ComponentNotFoundException("Not found component " + + componentName + " for update"); + } + + } + return toRemove; + } +} diff --git a/java/sca/modules/contribution-updater-impl/src/main/java/org/apache/tuscany/sca/contribution/updater/impl/CompositeUpdaterImpl.java b/java/sca/modules/contribution-updater-impl/src/main/java/org/apache/tuscany/sca/contribution/updater/impl/CompositeUpdaterImpl.java new file mode 100644 index 0000000000..c5a15b5108 --- /dev/null +++ b/java/sca/modules/contribution-updater-impl/src/main/java/org/apache/tuscany/sca/contribution/updater/impl/CompositeUpdaterImpl.java @@ -0,0 +1,156 @@ +/* + * 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.contribution.updater.impl; + +import java.util.List; + +import org.apache.tuscany.sca.assembly.AssemblyFactory; +import org.apache.tuscany.sca.assembly.Component; +import org.apache.tuscany.sca.assembly.Composite; +import org.apache.tuscany.sca.assembly.MetaComponent; +import org.apache.tuscany.sca.assembly.builder.CompositeBuilder; +import org.apache.tuscany.sca.assembly.builder.impl.CompositeBuilderImpl; +import org.apache.tuscany.sca.contribution.Artifact; +import org.apache.tuscany.sca.contribution.Contribution; +import org.apache.tuscany.sca.contribution.DeployedArtifact; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessorExtensionPoint; +import org.apache.tuscany.sca.contribution.service.ContributionService; +import org.apache.tuscany.sca.contribution.updater.CompositeUpdater; +import org.apache.tuscany.sca.contribution.updater.CompositeUpdaterException; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.assembly.CompositeActivator; +import org.apache.tuscany.sca.core.assembly.CompositeActivatorImpl; +import org.apache.tuscany.sca.contribution.updater.impl.ArtifactsFinder; +import org.apache.tuscany.sca.assembly.xml.MetaComponentProcessor; +import org.apache.tuscany.sca.runtime.RuntimeComponent; + +public class CompositeUpdaterImpl implements CompositeUpdater { + + private CompositeBuilder compositeBuilder; + private String compositeURI; + private AssemblyFactory assemblyFactory; + private String contribURI; + private CompositeActivator compositeActivator; + private ExtensionPointRegistry registry; + private ContributionService contribService; + + public CompositeUpdaterImpl(AssemblyFactory assembly, String contribURI, + String compositeURI, CompositeBuilder compositeBuilder, + CompositeActivator compositeActivator, + ExtensionPointRegistry registry, ContributionService contribService) { + this.compositeBuilder = compositeBuilder; + this.compositeURI = compositeURI; + this.assemblyFactory = assembly; + this.contribURI = contribURI; + this.compositeActivator = compositeActivator; + this.registry = registry; + this.contribService = contribService; + } + + public Component addComponent(MetaComponent c) + throws CompositeUpdaterException { + StAXArtifactProcessorExtensionPoint staxProcessors = registry + .getExtensionPoint(StAXArtifactProcessorExtensionPoint.class); + MetaComponentProcessor processor = (MetaComponentProcessor) staxProcessors + .getProcessor(Component.class); + Contribution contrib = contribService.getContribution(contribURI); + List<DeployedArtifact> artifacts = contrib.getArtifacts(); + Composite composite = ArtifactsFinder.findComposite(compositeURI, + artifacts); + boolean found = false; + + if (composite == null) + throw new CompositeUpdaterException( + "Composite not found in contribution" + contribURI); + else { + processor.setComposite(composite); + try { + processor.read(c.build()); + } catch (Exception e) { + throw new CompositeUpdaterException( + "Component error parsing in contribution" + contribURI); + + } + found = true; + } + + if (found) { + Component component = processor.getParsedComponent(); + Composite augmentedComposite = processor.getParsedComposite(); + try { + processor.resolve(component, contrib.getModelResolver()); + compositeBuilder.attach(augmentedComposite, component); + ((CompositeActivatorImpl) compositeActivator) + .activateComponent(component); + } catch (Exception e) { + + throw new CompositeUpdaterException( + "Cannot activate the component"); + } + + return component; + } + return null; + } + + public Component findComponent(String componentName) { + Contribution contrib = contribService.getContribution(contribURI); + List<Composite> artifacts = contrib.getDeployables(); + Composite composite = ArtifactsFinder.findComposite(compositeURI, + artifacts); + return ArtifactsFinder.findComponent(composite, componentName); + } + + public Component removeComponent(String componentName) + throws CompositeUpdaterException { + Contribution contrib = contribService.getContribution(contribURI); + List<DeployedArtifact> artifacts = contrib.getArtifacts(); + + Composite composite = ArtifactsFinder.findComposite(compositeURI, + artifacts); + List<Component> components = composite.getComponents(); + Component toRemove = null; + for (Component component : components) { + if (((RuntimeComponent) component).getName().equals(componentName)) { + toRemove = component; + break; + } + } + if (toRemove == null) { + throw new CompositeUpdaterException( + "Component not found in contribution" + contribURI); + } else { + // start again + try { + composite.getComponents().remove(toRemove); + CompositeActivatorImpl impl = (CompositeActivatorImpl) compositeActivator; + impl.stop(toRemove); + impl.deactivateComponent(toRemove); + CompositeBuilderImpl builder = (CompositeBuilderImpl) compositeBuilder; + builder.detach(composite, toRemove); + } catch (Exception e) { + throw new CompositeUpdaterException( + "Cannot remove composite from the contribution" + + contribURI); + } + return toRemove; + } + } + +} diff --git a/java/sca/modules/contribution-updater-impl/src/main/java/org/apache/tuscany/sca/contribution/updater/impl/ContributionUpdaterFactoryImpl.java b/java/sca/modules/contribution-updater-impl/src/main/java/org/apache/tuscany/sca/contribution/updater/impl/ContributionUpdaterFactoryImpl.java new file mode 100644 index 0000000000..7f5870ad55 --- /dev/null +++ b/java/sca/modules/contribution-updater-impl/src/main/java/org/apache/tuscany/sca/contribution/updater/impl/ContributionUpdaterFactoryImpl.java @@ -0,0 +1,75 @@ +/* + * 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.contribution.updater.impl; + +import org.apache.tuscany.sca.assembly.AssemblyFactory; +import org.apache.tuscany.sca.assembly.builder.CompositeBuilder; +import org.apache.tuscany.sca.contribution.ModelFactoryExtensionPoint; +import org.apache.tuscany.sca.contribution.service.ContributionService; +import org.apache.tuscany.sca.contribution.updater.ComponentUpdater; +import org.apache.tuscany.sca.contribution.updater.CompositeUpdater; +import org.apache.tuscany.sca.contribution.updater.ContributionUpdater; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.assembly.CompositeActivator; +import org.apache.tuscany.sca.interfacedef.InterfaceContractMapper; +import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceFactory; +import org.apache.tuscany.sca.contribution.updater.impl.ComponentUpdaterImpl; +import org.apache.tuscany.sca.contribution.updater.impl.CompositeUpdaterImpl; +import org.apache.tuscany.sca.host.embedded.impl.ReallySmallRuntime; + +public class ContributionUpdaterFactoryImpl implements ContributionUpdater { + private AssemblyFactory assembly; + private JavaInterfaceFactory javaFactory; + private CompositeActivator compositeActivator; + private CompositeBuilder compositeBuilder; + private ExtensionPointRegistry registry; + private ContributionService contribService; + private InterfaceContractMapper mapper; + + public ContributionUpdaterFactoryImpl(ReallySmallRuntime runtime) { + this.assembly = runtime.getAssemblyFactory(); + this.javaFactory = runtime.getExtensionPointRegistry() + .getExtensionPoint(ModelFactoryExtensionPoint.class) + .getFactory(JavaInterfaceFactory.class); + this.compositeActivator = runtime.getCompositeActivator(); + this.compositeBuilder = runtime.getCompositeBuilder(); + this.registry = runtime.getExtensionPointRegistry(); + this.contribService = runtime.getContributionService(); + this.mapper = runtime.getInterfaceContractMapper(); + } + + public void setContributionService(ContributionService s) { + this.contribService = s; + } + + public ComponentUpdater getComponentUpdater(String contribURI, + String compositeURI, String componentName) { + + return new ComponentUpdaterImpl(contribURI, compositeURI, + componentName, assembly, javaFactory, compositeBuilder, + compositeActivator, contribService, registry, mapper); + } + + public CompositeUpdater getCompositeUpdater(String contribURI, + String compositeURI) { + return new CompositeUpdaterImpl(assembly, contribURI, compositeURI, + compositeBuilder, compositeActivator, registry, contribService); + } + +} diff --git a/java/sca/modules/contribution-updater-impl/src/test/java/org/apache/tuscany/sca/contribution/updater/impl/ContributionUpdaterTestCase.java b/java/sca/modules/contribution-updater-impl/src/test/java/org/apache/tuscany/sca/contribution/updater/impl/ContributionUpdaterTestCase.java new file mode 100644 index 0000000000..83d0224ad4 --- /dev/null +++ b/java/sca/modules/contribution-updater-impl/src/test/java/org/apache/tuscany/sca/contribution/updater/impl/ContributionUpdaterTestCase.java @@ -0,0 +1,129 @@ +/* + * 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.contribution.updater.impl; + +import java.io.File; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.ArrayList; + +import javax.xml.namespace.QName; + +import org.apache.tuscany.sca.node.SCANode; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * Test ContributionUpdater. + * + */ +public class ContributionUpdaterTestCase { + + // private static SCADomain domain; + private static SCANode node; + private static MetaComponentHello helloMeta; + + @Before + public void setUp() throws Exception { + /* + * System.out.println("Setting up domain"); + * + * SCADomainFactory domainFactory = SCADomainFactory.newInstance(); + * domain = domainFactory.createSCADomain("http://localhost:9999"); + * + * System.out.println("Setting up calculator nodes"); + * + * ClassLoader cl = ContributionUpdaterTestCase.class.getClassLoader(); + * + * SCANodeFactory nodeFactory = SCANodeFactory.newInstance(); try { + * log.info("Staring entropy handler"); prng = + * SecureRandom.getInstance("SHA1PRNG"); } catch + * (NoSuchAlgorithmException e) { // TODO Auto-generated catch block + * e.printStackTrace(); } componentName = + * "HelloComponent"+this.uniqueID(); + * t.setAttribute("componentName",componentName); + * + * node = nodeFactory.createSCANode("http://localhost:8100/nodeA", + * "http://localhost:9999"); node.addContribution("nodeA", + * cl.getResource("nodeA/")); node.addToDomainLevelComposite(new + * QName("http://test", "HelloComposite")); node.start(); + * SCADomainFinder domainFinder = SCADomainFinder.newInstance(); domain = + * domainFinder.getSCADomain("http://localhost:9999"); helloMeta = new + * MetaComponentHello(); + * helloMeta.setComponentClass("org.apache.tuscany.sca.contribution.updater.impl.HelloComponent"); + */ + } + + @After + public void tearDown() throws Exception { + + } + + @Test + public void testAddNewComponent() throws ClassNotFoundException, + MalformedURLException { + /* + * String helloComponent1 = "<component + * xmlns=\"http://www.osoa.org/xmlns/sca/1.0\" + * name=\"HelloComponent1\">\n" + "<implementation.java + * class=\"org.apache.tuscany.sca.contribution.updater.impl.HelloComponent\"/>"+ "</component>"; + * MetaComponent mc = + * MetaComponentFactory.newMetaComponent("HelloComponent1",helloComponent1); + * try { ((SCANodeImpl) node).addComponentToComposite(mc, "nodeA", + * "HelloComposite.composite"); GreetService greet = + * node.getDomain().getService(GreetService.class, "HelloComponent1"); + * greet.sayHello("Italian").equals("Ciao"); } catch (Exception e) { + * Assert.fail(e.getMessage()); } + */ + Assert.assertEquals("skip", "skip"); + } + + @Test + public void testWireComponents() throws ClassNotFoundException, + MalformedURLException { + Assert.assertEquals("skip", "skip"); + /* + * String helloComponent1 = "<component + * xmlns=\"http://www.osoa.org/xmlns/sca/1.0\" + * name=\"HelloComponent1\">\n" + "<implementation.java + * class=\"org.apache.tuscany.sca.contribution.updater.impl.HelloComponent\"/>"+ "</component>"; + * String helloComponent2 = "<component + * xmlns=\"http://www.osoa.org/xmlns/sca/1.0\" + * name=\"HelloComponent2\">\n" + "<implementation.java + * class=\"org.apache.tuscany.sca.contribution.updater.impl.HelloComponent\"/>"+ "</component>"; + * MetaComponent mc1 = + * MetaComponentFactory.newMetaComponent("HelloComponent1",helloComponent1); + * MetaComponent mc2 = + * MetaComponentFactory.newMetaComponent("HelloComponent2",helloComponent2); + * + * try { ((SCANodeImpl) node).addComponentToComposite(mc1, "nodeA", + * "HelloComposite.composite"); ((SCANodeImpl) + * node).addComponentToComposite(mc2, "nodeA", + * "HelloComposite.composite"); GreetService greet = + * node.getDomain().getService(GreetService.class, "HelloComponent1"); + * greet.sayHello("Italian").equals("Ciao"); } catch (Exception e) { + * Assert.fail(e.getMessage()); } + */ + } + +} diff --git a/java/sca/modules/contribution-updater-impl/src/test/java/org/apache/tuscany/sca/contribution/updater/impl/GreetService.java b/java/sca/modules/contribution-updater-impl/src/test/java/org/apache/tuscany/sca/contribution/updater/impl/GreetService.java new file mode 100644 index 0000000000..0eb7fb692b --- /dev/null +++ b/java/sca/modules/contribution-updater-impl/src/test/java/org/apache/tuscany/sca/contribution/updater/impl/GreetService.java @@ -0,0 +1,26 @@ +/* + * 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.contribution.updater.impl; + +import org.osoa.sca.annotations.Remotable; + +@Remotable +public interface GreetService { + String sayHello(String language); +} diff --git a/java/sca/modules/contribution-updater-impl/src/test/java/org/apache/tuscany/sca/contribution/updater/impl/HelloComponent.java b/java/sca/modules/contribution-updater-impl/src/test/java/org/apache/tuscany/sca/contribution/updater/impl/HelloComponent.java new file mode 100644 index 0000000000..38fa2721bb --- /dev/null +++ b/java/sca/modules/contribution-updater-impl/src/test/java/org/apache/tuscany/sca/contribution/updater/impl/HelloComponent.java @@ -0,0 +1,39 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.contribution.updater.impl; + +import org.osoa.sca.annotations.Service; + +@Service(GreetService.class) +public class HelloComponent implements GreetService { + private String[][] languageGreets = { + { "English", "Italian", "Japanese", "German" }, + { "Hello!", "Ciao!", "Konnici wa!", "Hallo!" } }; + + public String sayHello(String language) { + for (int i = 0; i < 4; ++i) + for (int j = 0; j < 4; ++j) { + if (language.equals(languageGreets[i][j])) { + return languageGreets[0][0]; + } + } + return "I don't speak your language"; + } + +} diff --git a/java/sca/modules/contribution-updater-impl/src/test/java/org/apache/tuscany/sca/contribution/updater/impl/MetaComponentHello.java b/java/sca/modules/contribution-updater-impl/src/test/java/org/apache/tuscany/sca/contribution/updater/impl/MetaComponentHello.java new file mode 100644 index 0000000000..1495ff2cea --- /dev/null +++ b/java/sca/modules/contribution-updater-impl/src/test/java/org/apache/tuscany/sca/contribution/updater/impl/MetaComponentHello.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.contribution.updater.impl; + +import java.io.StringReader; +import java.net.URI; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.security.SecureRandom; +import java.util.logging.Logger; + +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLStreamReader; + +import org.apache.tuscany.sca.assembly.MetaComponent; + +public class MetaComponentHello implements MetaComponent { + private String componentName; + private String scdl; + private boolean loadedFromString = false; + private Logger log = Logger.getLogger(MetaComponentHello.class.getName()); + + public MetaComponentHello() { + } + + private MetaComponentHello(String scdl, boolean loadedFromString) { + this.scdl = scdl; + this.loadedFromString = loadedFromString; + } + + public MetaComponent fromSCDL(String scdl) { + return new MetaComponentHello(scdl, true); + } + + public XMLStreamReader build() throws Exception { + XMLInputFactory factory = XMLInputFactory.newInstance(); + return factory.createXMLStreamReader(new StringReader(scdl)); + } + + public String getName() { + + return componentName; + } + +} diff --git a/java/sca/modules/contribution-updater-impl/src/test/resources/HelloComposite.composite b/java/sca/modules/contribution-updater-impl/src/test/resources/HelloComposite.composite new file mode 100644 index 0000000000..76714a0e9c --- /dev/null +++ b/java/sca/modules/contribution-updater-impl/src/test/resources/HelloComposite.composite @@ -0,0 +1,27 @@ +<?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://test" + xmlns:test="http://test" + name="HelloComposite"> + <component name="HelloComponentService"> + <implementation.java="org.apache.tuscany.sca.contribution.updater.impl.HelloComponent"/> + </component> +</composite>
\ No newline at end of file diff --git a/java/sca/modules/contribution-updater-impl/src/test/resources/repository/sample-calculator.jar b/java/sca/modules/contribution-updater-impl/src/test/resources/repository/sample-calculator.jar Binary files differnew file mode 100644 index 0000000000..9c46c679d2 --- /dev/null +++ b/java/sca/modules/contribution-updater-impl/src/test/resources/repository/sample-calculator.jar |