tomee-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject svn commit: r1208501 - in /openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb: assembler/classic/ monitoring/
Date Wed, 30 Nov 2011 16:39:23 GMT
Author: rmannibucau
Date: Wed Nov 30 16:39:22 2011
New Revision: 1208501

URL: http://svn.apache.org/viewvc?rev=1208501&view=rev
Log:
adding a MBean by persistence unit to be able to modify PU properties through JMX

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/assembler/classic/PersistenceBuilder.java
    openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/ReloadableEntityManagerFactory.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=1208501&r1=1208500&r2=1208501&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
Wed Nov 30 16:39:22 2011
@@ -22,34 +22,9 @@ import org.apache.geronimo.connector.wor
 import org.apache.geronimo.connector.work.TransactionContextHandler;
 import org.apache.geronimo.connector.work.WorkContextHandler;
 import org.apache.geronimo.transaction.manager.GeronimoTransactionManager;
-import org.apache.openejb.AppContext;
-import org.apache.openejb.BeanContext;
-import org.apache.openejb.BeanType;
-import org.apache.openejb.ClassLoaderUtil;
-import org.apache.openejb.Container;
-import org.apache.openejb.DuplicateDeploymentIdException;
-import org.apache.openejb.Injection;
-import org.apache.openejb.JndiConstants;
-import org.apache.openejb.MethodContext;
-import org.apache.openejb.NoSuchApplicationException;
-import org.apache.openejb.OpenEJB;
-import org.apache.openejb.OpenEJBException;
-import org.apache.openejb.UndeployException;
-import org.apache.openejb.cdi.CdiAppContextsService;
-import org.apache.openejb.cdi.CdiBuilder;
-import org.apache.openejb.cdi.CdiResourceInjectionService;
-import org.apache.openejb.cdi.CdiScanner;
-import org.apache.openejb.cdi.CustomELAdapter;
-import org.apache.openejb.cdi.ManagedSecurityService;
-import org.apache.openejb.cdi.OpenEJBTransactionService;
-import org.apache.openejb.cdi.OptimizedLoaderService;
-import org.apache.openejb.core.ConnectorReference;
-import org.apache.openejb.core.CoreContainerSystem;
-import org.apache.openejb.core.CoreUserTransaction;
-import org.apache.openejb.core.JndiFactory;
-import org.apache.openejb.core.SimpleTransactionSynchronizationRegistry;
-import org.apache.openejb.core.TransactionSynchronizationRegistryWrapper;
-import org.apache.openejb.core.WebContext;
+import org.apache.openejb.*;
+import org.apache.openejb.cdi.*;
+import org.apache.openejb.core.*;
 import org.apache.openejb.core.ivm.naming.IvmContext;
 import org.apache.openejb.core.ivm.naming.IvmJndiFactory;
 import org.apache.openejb.core.security.SecurityContextHandler;
@@ -57,11 +32,7 @@ import org.apache.openejb.core.timer.Ejb
 import org.apache.openejb.core.timer.NullEjbTimerServiceImpl;
 import org.apache.openejb.core.timer.ScheduleData;
 import org.apache.openejb.core.timer.TimerStore;
-import org.apache.openejb.core.transaction.JtaTransactionPolicyFactory;
-import org.apache.openejb.core.transaction.SimpleBootstrapContext;
-import org.apache.openejb.core.transaction.SimpleWorkManager;
-import org.apache.openejb.core.transaction.TransactionPolicyFactory;
-import org.apache.openejb.core.transaction.TransactionType;
+import org.apache.openejb.core.transaction.*;
 import org.apache.openejb.javaagent.Agent;
 import org.apache.openejb.loader.Options;
 import org.apache.openejb.loader.SystemInstance;
@@ -71,46 +42,21 @@ import org.apache.openejb.resource.Geron
 import org.apache.openejb.spi.ApplicationServer;
 import org.apache.openejb.spi.ContainerSystem;
 import org.apache.openejb.spi.SecurityService;
-import org.apache.openejb.util.AsmParameterNameLoader;
-import org.apache.openejb.util.ContextUtil;
-import org.apache.openejb.util.LogCategory;
-import org.apache.openejb.util.Logger;
-import org.apache.openejb.util.Messages;
-import org.apache.openejb.util.OpenEJBErrorHandler;
-import org.apache.openejb.util.References;
-import org.apache.openejb.util.SafeToolkit;
+import org.apache.openejb.util.*;
 import org.apache.openejb.util.proxy.ProxyFactory;
 import org.apache.openejb.util.proxy.ProxyManager;
 import org.apache.webbeans.config.WebBeansContext;
-import org.apache.webbeans.spi.ContextsService;
-import org.apache.webbeans.spi.LoaderService;
-import org.apache.webbeans.spi.ResourceInjectionService;
-import org.apache.webbeans.spi.ScannerService;
-import org.apache.webbeans.spi.TransactionService;
+import org.apache.webbeans.spi.*;
 import org.apache.webbeans.spi.adaptor.ELAdaptor;
 import org.apache.xbean.finder.ResourceFinder;
 import org.apache.xbean.recipe.ObjectRecipe;
 import org.apache.xbean.recipe.Option;
 import org.apache.xbean.recipe.UnsetPropertiesRecipe;
 
-import javax.management.InstanceNotFoundException;
-import javax.management.MBeanRegistrationException;
-import javax.management.MBeanServer;
-import javax.management.MalformedObjectNameException;
-import javax.management.ObjectName;
-import javax.naming.Binding;
-import javax.naming.Context;
-import javax.naming.InitialContext;
-import javax.naming.NameAlreadyBoundException;
-import javax.naming.NamingEnumeration;
-import javax.naming.NamingException;
+import javax.management.*;
+import javax.naming.*;
 import javax.persistence.EntityManagerFactory;
-import javax.resource.spi.BootstrapContext;
-import javax.resource.spi.ConnectionManager;
-import javax.resource.spi.ManagedConnectionFactory;
-import javax.resource.spi.ResourceAdapter;
-import javax.resource.spi.ResourceAdapterInternalException;
-import javax.resource.spi.XATerminator;
+import javax.resource.spi.*;
 import javax.resource.spi.work.WorkManager;
 import javax.transaction.TransactionManager;
 import javax.transaction.TransactionSynchronizationRegistry;
@@ -124,23 +70,13 @@ import java.lang.management.ManagementFa
 import java.lang.reflect.Method;
 import java.net.MalformedURLException;
 import java.net.URL;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Comparator;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 import java.util.Map.Entry;
-import java.util.Properties;
-import java.util.Set;
-import java.util.TreeMap;
 import java.util.concurrent.Executor;
 import java.util.concurrent.Executors;
 import java.util.concurrent.ThreadFactory;
 import java.util.concurrent.atomic.AtomicInteger;
+import java.lang.IllegalStateException;
 
 public class Assembler extends AssemblerTool implements org.apache.openejb.spi.Assembler,
JndiConstants {
 
@@ -603,8 +539,9 @@ public class Assembler extends Assembler
             Map<String, String> units = new HashMap<String, String>();
             PersistenceBuilder persistenceBuilder = new PersistenceBuilder(persistenceClassLoaderHandler);
             for (PersistenceUnitInfo info : appInfo.persistenceUnits) {
+                ReloadableEntityManagerFactory factory;
                 try {
-                    EntityManagerFactory factory = persistenceBuilder.createEntityManagerFactory(info,
classLoader);
+                    factory = persistenceBuilder.createEntityManagerFactory(info, classLoader);
                     containerSystem.getJNDIContext().bind(PERSISTENCE_UNIT_NAMING_CONTEXT
+ info.id, factory);
                     units.put(info.name, PERSISTENCE_UNIT_NAMING_CONTEXT + info.id);
                 } catch (NameAlreadyBoundException e) {
@@ -612,6 +549,8 @@ public class Assembler extends Assembler
                 } catch (Exception e) {
                     throw new OpenEJBException(e);
                 }
+
+                factory.register();
             }
 
             // Connectors
@@ -1179,12 +1118,14 @@ public class Assembler extends Assembler
 
         for (PersistenceUnitInfo unitInfo : appInfo.persistenceUnits) {
             try {
-                Object object = globalContext.lookup("openejb/PersistenceUnit/" + unitInfo.id);
-                globalContext.unbind("openejb/PersistenceUnit/"+unitInfo.id);
+                Object object = globalContext.lookup(PERSISTENCE_UNIT_NAMING_CONTEXT + unitInfo.id);
+                globalContext.unbind(PERSISTENCE_UNIT_NAMING_CONTEXT + unitInfo.id);
 
                 // close EMF so all resources are released
-                ((EntityManagerFactory) object).close();
+                ReloadableEntityManagerFactory remf = ((ReloadableEntityManagerFactory) object);
+                remf.close();
                 persistenceClassLoaderHandler.destroy(unitInfo.id);
+                remf.unregister();
             } catch (Throwable t) {
                 undeployException.getCauses().add(new Exception("persistence-unit: " + unitInfo.id
+ ": " + t.getMessage(), t));
             }

Modified: openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/PersistenceBuilder.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/PersistenceBuilder.java?rev=1208501&r1=1208500&r2=1208501&view=diff
==============================================================================
--- openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/PersistenceBuilder.java
(original)
+++ openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/PersistenceBuilder.java
Wed Nov 30 16:39:22 2011
@@ -46,7 +46,7 @@ public class PersistenceBuilder {
         this.persistenceClassLoaderHandler = persistenceClassLoaderHandler;
     }
 
-    public EntityManagerFactory createEntityManagerFactory(PersistenceUnitInfo info, ClassLoader
classLoader) throws Exception {
+    public ReloadableEntityManagerFactory createEntityManagerFactory(PersistenceUnitInfo
info, ClassLoader classLoader) throws Exception {
         PersistenceUnitInfoImpl unitInfo = new PersistenceUnitInfoImpl(persistenceClassLoaderHandler);
 
         // Persistence Unit Id
@@ -91,18 +91,18 @@ public class PersistenceBuilder {
 
         // Handle Properties
         unitInfo.setProperties(info.properties);
-        
+
         // Schema version of the persistence.xml file
         unitInfo.setPersistenceXMLSchemaVersion(info.persistenceXMLSchemaVersion);
-        
+
         // Second-level cache mode for the persistence unit
         SharedCacheMode sharedCacheMode = Enum.valueOf(SharedCacheMode.class, info.sharedCacheMode);
         unitInfo.setSharedCacheMode(sharedCacheMode);
-        
+
         // The validation mode to be used for the persistence unit
         ValidationMode validationMode = Enum.valueOf(ValidationMode.class, info.validationMode);
         unitInfo.setValidationMode(validationMode);
-        
+
         // Persistence Unit Transaction Type
         PersistenceUnitTransactionType type = Enum.valueOf(PersistenceUnitTransactionType.class,
info.transactionType);
         unitInfo.setTransactionType(type);

Modified: openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/ReloadableEntityManagerFactory.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/ReloadableEntityManagerFactory.java?rev=1208501&r1=1208500&r2=1208501&view=diff
==============================================================================
--- openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/ReloadableEntityManagerFactory.java
(original)
+++ openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/ReloadableEntityManagerFactory.java
Wed Nov 30 16:39:22 2011
@@ -1,15 +1,20 @@
 package org.apache.openejb.assembler.classic;
 
+import org.apache.openejb.OpenEJBException;
+import org.apache.openejb.monitoring.DynamicMBeanWrapper;
+import org.apache.openejb.monitoring.ObjectNameBuilder;
 import org.apache.openejb.persistence.PersistenceUnitInfoImpl;
 import org.apache.openejb.util.LogCategory;
 import org.apache.openejb.util.Logger;
 
+import javax.management.*;
 import javax.persistence.Cache;
 import javax.persistence.EntityManager;
 import javax.persistence.EntityManagerFactory;
 import javax.persistence.PersistenceUnitUtil;
 import javax.persistence.criteria.CriteriaBuilder;
 import javax.persistence.metamodel.Metamodel;
+import java.lang.management.ManagementFactory;
 import java.util.Map;
 import java.util.Properties;
 
@@ -22,11 +27,12 @@ public class ReloadableEntityManagerFact
     private ClassLoader classLoader;
     private EntityManagerFactory delegate;
     private EntityManagerFactoryCallable entityManagerFactoryCallable;
+    private ObjectName objectName = null;
 
     public ReloadableEntityManagerFactory(final ClassLoader cl, final EntityManagerFactory
emf, EntityManagerFactoryCallable callable) {
         classLoader = cl;
         delegate = emf;
-        this.entityManagerFactoryCallable = callable;
+        entityManagerFactoryCallable = callable;
     }
 
     @Override public EntityManager createEntityManager() {
@@ -77,6 +83,43 @@ public class ReloadableEntityManagerFact
         return delegate;
     }
 
+    public void register() throws OpenEJBException {
+        final MBeanServer server = ManagementFactory.getPlatformMBeanServer();
+        try {
+            server.registerMBean(mBeanify(), generateObjectName());
+        } catch (Exception e) {
+            throw new OpenEJBException("can't register the mbean for the entity manager factory
" + getPUname(), e);
+        }
+    }
+
+    private ObjectName generateObjectName() {
+        ObjectNameBuilder jmxName = new ObjectNameBuilder("openejb.management");
+        jmxName.set("J2EEServer", "openejb");
+        jmxName.set("ObjectType", "persistence-unit");
+        jmxName.set("PersistenceUnit", getPUname());
+        objectName = jmxName.build();
+        return objectName;
+    }
+
+    public String getPUname() {
+        return entityManagerFactoryCallable.getUnitInfo().getPersistenceUnitName();
+    }
+
+    private Object mBeanify() {
+        return new DynamicMBeanWrapper(new JMXReloadableEntityManagerFactory(this));
+    }
+
+    public void unregister() throws OpenEJBException {
+        if (objectName != null) {
+            final MBeanServer server = ManagementFactory.getPlatformMBeanServer();
+            try {
+                server.unregisterMBean(objectName);
+            } catch (Exception e) {
+                throw new OpenEJBException("can't unregister the mbean for the entity manager
factory " + getPUname(), e);
+            }
+        }
+    }
+
     // only this method is synchronized since we want to avoid locks on other methods.
     // it is just to avoid problems due to the "double click syndrom"
     //
@@ -92,11 +135,60 @@ public class ReloadableEntityManagerFact
         }
     }
 
-    public synchronized void setProperty(String key , String value) {
+    public synchronized void setProperty(String key, String value) {
         PersistenceUnitInfoImpl unitInfo = entityManagerFactoryCallable.getUnitInfo();
         if (unitInfo.getProperties() == null) {
             unitInfo.setProperties(new Properties());
         }
         unitInfo.getProperties().setProperty(key, value);
     }
+
+    public synchronized void removeProperty(String key) {
+        PersistenceUnitInfoImpl unitInfo = entityManagerFactoryCallable.getUnitInfo();
+        if (unitInfo.getProperties() != null) {
+            unitInfo.getProperties().remove(key);
+        }
+    }
+
+    public Properties getUnitProperties() {
+        PersistenceUnitInfoImpl unitInfo = entityManagerFactoryCallable.getUnitInfo();
+        if (unitInfo.getProperties() != null) {
+            return unitInfo.getProperties();
+        }
+        return new Properties();
+    }
+
+    @MBean
+    @Description("represents a persistence unit managed by OpenEJB")
+    public static class JMXReloadableEntityManagerFactory {
+        private ReloadableEntityManagerFactory reloadableEntityManagerFactory;
+
+        public JMXReloadableEntityManagerFactory(ReloadableEntityManagerFactory remf) {
+            reloadableEntityManagerFactory = remf;
+        }
+
+        @ManagedOperation
+        @Description("recreate the entity manager factory using new properties")
+        public void reload() {
+            reloadableEntityManagerFactory.reload();
+        }
+
+        @ManagedOperation
+        @Description("create or modify a property of the persistence unit")
+        public void setProperty(String key, String value) {
+            reloadableEntityManagerFactory.setProperty(key, value);
+        }
+
+        @ManagedOperation
+        @Description("remove a property of the persistence unit if it exists")
+        public void removeProperty(String key) {
+            reloadableEntityManagerFactory.removeProperty(key);
+        }
+
+        @ManagedAttribute
+        @Description("get all properties colon separated")
+        public String getProperties() {
+            return reloadableEntityManagerFactory.getUnitProperties().toString();
+        }
+    }
 }

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=1208501&r1=1208500&r2=1208501&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
Wed Nov 30 16:39:22 2011
@@ -55,22 +55,15 @@ public class DynamicMBeanWrapper impleme
     private final Map<String, Method> operations = new HashMap<String, Method>();
     private final Object instance;
 
-    public DynamicMBeanWrapper(Class<?> annotatedMBean) {
+    public DynamicMBeanWrapper(Object givenInstance) {
+        final Class<?> annotatedMBean = givenInstance.getClass();
+
         String description;
         List<MBeanAttributeInfo> attributeInfos = new ArrayList<MBeanAttributeInfo>();
         List<MBeanOperationInfo> operationInfos = new ArrayList<MBeanOperationInfo>();
         List<MBeanNotificationInfo> notificationInfos = new ArrayList<MBeanNotificationInfo>();
 
-        // instantiation
-        try {
-            instance = annotatedMBean.newInstance();
-        } catch (InstantiationException ie) {
-            logger.error("can't instantiate " + annotatedMBean.getName(), ie);
-            throw new IllegalArgumentException(annotatedMBean.getName());
-        } catch (IllegalAccessException iae) {
-            logger.error("can't instantiate " + annotatedMBean.getName(), iae);
-            throw new IllegalArgumentException(annotatedMBean.getName());
-        }
+        instance = givenInstance;
 
         // class
         Description classDescription = annotatedMBean.getAnnotation(Description.class);
@@ -95,8 +88,8 @@ public class DynamicMBeanWrapper impleme
         for (Method m : annotatedMBean.getMethods()) {
             int modifiers = m.getModifiers();
             if (m.getDeclaringClass().equals(Object.class)
-                || !Modifier.isPublic(modifiers)
-                || Modifier.isAbstract(modifiers)) {
+                    || !Modifier.isPublic(modifiers)
+                    || Modifier.isAbstract(modifiers)) {
                 continue;
             }
 
@@ -104,8 +97,8 @@ public class DynamicMBeanWrapper impleme
                 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.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);
@@ -152,11 +145,27 @@ public class DynamicMBeanWrapper impleme
         }
 
         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()]));
+                description,
+                attributeInfos.toArray(new MBeanAttributeInfo[attributeInfos.size()]),
+                null, // default constructor is mandatory
+                operationInfos.toArray(new MBeanOperationInfo[operationInfos.size()]),
+                notificationInfos.toArray(new MBeanNotificationInfo[notificationInfos.size()]));
+    }
+
+    public DynamicMBeanWrapper(Class<?> annotatedMBean) {
+        this(instantiate(annotatedMBean));
+    }
+
+    private static Object instantiate(Class<?> annotatedMBean) {
+        try {
+            return annotatedMBean.newInstance();
+        } catch (InstantiationException ie) {
+            logger.error("can't instantiate " + annotatedMBean.getName(), ie);
+            throw new IllegalArgumentException(annotatedMBean.getName());
+        } catch (IllegalAccessException iae) {
+            logger.error("can't instantiate " + annotatedMBean.getName(), iae);
+            throw new IllegalArgumentException(annotatedMBean.getName());
+        }
     }
 
     private MBeanNotificationInfo getNotificationInfo(NotificationInfo n) {



Mime
View raw message