diff options
Diffstat (limited to 'java/sca/modules/core')
4 files changed, 76 insertions, 81 deletions
diff --git a/java/sca/modules/core/META-INF/MANIFEST.MF b/java/sca/modules/core/META-INF/MANIFEST.MF index 5b8ef14c13..a9e28df9af 100644 --- a/java/sca/modules/core/META-INF/MANIFEST.MF +++ b/java/sca/modules/core/META-INF/MANIFEST.MF @@ -52,6 +52,7 @@ Import-Package: javax.naming, org.apache.tuscany.sca.assembly;version="2.0.0",
org.apache.tuscany.sca.assembly.builder;version="2.0.0",
org.apache.tuscany.sca.assembly.impl;version="2.0.0",
+ org.apache.tuscany.sca.common.java.collection;version="2.0.0",
org.apache.tuscany.sca.context;version="2.0.0",
org.apache.tuscany.sca.contribution.processor;version="2.0.0",
org.apache.tuscany.sca.contribution.resolver;version="2.0.0",
diff --git a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/CachedProxy.java b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/CachedProxy.java deleted file mode 100644 index e2467d2e5c..0000000000 --- a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/CachedProxy.java +++ /dev/null @@ -1,70 +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.core.invocation; - -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationHandler; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Proxy; -import java.util.WeakHashMap; - -public class CachedProxy extends Proxy { - private static final long serialVersionUID = 783519311852563060L; - - protected CachedProxy(InvocationHandler handler) { - super(handler); - } - - // This is a cache containing the proxy class constructor for each business interface. - // This improves performance compared to calling Proxy.newProxyInstance() - // every time that a proxy is needed. - private final static WeakHashMap<Class<?>, Constructor<?>> cache = new WeakHashMap<Class<?>, Constructor<?>>(); - - public static Object newProxyInstance(ClassLoader classloader, Class<?> aclass[], InvocationHandler invocationhandler) - throws IllegalArgumentException { - try { - if (invocationhandler == null) - throw new NullPointerException(); - // Lookup cached constructor. aclass[0] is the reference's business interface. - Constructor<?> proxyCTOR; - synchronized (cache) { - proxyCTOR = cache.get(aclass[0]); - } - if (proxyCTOR == null) { - Class<?> proxyClass = getProxyClass(classloader, aclass); - proxyCTOR = proxyClass.getConstructor(constructorParams); - synchronized (cache) { - cache.put(aclass[0], proxyCTOR); - } - } - return proxyCTOR.newInstance(new Object[] {invocationhandler}); - } catch (NoSuchMethodException e) { - throw new InternalError(e.toString()); - } catch (IllegalAccessException e) { - throw new InternalError(e.toString()); - } catch (InstantiationException e) { - throw new InternalError(e.toString()); - } catch (InvocationTargetException e) { - throw new InternalError(e.toString()); - } - } - - private static final Class<?> constructorParams[] = {InvocationHandler.class}; - -} diff --git a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/DefaultProxyFactoryExtensionPoint.java b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/DefaultProxyFactoryExtensionPoint.java index bb212d5869..af6f039775 100644 --- a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/DefaultProxyFactoryExtensionPoint.java +++ b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/DefaultProxyFactoryExtensionPoint.java @@ -21,6 +21,7 @@ package org.apache.tuscany.sca.core.invocation; import org.apache.tuscany.sca.core.ExtensionPointRegistry; import org.apache.tuscany.sca.core.FactoryExtensionPoint; +import org.apache.tuscany.sca.core.LifeCycleListener; import org.apache.tuscany.sca.core.UtilityExtensionPoint; import org.apache.tuscany.sca.core.invocation.impl.JDKProxyFactory; import org.apache.tuscany.sca.interfacedef.InterfaceContractMapper; @@ -31,7 +32,7 @@ import org.apache.tuscany.sca.invocation.MessageFactory; * * @version $Rev$ $Date$ */ -public class DefaultProxyFactoryExtensionPoint implements ProxyFactoryExtensionPoint { +public class DefaultProxyFactoryExtensionPoint implements ProxyFactoryExtensionPoint, LifeCycleListener { private InterfaceContractMapper interfaceContractMapper; private MessageFactory messageFactory; @@ -41,10 +42,10 @@ public class DefaultProxyFactoryExtensionPoint implements ProxyFactoryExtensionP public DefaultProxyFactoryExtensionPoint(ExtensionPointRegistry extensionPoints) { UtilityExtensionPoint utilities = extensionPoints.getExtensionPoint(UtilityExtensionPoint.class); this.interfaceContractMapper = utilities.getUtility(InterfaceContractMapper.class); - + FactoryExtensionPoint modelFactories = extensionPoints.getExtensionPoint(FactoryExtensionPoint.class); this.messageFactory = modelFactories.getFactory(MessageFactory.class); - + interfaceFactory = new JDKProxyFactory(messageFactory, interfaceContractMapper); } @@ -72,4 +73,23 @@ public class DefaultProxyFactoryExtensionPoint implements ProxyFactoryExtensionP } + public void start() { + if (interfaceFactory instanceof LifeCycleListener) { + ((LifeCycleListener)interfaceFactory).start(); + } + if (classFactory instanceof LifeCycleListener) { + ((LifeCycleListener)classFactory).start(); + } + } + + public void stop() { + if (interfaceFactory instanceof LifeCycleListener) { + ((LifeCycleListener)interfaceFactory).stop(); + } + if (classFactory instanceof LifeCycleListener) { + ((LifeCycleListener)classFactory).stop(); + } + + } + } diff --git a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/impl/JDKProxyFactory.java b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/impl/JDKProxyFactory.java index ed994ce372..e0d0a40985 100644 --- a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/impl/JDKProxyFactory.java +++ b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/impl/JDKProxyFactory.java @@ -18,15 +18,17 @@ */ package org.apache.tuscany.sca.core.invocation.impl; +import java.lang.reflect.Constructor; import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Proxy; import java.security.AccessController; import java.security.PrivilegedAction; import java.util.List; -import org.apache.tuscany.sca.core.context.impl.CallableReferenceImpl; +import org.apache.tuscany.sca.common.java.collection.LRUCache; +import org.apache.tuscany.sca.core.LifeCycleListener; import org.apache.tuscany.sca.core.context.impl.CallbackServiceReferenceImpl; import org.apache.tuscany.sca.core.context.impl.ServiceReferenceImpl; -import org.apache.tuscany.sca.core.invocation.CachedProxy; import org.apache.tuscany.sca.core.invocation.ProxyCreationException; import org.apache.tuscany.sca.core.invocation.ProxyFactory; import org.apache.tuscany.sca.interfacedef.InterfaceContractMapper; @@ -34,12 +36,13 @@ import org.apache.tuscany.sca.invocation.MessageFactory; import org.apache.tuscany.sca.runtime.RuntimeWire; import org.oasisopen.sca.ServiceReference; + /** * the default implementation of a wire service that uses JDK dynamic proxies * * @version $Rev$ $Date$ */ -public class JDKProxyFactory implements ProxyFactory { +public class JDKProxyFactory implements ProxyFactory, LifeCycleListener { protected InterfaceContractMapper contractMapper; private MessageFactory messageFactory; @@ -67,7 +70,7 @@ public class JDKProxyFactory implements ProxyFactory { return interfaze.getClassLoader(); } }); - Object proxy = CachedProxy.newProxyInstance(cl, new Class[] {interfaze}, handler); + Object proxy = newProxyInstance(cl, new Class[] {interfaze}, handler); ((ServiceReferenceImpl)callableReference).setProxy(proxy); return interfaze.cast(proxy); } @@ -82,13 +85,13 @@ public class JDKProxyFactory implements ProxyFactory { Class<T> interfaze = callbackReference.getBusinessInterface(); InvocationHandler handler = new JDKCallbackInvocationHandler(messageFactory, callbackReference); ClassLoader cl = interfaze.getClassLoader(); - Object proxy = CachedProxy.newProxyInstance(cl, new Class[] {interfaze}, handler); - callbackReference.setProxy(proxy); + Object proxy = newProxyInstance(cl, new Class[] {interfaze}, handler); + callbackReference.setProxy(proxy); return interfaze.cast(proxy); } public <B, R extends ServiceReference<B>> R cast(B target) throws IllegalArgumentException { - InvocationHandler handler = CachedProxy.getInvocationHandler(target); + InvocationHandler handler = Proxy.getInvocationHandler(target); if (handler instanceof JDKInvocationHandler) { return (R)((JDKInvocationHandler)handler).getCallableReference(); } else { @@ -100,6 +103,47 @@ public class JDKProxyFactory implements ProxyFactory { * @see org.apache.tuscany.sca.core.invocation.ProxyFactory#isProxyClass(java.lang.Class) */ public boolean isProxyClass(Class<?> clazz) { - return CachedProxy.isProxyClass(clazz); + return Proxy.isProxyClass(clazz); } + + // This is a cache containing the proxy class constructor for each business interface. + // This improves performance compared to calling Proxy.newProxyInstance() + // every time that a proxy is needed. + private final LRUCache<Class<?>, Constructor<?>> cache = new LRUCache<Class<?>, Constructor<?>>(512); + + public Object newProxyInstance(ClassLoader classloader, + Class<?> interfaces[], + InvocationHandler invocationhandler) throws IllegalArgumentException { + if (interfaces.length > 1) { + // We only cache the proxy constructors with one single interface which the case in SCA where + // one reference can have one interface + return Proxy.newProxyInstance(classloader, interfaces, invocationhandler); + } + try { + if (invocationhandler == null) + throw new NullPointerException("InvocationHandler is null"); + // Lookup cached constructor. aclass[0] is the reference's business interface. + Constructor<?> proxyCTOR; + synchronized (cache) { + proxyCTOR = cache.get(interfaces[0]); + } + if (proxyCTOR == null) { + Class<?> proxyClass = Proxy.getProxyClass(classloader, interfaces); + proxyCTOR = proxyClass.getConstructor(InvocationHandler.class); + synchronized (cache) { + cache.put(interfaces[0], proxyCTOR); + } + } + return proxyCTOR.newInstance(invocationhandler); + } catch (Throwable e) { + throw new IllegalArgumentException(e); + } + } + + public void start() { + } + + public void stop() { + cache.clear(); + } } |