View Javadoc
1   package org.andromda.maven.plugin.andromdapp.hibernate;
2   
3   import java.io.File;
4   import java.net.MalformedURLException;
5   import java.net.URL;
6   import java.net.URLClassLoader;
7   import java.util.List;
8   import java.util.Map;
9   import java.util.Random;
10  import org.andromda.core.common.Constants;
11  import org.andromda.core.common.ResourceWriter;
12  import org.apache.commons.lang.StringUtils;
13  
14  /**
15   * Provides the ability to create a schema from Hibernate
16   * mapping files.
17   *
18   * @author Chad Brandon
19   */
20  public class HibernateUpdateSchema
21      extends HibernateSchemaManagement
22  {
23      /**
24       * The temporary directory.
25       */
26      private static final String HIBERNATE_PROPERTIES_TEMP_DIRECTORY =
27          Constants.TEMPORARY_DIRECTORY + "andromdapp/hibernate-schema-update";
28  
29      private static final Random random = new Random();
30      /**
31       * @see org.andromda.maven.plugin.andromdapp.hibernate.HibernateSchemaManagement#addArguments(java.util.Map, java.util.List)
32       */
33      protected void addArguments(
34          final Map options,
35          final List<String> arguments)
36          throws Exception
37      {
38          final String driverClass = this.getRequiredProperty(
39                  options,
40                  "jdbcDriver");
41          final String connectionUrl = this.getRequiredProperty(
42                  options,
43                  "jdbcConnectionUrl");
44          final String username = this.getRequiredProperty(
45                  options,
46                  "jdbcUsername");
47          final String password = this.getProperty(
48                  options,
49                  "jdbcPassword");
50          final StringBuilder contents = new StringBuilder();
51          contents.append("hibernate.connection.driver_class=").append(driverClass).append('\n');
52          contents.append("hibernate.connection.url=").append(connectionUrl).append('\n');
53          contents.append("hibernate.connection.username=").append(username).append('\n');
54          if(StringUtils.isNotBlank(password))
55          {
56              contents.append("hibernate.connection.password=").append(password).append('\n');
57          }
58          final File temporaryProperitesFile =
59              new File(HIBERNATE_PROPERTIES_TEMP_DIRECTORY, String.valueOf(random.nextDouble()));
60          temporaryProperitesFile.deleteOnExit();
61          ResourceWriter.instance().writeStringToFile(
62              contents.toString(),
63              temporaryProperitesFile.toString());
64          arguments.add("--properties=" + temporaryProperitesFile);
65      }
66  
67      /**
68       * The class loader containing the jdbc driver.
69       */
70      private ClassLoader jdbcDriverJarLoader = null;
71  
72      /**
73       * Sets the current context class loader from the given
74       * <code>jdbcDriverJar</code>
75       * @param options
76       * @return jdbcDriverJarLoader
77       * @throws MalformedURLException
78       */
79      protected ClassLoader getJdbcDriverJarLoader(final Map options)
80          throws MalformedURLException
81      {
82          final String jdbcDriverJar = this.getRequiredProperty(
83                  options,
84                  "jdbcDriverJar");
85          if (jdbcDriverJarLoader == null)
86          {
87              jdbcDriverJarLoader = new URLClassLoader(new URL[] {new File(jdbcDriverJar).toURI().toURL()});
88          }
89          return jdbcDriverJarLoader;
90      }
91  
92      /**
93       * @see org.andromda.maven.plugin.andromdapp.hibernate.HibernateSchemaManagement#getExecutionOuputPath(java.util.Map)
94       */
95      protected String getExecutionOuputPath(final Map options)
96      {
97          return null;
98      }
99  
100     /**
101      * @see org.andromda.maven.plugin.andromdapp.hibernate.HibernateSchemaManagement#getExecutionClassName()
102      */
103     protected String getExecutionClassName()
104     {
105         return "SchemaUpdate";
106     }
107 }