From 28185a36579c352f340b79c385d1b91870186f43 Mon Sep 17 00:00:00 2001 From: rfeng Date: Fri, 18 Sep 2009 16:43:41 +0000 Subject: Remove the need of subclassing Proxy and JXM so that Tuscany can run on Google App Engine git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@816712 13f79535-47bb-0310-9956-ffa450edef68 --- .../sca/common/java/collection/LRUCache.java | 2 +- java/sca/modules/core/META-INF/MANIFEST.MF | 1 + .../tuscany/sca/core/invocation/CachedProxy.java | 70 ---------------------- .../DefaultProxyFactoryExtensionPoint.java | 26 +++++++- .../sca/core/invocation/impl/JDKProxyFactory.java | 60 ++++++++++++++++--- .../org/apache/tuscany/sca/node/impl/NodeImpl.java | 11 ++-- 6 files changed, 84 insertions(+), 86 deletions(-) delete mode 100644 java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/CachedProxy.java (limited to 'java') diff --git a/java/sca/modules/common-java/src/main/java/org/apache/tuscany/sca/common/java/collection/LRUCache.java b/java/sca/modules/common-java/src/main/java/org/apache/tuscany/sca/common/java/collection/LRUCache.java index c9b46ca560..d3f472d463 100644 --- a/java/sca/modules/common-java/src/main/java/org/apache/tuscany/sca/common/java/collection/LRUCache.java +++ b/java/sca/modules/common-java/src/main/java/org/apache/tuscany/sca/common/java/collection/LRUCache.java @@ -34,7 +34,7 @@ public class LRUCache extends LinkedHashMap { protected int maxCacheSize = 4096; /** - * Default constructor for an LRU Cache The default capacity is 10000 + * Default constructor for an LRU Cache The default capacity is 4096 */ public LRUCache() { this(0, 4096, 0.75f, true); 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, Constructor> cache = new WeakHashMap, 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 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 > 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, Constructor> cache = new LRUCache, 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(); + } } diff --git a/java/sca/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/impl/NodeImpl.java b/java/sca/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/impl/NodeImpl.java index 92bd54a3d8..48cb5a7e07 100644 --- a/java/sca/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/impl/NodeImpl.java +++ b/java/sca/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/impl/NodeImpl.java @@ -119,8 +119,10 @@ public class NodeImpl implements Node, Client { NodeFinder.addNode(IOHelper.createURI(configuration.getDomainURI()), this); - MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer(); + // FIXME: [rfeng] We should turn the management capability into a system utility. + // In certain environment such as Google App Engine, the JMX API is not allowed try { + MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer(); mbean = new NodeManager(this); mBeanServer.registerMBean(mbean, mbean.getName()); /* @@ -130,7 +132,8 @@ public class NodeImpl implements Node, Client { JMXConnectorServer connectorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mBeanServer); connectorServer.start(); */ - } catch (Exception e) { + } catch (Throwable e) { + // Ignore the error for now mbean = null; logger.log(Level.SEVERE, e.getMessage(), e); } @@ -152,10 +155,10 @@ public class NodeImpl implements Node, Client { } if (mbean != null) { - MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer(); try { + MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer(); mBeanServer.unregisterMBean(mbean.getName()); - } catch (Exception e) { + } catch (Throwable e) { logger.log(Level.SEVERE, e.getMessage(), e); } finally { mbean = null; -- cgit v1.2.3