View Javadoc
1   package org.apache.maven.plugin.eclipse.writers.myeclipse;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   * 
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  
23  import java.io.File;
24  import java.io.FileWriter;
25  import java.io.IOException;
26  import java.io.PrintWriter;
27  import java.util.Map;
28  
29  import org.apache.maven.plugin.MojoExecutionException;
30  import org.apache.maven.plugin.eclipse.Messages;
31  import org.apache.maven.plugin.eclipse.writers.AbstractEclipseWriter;
32  import org.apache.maven.plugin.ide.IdeUtils;
33  import org.codehaus.plexus.util.IOUtil;
34  
35  /**
36   * Writer for MyEclipse Hibernate Capability
37   * 
38   * @author <a href="mailto:olivier.jacob@gmail.com">Olivier Jacob</a>
39   */
40  public class MyEclipseHibernateWriter
41      extends AbstractEclipseWriter
42  {
43  
44      private static final String MYECLIPSE_HIBERNATE_DATA_FILE = ".myhibernatedata";
45  
46      private static final String MYECLIPSE_HB_GEN_COMP_ID = "genBasicCompId";
47  
48      private static final String MYECLIPSE_HB_SESSION_FACTORY_NAME = "sessionFactoryName";
49  
50      private static final String MYECLIPSE_HB_PROFILE = "profile";
51  
52      private static final String MYECLIPSE_HB_DAO_SF_ID = "daoSFId";
53  
54      private static final String MYECLIPSE_HB_VERSION = "version";
55  
56      private static final String MYECLIPSE_HB_JNDI_PATH = "jndiPath";
57  
58      private static final String MYECLIPSE_HB_DETECT_M2M = "detectM2M";
59  
60      private static final String MYECLIPSE_HB_RE_STRATEGY_CLASS = "reStrategyClass";
61  
62      private static final String MYECLIPSE_HB_SPRING_DAO_FILE = "springDaoFile";
63  
64      private static final String MYECLIPSE_HB_USE_JAVA_TYPES = "useJavaTypes";
65  
66      private static final String MYECLIPSE_HB_KEY_GENERATOR = "keyGenerator";
67  
68      private static final String MYECLIPSE_HB_LIB_INSTALL_FOLDER = "libInstallFolder";
69  
70      private static final String MYECLIPSE_HB_ADD_LIBS_TO_PROJECT = "addLibs2Project";
71  
72      private static final String MYECLIPSE_HB_GEN_VERSION_TAG = "genVersionTag";
73  
74      private static final String MYECLIPSE_HB_SESSION_FACTORY_ID = "sessionFactoryId";
75  
76      private static final String MYECLIPSE_HB_BASE_PERSISTENT_CLASS = "basePersistenceClass";
77  
78      private static final String MYECLIPSE_HB_RE_SETTINGS_FILE = "reSettingsFile";
79  
80      private static final String MYECLIPSE_HB_CONFIG_FILE = "configFile";
81  
82      private static final String MYECLIPSE_HB_CREATE_CONFIG_FILE = "createConfigFile";
83  
84      private static final String MYECLIPSE_HB_ADD_LIBS_TO_CLASSPATH = "addLibs2Classpath";
85  
86      private static final String MYECLIPSE_HB_BASE_DAO_CLASS = "baseDaoClass";
87  
88      /**
89       * Relative path to the Hibernate configuration file to use in MyEclipse
90       */
91      private Map hibernateConfig;
92  
93      /**
94       * Constructor
95       * 
96       * @param hibernateConfig path to the configuration file to use
97       */
98      public MyEclipseHibernateWriter( Map hibernateConfig )
99      {
100         this.hibernateConfig = hibernateConfig;
101     }
102 
103     /**
104      * Write MyEclipse Hibernate configuration
105      * 
106      * @throws MojoExecutionException if an error occurs
107      */
108     public void write()
109         throws MojoExecutionException
110     {
111         FileWriter w;
112 
113         try
114         {
115             w = new FileWriter( new File( config.getEclipseProjectDirectory(), MYECLIPSE_HIBERNATE_DATA_FILE ) );
116         }
117         catch ( IOException ex )
118         {
119             throw new MojoExecutionException( Messages.getString( "EclipsePlugin.erroropeningfile" ), ex ); //$NON-NLS-1$
120         }
121 
122         PrintWriter pw = new PrintWriter( w );
123 
124         pw.println( "# Generated by Maven" );
125         addProperty( pw, MYECLIPSE_HB_GEN_COMP_ID, "false" );
126         addProperty( pw, MYECLIPSE_HB_SESSION_FACTORY_NAME, null );
127         addProperty( pw, MYECLIPSE_HB_PROFILE, null );
128         addProperty( pw, MYECLIPSE_HB_DAO_SF_ID, null );
129         addProperty( pw, MYECLIPSE_HB_VERSION, getHibernateVersion() );
130         addProperty( pw, MYECLIPSE_HB_JNDI_PATH, null );
131         addProperty( pw, MYECLIPSE_HB_DETECT_M2M, "false" );
132         addProperty( pw, MYECLIPSE_HB_RE_STRATEGY_CLASS, null );
133         addProperty( pw, MYECLIPSE_HB_SPRING_DAO_FILE, null );
134         addProperty( pw, MYECLIPSE_HB_USE_JAVA_TYPES, "true" );
135         addProperty( pw, MYECLIPSE_HB_KEY_GENERATOR, null );
136         addProperty( pw, MYECLIPSE_HB_LIB_INSTALL_FOLDER, null );
137         addProperty( pw, MYECLIPSE_HB_ADD_LIBS_TO_PROJECT, "false" );
138         addProperty( pw, MYECLIPSE_HB_GEN_VERSION_TAG, "false" );
139         addProperty( pw, MYECLIPSE_HB_SESSION_FACTORY_ID, (String) hibernateConfig.get( "session-factory-id" ) );
140         addProperty( pw, MYECLIPSE_HB_BASE_PERSISTENT_CLASS, null );
141         addProperty( pw, MYECLIPSE_HB_RE_SETTINGS_FILE, null );
142         addProperty( pw, MYECLIPSE_HB_CONFIG_FILE,
143                      makePathToHibernateConfigFile( (String) hibernateConfig.get( "config-file" ) ) );
144         addProperty( pw, MYECLIPSE_HB_CREATE_CONFIG_FILE, "false" );
145         addProperty( pw, MYECLIPSE_HB_ADD_LIBS_TO_CLASSPATH, "false" );
146         addProperty( pw, MYECLIPSE_HB_BASE_DAO_CLASS, null );
147 
148         IOUtil.close( w );
149     }
150 
151     /**
152      * Writes a configuration property to the PrintWriter given in parameter
153      * 
154      * @param pw the PrintWriter to write to
155      * @param propName the property name
156      * @param propValue the property value (writes empty String if null)
157      */
158     private void addProperty( PrintWriter pw, String propName, String propValue )
159     {
160         StringBuilder sb = new StringBuilder( 64 );
161 
162         sb.append( propName ).append( "=" );
163 
164         if ( propValue != null )
165         {
166             sb.append( propValue );
167         }
168 
169         pw.println( sb.toString() );
170     }
171 
172     /**
173      * Find Hibernate version in project dependencies
174      * 
175      * @return the version of the hibernate artifact if found in the dependencies or 3.2 (default value)
176      */
177     private String getHibernateVersion()
178     {
179         String version =
180             IdeUtils.getArtifactVersion( new String[] { "hibernate" }, config.getProject().getDependencies(), 3 );
181 
182         return version != null ? version : "3.2";
183     }
184 
185     /**
186      * Prepend the project artifactId to the path given in the plugin configuration
187      * 
188      * @return the path to the file relative to the root of the Eclipse Workspace
189      */
190     private String makePathToHibernateConfigFile( String configFile )
191     {
192 
193         return "/" + config.getProject().getArtifactId() + "/" + configFile;
194     }
195 }