Revision: 7328
Author: emmartins
Date: Wed Aug 5 16:09:33 2009
Log: adding persistence to profiles on 2.x
http://code.google.com/p/mobicents/source/detail?r=7328
Added:
/trunk/servers/jain-slee/core/jar/src/main/java/org/mobicents/slee/container/deployment/profile/jpa/JPAProfileTable.java
/trunk/servers/jain-slee/core/jar/src/main/java/org/mobicents/slee/container/deployment/profile/jpa/JPAProfileTableFramework.java
Modified:
/trunk/servers/jain-slee/core/jar/src/main/java/org/mobicents/slee/container/deployment/profile/jpa/JPAProfileEntityFramework.java
/trunk/servers/jain-slee/core/jar/src/main/java/org/mobicents/slee/container/management/SleeProfileTableManager.java
/trunk/servers/jain-slee/core/jar/src/main/java/org/mobicents/slee/container/management/jmx/MobicentsManagement.java
/trunk/servers/jain-slee/core/jar/src/main/java/org/mobicents/slee/container/management/jmx/MobicentsManagementMBean.java
/trunk/servers/jain-slee/core/sar/src/main/resources/META-INF/jboss-beans.xml
=======================================
--- /dev/null
+++
/trunk/servers/jain-slee/core/jar/src/main/java/org/mobicents/slee/container/deployment/profile/jpa/JPAProfileTable.java
Wed Aug 5 16:09:33 2009
@@ -0,0 +1,54 @@
+package org.mobicents.slee.container.deployment.profile.jpa;
+
+import java.io.Serializable;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.Table;
+
+import org.mobicents.slee.container.profile.ProfileTableImpl;
+
+/**
+ *
+ * JPAProfileTable.java
+ *
+ * @author <a href="mailto:[email protected]"> Alexandre Mendonca </a>
+ */
+...@entity
+...@table(name="JAINSLEEPROFILETABLES")
+public class JPAProfileTable implements Serializable {
+
+ private static final long serialVersionUID = 5170225158725752562L;
+
+ @Id
+ private String profileTableName;
+ private String profileSpecId;
+
+ private JPAProfileTable() {
+ }
+
+ public JPAProfileTable(ProfileTableImpl profileTable) {
+ this();
+ this.profileTableName = profileTable.getProfileTableName();
+ this.profileSpecId =
profileTable.getProfileSpecificationComponent().getProfileSpecificationID().toString();
+ }
+
+ /**
+ * @return the profileTableName
+ */
+ public String getProfileTableName() {
+ return profileTableName;
+ }
+
+ /**
+ * @return the profileSpecId
+ */
+ public String getProfileSpecId() {
+ return profileSpecId;
+ }
+
+ @Override
+ public String toString() {
+ return "JPAProfileTable{tableName=" + profileTableName + ";
profileSpecId=" + profileSpecId + "}";
+ }
+}
=======================================
--- /dev/null
+++
/trunk/servers/jain-slee/core/jar/src/main/java/org/mobicents/slee/container/deployment/profile/jpa/JPAProfileTableFramework.java
Wed Aug 5 16:09:33 2009
@@ -0,0 +1,229 @@
+package org.mobicents.slee.container.deployment.profile.jpa;
+
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
+
+import javax.naming.InitialContext;
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.Query;
+import javax.slee.SLEEException;
+import javax.slee.profile.ProfileTableAlreadyExistsException;
+import javax.transaction.Transaction;
+
+import org.apache.log4j.Logger;
+import org.hibernate.cfg.Environment;
+import org.hibernate.ejb.HibernatePersistence;
+import org.jboss.jpa.deployment.PersistenceUnitInfoImpl;
+import org.jboss.metadata.jpa.spec.PersistenceUnitMetaData;
+import org.mobicents.slee.container.SleeContainer;
+import
org.mobicents.slee.container.component.ProfileSpecificationComponent;
+import org.mobicents.slee.container.management.SleeProfileTableManager;
+import org.mobicents.slee.container.management.jmx.MobicentsManagement;
+
+/**
+ *
+ * JPAProfileTableFramework.java
+ *
+ * @author <a href="mailto:[email protected]"> Alexandre Mendonca </a>
+ */
+public class JPAProfileTableFramework {
+
+ private static Logger logger =
Logger.getLogger(JPAProfileTableFramework.class);
+
+ private EntityManagerFactory entityManagerFactory;
+ private final SleeProfileTableManager sptm;
+
+ private boolean isInitialized = false;
+ private static Boolean isLoading = false;
+
+ public JPAProfileTableFramework(SleeProfileTableManager sptm) {
+ this.sptm = sptm;
+ }
+
+ private void createPersistenceUnit()
+ {
+ try {
+ HibernatePersistence hp = new HibernatePersistence();
+ PersistenceUnitMetaData pumd = new PersistenceUnitMetaData();
+
+ pumd.setProvider("org.hibernate.ejb.HibernatePersistence");
+ pumd.setJtaDataSource("java:/DefaultDS");
+ pumd.setExcludeUnlistedClasses(false);
+
+ boolean persistProfiles = MobicentsManagement.persistProfiles;
+
+ Map pumdProps = new HashMap();
+ pumdProps.put(Environment.HBM2DDL_AUTO,
persistProfiles ? "update" : "create-drop");
+
pumdProps.put(Environment.DIALECT, "org.hibernate.dialect.HSQLDialect");
+
+ pumd.setProperties(pumdProps);
+ pumd.setName("mobicents-profile-persistence-pu");
+
+ Set classes = new HashSet<String>();
+ classes.add(JPAProfileTable.class.getName());
+
+ pumd.setClasses(classes);
+
+ Properties properties = new Properties();
+
+ properties.setProperty(Environment.DATASOURCE, "java:/DefaultDS");
+
properties.setProperty(Environment.TRANSACTION_STRATEGY, "org.hibernate.ejb.transaction.JoinableCMTTransactionFactory");
+
properties.setProperty(Environment.CONNECTION_PROVIDER, "org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider");
+
properties.setProperty("hibernate.jndi.java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
+
properties.setProperty(Environment.CACHE_PROVIDER, "org.hibernate.cache.HashtableCacheProvider");
+
properties.setProperty(Environment.TRANSACTION_MANAGER_STRATEGY, "org.hibernate.transaction.JBossTransactionManagerLookup");
+
properties.setProperty("hibernate.jndi.java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
+
properties.setProperty(Environment.DIALECT, "org.hibernate.dialect.HSQLDialect");
+ // FIXME: Should be Environment.JACC_CONTEXTID but it's
+ // hibernate.jacc_context_id vs hibernate.jacc.ctx.id. Bug?
+ properties.setProperty("hibernate.jacc.ctx.id", "persistence.xml");
+
properties.setProperty(Environment.CACHE_REGION_PREFIX, "persistence.unit:unitName=#"
+ pumd.getName());
+
properties.setProperty(Environment.SESSION_FACTORY_NAME, "persistence.unit:unitName=#"
+ pumd.getName());
+ properties.setProperty(Environment.HBM2DDL_AUTO,
persistProfiles ? "update" : "create-drop");
+
properties.setProperty(Environment.USE_REFLECTION_OPTIMIZER, "false");
+ properties.setProperty(Environment.BYTECODE_PROVIDER, "javassist");
+ properties.setProperty(Environment.STATEMENT_BATCH_SIZE, "0");
+ properties.setProperty(Environment.SHOW_SQL, "true");
+ properties.setProperty(Environment.FORMAT_SQL, "true");
+
+ PersistenceUnitInfoImpl pi = new PersistenceUnitInfoImpl(pumd,
+ properties, Thread.currentThread().getContextClassLoader(),
+ ClassLoader.getSystemResource("."), new ArrayList<URL>(),
+ new InitialContext());
+
+ Transaction tx = null;
+
+ try {
+ tx =
SleeContainer.lookupFromJndi().getTransactionManager().suspend();
+ entityManagerFactory = hp.createContainerEntityManagerFactory(pi,
null);
+ isInitialized = true;
+ } catch (Exception e) {
+ logger.error("Failure creating Persistence Unit for profile
persistency.", e);
+ } catch (Throwable t) {
+ logger.error("Failure creating Persistence Unit for profile
persistency.", t);
+ } finally {
+ if (tx != null)
+
SleeContainer.lookupFromJndi().getTransactionManager().resume(tx);
+ }
+ }
+ catch (Exception e) {
+ logger.error("Failure creating Persistence Unit for profile
persistency.", e);
+ }
+ }
+
+ public void storeProfileTable(JPAProfileTable profileTable) {
+ if(isLoading) {
+ return;
+ }
+
+ if(logger.isDebugEnabled()) {
+ logger.debug( "Storing into backend storage profile table " +
profileTable );
+ }
+
+ EntityManager em = null;
+
+ try {
+ em = entityManagerFactory.createEntityManager();
+ em.persist(profileTable);
+ }
+ finally {
+ if(em != null) {
+ em.close();
+ em = null;
+ }
+ }
+ }
+
+ public void removeProfileTable(String profileTableName) {
+ if(logger.isDebugEnabled()) {
+ logger.debug( "Removing from backend storage profile table" +
profileTableName );
+ }
+
+ EntityManager em = null;
+
+ try {
+ em = entityManagerFactory.createEntityManager();
+
+ Query q = em.createQuery("DELETE FROM " +
JPAProfileTable.class.getName() + " x WHERE x.profileTableName
= :profileTableName")
+ .setParameter("profileTableName", profileTableName);
+
+ q.executeUpdate();
+ }
+ finally {
+ if(em != null) {
+ em.close();
+ em = null;
+ }
+ }
+ }
+
+ public void renameProfileTable(String oldProfileTableName, String
newProfileTableName) {
+ if(logger.isDebugEnabled()) {
+ logger.debug("Renaming " + oldProfileTableName + " to " +
newProfileTableName + " in backend storage.");
+ }
+
+ EntityManager em = null;
+
+ try {
+ em = entityManagerFactory.createEntityManager();
+
+ Query q = em.createQuery("UPDATE " + JPAProfileTable.class.getName()
+ " x SET x.profileTableName = :newProfileTableName WHERE
x.profileTableName = :oldProfileTableName")
+ .setParameter("newProfileTableName",
newProfileTableName).setParameter("oldProfileTableName",
oldProfileTableName);
+
+ q.executeUpdate();
+ }
+ finally {
+ if(em != null) {
+ em.close();
+ em = null;
+ }
+ }
+ }
+
+ public void loadProfileTables(ProfileSpecificationComponent component)
throws SLEEException, ProfileTableAlreadyExistsException {
+ if(logger.isDebugEnabled()) {
+ logger.debug("Loading from backend storage profile tables for " +
component.getProfileSpecificationID());
+ }
+
+ EntityManager em = null;
+
+ try {
+
+ if(!isInitialized) {
+ createPersistenceUnit();
+ }
+
+ em = entityManagerFactory.createEntityManager();
+
+ Query q = em.createQuery("SELECT x FROM " +
JPAProfileTable.class.getName() + " x WHERE x.profileSpecId
= :profileSpecId")
+ .setParameter("profileSpecId",
component.getProfileSpecificationID().toString());
+
+ List<JPAProfileTable> tables = q.getResultList();
+
+ for(JPAProfileTable table : tables) {
+ if(logger.isDebugEnabled()) {
+ logger.debug("Table [" + table.getProfileTableName() + "] found
in storage.");
+ }
+ synchronized (isLoading) {
+ isLoading = true;
+ sptm.addProfileTable(table.getProfileTableName(), component);
+ isLoading = false;
+ }
+ }
+ }
+ finally {
+ if(em != null) {
+ em.close();
+ em = null;
+ }
+ }
+ }
+
+}
=======================================
---
/trunk/servers/jain-slee/core/jar/src/main/java/org/mobicents/slee/container/deployment/profile/jpa/JPAProfileEntityFramework.java
Thu Jun 25 12:09:55 2009
+++
/trunk/servers/jain-slee/core/jar/src/main/java/org/mobicents/slee/container/deployment/profile/jpa/JPAProfileEntityFramework.java
Wed Aug 5 16:09:33 2009
@@ -38,6 +38,7 @@
import org.mobicents.slee.container.component.profile.ProfileEntity;
import org.mobicents.slee.container.component.profile.ProfileEntityFactory;
import
org.mobicents.slee.container.component.profile.ProfileEntityFramework;
+import org.mobicents.slee.container.management.jmx.MobicentsManagement;
import org.mobicents.slee.runtime.transaction.SleeTransactionManager;
import org.mobicents.slee.runtime.transaction.TransactionalAction;
@@ -565,8 +566,10 @@
pumd.setJtaDataSource("java:/DefaultDS");
pumd.setExcludeUnlistedClasses(false);
- Map pumdProps = new HashMap();
- pumdProps.put(Environment.HBM2DDL_AUTO, "create-drop");
+ boolean persistProfiles = MobicentsManagement.persistProfiles;
+
+ Map pumdProps = new HashMap();
+ pumdProps.put(Environment.HBM2DDL_AUTO,
persistProfiles ? "update" : "create-drop");
pumdProps.put(Environment.DIALECT,
"org.hibernate.dialect.HSQLDialect");
@@ -610,7 +613,7 @@
"persistence.unit:unitName=#" +
pumd.getName());
properties.setProperty(Environment.SESSION_FACTORY_NAME,
"persistence.unit:unitName=#" +
pumd.getName());
- properties.setProperty(Environment.HBM2DDL_AUTO,
"create-drop");
+ properties.setProperty(Environment.HBM2DDL_AUTO,
persistProfiles ? "update" : "create-drop");
properties.setProperty(Environment.USE_REFLECTION_OPTIMIZER,
"false");
properties.setProperty(Environment.BYTECODE_PROVIDER,
"javassist");
=======================================
---
/trunk/servers/jain-slee/core/jar/src/main/java/org/mobicents/slee/container/management/SleeProfileTableManager.java
Wed Jul 1 07:31:13 2009
+++
/trunk/servers/jain-slee/core/jar/src/main/java/org/mobicents/slee/container/management/SleeProfileTableManager.java
Wed Aug 5 16:09:33 2009
@@ -26,6 +26,8 @@
import
org.mobicents.slee.container.component.profile.ProfileEntityFramework;
import
org.mobicents.slee.container.deployment.profile.SleeProfileClassCodeGenerator;
import
org.mobicents.slee.container.deployment.profile.jpa.JPAProfileEntityFramework;
+import org.mobicents.slee.container.deployment.profile.jpa.JPAProfileTable;
+import
org.mobicents.slee.container.deployment.profile.jpa.JPAProfileTableFramework;
import org.mobicents.slee.container.profile.ProfileTableImpl;
import org.mobicents.slee.runtime.facilities.ProfileAlarmFacilityImpl;
import org.mobicents.slee.runtime.transaction.TransactionalAction;
@@ -46,7 +48,8 @@
private final static SleeProfileClassCodeGenerator
sleeProfileClassCodeGenerator = new SleeProfileClassCodeGenerator();
private SleeContainer sleeContainer = null;
- // FIXME: Alex this has to be moved into cache structure
+ private JPAProfileTableFramework jpaPTF = null;
+
/**
* This map contains mapping - profieltable name ---> profile table
concrete
* object. see 10.2.4 section of JSLEE 1.1 specs - there can be only
single
@@ -62,6 +65,7 @@
throw new NullPointerException("Parameter must not be
null");
this.sleeContainer = sleeContainer;
this.nameToProfileTableMap=new ConcurrentHashMap<String,
ProfileTableImpl>(); //
this.sleeContainer.getCache().getProfileManagementCacheData();
+ this.jpaPTF = new JPAProfileTableFramework(this);
}
@@ -88,6 +92,7 @@
ProfileEntityFramework profileEntityFramework = new
JPAProfileEntityFramework(component);
profileEntityFramework.install();
sleeProfileClassCodeGenerator.process(component);
+ jpaPTF.loadProfileTables(component);
} catch (DeploymentException de) {
throw de;
} catch (Throwable t) {
@@ -374,6 +379,9 @@
} catch (Throwable e) {
throw new SLEEException(e.getMessage(),e);
}
+
+ jpaPTF.storeProfileTable( new JPAProfileTable(profileTable) );
+
return profileTable;
}
@@ -383,10 +391,20 @@
* @throws NullPointerException
* @throws UnrecognizedProfileTableNameException
*/
- public void removeProfileTable(String profileTableName) throws
NullPointerException, UnrecognizedProfileTableNameException {
- ProfileTableImpl profileTable =
getProfileTable(profileTableName);
- nameToProfileTableMap.remove(profileTableName);
- profileTable.remove();
+ public void removeProfileTable(final String profileTableName) throws
NullPointerException, UnrecognizedProfileTableNameException {
+ ProfileTableImpl profileTable = getProfileTable(profileTableName);
+ TransactionalAction action = new TransactionalAction() {
+ public void execute() {
+ nameToProfileTableMap.remove(profileTableName);
+ }
+ };
+ try {
+ sleeContainer.getTransactionManager().addAfterCommitAction(action);
+ } catch (SystemException e) {
+ throw new SLEEException(e.getMessage(),e);
+ }
+ profileTable.remove();
+ jpaPTF.removeProfileTable(profileTable.getProfileTableName());
}
/**
=======================================
---
/trunk/servers/jain-slee/core/jar/src/main/java/org/mobicents/slee/container/management/jmx/MobicentsManagement.java
Thu Jan 15 16:07:51 2009
+++
/trunk/servers/jain-slee/core/jar/src/main/java/org/mobicents/slee/container/management/jmx/MobicentsManagement.java
Wed Aug 5 16:09:33 2009
@@ -76,4 +76,13 @@
logger.warn("Setting event router executors
to "+eventRouterExecutors+". If called with server running a stop and start
is need to apply changes.");
}
-}
+ public static boolean persistProfiles;
+
+ public boolean getPersistProfiles() {
+ return persistProfiles;
+ }
+
+ public void setPersistProfiles(boolean persist) {
+ persistProfiles = persist;
+ }
+}
=======================================
---
/trunk/servers/jain-slee/core/jar/src/main/java/org/mobicents/slee/container/management/jmx/MobicentsManagementMBean.java
Thu Jan 15 16:07:51 2009
+++
/trunk/servers/jain-slee/core/jar/src/main/java/org/mobicents/slee/container/management/jmx/MobicentsManagementMBean.java
Wed Aug 5 16:09:33 2009
@@ -53,6 +53,22 @@
public void setEventRouterExecutors(
int eventRouterExecutors);
+ /**
+ * Retrieves the flag to indicate if profiles should be persisted between
server restart.
+ *
+ * @return
+ */
+ public boolean getPersistProfiles();
+
+ /**
+ *
+ * Defines if profiles should be persisted between server restart.
+ *
+ * @param persist
+ * @return
+ */
+ public void setPersistProfiles(boolean persist);
+
/**
*
* @return string representation of container's version.
=======================================
---
/trunk/servers/jain-slee/core/sar/src/main/resources/META-INF/jboss-beans.xml
Thu Jun 25 12:09:55 2009
+++
/trunk/servers/jain-slee/core/sar/src/main/resources/META-INF/jboss-beans.xml
Wed Aug 5 16:09:33 2009
@@ -106,7 +106,8 @@
<annotation>@org.jboss.aop.microcontainer.aspects.jmx.JMX(name="slee:service=MobicentsManagement",exposedInterface=org.mobicents.slee.container.management.jmx.MobicentsManagementMBean.class,
registerDirectly=true)</annotation>
<property name="entitiesRemovalDelay">1</property>
<property name="monitoringUncommittedAcAttachs">true</property>
- <property name="eventRouterExecutors">313</property>
+ <property name="eventRouterExecutors">313</property>
+ <property name="persistProfiles">true</property>
</bean>
<!-- JBOSS CACHE 3 -->