001package org.andromda.maven.plugin.andromdapp.hibernate; 002 003import java.io.File; 004import java.net.MalformedURLException; 005import java.net.URL; 006import java.net.URLClassLoader; 007import java.util.List; 008import java.util.Map; 009import java.util.Random; 010import org.andromda.core.common.Constants; 011import org.andromda.core.common.ResourceWriter; 012import org.apache.commons.lang.StringUtils; 013 014/** 015 * Provides the ability to create a schema from Hibernate 016 * mapping files. 017 * 018 * @author Chad Brandon 019 */ 020public class HibernateUpdateSchema 021 extends HibernateSchemaManagement 022{ 023 /** 024 * The temporary directory. 025 */ 026 private static final String HIBERNATE_PROPERTIES_TEMP_DIRECTORY = 027 Constants.TEMPORARY_DIRECTORY + "andromdapp/hibernate-schema-update"; 028 029 private static final Random random = new Random(); 030 /** 031 * @see org.andromda.maven.plugin.andromdapp.hibernate.HibernateSchemaManagement#addArguments(java.util.Map, java.util.List) 032 */ 033 protected void addArguments( 034 final Map options, 035 final List<String> arguments) 036 throws Exception 037 { 038 final String driverClass = this.getRequiredProperty( 039 options, 040 "jdbcDriver"); 041 final String connectionUrl = this.getRequiredProperty( 042 options, 043 "jdbcConnectionUrl"); 044 final String username = this.getRequiredProperty( 045 options, 046 "jdbcUsername"); 047 final String password = this.getProperty( 048 options, 049 "jdbcPassword"); 050 final StringBuilder contents = new StringBuilder(); 051 contents.append("hibernate.connection.driver_class=").append(driverClass).append('\n'); 052 contents.append("hibernate.connection.url=").append(connectionUrl).append('\n'); 053 contents.append("hibernate.connection.username=").append(username).append('\n'); 054 if(StringUtils.isNotBlank(password)) 055 { 056 contents.append("hibernate.connection.password=").append(password).append('\n'); 057 } 058 final File temporaryProperitesFile = 059 new File(HIBERNATE_PROPERTIES_TEMP_DIRECTORY, String.valueOf(random.nextDouble())); 060 temporaryProperitesFile.deleteOnExit(); 061 ResourceWriter.instance().writeStringToFile( 062 contents.toString(), 063 temporaryProperitesFile.toString()); 064 arguments.add("--properties=" + temporaryProperitesFile); 065 } 066 067 /** 068 * The class loader containing the jdbc driver. 069 */ 070 private ClassLoader jdbcDriverJarLoader = null; 071 072 /** 073 * Sets the current context class loader from the given 074 * <code>jdbcDriverJar</code> 075 * @param options 076 * @return jdbcDriverJarLoader 077 * @throws MalformedURLException 078 */ 079 protected ClassLoader getJdbcDriverJarLoader(final Map options) 080 throws MalformedURLException 081 { 082 final String jdbcDriverJar = this.getRequiredProperty( 083 options, 084 "jdbcDriverJar"); 085 if (jdbcDriverJarLoader == null) 086 { 087 jdbcDriverJarLoader = new URLClassLoader(new URL[] {new File(jdbcDriverJar).toURI().toURL()}); 088 } 089 return jdbcDriverJarLoader; 090 } 091 092 /** 093 * @see org.andromda.maven.plugin.andromdapp.hibernate.HibernateSchemaManagement#getExecutionOuputPath(java.util.Map) 094 */ 095 protected String getExecutionOuputPath(final Map options) 096 { 097 return null; 098 } 099 100 /** 101 * @see org.andromda.maven.plugin.andromdapp.hibernate.HibernateSchemaManagement#getExecutionClassName() 102 */ 103 protected String getExecutionClassName() 104 { 105 return "SchemaUpdate"; 106 } 107}