You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@aries.apache.org by gn...@apache.org on 2018/01/31 20:10:22 UTC

svn commit: r1822827 - in /aries/trunk/blueprint/blueprint-core/src: main/java/org/apache/aries/blueprint/container/ main/java/org/apache/aries/blueprint/ext/impl/ main/resources/org/apache/aries/blueprint/ext/impl/ test/java/org/apache/aries/blueprint...

Author: gnodet
Date: Wed Jan 31 20:10:21 2018
New Revision: 1822827

URL: http://svn.apache.org/viewvc?rev=1822827&view=rev
Log:
[ARIES-1129] Implement a null proxy element

Added:
    aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/NullProxy.java
    aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/NullProxyTest.java
    aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/pojos/PojoC.java
    aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/pojos/Service.java
    aries/trunk/blueprint/blueprint-core/src/test/resources/test-null-proxy.xml
Modified:
    aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/ReferenceRecipe.java
    aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/ext/impl/ExtNamespaceHandler.java
    aries/trunk/blueprint/blueprint-core/src/main/resources/org/apache/aries/blueprint/ext/impl/blueprint-ext-1.6.0.xsd
    aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/TestBlueprintContainer.java

Added: aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/NullProxy.java
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/NullProxy.java?rev=1822827&view=auto
==============================================================================
--- aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/NullProxy.java (added)
+++ aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/NullProxy.java Wed Jan 31 20:10:21 2018
@@ -0,0 +1,78 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.aries.blueprint.container;
+
+import org.apache.aries.blueprint.container.AggregateConverter.Convertible;
+import org.apache.aries.blueprint.services.ExtendedBlueprintContainer;
+import org.osgi.service.blueprint.container.ReifiedType;
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+public class NullProxy implements Convertible {
+
+    private static final Map<Class<?>, Object> returns;
+
+    static {
+		Map<Class<?>, Object> tmp = new HashMap<Class<?>, Object>();
+
+		tmp.put(boolean.class, false);
+		tmp.put(byte.class, Byte.valueOf("0"));
+		tmp.put(short.class, Short.valueOf("0"));
+		tmp.put(char.class, Character.valueOf((char)0));
+		tmp.put(int.class, Integer.valueOf("0"));
+		tmp.put(float.class, Float.valueOf("0"));
+		tmp.put(long.class, Long.valueOf("0"));
+		tmp.put(Double.class, Double.valueOf("0"));
+
+		returns = Collections.unmodifiableMap(tmp);
+	}
+
+    private final ExtendedBlueprintContainer container;
+
+    private final InvocationHandler nullProxyHandler = new InvocationHandler() {
+        @Override
+        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
+            if ("toString".equals(method.getName()) && (args == null || args.length == 0)) {
+                return NullProxy.this.toString();
+            } else {
+                return returns.get(method.getReturnType());
+            }
+        }
+    };
+
+    public NullProxy(ExtendedBlueprintContainer container) {
+        this.container = container;
+    }
+
+    @Override
+    public Object convert(ReifiedType type) {
+        ClassLoader cl = container.getClassLoader();
+        return Proxy.newProxyInstance(cl, new Class<?>[] { type.getRawClass() }, nullProxyHandler);
+    }
+
+    @Override
+    public String toString() {
+        return "Aries Blueprint Null Proxy";
+    }
+}

Modified: aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/ReferenceRecipe.java
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/ReferenceRecipe.java?rev=1822827&r1=1822826&r2=1822827&view=diff
==============================================================================
--- aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/ReferenceRecipe.java (original)
+++ aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/ReferenceRecipe.java Wed Jan 31 20:10:21 2018
@@ -29,6 +29,7 @@ import java.util.concurrent.Callable;
 
 import org.apache.aries.blueprint.ExtendedReferenceMetadata;
 import org.apache.aries.blueprint.di.CollectionRecipe;
+import org.apache.aries.blueprint.di.ExecutionContext;
 import org.apache.aries.blueprint.di.Recipe;
 import org.apache.aries.blueprint.di.ValueRecipe;
 import org.apache.aries.blueprint.services.ExtendedBlueprintContainer;
@@ -250,7 +251,16 @@ public class ReferenceRecipe extends Abs
                         } else {
                             failed = false;
                         }
-                        result = defaultBean;
+                        BlueprintRepository repository = ((BlueprintContainerImpl) blueprintContainer).getRepository();
+                        ExecutionContext oldContext = null;
+                        try {
+                            oldContext = ExecutionContext.Holder.setContext(repository);
+                            result = convert(defaultBean, new GenericType(getInterfaceClass()));
+                        } catch (Exception e) {
+                            throw new IllegalStateException("Wrong type for defaultBean", e);
+                        } finally {
+                            ExecutionContext.Holder.setContext(oldContext);
+                        }
                     }
 
                     if (failed) {

Modified: aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/ext/impl/ExtNamespaceHandler.java
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/ext/impl/ExtNamespaceHandler.java?rev=1822827&r1=1822826&r2=1822827&view=diff
==============================================================================
--- aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/ext/impl/ExtNamespaceHandler.java (original)
+++ aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/ext/impl/ExtNamespaceHandler.java Wed Jan 31 20:10:21 2018
@@ -21,10 +21,10 @@ package org.apache.aries.blueprint.ext.i
 import java.net.URL;
 import java.util.*;
 
-import org.apache.aries.blueprint.ExtendedBeanMetadata;
 import org.apache.aries.blueprint.ExtendedReferenceListMetadata;
 import org.apache.aries.blueprint.ExtendedReferenceMetadata;
 import org.apache.aries.blueprint.ParserContext;
+import org.apache.aries.blueprint.container.NullProxy;
 import org.apache.aries.blueprint.ext.PlaceholdersUtils;
 import org.apache.aries.blueprint.ext.PropertyPlaceholder;
 import org.apache.aries.blueprint.ext.evaluator.PropertyEvaluator;
@@ -118,6 +118,8 @@ public class ExtNamespaceHandler impleme
 
     public static final String RAW_CONVERSION_ATTRIBUTE = "raw-conversion";
 
+    public static final String NULL_PROXY_ELEMENT = "null-proxy";
+
     private static final Set<String> EXT_URIS = Collections.unmodifiableSet(new LinkedHashSet<String>(Arrays.asList(
             BLUEPRINT_EXT_NAMESPACE_V1_0,
             BLUEPRINT_EXT_NAMESPACE_V1_1,
@@ -166,6 +168,8 @@ public class ExtNamespaceHandler impleme
         } else if (nodeNameEquals(element, REFERENCE)) {
             RefMetadata rd = context.parseElement(RefMetadata.class, context.getEnclosingComponent(), element);
             return createReference(context, rd.getComponentId());
+        } else if (nodeNameEquals(element, NULL_PROXY_ELEMENT)) {
+            return parseNullProxy(element, context);
         } else {
             throw new ComponentDefinitionException("Unsupported element: " + element.getNodeName());
         }
@@ -201,7 +205,15 @@ public class ExtNamespaceHandler impleme
             throw new ComponentDefinitionException("Unsupported node: " + node.getNodeName());
         }
     }
-    
+
+    private ComponentMetadata parseNullProxy(Element element, ParserContext context) {
+        MutableBeanMetadata mb = context.createMetadata(MutableBeanMetadata.class);
+        mb.setRuntimeClass(NullProxy.class);
+        mb.addArgument(createRef(context, "blueprintContainer"), null, -1);
+        mb.setId(element.hasAttribute(ID_ATTRIBUTE) ? element.getAttribute(ID_ATTRIBUTE) : "null-proxy");
+        return mb;
+    }
+
     private ComponentMetadata decorateAdditionalInterfaces(Node node, ComponentMetadata component,
                                                            ParserContext context) {
         if (!(component instanceof MutableReferenceMetadata)) {

Modified: aries/trunk/blueprint/blueprint-core/src/main/resources/org/apache/aries/blueprint/ext/impl/blueprint-ext-1.6.0.xsd
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-core/src/main/resources/org/apache/aries/blueprint/ext/impl/blueprint-ext-1.6.0.xsd?rev=1822827&r1=1822826&r2=1822827&view=diff
==============================================================================
--- aries/trunk/blueprint/blueprint-core/src/main/resources/org/apache/aries/blueprint/ext/impl/blueprint-ext-1.6.0.xsd (original)
+++ aries/trunk/blueprint/blueprint-core/src/main/resources/org/apache/aries/blueprint/ext/impl/blueprint-ext-1.6.0.xsd Wed Jan 31 20:10:21 2018
@@ -149,5 +149,11 @@
             </xsd:restriction>
         </xsd:simpleType>
     </xsd:attribute>
+    
+    <xsd:element name="null-proxy">
+        <xsd:complexType>
+            <xsd:attribute name="id" type="xsd:ID" />
+        </xsd:complexType>
+    </xsd:element>
 
 </xsd:schema>

Added: aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/NullProxyTest.java
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/NullProxyTest.java?rev=1822827&view=auto
==============================================================================
--- aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/NullProxyTest.java (added)
+++ aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/NullProxyTest.java Wed Jan 31 20:10:21 2018
@@ -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.aries.blueprint;
+
+import org.apache.aries.blueprint.container.SatisfiableRecipe;
+import org.apache.aries.blueprint.di.Repository;
+import org.apache.aries.blueprint.parser.ComponentDefinitionRegistryImpl;
+import org.apache.aries.blueprint.pojos.PojoC;
+import org.apache.aries.blueprint.pojos.Service;
+import org.apache.aries.proxy.ProxyManager;
+import org.apache.aries.proxy.impl.JdkProxyManager;
+
+public class NullProxyTest extends AbstractBlueprintTest {
+
+    public void testNullProxy() throws Exception {
+        ComponentDefinitionRegistryImpl registry = parse("/test-null-proxy.xml");
+        ProxyManager proxyManager = new JdkProxyManager();
+        Repository repository = new TestBlueprintContainer(registry, proxyManager).getRepository();
+        ((SatisfiableRecipe) repository.getRecipe("refDefNullProxy")).start(new SatisfiableRecipe.SatisfactionListener() {
+            @Override
+            public void notifySatisfaction(SatisfiableRecipe satisfiable) {
+
+            }
+        });
+        PojoC pojoC = (PojoC) repository.create("pojoC");
+        assertTrue(pojoC.getService() instanceof Service);
+        assertEquals(0, pojoC.getService().getInt());
+        assertNull(pojoC.getService().getPojoA());
+
+    }
+
+    static class ProxyGenerationException extends RuntimeException {
+    }
+    
+}

Modified: aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/TestBlueprintContainer.java
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/TestBlueprintContainer.java?rev=1822827&r1=1822826&r2=1822827&view=diff
==============================================================================
--- aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/TestBlueprintContainer.java (original)
+++ aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/TestBlueprintContainer.java Wed Jan 31 20:10:21 2018
@@ -64,4 +64,8 @@ public class TestBlueprintContainer exte
         return Thread.currentThread().getContextClassLoader().loadClass(name);
     }
 
+    @Override
+    public ClassLoader getClassLoader() {
+        return Thread.currentThread().getContextClassLoader();
+    }
 }

Added: aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/pojos/PojoC.java
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/pojos/PojoC.java?rev=1822827&view=auto
==============================================================================
--- aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/pojos/PojoC.java (added)
+++ aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/pojos/PojoC.java Wed Jan 31 20:10:21 2018
@@ -0,0 +1,32 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.aries.blueprint.pojos;
+
+public class PojoC {
+
+    private final Service service;
+
+    public PojoC(Service service) {
+        this.service = service;
+    }
+
+    public Service getService() {
+        return service;
+    }
+}

Added: aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/pojos/Service.java
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/pojos/Service.java?rev=1822827&view=auto
==============================================================================
--- aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/pojos/Service.java (added)
+++ aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/pojos/Service.java Wed Jan 31 20:10:21 2018
@@ -0,0 +1,27 @@
+/*
+ * 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.aries.blueprint.pojos;
+
+public interface Service {
+
+    int getInt();
+
+    PojoA getPojoA();
+
+}

Added: aries/trunk/blueprint/blueprint-core/src/test/resources/test-null-proxy.xml
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-core/src/test/resources/test-null-proxy.xml?rev=1822827&view=auto
==============================================================================
--- aries/trunk/blueprint/blueprint-core/src/test/resources/test-null-proxy.xml (added)
+++ aries/trunk/blueprint/blueprint-core/src/test/resources/test-null-proxy.xml Wed Jan 31 20:10:21 2018
@@ -0,0 +1,35 @@
+<?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.
+-->
+<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
+           xmlns:ext="http://aries.apache.org/blueprint/xmlns/blueprint-ext/v1.6.0">
+
+    <reference id="refItf" interface="org.apache.aries.blueprint.pojos.InterfaceA"/>
+
+    <reference id="refClsErr" interface="org.apache.aries.blueprint.pojos.PojoA"/>
+
+    <reference id="refClsOk" interface="org.apache.aries.blueprint.pojos.PojoA" ext:proxy-method="classes" />
+
+    <bean id="pojoC" class="org.apache.aries.blueprint.pojos.PojoC">
+        <argument ref="refDefNullProxy" />
+    </bean>
+    <reference id="refDefNullProxy" interface="org.apache.aries.blueprint.pojos.Service"
+               ext:default="null-proxy" availability="optional" />
+
+    <ext:null-proxy/>
+
+</blueprint>
\ No newline at end of file