[jbpm-commits] JBoss JBPM SVN: r3738 - in jbpm4/branches/hbraun/modules: examples/src/test/resources and 4 other directories.

do-not-reply at jboss.org do-not-reply at jboss.org
Mon Feb 2 08:46:16 EST 2009


Author: heiko.braun at jboss.com
Date: 2009-02-02 08:46:16 -0500 (Mon, 02 Feb 2009)
New Revision: 3738

Modified:
   jbpm4/branches/hbraun/modules/examples/pom.xml
   jbpm4/branches/hbraun/modules/examples/src/test/resources/jbpm.cfg.xml
   jbpm4/branches/hbraun/modules/pvm/src/main/java/org/jbpm/pvm/internal/wire/binding/HibernateConfigurationBinding.java
   jbpm4/branches/hbraun/modules/pvm/src/main/java/org/jbpm/pvm/internal/wire/descriptor/HibernateConfigurationDescriptor.java
   jbpm4/branches/hbraun/modules/pvm/src/main/java/org/jbpm/pvm/internal/wire/descriptor/HibernateSessionFactoryDescriptor.java
   jbpm4/branches/hbraun/modules/test-db/pom.xml
   jbpm4/branches/hbraun/modules/test-db/src/main/resources/jbpm.cfg.xml
Log:
Allow external hibernate configutation files

Modified: jbpm4/branches/hbraun/modules/examples/pom.xml
===================================================================
--- jbpm4/branches/hbraun/modules/examples/pom.xml	2009-01-30 20:29:00 UTC (rev 3737)
+++ jbpm4/branches/hbraun/modules/examples/pom.xml	2009-02-02 13:46:16 UTC (rev 3738)
@@ -60,7 +60,7 @@
                 </artifactItem>
               </artifactItems>
               <excludeTransitive>true</excludeTransitive>
-              <outputDirectory>target/database-config</outputDirectory>
+              <outputDirectory>target/jpdl-config</outputDirectory>
             </configuration>
           </execution>
         </executions>
@@ -77,8 +77,8 @@
             </goals>
             <configuration>
               <tasks>
-                <copy file="target/database-config/db.properties/${database}.properties"
-                      tofile="target/test-classes/hibernate.properties"
+                <copy file="target/jpdl-config/hibernate.cfg.${database}.xml"
+                      tofile="target/test-classes/examples-hibernate.cfg.xml"
                       overwrite="true" />
               </tasks>
             </configuration>

Modified: jbpm4/branches/hbraun/modules/examples/src/test/resources/jbpm.cfg.xml
===================================================================
--- jbpm4/branches/hbraun/modules/examples/src/test/resources/jbpm.cfg.xml	2009-01-30 20:29:00 UTC (rev 3737)
+++ jbpm4/branches/hbraun/modules/examples/src/test/resources/jbpm.cfg.xml	2009-02-02 13:46:16 UTC (rev 3738)
@@ -25,19 +25,7 @@
       <standard-transaction-interceptor />
     </command-service>
     
-    <hibernate-configuration>
-      <properties resource="hibernate.properties" />
-      <mapping resource="jbpm.pvm.typedefs.hbm.xml" />
-      <mapping resource="jbpm.pvm.wire.hbm.xml" />
-      <mapping resource="jbpm.pvm.definition.hbm.xml" />
-      <mapping resource="jbpm.pvm.execution.hbm.xml" />
-      <mapping resource="jbpm.pvm.variable.hbm.xml" />
-      <mapping resource="jbpm.pvm.job.hbm.xml" />
-      <mapping resource="jbpm.jpdl.hbm.xml" />
-      <mapping resource="jbpm.task.hbm.xml" />
-      <cache-configuration resource="jbpm.pvm.cache.xml" 
-                           usage="nonstrict-read-write" />
-    </hibernate-configuration>
+    <hibernate-configuration resource="examples-hibernate.cfg.xml"/>      
     
     <hibernate-session-factory />
     

Modified: jbpm4/branches/hbraun/modules/pvm/src/main/java/org/jbpm/pvm/internal/wire/binding/HibernateConfigurationBinding.java
===================================================================
--- jbpm4/branches/hbraun/modules/pvm/src/main/java/org/jbpm/pvm/internal/wire/binding/HibernateConfigurationBinding.java	2009-01-30 20:29:00 UTC (rev 3737)
+++ jbpm4/branches/hbraun/modules/pvm/src/main/java/org/jbpm/pvm/internal/wire/binding/HibernateConfigurationBinding.java	2009-02-02 13:46:16 UTC (rev 3738)
@@ -120,10 +120,34 @@
         descriptor.setSchemaOperation(HibernateConfigurationDescriptor.UpdateSchema.getInstance());
       }
     }
-    
+
+    // [hb] parse external configuration file or use inlined elements
+    // but for no, it's one or the other to avoid side effects and overlays
+    if(element.hasAttribute("resource"))
+    {
+      parseConfigurationFile(element, descriptor);
+      log.info("Configuring form external resource "+element.getAttribute("resource")+
+          ", skip inline configuration elements.");
+    }
+    else
+    {
+      parseInlineConfiguration(element, parse, descriptor, parser);
+    }
+    return descriptor;
+  }
+
+  private void parseConfigurationFile(Element element, HibernateConfigurationDescriptor descriptor)
+  {
+    String hibernateConfigResource = element.getAttribute("resource");
+    descriptor.setHibernateConfigResource(hibernateConfigResource);
+
+  }
+
+  private void parseInlineConfiguration(Element element, Parse parse, HibernateConfigurationDescriptor descriptor, Parser parser)
+  {
     List<Element> configElements = XmlUtil.elements(element);
     for (Element configElement: configElements) {
-      
+
       if ("mappings".equals(XmlUtil.getTagLocalName(configElement))) {
         if (configElement.hasAttribute("resource")) {
           String resources = configElement.getAttribute("resource");
@@ -144,28 +168,28 @@
             parse.addProblem("couldn't parse hibernate mapping resources '"+resources+"'", e);
           }
         }
-       
+
       } else if ("mapping".equals(XmlUtil.getTagLocalName(configElement))) {
-        
+
         parseMapping(configElement, descriptor, parse);
-        
+
       } else if ("properties".equals(XmlUtil.getTagLocalName(configElement))) {
         PropertiesDescriptor propertiesDescriptor = (PropertiesDescriptor) propertiesBinding.parse(configElement, parse, parser);
         descriptor.setPropertiesDescriptor(propertiesDescriptor);
-        
+
       } else if ("cache-configuration".equals(XmlUtil.getTagLocalName(configElement))) {
         StreamInput streamSource = null;
-        
+
         String cacheUsage = configElement.getAttribute("usage");
         if (! ( ("read-only".equals(cacheUsage))
-                || ("nonstrict-read-write".equals(cacheUsage)) 
-                || ("read-write".equals(cacheUsage)) 
-                || ("transactional".equals(cacheUsage)) 
+                || ("nonstrict-read-write".equals(cacheUsage))
+                || ("read-write".equals(cacheUsage))
+                || ("transactional".equals(cacheUsage))
               )
            ){
           parse.addProblem("problem in cache-configuration: no usage attribute or illegal value: "+cacheUsage+" Possible values are {read-only, nonstrict-read-write, read-write, transactional}");
         } else {
-          
+
           if (configElement.hasAttribute("file")) {
             String fileName = configElement.getAttribute("file");
             File file = new File(fileName);
@@ -175,12 +199,12 @@
               parse.addProblem("file "+fileName+" isn't a file");
             }
           }
-          
+
           if (configElement.hasAttribute("resource")) {
             String resource = configElement.getAttribute("resource");
             streamSource = new ResourceStreamInput(resource, parse.getClassLoader());
           }
-          
+
           if (configElement.hasAttribute("url")) {
             String urlText = configElement.getAttribute("url");
             try {
@@ -190,22 +214,22 @@
               parse.addProblem("couldn't open url "+urlText, e);
             }
           }
-          
+
           if (streamSource != null) {
             parser.importStream(streamSource, configElement, parse);
           }
-          
+
           // parse the cache configurations in the same way as the hibernate cfg schema
           // translate the contents of the file into invoke operations for methods
           // Configuration.setCacheConcurrencyStrategy(String clazz, String concurrencyStrategy, String region)
-          // Configuration.setCollectionCacheConcurrencyStrategy(String collectionRole, String concurrencyStrategy) 
+          // Configuration.setCollectionCacheConcurrencyStrategy(String collectionRole, String concurrencyStrategy)
           //         <class-cache class="org.hibernate.auction.Item" usage="read-write"/>
           //         <class-cache class="org.hibernate.auction.Bid" usage="read-only"/>
           //         <collection-cache collection="org.hibernate.auction.Item.bids" usage="read-write"/>
           List<Element> cacheElements = XmlUtil.elements(configElement);
           if (cacheElements!=null) {
             for (Element cacheElement : cacheElements) {
-              
+
               if ("class-cache".equals(XmlUtil.getTagLocalName(cacheElement))) {
                 String className = cacheElement.getAttribute("class");
                 descriptor.addCacheOperation(new HibernateConfigurationDescriptor.SetCacheConcurrencyStrategy(className, cacheUsage));
@@ -225,8 +249,6 @@
         parse.addProblem("unknown hibernate configuration element "+XmlUtil.toString(configElement));
       }
     }
-
-    return descriptor;
   }
 
 }

Modified: jbpm4/branches/hbraun/modules/pvm/src/main/java/org/jbpm/pvm/internal/wire/descriptor/HibernateConfigurationDescriptor.java
===================================================================
--- jbpm4/branches/hbraun/modules/pvm/src/main/java/org/jbpm/pvm/internal/wire/descriptor/HibernateConfigurationDescriptor.java	2009-01-30 20:29:00 UTC (rev 3737)
+++ jbpm4/branches/hbraun/modules/pvm/src/main/java/org/jbpm/pvm/internal/wire/descriptor/HibernateConfigurationDescriptor.java	2009-02-02 13:46:16 UTC (rev 3738)
@@ -21,6 +21,23 @@
  */
 package org.jbpm.pvm.internal.wire.descriptor;
 
+import org.hibernate.JDBCException;
+import org.hibernate.HibernateException;
+import org.hibernate.cfg.Configuration;
+import org.hibernate.connection.ConnectionProvider;
+import org.hibernate.connection.ConnectionProviderFactory;
+import org.hibernate.dialect.Dialect;
+import org.hibernate.tool.hbm2ddl.DatabaseMetadata;
+import org.hibernate.util.JDBCExceptionReporter;
+import org.jbpm.JbpmException;
+import org.jbpm.log.Log;
+import org.jbpm.pvm.internal.util.ReflectUtil;
+import org.jbpm.pvm.internal.wire.WireContext;
+import org.jbpm.pvm.internal.wire.WireDefinition;
+import org.jbpm.pvm.internal.wire.WireException;
+import org.jbpm.pvm.internal.wire.operation.Operation;
+
+import java.io.InputStream;
 import java.net.URL;
 import java.sql.Connection;
 import java.sql.SQLException;
@@ -30,29 +47,13 @@
 import java.util.List;
 import java.util.Properties;
 
-import org.jbpm.JbpmException;
-import org.jbpm.log.Log;
-import org.jbpm.pvm.internal.util.ReflectUtil;
-import org.jbpm.pvm.internal.wire.WireContext;
-import org.jbpm.pvm.internal.wire.WireDefinition;
-import org.jbpm.pvm.internal.wire.WireException;
-import org.jbpm.pvm.internal.wire.operation.Operation;
-
-import org.hibernate.JDBCException;
-import org.hibernate.cfg.Configuration;
-import org.hibernate.connection.ConnectionProvider;
-import org.hibernate.connection.ConnectionProviderFactory;
-import org.hibernate.dialect.Dialect;
-import org.hibernate.tool.hbm2ddl.DatabaseMetadata;
-import org.hibernate.util.JDBCExceptionReporter;
-
 /**
  * @author Tom Baeyens
  */
 public class HibernateConfigurationDescriptor extends AbstractDescriptor {
-  
+
   private static final long serialVersionUID = 1L;
-  
+
   private static final Log log = Log.getLog(HibernateConfigurationDescriptor.class.getName());
 
   String className;
@@ -62,32 +63,47 @@
   PropertiesDescriptor propertiesDescriptor;
   private Operation schemaOperation;
 
+  // a hibernate xml config, i.e. hibernate.cfg.xml  
+  private String hibernateConfigResource = null;
+
   public Object construct(WireContext wireContext) {
     // instantiation of the configuration
     Configuration configuration = null;
     if (className!=null) {
       ClassLoader classLoader = wireContext.getClassLoader();
-      log.trace("instantiating hibernate configation class "+className);
+      log.trace("instantiating hibernate configuration class "+className);
       Class<?> configurationClass = ReflectUtil.loadClass(classLoader, className);
       configuration = (Configuration) ReflectUtil.newInstance(configurationClass);
     } else {
-      log.trace("instantiating default hibernate configation");
+      log.trace("instantiating default hibernate configuration");
       configuration = new Configuration();
     }
     return configuration;
   }
-  
+
   public void initialize(Object object, WireContext wireContext) {
+
     Configuration configuration = (Configuration) object;
-    apply(mappingOperations, configuration, wireContext);
-    apply(cacheOperations, configuration, wireContext);
-    if (propertiesDescriptor!=null) {
-      Properties properties = (Properties) wireContext.create(propertiesDescriptor, false);
-      if (log.isDebugEnabled()) log.debug("adding properties to hibernate configuration: "+properties);
-      configuration.addProperties(properties);
+
+    // [hb] use one or the other for now, but don't mix external and inline configuration
+    if(hibernateConfigResource!=null)
+    {
+      configuration.configure(hibernateConfigResource);
     }
-    if (schemaOperation != null) {
-      schemaOperation.apply(configuration, wireContext);
+    else
+    {
+      apply(mappingOperations, configuration, wireContext);
+      apply(cacheOperations, configuration, wireContext);
+
+      if (propertiesDescriptor!=null) {
+        Properties properties = (Properties) wireContext.create(propertiesDescriptor, false);
+        if (log.isDebugEnabled()) log.debug("adding properties to hibernate configuration: "+properties);
+        configuration.addProperties(properties);
+      }
+
+      if (schemaOperation != null) {
+        schemaOperation.apply(configuration, wireContext);
+      }
     }
   }
 
@@ -123,9 +139,31 @@
     }
     cacheOperations.add(operation);
   }
-  
+
   // operations ///////////////////////////////////////////////////////////////
 
+  /**
+   * Applies an external configuration (i.e. hibernate.cfg.xml)
+   */
+  public static class ExternalConfiguration implements Operation {
+    private static final long serialVersionUID = 1L;
+    String resource;
+    public ExternalConfiguration(String resource) {
+      this.resource = resource;
+    }
+    public void apply(Object target, WireContext wireContext) {
+      Configuration configuration = (Configuration) target;
+      InputStream in = wireContext.getClassLoader().getResourceAsStream(resource);
+      if(null==in)
+        throw new IllegalArgumentException("No such resource " +resource);
+      configuration.addInputStream(in);
+    }
+
+    public String toString() {
+      return "using external hibernate configuration "+resource;
+    }
+  }
+
   public static class AddResource implements Operation {
     private static final long serialVersionUID = 1L;
     String resource;
@@ -229,7 +267,7 @@
   public static class CreateSchema implements Operation {
 
     private static final long serialVersionUID = 1L;
-    
+
     /** The sole instance of this class */
     private static final Operation instance = new CreateSchema();
 
@@ -273,7 +311,7 @@
   public static class UpdateSchema implements Operation {
 
     private static final long serialVersionUID = 1L;
-    
+
     private static final Operation instance = new UpdateSchema();
 
     private UpdateSchema() {
@@ -289,7 +327,7 @@
         Connection connection = connectionProvider.getConnection();
         try {
           DatabaseMetadata metadata = new DatabaseMetadata(connection, dialect);
-          String[] updateScript = configuration.generateSchemaUpdateScript(dialect, metadata);          
+          String[] updateScript = configuration.generateSchemaUpdateScript(dialect, metadata);
           log.debug("updating db schema");
           executeScript(connection, updateScript);
         }
@@ -335,9 +373,9 @@
     }
     return exceptions;
   }
-  
+
   // getters and setters //////////////////////////////////////////////////////
-  
+
   public String getClassName() {
     return className;
   }
@@ -362,4 +400,8 @@
   public void setSchemaOperation(Operation schemaOperation) {
     this.schemaOperation = schemaOperation;
   }
+  public void setHibernateConfigResource(String hibernateConfigResource)
+  {
+    this.hibernateConfigResource = hibernateConfigResource;
+  }
 }

Modified: jbpm4/branches/hbraun/modules/pvm/src/main/java/org/jbpm/pvm/internal/wire/descriptor/HibernateSessionFactoryDescriptor.java
===================================================================
--- jbpm4/branches/hbraun/modules/pvm/src/main/java/org/jbpm/pvm/internal/wire/descriptor/HibernateSessionFactoryDescriptor.java	2009-01-30 20:29:00 UTC (rev 3737)
+++ jbpm4/branches/hbraun/modules/pvm/src/main/java/org/jbpm/pvm/internal/wire/descriptor/HibernateSessionFactoryDescriptor.java	2009-02-02 13:46:16 UTC (rev 3738)
@@ -31,6 +31,8 @@
 import org.hibernate.SessionFactory;
 import org.hibernate.cfg.Configuration;
 
+import java.net.URL;
+
 /**
  * @author Tom Baeyens
  */
@@ -58,7 +60,7 @@
     if (configuration==null) {
       throw new WireException("couldn't find configuration");
     }
-    
+
     SessionFactory sessionFactory = configuration.buildSessionFactory();
     
     wireContext.addListener(new SessionFactoryCloser(sessionFactory));

Modified: jbpm4/branches/hbraun/modules/test-db/pom.xml
===================================================================
--- jbpm4/branches/hbraun/modules/test-db/pom.xml	2009-01-30 20:29:00 UTC (rev 3737)
+++ jbpm4/branches/hbraun/modules/test-db/pom.xml	2009-02-02 13:46:16 UTC (rev 3738)
@@ -74,7 +74,7 @@
                 </artifactItem>
               </artifactItems>
               <excludeTransitive>true</excludeTransitive>
-              <outputDirectory>target/database-config</outputDirectory>
+              <outputDirectory>target/jpdl-config</outputDirectory>
             </configuration>
           </execution>
         </executions>
@@ -88,12 +88,12 @@
             <phase>generate-test-resources</phase>
             <goals>
               <goal>run</goal>
-            </goals>                 <!-- TODO: Change to test-classes -->
+            </goals>                 
             <configuration>
               <tasks>
-                <copy file="target/database-config/db.properties/${database}.properties"
-                      tofile="target/classes/hibernate.properties"
-                      overwrite="true" />
+               <copy file="target/jpdl-config/hibernate.cfg.${database}.xml"
+                      tofile="target/classes/testDb-hibernate.cfg.xml"
+                      overwrite="true" />         
               </tasks>
             </configuration>
           </execution>

Modified: jbpm4/branches/hbraun/modules/test-db/src/main/resources/jbpm.cfg.xml
===================================================================
--- jbpm4/branches/hbraun/modules/test-db/src/main/resources/jbpm.cfg.xml	2009-01-30 20:29:00 UTC (rev 3737)
+++ jbpm4/branches/hbraun/modules/test-db/src/main/resources/jbpm.cfg.xml	2009-02-02 13:46:16 UTC (rev 3738)
@@ -25,19 +25,7 @@
       <standard-transaction-interceptor />
     </command-service>
     
-    <hibernate-configuration>
-      <properties resource="hibernate.properties" />
-      <mapping resource="jbpm.pvm.typedefs.hbm.xml" />
-      <mapping resource="jbpm.pvm.wire.hbm.xml" />
-      <mapping resource="jbpm.pvm.definition.hbm.xml" />
-      <mapping resource="jbpm.pvm.execution.hbm.xml" />
-      <mapping resource="jbpm.pvm.variable.hbm.xml" />
-      <mapping resource="jbpm.pvm.job.hbm.xml" />
-      <mapping resource="jbpm.jpdl.hbm.xml" />
-      <mapping resource="jbpm.task.hbm.xml" />
-      <cache-configuration resource="jbpm.pvm.cache.xml" 
-                           usage="nonstrict-read-write" />
-    </hibernate-configuration>
+    <hibernate-configuration resource="testDb-hibernate.cfg.xml"/>     
     
     <hibernate-session-factory />
     




More information about the jbpm-commits mailing list