Author: jeff.yuchang
Date: 2009-06-15 02:00:42 -0400 (Mon, 15 Jun 2009)
New Revision: 609
Modified:
idm/trunk/integration/deployer/src/main/java/org/jboss/identity/idm/integration/jboss5/HibernatePopulator.java
idm/trunk/integration/deployer/src/main/java/org/jboss/identity/idm/integration/jboss5/IDMDeployer.java
idm/trunk/integration/deployer/src/main/resources/deployer-assembly.xml
Log:
* Add the portal related common jar into the distribution.
Modified:
idm/trunk/integration/deployer/src/main/java/org/jboss/identity/idm/integration/jboss5/HibernatePopulator.java
===================================================================
---
idm/trunk/integration/deployer/src/main/java/org/jboss/identity/idm/integration/jboss5/HibernatePopulator.java 2009-06-12
12:24:01 UTC (rev 608)
+++
idm/trunk/integration/deployer/src/main/java/org/jboss/identity/idm/integration/jboss5/HibernatePopulator.java 2009-06-15
06:00:42 UTC (rev 609)
@@ -3,32 +3,29 @@
*/
package org.jboss.identity.idm.integration.jboss5;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
import java.net.URL;
import java.util.Collection;
import java.util.Iterator;
+import java.util.logging.Level;
import java.util.logging.Logger;
-import javax.naming.Context;
-import javax.naming.InitialContext;
-import javax.naming.NamingException;
import javax.transaction.TransactionManager;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
-import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import org.hibernate.exception.SQLGrammarException;
import org.hibernate.metadata.ClassMetadata;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.tool.hbm2ddl.SchemaUpdate;
-import org.jboss.identity.idm.api.cfg.IdentityConfiguration;
import org.jboss.identity.idm.common.transaction.TransactionManagerProvider;
import org.jboss.identity.idm.common.transaction.Transactions;
+import org.jboss.identity.idm.impl.helper.SecurityActions;
import org.jboss.identity.idm.integration.jboss5.jaxb2.HibernateInitializerType;
-import org.jboss.portal.common.net.URLTools;
-import org.jboss.portal.common.util.CLResourceLoader;
-import org.jboss.portal.common.util.LoaderResource;
import bsh.EvalError;
import bsh.Interpreter;
@@ -54,89 +51,47 @@
/** doCheck result - schema not exist */
private static final int RESULT_CREATE = 2;
-
- private Configuration config;
- private SessionFactory sessionFactory;
-
private HibernateInitializerType type;
- private String hibernateSessionFactoryJNDIName;
+ private Configuration config;
- private IdentityConfiguration identityConfiguration;
+ private SessionFactory sessionFactory;
-
- public HibernatePopulator(HibernateInitializerType type, IdentityConfiguration
identityConfig) {
+ public HibernatePopulator(HibernateInitializerType type, Configuration config) {
this.type = type;
- this.identityConfiguration = identityConfig;
+ this.config = config;
}
- public void populateSchema() throws Exception {
+ public void populateSchema() {
+ sessionFactory = config.buildSessionFactory();
- // If hibernate config is present than create SessionFactory manually and register
it into identity config
- if (type.getHibernateConfigLocation() != null)
- {
- URL configURL =
Thread.currentThread().getContextClassLoader().getResource(type.getHibernateConfigLocation());
- if (configURL == null)
- {
- throw new Exception("The config " +
type.getHibernateConfigLocation() + " does not exist");
- }
-
- if (!URLTools.exists(configURL))
- {
- throw new Exception("The config " + configURL + " does not
exist");
- }
-
- config = new AnnotationConfiguration().configure(configURL);
-
- createHibernateSessionFactory();
-
- Context context = new InitialContext();
- if (getHibernateSessionFactoryJNDIName() != null)
- {
- try
- {
- context.bind(getHibernateSessionFactoryJNDIName(), sessionFactory);
- }
- catch (NamingException e)
- {
- logger.severe(e.getMessage());
-
- }
- }
-
- if (type.getHibernateSessionFactoryRegistryName() != null)
- {
- identityConfiguration.getIdentityConfigurationRegistry().
- register(sessionFactory, type.getHibernateSessionFactoryRegistryName());
- }
-
- if (type.isDoChecking())
- {
- //check the schema
- int check = doCheck();
- switch (check)
- {
- case RESULT_NONE:
- break;
- case RESULT_UPDATE:
- updateSchema();
- break;
- case RESULT_CREATE:
- createSchema();
- createContent();
-
- break;
- }
- } else {
- createSchema();
- }
- }
-
- }
+ if (type.isDoChecking())
+ {
+ //check the schema
+ int check = doCheck();
+ switch (check)
+ {
+ case RESULT_NONE:
+ break;
+ case RESULT_UPDATE:
+ updateSchema();
+ break;
+ case RESULT_CREATE:
+ createSchema();
+ createContent();
+ break;
+ }
+ } else {
+ createSchema();
+ }
+
+ sessionFactory.close();
+ }
+
private int doCheck()
{
Session session = null;
@@ -215,62 +170,25 @@
SchemaUpdate update = new SchemaUpdate(config);
update.execute(false, true);
}
-
- public String getHibernateSessionFactoryJNDIName() {
- return hibernateSessionFactoryJNDIName;
- }
-
-
- public void setHibernateSessionFactoryJNDIName(
- String hibernateSessionFactoryJNDIName) {
- this.hibernateSessionFactoryJNDIName = hibernateSessionFactoryJNDIName;
- }
-
-
- private void createHibernateSessionFactory() throws Exception
- {
- // Set JNDI name if present and absent
- if (getHibernateSessionFactoryJNDIName() != null)
- {
- setPropertyIfAbsent("hibernate.session_factory_name",
getHibernateSessionFactoryJNDIName());
- }
-
- sessionFactory = config.buildSessionFactory();
- }
-
- private void setPropertyIfAbsent(String name, String value)
- {
- if (config.getProperty(name) == null)
- {
- config.setProperty(name, value);
- }
- }
- private void createContent()
- {
- LoaderResource setupResource = null;
-
- if (type.getSetupLocation() != null && setupResource == null)
- {
- setupResource = new CLResourceLoader().getResource(type.getSetupLocation());
- }
-
- if (setupResource != null)
- {
- if (setupResource.exists())
- {
- try
+ private void createContent()
+ {
+
+ if (type.getSetupLocation() != null) {
+ URL setupResource =
SecurityActions.getContextClassLoader().getResource(type.getSetupLocation()) ;
+ if ( setupResource != null ) {
+ try
{
logger.info("Creating database content");
- final String script = setupResource.asString("UTF-8");
-
+ final String script = getString(setupResource.openStream());
+
// Create an interpreter and configures it
final Interpreter interpreter = new Interpreter();
interpreter.setClassLoader(Thread.currentThread().getContextClassLoader());
interpreter.setOut(System.out);
interpreter.set("SessionFactory", sessionFactory);
-
+
TransactionManager tm =
TransactionManagerProvider.JBOSS_PROVIDER.getTransactionManager();
Transactions.required(tm, new Transactions.Runnable()
{
@@ -294,11 +212,40 @@
logger.info("Error in bsh script execution: " + e);
}
}
- else
- {
- logger.info("There is a setup URL but the not valid " +
setupResource);
- }
- }
- }
+ }
+ }
+
+ private String getString(InputStream in) {
+ try {
+ ByteArrayOutputStream out = new ByteArrayOutputStream(in.available());
+ byte[] buffer = new byte[512];
+ while (true)
+ {
+ int i = in.read(buffer);
+ if (i == 0)
+ {
+ continue;
+ }
+ if (i == -1)
+ {
+ break;
+ }
+ out.write(buffer, 0, i);
+ }
+ return out.toString("UTF-8");
+ } catch (IOException e) {
+ logger.log(Level.SEVERE, "error in converting inputstream into
string", e);
+ return null;
+ } finally {
+ if (in != null) {
+ try {
+ in.close();
+ } catch (IOException e) {
+ logger.log(Level.SEVERE, "error in closing inputstream", e);
+ }
+ }
+ }
+ }
+
}
Modified:
idm/trunk/integration/deployer/src/main/java/org/jboss/identity/idm/integration/jboss5/IDMDeployer.java
===================================================================
---
idm/trunk/integration/deployer/src/main/java/org/jboss/identity/idm/integration/jboss5/IDMDeployer.java 2009-06-12
12:24:01 UTC (rev 608)
+++
idm/trunk/integration/deployer/src/main/java/org/jboss/identity/idm/integration/jboss5/IDMDeployer.java 2009-06-15
06:00:42 UTC (rev 609)
@@ -18,6 +18,7 @@
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
+import org.hibernate.cfg.Configuration;
import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.DialectFactory;
import org.jboss.deployers.spi.DeploymentException;
@@ -39,7 +40,11 @@
/**
*
+ * Deploy the JBoss identity Management:
*
+ * 1. Start the IdentitySessionFactory.
+ * 2. Register it into the JNDI.
+ *
* @author Jeff Yu
*
*/
@@ -112,14 +117,19 @@
SqlInitializerType sqlInitializer = config.getInitializers().getSqlInitializer();
if (hibernateInitializer != null) {
- for (IdentityStoreConfigurationMetaData store : identityMetadata.getIdentityStores())
{
- String hibernateConfigFile = store.getOptionSingleValue(HIBERNATE_CONFIGFILE);
- if (hibernateConfigFile != null &&
!"".equals(hibernateConfigFile.trim())) {
- logger.fine("starting to populate the schema from file [" +
hibernateConfigFile + "]");
- HibernatePopulator hibernatePopulator = new HibernatePopulator(hibernateInitializer,
identityConfiguration);
- hibernatePopulator.populateSchema();
+ String hibernateConfigFile =
config.getHibernateDeployer().getHibernateConfiguration();
+ if (hibernateConfigFile == null) {
+ for (IdentityStoreConfigurationMetaData store : identityMetadata.getIdentityStores())
{
+ hibernateConfigFile = store.getOptionSingleValue(HIBERNATE_CONFIGFILE);
}
}
+ if (hibernateConfigFile == null) {
+ throw new Exception ("Couldn't find the hibernate configuration
file");
+ }
+ Configuration configuration = new
AnnotationConfiguration().configure(hibernateConfigFile);
+ logger.fine("starting to populate the schema from file [" +
hibernateConfigFile + "]");
+ HibernatePopulator hibernatePopulator = new HibernatePopulator(hibernateInitializer,
configuration);
+ hibernatePopulator.populateSchema();
}else if (sqlInitializer != null) {
logger.fine("starting to populate the schema from script file [" +
sqlInitializer.getSqlFile() + "]");
Modified: idm/trunk/integration/deployer/src/main/resources/deployer-assembly.xml
===================================================================
--- idm/trunk/integration/deployer/src/main/resources/deployer-assembly.xml 2009-06-12
12:24:01 UTC (rev 608)
+++ idm/trunk/integration/deployer/src/main/resources/deployer-assembly.xml 2009-06-15
06:00:42 UTC (rev 609)
@@ -27,6 +27,7 @@
<include>org.jboss.identity.idm:idm-api</include>
<include>org.jboss.identity.idm:idm-common</include>
<include>org.jboss.identity.idm:idm-spi</include>
+ <include>org.jboss.portal.common:common-common</include>
</includes>
</dependencySet>
</dependencySets>
Show replies by date