Author: rmannibucau
Date: Thu Jan 10 00:01:22 2013
New Revision: 1431159

URL: http://svn.apache.org/viewvc?rev=1431159&view=rev
Log:
TOMEE-719 cache internal mbean metadatas

Modified:
    
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/Assembler.java
    
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/monitoring/DynamicMBeanWrapper.java

Modified: 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/Assembler.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/Assembler.java?rev=1431159&r1=1431158&r2=1431159&view=diff
==============================================================================
--- 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/Assembler.java
 (original)
+++ 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/Assembler.java
 Thu Jan 10 00:01:22 2013
@@ -124,6 +124,8 @@ import org.apache.xbean.recipe.ObjectRec
 import org.apache.xbean.recipe.Option;
 import org.apache.xbean.recipe.UnsetPropertiesRecipe;
 
+import javax.enterprise.context.Dependent;
+import javax.enterprise.context.spi.CreationalContext;
 import javax.enterprise.inject.spi.Bean;
 import javax.enterprise.inject.spi.BeanManager;
 import javax.management.InstanceNotFoundException;
@@ -794,16 +796,16 @@ public class Assembler extends Assembler
 
             // deploy MBeans
             for (final String mbean : appInfo.mbeans) {
-                deployMBean(appContext.getBeanManager(), classLoader, mbean, 
appInfo.jmx, appInfo.appId);
+                deployMBean(appContext.getWebBeansContext(), classLoader, 
mbean, appInfo.jmx, appInfo.appId);
             }
             for (final EjbJarInfo ejbJarInfo : appInfo.ejbJars) {
                 for (final String mbean : ejbJarInfo.mbeans) {
-                    deployMBean(appContext.getBeanManager(), classLoader, 
mbean, appInfo.jmx, ejbJarInfo.moduleName);
+                    deployMBean(appContext.getWebBeansContext(), classLoader, 
mbean, appInfo.jmx, ejbJarInfo.moduleName);
                 }
             }
             for (final ConnectorInfo connectorInfo : appInfo.connectors) {
                 for (final String mbean : connectorInfo.mbeans) {
-                    deployMBean(appContext.getBeanManager(), classLoader, 
mbean, appInfo.jmx, appInfo.appId + ".add-lib");
+                    deployMBean(appContext.getWebBeansContext(), classLoader, 
mbean, appInfo.jmx, appInfo.appId + ".add-lib");
                 }
             }
 
@@ -981,13 +983,14 @@ public class Assembler extends Assembler
     }
 
     @SuppressWarnings("unchecked")
-    private static void deployMBean(final BeanManager bm, final ClassLoader 
cl, final String mbeanClass, final Properties appMbeans, final String id) {
+    private static void deployMBean(final WebBeansContext wc, final 
ClassLoader cl, final String mbeanClass, final Properties appMbeans, final 
String id) {
         final Class<?> clazz;
         try {
             clazz = cl.loadClass(mbeanClass);
         } catch (ClassNotFoundException e) {
             throw new OpenEJBRuntimeException(e);
         }
+        final BeanManager bm = wc.getBeanManagerImpl();
         final Set<Bean<?>> beans = bm.getBeans(clazz);
         final Bean bean = bm.resolve(beans);
         final Object instance;
@@ -1002,7 +1005,11 @@ public class Assembler extends Assembler
                 return;
             }
         } else {
-            instance = bm.getReference(bean, clazz, 
bm.createCreationalContext(bean));
+            final CreationalContext creationalContext = 
bm.createCreationalContext(bean);
+            instance = bm.getReference(bean, clazz, creationalContext);
+            if (Dependent.class.equals(bean.getScope())) {
+                creationalContext.release();
+            }
         }
 
         if (LocalMBeanServer.isJMXActive()) {
@@ -1014,7 +1021,7 @@ public class Assembler extends Assembler
                         .set("name", clazz.getSimpleName())
                         .build();
 
-                server.registerMBean(new DynamicMBeanWrapper(instance), leaf);
+                server.registerMBean(new DynamicMBeanWrapper(wc, instance), 
leaf);
                 appMbeans.put(mbeanClass, leaf.getCanonicalName());
                 logger.info("Deployed MBean(" + leaf.getCanonicalName() + ")");
             } catch (Exception e) {

Modified: 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/monitoring/DynamicMBeanWrapper.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/monitoring/DynamicMBeanWrapper.java?rev=1431159&r1=1431158&r2=1431159&view=diff
==============================================================================
--- 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/monitoring/DynamicMBeanWrapper.java
 (original)
+++ 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/monitoring/DynamicMBeanWrapper.java
 Thu Jan 10 00:01:22 2013
@@ -16,7 +16,6 @@
  */
 package org.apache.openejb.monitoring;
 
-import javassist.util.proxy.ProxyFactory;
 import org.apache.openejb.api.jmx.Description;
 import org.apache.openejb.api.jmx.MBean;
 import org.apache.openejb.api.jmx.ManagedAttribute;
@@ -25,6 +24,8 @@ import org.apache.openejb.api.jmx.Notifi
 import org.apache.openejb.api.jmx.NotificationInfos;
 import org.apache.openejb.util.LogCategory;
 import org.apache.openejb.util.Logger;
+import org.apache.webbeans.config.WebBeansContext;
+import org.apache.webbeans.proxy.ProxyFactory;
 
 import javax.management.Attribute;
 import javax.management.AttributeList;
@@ -40,7 +41,6 @@ import javax.management.MBeanNotificatio
 import javax.management.MBeanOperationInfo;
 import javax.management.ReflectionException;
 import java.lang.annotation.Annotation;
-import java.lang.reflect.Field;
 import java.lang.reflect.InvocationHandler;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
@@ -55,6 +55,8 @@ import java.util.ResourceBundle;
 public class DynamicMBeanWrapper implements DynamicMBean {
     public static final Logger logger = 
Logger.getInstance(LogCategory.OPENEJB_DEPLOY, DynamicMBeanWrapper.class);
 
+    private static final Map<Class<?>, CacheInfo> CACHE = new 
HashMap<Class<?>, CacheInfo>();
+
     private static final Map<Class<?>, Class<? extends Annotation>> 
OPENEJB_API_TO_JAVAX = new HashMap<Class<?>, Class<? extends Annotation>>();
     static {
         final ClassLoader loader = DynamicMBeanWrapper.class.getClassLoader();
@@ -79,108 +81,148 @@ public class DynamicMBeanWrapper impleme
     private final Object instance;
     private final ClassLoader classloader;
 
-    public DynamicMBeanWrapper(Object givenInstance) {
+    public DynamicMBeanWrapper(final Object givenInstance) {
+        this(null, givenInstance);
+    }
+
+    public DynamicMBeanWrapper(final WebBeansContext wc, final Object 
givenInstance) {
         Class<?> annotatedMBean = givenInstance.getClass();
 
         // javaassist looses annotation so simply unwrap it
-        while (ProxyFactory.isProxyClass(annotatedMBean)) {
-            annotatedMBean = annotatedMBean.getSuperclass();
+        if (wc != null) {
+            final ProxyFactory pf = wc.getProxyFactory();
+            if (pf.isProxyInstance(givenInstance)) {
+                annotatedMBean = annotatedMBean.getSuperclass();
+            }
         }
 
         classloader = annotatedMBean.getClassLoader();
-
-        String description;
-        List<MBeanAttributeInfo> attributeInfos = new 
ArrayList<MBeanAttributeInfo>();
-        List<MBeanOperationInfo> operationInfos = new 
ArrayList<MBeanOperationInfo>();
-        List<MBeanNotificationInfo> notificationInfos = new 
ArrayList<MBeanNotificationInfo>();
-
         instance = givenInstance;
 
-        // class
-        Description classDescription = findAnnotation(annotatedMBean, 
Description.class);
-        description = getDescription(classDescription, "a MBean built by 
OpenEJB");
-
-        NotificationInfo notification = findAnnotation(annotatedMBean, 
NotificationInfo.class);
-        if (notification != null) {
-            MBeanNotificationInfo notificationInfo = 
getNotificationInfo(notification);
-            notificationInfos.add(notificationInfo);
-        }
-
-        NotificationInfos notifications = findAnnotation(annotatedMBean, 
NotificationInfos.class);
-        if (notifications != null && notifications.value() != null) {
-            for (NotificationInfo n : notifications.value()) {
-                MBeanNotificationInfo notificationInfo = 
getNotificationInfo(n);
+        CacheInfo cache = CACHE.get(annotatedMBean);
+        if (cache == null) {
+            String description;
+            List<MBeanAttributeInfo> attributeInfos = new 
ArrayList<MBeanAttributeInfo>();
+            List<MBeanOperationInfo> operationInfos = new 
ArrayList<MBeanOperationInfo>();
+            List<MBeanNotificationInfo> notificationInfos = new 
ArrayList<MBeanNotificationInfo>();
+
+            // class
+            Description classDescription = findAnnotation(annotatedMBean, 
Description.class);
+            description = getDescription(classDescription, "a MBean built by 
OpenEJB");
+
+            NotificationInfo notification = findAnnotation(annotatedMBean, 
NotificationInfo.class);
+            if (notification != null) {
+                MBeanNotificationInfo notificationInfo = 
getNotificationInfo(notification);
                 notificationInfos.add(notificationInfo);
             }
-        }
 
+            NotificationInfos notifications = findAnnotation(annotatedMBean, 
NotificationInfos.class);
+            if (notifications != null && notifications.value() != null) {
+                for (NotificationInfo n : notifications.value()) {
+                    MBeanNotificationInfo notificationInfo = 
getNotificationInfo(n);
+                    notificationInfos.add(notificationInfo);
+                }
+            }
+
+
+            // methods
+            for (Method m : annotatedMBean.getMethods()) {
+                int modifiers = m.getModifiers();
+                if (m.getDeclaringClass().equals(Object.class)
+                        || !Modifier.isPublic(modifiers)
+                        || Modifier.isAbstract(modifiers)) {
+                    continue;
+                }
 
-        // methods
-        for (Method m : annotatedMBean.getMethods()) {
-            int modifiers = m.getModifiers();
-            if (m.getDeclaringClass().equals(Object.class)
-                    || !Modifier.isPublic(modifiers)
-                    || Modifier.isAbstract(modifiers)) {
-                continue;
-            }
-
-            if (findAnnotation(m, ManagedAttribute.class) != null) {
-                String methodName = m.getName();
-                String attrName = methodName;
-                if (((attrName.startsWith("get") && 
m.getParameterTypes().length == 0)
-                        || (attrName.startsWith("set") && 
m.getParameterTypes().length == 1))
-                        && attrName.length() > 3) {
-                    attrName = attrName.substring(3);
-                    if (attrName.length() > 1) {
-                        attrName = Character.toLowerCase(attrName.charAt(0)) + 
attrName.substring(1);
+                if (findAnnotation(m, ManagedAttribute.class) != null) {
+                    String methodName = m.getName();
+                    String attrName = methodName;
+                    if (((attrName.startsWith("get") && 
m.getParameterTypes().length == 0)
+                            || (attrName.startsWith("set") && 
m.getParameterTypes().length == 1))
+                            && attrName.length() > 3) {
+                        attrName = attrName.substring(3);
+                        if (attrName.length() > 1) {
+                            attrName = 
Character.toLowerCase(attrName.charAt(0)) + attrName.substring(1);
+                        } else {
+                            attrName = attrName.toLowerCase();
+                        }
                     } else {
-                        attrName = attrName.toLowerCase();
+                        logger.warning("ignoring attribute " + m.getName() + " 
for " + annotatedMBean.getName());
                     }
-                } else {
-                    logger.warning("ignoring attribute " + m.getName() + " for 
" + annotatedMBean.getName());
-                }
 
-                if (methodName.startsWith("get")) {
-                    getters.put(attrName, m);
-                } else if (methodName.startsWith("set")) {
-                    setters.put(attrName, m);
+                    if (methodName.startsWith("get")) {
+                        getters.put(attrName, m);
+                    } else if (methodName.startsWith("set")) {
+                        setters.put(attrName, m);
+                    }
+                } else if (findAnnotation(m, ManagedOperation.class) != null) {
+                    operations.put(m.getName(), m);
+
+                    String operationDescr = "";
+                    Description descr = findAnnotation(m, Description.class);
+                    if (descr != null) {
+                        operationDescr = getDescription(descr, "-");
+                    }
+
+                    operationInfos.add(new MBeanOperationInfo(operationDescr, 
m));
                 }
-            } else if (findAnnotation(m, ManagedOperation.class) != null) {
-                operations.put(m.getName(), m);
+            }
 
-                String operationDescr = "";
-                Description descr = findAnnotation(m, Description.class);
+            for (Map.Entry<String, Method> e : getters.entrySet()) {
+                String key = e.getKey();
+                Method mtd = e.getValue();
+
+                String attrDescr = "";
+                Description descr = findAnnotation(mtd, Description.class);
                 if (descr != null) {
-                    operationDescr = getDescription(descr, "-");
+                    attrDescr = getDescription(descr, "-");
                 }
 
-                operationInfos.add(new MBeanOperationInfo(operationDescr, m));
+                try {
+                    attributeInfos.add(new MBeanAttributeInfo(key, attrDescr, 
mtd, setters.get(key)));
+                } catch (IntrospectionException ex) {
+                    logger.warning("can't manage " + key + " for " + 
mtd.getName(), ex);
+                }
             }
-        }
 
-        for (Map.Entry<String, Method> e : getters.entrySet()) {
-            String key = e.getKey();
-            Method mtd = e.getValue();
+            // for updatable but not readable attributes
+            for (Map.Entry<String, Method> e : setters.entrySet()) {
+                String key = e.getKey();
+                if (getters.get(key) != null) {
+                    continue; //already done
+                }
+
+                Method mtd = e.getValue();
 
-            String attrDescr = "";
-            Description descr = findAnnotation(mtd, Description.class);
-            if (descr != null) {
-                attrDescr = getDescription(descr, "-");
+                String attrDescr = "";
+                Description descr = findAnnotation(mtd, Description.class);
+                if (descr != null) {
+                    attrDescr = getDescription(descr, "-");
+                }
+
+                try {
+                    attributeInfos.add(new MBeanAttributeInfo(key, attrDescr, 
null, setters.get(key)));
+                } catch (IntrospectionException ex) {
+                    logger.warning("can't manage " + key + " for " + 
mtd.getName(), ex);
+                }
             }
 
-            try {
-                attributeInfos.add(new MBeanAttributeInfo(key, attrDescr, mtd, 
setters.get(key)));
-            } catch (IntrospectionException ex) {
-                logger.warning("can't manage " + key + " for " + 
mtd.getName(), ex);
+            info = new MBeanInfo(annotatedMBean.getName(),
+                    description,
+                    attributeInfos.toArray(new 
MBeanAttributeInfo[attributeInfos.size()]),
+                    null, // default constructor is mandatory
+                    operationInfos.toArray(new 
MBeanOperationInfo[operationInfos.size()]),
+                    notificationInfos.toArray(new 
MBeanNotificationInfo[notificationInfos.size()]));
+
+            if (annotatedMBean.getName().startsWith("org.apache.openejb") && 
classloader == DynamicMBeanWrapper.class.getClassLoader()) {
+                CACHE.put(annotatedMBean, new CacheInfo(info, getters, 
setters, operations));
             }
+        } else {
+            info = cache.mBeanInfo;
+            getters.putAll(cache.getters);
+            setters.putAll(cache.setters);
+            operations.putAll(cache.operations);
         }
-
-        info = new MBeanInfo(annotatedMBean.getName(),
-                description,
-                attributeInfos.toArray(new 
MBeanAttributeInfo[attributeInfos.size()]),
-                null, // default constructor is mandatory
-                operationInfos.toArray(new 
MBeanOperationInfo[operationInfos.size()]),
-                notificationInfos.toArray(new 
MBeanNotificationInfo[notificationInfos.size()]));
     }
 
     private <T extends Annotation> T findAnnotation(final Method method, final 
Class<T> searchedAnnotation) {
@@ -199,22 +241,6 @@ public class DynamicMBeanWrapper impleme
         return null;
     }
 
-    private <T extends Annotation> T findAnnotation(final Field field, final 
Class<T> searchedAnnotation) {
-        final T annotation = field.getAnnotation(searchedAnnotation);
-        if (annotation != null) {
-            return annotation;
-        }
-
-        if (OPENEJB_API_TO_JAVAX.containsKey(searchedAnnotation)) {
-            final Class<? extends Annotation> clazz = 
OPENEJB_API_TO_JAVAX.get(searchedAnnotation);
-            final Object javaxAnnotation = field.getAnnotation(clazz);
-            if (javaxAnnotation != null) {
-                return annotationProxy(javaxAnnotation, searchedAnnotation);
-            }
-        }
-        return null;
-    }
-
     private <T extends Annotation> T findAnnotation(final Class<?> 
annotatedMBean, final Class<T> searchedAnnotation) {
         final T annotation = annotatedMBean.getAnnotation(searchedAnnotation);
         if (annotation != null) {
@@ -394,4 +420,20 @@ public class DynamicMBeanWrapper impleme
             return result;
         }
     }
+
+    private static class CacheInfo {
+        public final MBeanInfo mBeanInfo;
+        public final Map<String, Method> getters;
+        public final Map<String, Method> setters;
+        public final Map<String, Method> operations;
+
+        private CacheInfo(final MBeanInfo mBeanInfo,
+                          final Map<String, Method> getters, final Map<String, 
Method> setters,
+                          final Map<String, Method> operations) {
+            this.mBeanInfo = mBeanInfo;
+            this.getters = getters;
+            this.setters = setters;
+            this.operations = operations;
+        }
+    }
 }


Reply via email to