diff options
author | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2009-03-06 18:16:45 +0000 |
---|---|---|
committer | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2009-03-06 18:16:45 +0000 |
commit | 1c2980fe064a325e4f30cbd7ce846f39a5edd2a4 (patch) | |
tree | 6bb2297a130aa3f19558771ca49456b893337a07 /java/sca/modules/implementation-osgi/src | |
parent | 02aef1e531c4f274ebcb929cb5cee16c01a08cd4 (diff) |
Work in progress, not compiling yet
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@751018 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'java/sca/modules/implementation-osgi/src')
9 files changed, 51 insertions, 831 deletions
diff --git a/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/context/OSGiAnnotations.java b/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/context/OSGiAnnotations.java deleted file mode 100644 index 7bae686e67..0000000000 --- a/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/context/OSGiAnnotations.java +++ /dev/null @@ -1,302 +0,0 @@ -/* - * 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.implementation.osgi.context; - -import java.lang.reflect.Method; -import java.security.AccessController; -import java.security.PrivilegedAction; -import java.util.ArrayList; -import java.util.Hashtable; - -import org.apache.tuscany.sca.assembly.AssemblyFactory; -import org.apache.tuscany.sca.context.RequestContextFactory; -import org.apache.tuscany.sca.core.FactoryExtensionPoint; -import org.apache.tuscany.sca.core.invocation.ProxyFactory; -import org.apache.tuscany.sca.core.scope.Scope; -import org.apache.tuscany.sca.implementation.java.DefaultJavaImplementationFactory; -import org.apache.tuscany.sca.implementation.java.IntrospectionException; -import org.apache.tuscany.sca.implementation.java.JavaImplementation; -import org.apache.tuscany.sca.implementation.java.JavaImplementationFactory; -import org.apache.tuscany.sca.implementation.java.JavaScopeImpl; -import org.apache.tuscany.sca.implementation.java.injection.JavaPropertyValueObjectFactory; -import org.apache.tuscany.sca.implementation.java.introspect.BaseJavaClassVisitor; -import org.apache.tuscany.sca.implementation.java.introspect.JavaClassVisitor; -import org.apache.tuscany.sca.implementation.java.introspect.impl.AllowsPassByReferenceProcessor; -import org.apache.tuscany.sca.implementation.java.introspect.impl.ComponentNameProcessor; -import org.apache.tuscany.sca.implementation.java.introspect.impl.ConstructorProcessor; -import org.apache.tuscany.sca.implementation.java.introspect.impl.ContextProcessor; -import org.apache.tuscany.sca.implementation.java.introspect.impl.ConversationProcessor; -import org.apache.tuscany.sca.implementation.java.introspect.impl.DestroyProcessor; -import org.apache.tuscany.sca.implementation.java.introspect.impl.EagerInitProcessor; -import org.apache.tuscany.sca.implementation.java.introspect.impl.HeuristicPojoProcessor; -import org.apache.tuscany.sca.implementation.java.introspect.impl.InitProcessor; -import org.apache.tuscany.sca.implementation.java.introspect.impl.PolicyProcessor; -import org.apache.tuscany.sca.implementation.java.introspect.impl.PropertyProcessor; -import org.apache.tuscany.sca.implementation.java.introspect.impl.ReferenceProcessor; -import org.apache.tuscany.sca.implementation.java.introspect.impl.ResourceProcessor; -import org.apache.tuscany.sca.implementation.java.introspect.impl.ServiceProcessor; -import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceFactory; -import org.apache.tuscany.sca.policy.PolicyFactory; -import org.apache.tuscany.sca.runtime.RuntimeComponent; -import org.osgi.framework.Bundle; - -/** - * OSGi annotation processing - * OSGi bundles are not introspected by OSGiImplementation when a component is created. - * Instead if the list of implementation classes is specified in <implementation.osgi/>, - * the classes are introspected when the bundle is resolved. The classes are loaded using - * the bundle ClassLoader, and hence the delay in annotation processing is inevitable. - * There is one other difference compared to implementation.java. While instances (and - * the instance class) are associated with a component in Java, all Java annotations from - * the component implementation class apply to all the component instances. In OSGi, - * instances are associated with services, and a bundle can register multiple services. - * Hence annotations from classes need to be stored separately so that the right ones - * can be associated with the service instance. - * - * @version $Rev$ $Date$ - */ -public class OSGiAnnotations { - - private Scope scope = Scope.COMPOSITE; - private boolean isEagerInit; - - private String[] classList; - - private Bundle[] bundles; - - private RuntimeComponent runtimeComponent; - private JavaPropertyValueObjectFactory propertyValueFactory; - private ProxyFactory proxyFactory; - - private JavaImplementationFactory javaImplementationFactory; - private JavaInterfaceFactory javaInterfaceFactory; - private PolicyFactory policyFactory; - private RequestContextFactory requestContextFactory; - - private Hashtable<Class<?>, JavaImplementation> javaAnnotationInfo = new Hashtable<Class<?>, JavaImplementation>(); - private Hashtable<JavaImplementation, OSGiPropertyInjector> propertyInjectors = - new Hashtable<JavaImplementation, OSGiPropertyInjector>(); - - private long maxAge = -1; - private long maxIdleTime = -1; - - private boolean annotationsProcessed; - - public OSGiAnnotations(FactoryExtensionPoint modelFactories, - String[] classList, - RuntimeComponent runtimeComponent, - JavaPropertyValueObjectFactory propertyValueFactory, - ProxyFactory proxyFactory, - RequestContextFactory requestContextFactory, - Bundle mainBundle, - ArrayList<Bundle> dependentBundles) { - - this.classList = classList; - this.runtimeComponent = runtimeComponent; - this.propertyValueFactory = propertyValueFactory; - this.proxyFactory = proxyFactory; - - AssemblyFactory assemblyFactory = modelFactories.getFactory(AssemblyFactory.class); - this.javaInterfaceFactory = modelFactories.getFactory(JavaInterfaceFactory.class); - this.javaImplementationFactory = createJavaImplementationFactory(assemblyFactory); - this.policyFactory = modelFactories.getFactory(PolicyFactory.class); - - bundles = new Bundle[dependentBundles.size() + 1]; - bundles[0] = mainBundle; - for (int i = 0; i < dependentBundles.size(); i++) - bundles[i + 1] = dependentBundles.get(i); - - } - - public void processAnnotations() throws IntrospectionException { - - if (annotationsProcessed) - return; - annotationsProcessed = true; - for (String className : classList) { - for (Bundle bundle : bundles) { - try { - Class<?> clazz = bundle.loadClass(className); - - processAnnotations(clazz); - - break; - - } catch (ClassNotFoundException e) { - } - } - } - } - - public void injectProperties(Object instance) { - JavaImplementation javaImpl = getAnnotationInfo(instance); - if (javaImpl != null) { - OSGiPropertyInjector injector = propertyInjectors.get(javaImpl); - if (injector != null) - injector.injectProperties(instance); - } - } - - public Scope getScope() { - return scope; - } - - public boolean isAllowsPassByReference(Object instance, Method method) { - - JavaImplementation javaImpl = getAnnotationInfo(instance); - if (javaImpl == null) { - return false; - } - if (javaImpl.isAllowsPassByReference()) { - return true; - } - return javaImpl.isAllowsPassByReference(method); - } - - public boolean isEagerInit() { - return isEagerInit; - } - - public long getMaxAge() { - return maxAge; - } - - public long getMaxIdleTime() { - return maxIdleTime; - } - - public Method getInitMethod(Object instance) { - JavaImplementation javaImpl = getAnnotationInfo(instance); - return javaImpl == null ? null : javaImpl.getInitMethod(); - } - - public Method getDestroyMethod(Object instance) { - JavaImplementation javaImpl = getAnnotationInfo(instance); - return javaImpl == null ? null : javaImpl.getDestroyMethod(); - } - - /* - * Get the annotation corresponding to an instance - * - */ - private JavaImplementation getAnnotationInfo(final Object instance) { - - // The simplest case where the implementation class was listed under the - // classes attribute of <implementation.osgi/>, or this is the second call - // to this method for the implementation class. - // Allow privileged access to get classloader. Requires getClassLoader in security policy. - JavaImplementation javaImpl = AccessController.doPrivileged(new PrivilegedAction<JavaImplementation>() { - public JavaImplementation run() { - return javaAnnotationInfo.get(instance.getClass()); - } - }); - if (javaImpl != null) - return javaImpl; - - // Process annotations from the instance class. - try { - return processAnnotations(instance.getClass()); - } catch (IntrospectionException e) { - // e.printStackTrace(); - } - - return null; - } - - private JavaImplementation processAnnotations(Class<?> clazz) throws IntrospectionException { - - JavaImplementation javaImpl = javaImplementationFactory.createJavaImplementation(clazz); - - javaAnnotationInfo.put(clazz, javaImpl); - - OSGiPropertyInjector propertyInjector = - new OSGiPropertyInjector(javaImpl, runtimeComponent, propertyValueFactory, proxyFactory, - requestContextFactory); - - propertyInjectors.put(javaImpl, propertyInjector); - - if (javaImpl.isEagerInit()) - isEagerInit = true; - if (javaImpl.getMaxAge() != -1) - maxAge = javaImpl.getMaxAge(); - if (javaImpl.getMaxIdleTime() != -1) - maxIdleTime = javaImpl.getMaxIdleTime(); - if (javaImpl.getJavaScope() != JavaScopeImpl.COMPOSITE) - scope = new Scope(javaImpl.getJavaScope().getScope()); - - return javaImpl; - } - - private JavaImplementationFactory createJavaImplementationFactory(AssemblyFactory assemblyFactory) { - JavaImplementationFactory javaImplementationFactory = new DefaultJavaImplementationFactory(); - - // Create the list of class visitors - BaseJavaClassVisitor[] extensions = - new BaseJavaClassVisitor[] {new ConstructorProcessor(assemblyFactory), - new AllowsPassByReferenceProcessor(assemblyFactory), - new ComponentNameProcessor(assemblyFactory), - new ContextProcessor(assemblyFactory), - new ConversationProcessor(assemblyFactory), - new DestroyProcessor(assemblyFactory), new EagerInitProcessor(assemblyFactory), - new InitProcessor(assemblyFactory), new PropertyProcessor(assemblyFactory), - new ReferenceProcessor(assemblyFactory, javaInterfaceFactory), - new ResourceProcessor(assemblyFactory), - new OSGiScopeProcessor(assemblyFactory), - new ServiceProcessor(assemblyFactory, javaInterfaceFactory), - new HeuristicPojoProcessor(assemblyFactory, javaInterfaceFactory), - new PolicyProcessor(assemblyFactory, policyFactory)}; - for (JavaClassVisitor extension : extensions) { - javaImplementationFactory.addClassVisitor(extension); - } - - return javaImplementationFactory; - } - - private class OSGiScopeProcessor extends BaseJavaClassVisitor { - - public OSGiScopeProcessor(AssemblyFactory factory) { - super(factory); - } - - @Override - public <T> void visitClass(Class<T> clazz, JavaImplementation type) throws IntrospectionException { - org.oasisopen.sca.annotation.Scope annotation = - clazz.getAnnotation(org.oasisopen.sca.annotation.Scope.class); - if (annotation == null) { - type.setJavaScope(JavaScopeImpl.COMPOSITE); - return; - } - String name = annotation.value(); - JavaScopeImpl scope; - if ("COMPOSITE".equals(name)) { - scope = JavaScopeImpl.COMPOSITE; - } else if ("SESSION".equals(name)) { - scope = JavaScopeImpl.SESSION; - } else if ("CONVERSATION".equals(name)) { - scope = JavaScopeImpl.CONVERSATION; - } else if ("REQUEST".equals(name)) { - scope = JavaScopeImpl.REQUEST; - } else { - scope = new JavaScopeImpl(name); - } - type.setJavaScope(scope); - } - } - -} diff --git a/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/context/OSGiPropertyInjector.java b/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/context/OSGiPropertyInjector.java deleted file mode 100644 index 6c2e12523d..0000000000 --- a/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/context/OSGiPropertyInjector.java +++ /dev/null @@ -1,276 +0,0 @@ -/* - * 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.implementation.osgi.context; - -import java.lang.annotation.ElementType; -import java.lang.reflect.Constructor; -import java.lang.reflect.Field; -import java.lang.reflect.Member; -import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.Hashtable; -import java.util.List; -import java.util.Map; - -import org.apache.tuscany.sca.assembly.ComponentProperty; -import org.apache.tuscany.sca.assembly.ComponentService; -import org.apache.tuscany.sca.context.RequestContextFactory; -import org.apache.tuscany.sca.core.factory.ObjectCreationException; -import org.apache.tuscany.sca.core.factory.ObjectFactory; -import org.apache.tuscany.sca.core.invocation.CallbackWireObjectFactory; -import org.apache.tuscany.sca.core.invocation.ProxyFactory; -import org.apache.tuscany.sca.implementation.java.IntrospectionException; -import org.apache.tuscany.sca.implementation.java.JavaElementImpl; -import org.apache.tuscany.sca.implementation.java.JavaImplementation; -import org.apache.tuscany.sca.implementation.java.JavaResourceImpl; -import org.apache.tuscany.sca.implementation.java.injection.ArrayMultiplicityObjectFactory; -import org.apache.tuscany.sca.implementation.java.injection.ConversationIDObjectFactory; -import org.apache.tuscany.sca.implementation.java.injection.FieldInjector; -import org.apache.tuscany.sca.implementation.java.injection.Injector; -import org.apache.tuscany.sca.implementation.java.injection.InvalidAccessorException; -import org.apache.tuscany.sca.implementation.java.injection.JavaPropertyValueObjectFactory; -import org.apache.tuscany.sca.implementation.java.injection.ListMultiplicityObjectFactory; -import org.apache.tuscany.sca.implementation.java.injection.MethodInjector; -import org.apache.tuscany.sca.implementation.java.injection.ResourceObjectFactory; -import org.apache.tuscany.sca.implementation.java.introspect.JavaIntrospectionHelper; -import org.apache.tuscany.sca.runtime.RuntimeComponent; -import org.apache.tuscany.sca.runtime.RuntimeComponentReference; -import org.apache.tuscany.sca.runtime.RuntimeWire; -import org.oasisopen.sca.ComponentContext; -import org.oasisopen.sca.RequestContext; -import org.oasisopen.sca.annotation.ConversationID; - -/** - * OSGi property injection support - * - * @version $Rev$ $Date$ - */ -public class OSGiPropertyInjector { - - private ArrayList<JavaElementImpl> injectionSites = new ArrayList<JavaElementImpl>(); - private Hashtable<JavaElementImpl, ObjectFactory> factories = new Hashtable<JavaElementImpl, ObjectFactory>(); - - private Injector[] injectors; - - public OSGiPropertyInjector(JavaImplementation javaImpl, - RuntimeComponent component, - JavaPropertyValueObjectFactory propertyValueFactory, - ProxyFactory proxyFactory, - RequestContextFactory requestContextFactory) throws IntrospectionException { - - createInjectionSites(javaImpl, component, propertyValueFactory, proxyFactory, requestContextFactory); - - injectors = createInjectors(); - - } - - @SuppressWarnings("unchecked") - public void injectProperties(Object instance) { - - for (Injector injector : injectors) { - injector.inject(instance); - } - - } - - @SuppressWarnings("unchecked") - private void createInjectionSites(JavaImplementation javaImpl, - RuntimeComponent component, - JavaPropertyValueObjectFactory propertyValueFactory, - ProxyFactory proxyFactory, - RequestContextFactory requestContextFactory) { - - List<ComponentProperty> componentProperties = component.getProperties(); - Map<String, JavaElementImpl> propertyMembers = javaImpl.getPropertyMembers(); - - for (ComponentProperty prop : componentProperties) { - JavaElementImpl element = propertyMembers.get(prop.getName()); - - if (element != null && !(element.getAnchor() instanceof Constructor) && prop.getValue() != null) { - Class propertyJavaType = - JavaIntrospectionHelper.getBaseType(element.getType(), element.getGenericType()); - ObjectFactory objFactory = - propertyValueFactory.createValueFactory(prop, prop.getValue(), propertyJavaType); - - factories.put(element, objFactory); - injectionSites.add(element); - } - } - - for (Member member : javaImpl.getConversationIDMembers()) { - ObjectFactory<String> factory = new ConversationIDObjectFactory(); - if (member instanceof Field) { - JavaElementImpl element = new JavaElementImpl((Field)member); - element.setClassifer(ConversationID.class); - injectionSites.add(element); - factories.put(element, factory); - } else if (member instanceof Method) { - JavaElementImpl element = new JavaElementImpl((Method)member, 0); - element.setName(JavaIntrospectionHelper.toPropertyName(member.getName())); - element.setClassifer(ConversationID.class); - injectionSites.add(element); - factories.put(element, factory); - } else { - throw new InvalidAccessorException("Member must be a field or method: " + member.getName()); - } - - } - - if (!javaImpl.getCallbackMembers().isEmpty()) { - Map<String, List<RuntimeWire>> callbackWires = new HashMap<String, List<RuntimeWire>>(); - for (ComponentService service : component.getServices()) { - - RuntimeComponentReference callbackReference = (RuntimeComponentReference)service.getCallbackReference(); - if (callbackReference != null) { - List<RuntimeWire> wires = callbackReference.getRuntimeWires(); - if (!wires.isEmpty()) { - callbackWires.put(wires.get(0).getSource().getInterfaceContract().getInterface().toString(), - wires); - } - } - } - - for (Map.Entry<String, Collection<JavaElementImpl>> entry : javaImpl.getCallbackMembers().entrySet()) { - List<RuntimeWire> wires = callbackWires.get(entry.getKey()); - if (wires == null) { - // this can happen when there are no client wires to a - // component that has a callback - continue; - } - for (JavaElementImpl element : entry.getValue()) { - ObjectFactory<?> factory = new CallbackWireObjectFactory(element.getType(), proxyFactory, wires); - if (!(element.getAnchor() instanceof Constructor)) { - injectionSites.add(element); - } - factories.put(element, factory); - } - } - } - - for (JavaResourceImpl resource : javaImpl.getResources().values()) { - - ObjectFactory<?> objectFactory; - Class<?> type = resource.getElement().getType(); - if (ComponentContext.class.equals(type)) { - objectFactory = new ComponentContextFactory(component); - - } else if (RequestContext.class.equals(type)) { - objectFactory = new RequestContextObjectFactory(requestContextFactory, proxyFactory, component); - - } else { - boolean optional = resource.isOptional(); - String mappedName = resource.getMappedName(); - objectFactory = new ResourceObjectFactory(type, mappedName, optional, null); - } - factories.put(resource.getElement(), objectFactory); - if (!(resource.getElement().getAnchor() instanceof Constructor)) { - injectionSites.add(resource.getElement()); - } - } - - } - - @SuppressWarnings("unchecked") - private Injector[] createInjectors() { - - Injector[] injectors = (Injector[])new Injector[injectionSites.size()]; - - int i = 0; - for (JavaElementImpl element : injectionSites) { - Object obj = factories.get(element); - if (obj != null) { - if (obj instanceof ObjectFactory) { - ObjectFactory<?> factory = (ObjectFactory<?>)obj; - Member member = (Member)element.getAnchor(); - if (element.getElementType() == ElementType.FIELD) { - injectors[i++] = new FieldInjector((Field)member, factory); - } else if (element.getElementType() == ElementType.PARAMETER && member instanceof Method) { - injectors[i++] = new MethodInjector((Method)member, factory); - } else if (member instanceof Constructor) { - // Ignore - } else { - throw new AssertionError(String.valueOf(element)); - } - } else { - injectors[i++] = createMultiplicityInjector(element, (List<ObjectFactory<?>>)obj); - } - } - } - return injectors; - } - - @SuppressWarnings("unchecked") - protected Injector createMultiplicityInjector(JavaElementImpl element, List<ObjectFactory<?>> factories) { - Class<?> interfaceType = JavaIntrospectionHelper.getBaseType(element.getType(), element.getGenericType()); - - if (element.getAnchor() instanceof Field) { - Field field = (Field)element.getAnchor(); - if (field.getType().isArray()) { - return new FieldInjector(field, new ArrayMultiplicityObjectFactory(interfaceType, factories)); - } else { - return new FieldInjector(field, new ListMultiplicityObjectFactory(factories)); - } - } else if (element.getAnchor() instanceof Method) { - Method method = (Method)element.getAnchor(); - if (method.getParameterTypes()[0].isArray()) { - return new MethodInjector(method, new ArrayMultiplicityObjectFactory(interfaceType, factories)); - } else { - return new MethodInjector(method, new ListMultiplicityObjectFactory(factories)); - } - } else { - throw new InvalidAccessorException("Member must be a field or method: " + element.getName()); - } - } - - private static class ComponentContextFactory implements ObjectFactory { - - RuntimeComponent component; - - private ComponentContextFactory(RuntimeComponent component) { - this.component = component; - } - - public Object getInstance() throws ObjectCreationException { - return component.getComponentContext(); - } - - } - - private static class RequestContextObjectFactory implements ObjectFactory<RequestContext> { - private RuntimeComponent component; - private RequestContextFactory factory; - private ProxyFactory proxyFactory; - - public RequestContextObjectFactory(RequestContextFactory factory, - ProxyFactory proxyFactory, - RuntimeComponent component) { - this.factory = factory; - this.proxyFactory = proxyFactory; - this.component = component; - } - - public RequestContext getInstance() throws ObjectCreationException { - return factory.createRequestContext(component); - } - - } - -} diff --git a/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/context/OSGiPropertyValueObjectFactory.java b/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/context/OSGiPropertyValueObjectFactory.java deleted file mode 100644 index a43566165a..0000000000 --- a/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/context/OSGiPropertyValueObjectFactory.java +++ /dev/null @@ -1,209 +0,0 @@ -/* - * 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.implementation.osgi.context; - -import java.util.ArrayList; -import java.util.List; -import java.util.StringTokenizer; - -import org.apache.tuscany.sca.assembly.Property; -import org.apache.tuscany.sca.core.factory.ObjectCreationException; -import org.apache.tuscany.sca.core.factory.ObjectFactory; -import org.apache.tuscany.sca.databinding.Mediator; -import org.apache.tuscany.sca.databinding.SimpleTypeMapper; -import org.apache.tuscany.sca.databinding.impl.SimpleTypeMapperImpl; -import org.apache.tuscany.sca.databinding.xml.DOMDataBinding; -import org.apache.tuscany.sca.interfacedef.DataType; -import org.apache.tuscany.sca.interfacedef.impl.DataTypeImpl; -import org.apache.tuscany.sca.interfacedef.util.TypeInfo; -import org.apache.tuscany.sca.interfacedef.util.XMLType; -import org.w3c.dom.Document; -import org.w3c.dom.Element; -import org.w3c.dom.Node; - -/** - * - * Process properties and create property values. - * - * This code has been copied from the Java implementation to avoid dependencies on the Java implementation. - * - * @version $Rev$ $Date$ - */ -public class OSGiPropertyValueObjectFactory { - protected SimpleTypeMapper simpleTypeMapper = new SimpleTypeMapperImpl(); - boolean isSimpleType; - - public OSGiPropertyValueObjectFactory() { - } - - public OSGiPropertyValueObjectFactory(Mediator mediator) { - } - - public ObjectFactory createValueFactory(Property property, Object propertyValue) { - - Class javaType = SimpleTypeMapperImpl.getJavaType(property.getXSDType()); - isSimpleType = isSimpleType(property); - Document doc = (Document)propertyValue; - Element rootElement = doc.getDocumentElement(); - if (property.isMany()) { - if (isSimpleType) { - String value = ""; - if (rootElement.getChildNodes().getLength() > 0) { - value = rootElement.getChildNodes().item(0).getTextContent(); - } - List<String> values = getSimplePropertyValues(value, javaType); - return new ListObjectFactoryImpl(property, values, isSimpleType, javaType); - } else { - return new ListObjectFactoryImpl(property, getComplexPropertyValues(doc), isSimpleType, javaType); - } - } else { - if (isSimpleType) { - String value = ""; - if (rootElement.getChildNodes().getLength() > 0) { - value = rootElement.getChildNodes().item(0).getTextContent(); - } - return new ObjectFactoryImpl(property, value, isSimpleType, javaType); - } else { - Object value = getComplexPropertyValues(doc).get(0); - return new ObjectFactoryImpl(property, value, isSimpleType, javaType); - } - - } - } - - private boolean isSimpleType(Property property) { - if (property.getXSDType() != null) { - return SimpleTypeMapperImpl.isSimpleXSDType(property.getXSDType()); - } else { - if (property instanceof Document) { - Document doc = (Document)property; - Element element = doc.getDocumentElement(); - if (element.getChildNodes().getLength() == 1 && element.getChildNodes().item(0).getNodeType() == Node.TEXT_NODE) { - return true; - } - } - } - return false; - } - - private List<String> getSimplePropertyValues(String concatenatedValue, Class javaType) { - List<String> propValues = new ArrayList<String>(); - StringTokenizer st = null; - if (javaType.getName().equals("java.lang.String")) { - st = new StringTokenizer(concatenatedValue, "\""); - } else { - st = new StringTokenizer(concatenatedValue); - } - String aToken = null; - while (st.hasMoreTokens()) { - aToken = st.nextToken(); - if (aToken.trim().length() > 0) { - propValues.add(aToken); - } - } - return propValues; - } - - private List<Node> getComplexPropertyValues(Document document) { - Element rootElement = document.getDocumentElement(); - List<Node> propValues = new ArrayList<Node>(); - for (int count = 0; count < rootElement.getChildNodes().getLength(); ++count) { - if (rootElement.getChildNodes().item(count).getNodeType() == Node.ELEMENT_NODE) { - propValues.add(rootElement.getChildNodes().item(count)); - } - } - return propValues; - } - - public abstract class ObjectFactoryImplBase implements ObjectFactory { - protected SimpleTypeMapper simpleTypeMapper = new SimpleTypeMapperImpl(); - protected Property property; - protected Object propertyValue; - protected Class javaType; - protected DataType<XMLType> sourceDataType; - protected DataType<?> targetDataType; - boolean isSimpleType; - - public ObjectFactoryImplBase(Property property, Object propertyValue, boolean isSimpleType, Class javaType) { - this.isSimpleType = isSimpleType; - this.property = property; - this.propertyValue = propertyValue; - this.javaType = javaType; - sourceDataType = - new DataTypeImpl<XMLType>(DOMDataBinding.NAME, Node.class, - new XMLType(null, this.property.getXSDType())); - TypeInfo typeInfo = null; - if (this.property.getXSDType() != null) { - if (SimpleTypeMapperImpl.isSimpleXSDType(this.property.getXSDType())) { - typeInfo = new TypeInfo(property.getXSDType(), true, null); - } else { - typeInfo = new TypeInfo(property.getXSDType(), false, null); - } - } else { - typeInfo = new TypeInfo(property.getXSDType(), false, null); - } - - XMLType xmlType = new XMLType(typeInfo); - String dataBinding = null; //(String)property.getExtensions().get(DataBinding.class.getName()); - if (dataBinding != null) { - targetDataType = new DataTypeImpl<XMLType>(dataBinding, javaType, xmlType); - } else { - targetDataType = new DataTypeImpl<XMLType>(dataBinding, javaType, xmlType); - } - } - } - - public class ObjectFactoryImpl extends ObjectFactoryImplBase { - public ObjectFactoryImpl(Property property, Object propertyValue, boolean isSimpleType, Class javaType) { - super(property, propertyValue, isSimpleType, javaType); - } - - @SuppressWarnings("unchecked") - public Object getInstance() throws ObjectCreationException { - if (isSimpleType) { - return simpleTypeMapper.toJavaObject(property.getXSDType(), (String)propertyValue, null); - } else { - return null; - } - } - } - - public class ListObjectFactoryImpl extends ObjectFactoryImplBase { - public ListObjectFactoryImpl(Property property, List<?> propertyValues, boolean isSimpleType, Class javaType) { - super(property, propertyValues, isSimpleType, javaType); - } - - @SuppressWarnings("unchecked") - public List<?> getInstance() throws ObjectCreationException { - if (isSimpleType) { - List<Object> values = new ArrayList<Object>(); - for (String aValue : (List<String>)propertyValue) { - values.add(simpleTypeMapper.toJavaObject(property.getXSDType(), aValue, null)); - } - return values; - } else { - List instances = new ArrayList(); - for (Node aValue : (List<Node>)propertyValue) { - instances.add(aValue); - } - return instances; - } - } - } -} diff --git a/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/impl/OSGiImplementationImpl.java b/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/impl/OSGiImplementationImpl.java index 60b6d2e3f9..dd7da7f60e 100644 --- a/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/impl/OSGiImplementationImpl.java +++ b/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/impl/OSGiImplementationImpl.java @@ -90,14 +90,6 @@ public class OSGiImplementationImpl extends ImplementationImpl implements OSGiIm return bundleVersion; } - public String[] getImports() { - return imports; - } - - public String[] getClassList() { - return classList; - } - public FactoryExtensionPoint getModelFactories() { return modelFactories; } @@ -127,11 +119,11 @@ public class OSGiImplementationImpl extends ImplementationImpl implements OSGiIm return true; } - public Bundle getOSGiBundle() { + public Bundle getBundle() { return osgiBundle; } - public void setOSGiBundle(Bundle osgiBundle) { + public void setBundle(Bundle osgiBundle) { this.osgiBundle = osgiBundle; } diff --git a/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationActivator.java b/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationActivator.java index 6f9042d1a1..4843f0fe52 100644 --- a/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationActivator.java +++ b/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiImplementationActivator.java @@ -33,4 +33,8 @@ public class OSGiImplementationActivator implements BundleActivator { bundleContext = context; } + public static BundleContext getBundleContext() { + return bundleContext; + } + } diff --git a/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiRemotableInvoker.java b/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiRemotableInvoker.java index deb44d26fb..b74e98bcfd 100644 --- a/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiRemotableInvoker.java +++ b/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/runtime/OSGiRemotableInvoker.java @@ -26,7 +26,6 @@ import java.util.Map; import org.apache.tuscany.sca.databinding.DataBinding; import org.apache.tuscany.sca.databinding.DataBindingExtensionPoint; -import org.apache.tuscany.sca.implementation.osgi.context.OSGiAnnotations; import org.apache.tuscany.sca.interfacedef.DataType; import org.apache.tuscany.sca.interfacedef.Operation; import org.apache.tuscany.sca.invocation.DataExchangeSemantics; @@ -42,7 +41,6 @@ public class OSGiRemotableInvoker extends OSGiTargetInvoker implements DataExcha private DataBindingExtensionPoint registry; private Operation operation; - private OSGiAnnotations osgiAnnotations; /** * @param registry @@ -56,7 +54,6 @@ public class OSGiRemotableInvoker extends OSGiTargetInvoker implements DataExcha OSGiImplementationProvider provider, RuntimeComponentService service) { super(operation, provider, service); - this.osgiAnnotations = osgiAnnotations; this.registry = registry; this.operation = operation; } diff --git a/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiImplementationProcessor.java b/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiImplementationProcessor.java index a5ea66e231..46667d4abc 100644 --- a/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiImplementationProcessor.java +++ b/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiImplementationProcessor.java @@ -22,6 +22,7 @@ import static javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI; import static javax.xml.XMLConstants.XMLNS_ATTRIBUTE_NS_URI; import static javax.xml.stream.XMLStreamConstants.END_ELEMENT; import static javax.xml.stream.XMLStreamConstants.START_ELEMENT; +import static org.apache.tuscany.sca.assembly.xml.Constants.SCA11_TUSCANY_NS; import java.util.ArrayList; import java.util.Hashtable; @@ -29,7 +30,6 @@ import java.util.List; import java.util.StringTokenizer; import javax.xml.namespace.QName; -import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.stream.XMLStreamConstants; import javax.xml.stream.XMLStreamException; @@ -42,7 +42,6 @@ import org.apache.tuscany.sca.assembly.ComponentType; 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.assembly.xml.Constants; import org.apache.tuscany.sca.contribution.processor.ContributionReadException; import org.apache.tuscany.sca.contribution.processor.ContributionResolveException; import org.apache.tuscany.sca.contribution.processor.ContributionWriteException; @@ -53,6 +52,7 @@ import org.apache.tuscany.sca.core.FactoryExtensionPoint; import org.apache.tuscany.sca.databinding.impl.SimpleTypeMapperImpl; import org.apache.tuscany.sca.implementation.osgi.OSGiImplementation; import org.apache.tuscany.sca.implementation.osgi.impl.OSGiImplementationImpl; +import org.apache.tuscany.sca.implementation.osgi.runtime.OSGiImplementationActivator; import org.apache.tuscany.sca.interfacedef.Interface; import org.apache.tuscany.sca.interfacedef.InvalidInterfaceException; import org.apache.tuscany.sca.interfacedef.java.JavaInterface; @@ -63,6 +63,8 @@ import org.apache.tuscany.sca.monitor.Problem; import org.apache.tuscany.sca.monitor.Problem.Severity; import org.apache.tuscany.sca.monitor.impl.ProblemImpl; import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; +import org.osgi.framework.Version; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NamedNodeMap; @@ -78,26 +80,21 @@ import org.w3c.dom.Node; */ public class OSGiImplementationProcessor implements StAXArtifactProcessor<OSGiImplementation> { - public static final QName IMPLEMENTATION_OSGI = new QName(Constants.SCA10_TUSCANY_NS, "implementation.osgi"); + public static final QName IMPLEMENTATION_OSGI = new QName(SCA11_TUSCANY_NS, "implementation.osgi"); private static final String BUNDLE_SYMBOLICNAME = "bundleSymbolicName"; private static final String BUNDLE_VERSION = "bundleVersion"; private static final String CLASSES = "classes"; private static final String IMPORTS = "imports"; - private static final QName PROPERTIES_QNAME = new QName(Constants.SCA10_TUSCANY_NS, "properties"); - private static final QName PROPERTY_QNAME = new QName(Constants.SCA10_TUSCANY_NS, "property"); + private static final QName PROPERTIES_QNAME = new QName(SCA11_TUSCANY_NS, "properties"); + private static final QName PROPERTY_QNAME = new QName(SCA11_TUSCANY_NS, "property"); private JavaInterfaceFactory javaInterfaceFactory; private AssemblyFactory assemblyFactory; private FactoryExtensionPoint modelFactories; private Monitor monitor; - private static final DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); - static { - domFactory.setNamespaceAware(true); - } - public OSGiImplementationProcessor(FactoryExtensionPoint modelFactories, Monitor monitor) { this.monitor = monitor; this.modelFactories = modelFactories; @@ -233,12 +230,23 @@ public class OSGiImplementationProcessor implements StAXArtifactProcessor<OSGiIm impl.setUnresolved(false); - BundleReference bundleReference = - new BundleReference(impl.getBundleSymbolicName(), impl.getBundleVersion()); - BundleReference resolvedBundle = resolver.resolveModel(BundleReference.class, bundleReference); - Bundle bundle = (Bundle)resolvedBundle.getBundle(); + BundleContext bundleContext = OSGiImplementationActivator.getBundleContext(); + Bundle bundle = null; + for (Bundle b : bundleContext.getBundles()) { + String sn = b.getSymbolicName(); + String ver = (String)b.getHeaders().get(BUNDLE_VERSION); + if (!impl.getBundleSymbolicName().equals(sn)) { + continue; + } + Version v1 = Version.parseVersion(ver); + Version v2 = Version.parseVersion(impl.getBundleVersion()); + if (v1.equals(v2)) { + bundle = b; + break; + } + } if (bundle != null) { - impl.setOSGiBundle(bundle); + impl.setBundle(bundle); } else { error("CouldNotLocateOSGiBundle", impl, impl.getBundleSymbolicName()); //throw new ContributionResolveException("Could not locate OSGi bundle " + diff --git a/java/sca/modules/implementation-osgi/src/main/resources/META-INF/services/org.apache.tuscany.sca.implementation.osgi.OSGiImplementationFactory b/java/sca/modules/implementation-osgi/src/main/resources/META-INF/services/org.apache.tuscany.sca.implementation.osgi.OSGiImplementationFactory new file mode 100644 index 0000000000..6ec156ae81 --- /dev/null +++ b/java/sca/modules/implementation-osgi/src/main/resources/META-INF/services/org.apache.tuscany.sca.implementation.osgi.OSGiImplementationFactory @@ -0,0 +1,18 @@ +# 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.
+
+org.apache.tuscany.sca.implementation.osgi.DefaultOSGiImplementationFactory
diff --git a/java/sca/modules/implementation-osgi/src/test/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiReadImplTestCase.java b/java/sca/modules/implementation-osgi/src/test/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiReadImplTestCase.java index fe86c9ae95..99268bde5f 100644 --- a/java/sca/modules/implementation-osgi/src/test/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiReadImplTestCase.java +++ b/java/sca/modules/implementation-osgi/src/test/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiReadImplTestCase.java @@ -27,24 +27,20 @@ import javax.xml.stream.XMLStreamReader; import junit.framework.TestCase; -import org.apache.tuscany.sca.assembly.AssemblyFactory; import org.apache.tuscany.sca.assembly.ComponentType; import org.apache.tuscany.sca.assembly.Composite; -import org.apache.tuscany.sca.assembly.SCABindingFactory; import org.apache.tuscany.sca.assembly.builder.CompositeBuilder; -import org.apache.tuscany.sca.assembly.builder.impl.CompositeBuilderImpl; +import org.apache.tuscany.sca.assembly.builder.CompositeBuilderExtensionPoint; import org.apache.tuscany.sca.contribution.processor.DefaultStAXArtifactProcessorExtensionPoint; import org.apache.tuscany.sca.contribution.processor.ExtensibleStAXArtifactProcessor; import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessorExtensionPoint; import org.apache.tuscany.sca.contribution.resolver.ModelResolver; import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; -import org.apache.tuscany.sca.core.UtilityExtensionPoint; import org.apache.tuscany.sca.implementation.osgi.impl.OSGiImplementationImpl; import org.apache.tuscany.sca.implementation.osgi.test.OSGiTestBundles; import org.apache.tuscany.sca.implementation.osgi.test.OSGiTestImpl; import org.apache.tuscany.sca.implementation.osgi.test.OSGiTestInterface; -import org.apache.tuscany.sca.interfacedef.InterfaceContractMapper; /** * Test reading OSGi implementations. @@ -65,15 +61,7 @@ public class OSGiReadImplTestCase extends TestCase { new DefaultStAXArtifactProcessorExtensionPoint(extensionPoints); staxProcessor = new ExtensibleStAXArtifactProcessor(staxProcessors, inputFactory, null, null); - ModelFactoryExtensionPoint modelFactories = extensionPoints.getExtensionPoint(ModelFactoryExtensionPoint.class); - AssemblyFactory assemblyFactory = modelFactories.getFactory(AssemblyFactory.class); - SCABindingFactory scaBindingFactory = modelFactories.getFactory(SCABindingFactory.class); - UtilityExtensionPoint utilities = extensionPoints.getExtensionPoint(UtilityExtensionPoint.class); - InterfaceContractMapper mapper = utilities.getUtility(InterfaceContractMapper.class); - IntentAttachPointTypeFactory attachPointTypeFactory = - modelFactories.getFactory(IntentAttachPointTypeFactory.class); - compositeBuilder = - new CompositeBuilderImpl(assemblyFactory, scaBindingFactory, attachPointTypeFactory, mapper, null); + compositeBuilder = extensionPoints.getExtensionPoint(CompositeBuilderExtensionPoint.class).getCompositeBuilder("org.apache.tuscany.sca.assembly.builder.CompositeBuilder"); OSGiTestBundles.createBundle("target/test-classes/OSGiTestService.jar", OSGiTestInterface.class, @@ -87,7 +75,7 @@ public class OSGiReadImplTestCase extends TestCase { Composite composite = (Composite)staxProcessor.read(reader); assertNotNull(composite); - compositeBuilder.build(composite); + compositeBuilder.build(composite, null, null); } public void testReadAndResolveComposite() throws Exception { @@ -106,7 +94,7 @@ public class OSGiReadImplTestCase extends TestCase { staxProcessor.resolve(composite, resolver); - compositeBuilder.build(composite); + compositeBuilder.build(composite, null, null); } public void testReadOSGiImplementation() throws Exception { |