1 | |
package org.apache.maven.plugin.eclipse.writers.myeclipse; |
2 | |
|
3 | |
import java.io.File; |
4 | |
import java.io.FileWriter; |
5 | |
import java.io.IOException; |
6 | |
import java.io.PrintWriter; |
7 | |
import java.util.Map; |
8 | |
|
9 | |
import org.apache.maven.plugin.MojoExecutionException; |
10 | |
import org.apache.maven.plugin.eclipse.Messages; |
11 | |
import org.apache.maven.plugin.eclipse.writers.AbstractEclipseWriter; |
12 | |
import org.apache.maven.plugin.ide.IdeUtils; |
13 | |
import org.codehaus.plexus.util.IOUtil; |
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
public class MyEclipseHibernateWriter |
21 | |
extends AbstractEclipseWriter |
22 | |
{ |
23 | |
|
24 | |
private static final String MYECLIPSE_HIBERNATE_DATA_FILE = ".myhibernatedata"; |
25 | |
|
26 | |
private static final String MYECLIPSE_HB_GEN_COMP_ID = "genBasicCompId"; |
27 | |
|
28 | |
private static final String MYECLIPSE_HB_SESSION_FACTORY_NAME = "sessionFactoryName"; |
29 | |
|
30 | |
private static final String MYECLIPSE_HB_PROFILE = "profile"; |
31 | |
|
32 | |
private static final String MYECLIPSE_HB_DAO_SF_ID = "daoSFId"; |
33 | |
|
34 | |
private static final String MYECLIPSE_HB_VERSION = "version"; |
35 | |
|
36 | |
private static final String MYECLIPSE_HB_JNDI_PATH = "jndiPath"; |
37 | |
|
38 | |
private static final String MYECLIPSE_HB_DETECT_M2M = "detectM2M"; |
39 | |
|
40 | |
private static final String MYECLIPSE_HB_RE_STRATEGY_CLASS = "reStrategyClass"; |
41 | |
|
42 | |
private static final String MYECLIPSE_HB_SPRING_DAO_FILE = "springDaoFile"; |
43 | |
|
44 | |
private static final String MYECLIPSE_HB_USE_JAVA_TYPES = "useJavaTypes"; |
45 | |
|
46 | |
private static final String MYECLIPSE_HB_KEY_GENERATOR = "keyGenerator"; |
47 | |
|
48 | |
private static final String MYECLIPSE_HB_LIB_INSTALL_FOLDER = "libInstallFolder"; |
49 | |
|
50 | |
private static final String MYECLIPSE_HB_ADD_LIBS_TO_PROJECT = "addLibs2Project"; |
51 | |
|
52 | |
private static final String MYECLIPSE_HB_GEN_VERSION_TAG = "genVersionTag"; |
53 | |
|
54 | |
private static final String MYECLIPSE_HB_SESSION_FACTORY_ID = "sessionFactoryId"; |
55 | |
|
56 | |
private static final String MYECLIPSE_HB_BASE_PERSISTENT_CLASS = "basePersistenceClass"; |
57 | |
|
58 | |
private static final String MYECLIPSE_HB_RE_SETTINGS_FILE = "reSettingsFile"; |
59 | |
|
60 | |
private static final String MYECLIPSE_HB_CONFIG_FILE = "configFile"; |
61 | |
|
62 | |
private static final String MYECLIPSE_HB_CREATE_CONFIG_FILE = "createConfigFile"; |
63 | |
|
64 | |
private static final String MYECLIPSE_HB_ADD_LIBS_TO_CLASSPATH = "addLibs2Classpath"; |
65 | |
|
66 | |
private static final String MYECLIPSE_HB_BASE_DAO_CLASS = "baseDaoClass"; |
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
private Map hibernateConfig; |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
public MyEclipseHibernateWriter( Map hibernateConfig ) |
79 | 0 | { |
80 | 0 | this.hibernateConfig = hibernateConfig; |
81 | 0 | } |
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
public void write() |
89 | |
throws MojoExecutionException |
90 | |
{ |
91 | |
FileWriter w; |
92 | |
|
93 | |
try |
94 | |
{ |
95 | 0 | w = new FileWriter( new File( config.getEclipseProjectDirectory(), MYECLIPSE_HIBERNATE_DATA_FILE ) ); |
96 | |
} |
97 | 0 | catch ( IOException ex ) |
98 | |
{ |
99 | 0 | throw new MojoExecutionException( Messages.getString( "EclipsePlugin.erroropeningfile" ), ex ); |
100 | 0 | } |
101 | |
|
102 | 0 | PrintWriter pw = new PrintWriter( w ); |
103 | |
|
104 | 0 | pw.println( "# Generated by Maven" ); |
105 | 0 | addProperty( pw, MYECLIPSE_HB_GEN_COMP_ID, "false" ); |
106 | 0 | addProperty( pw, MYECLIPSE_HB_SESSION_FACTORY_NAME, null ); |
107 | 0 | addProperty( pw, MYECLIPSE_HB_PROFILE, null ); |
108 | 0 | addProperty( pw, MYECLIPSE_HB_DAO_SF_ID, null ); |
109 | 0 | addProperty( pw, MYECLIPSE_HB_VERSION, getHibernateVersion() ); |
110 | 0 | addProperty( pw, MYECLIPSE_HB_JNDI_PATH, null ); |
111 | 0 | addProperty( pw, MYECLIPSE_HB_DETECT_M2M, "false" ); |
112 | 0 | addProperty( pw, MYECLIPSE_HB_RE_STRATEGY_CLASS, null ); |
113 | 0 | addProperty( pw, MYECLIPSE_HB_SPRING_DAO_FILE, null ); |
114 | 0 | addProperty( pw, MYECLIPSE_HB_USE_JAVA_TYPES, "true" ); |
115 | 0 | addProperty( pw, MYECLIPSE_HB_KEY_GENERATOR, null ); |
116 | 0 | addProperty( pw, MYECLIPSE_HB_LIB_INSTALL_FOLDER, null ); |
117 | 0 | addProperty( pw, MYECLIPSE_HB_ADD_LIBS_TO_PROJECT, "false" ); |
118 | 0 | addProperty( pw, MYECLIPSE_HB_GEN_VERSION_TAG, "false" ); |
119 | 0 | addProperty( pw, MYECLIPSE_HB_SESSION_FACTORY_ID, (String) hibernateConfig.get( "session-factory-id" ) ); |
120 | 0 | addProperty( pw, MYECLIPSE_HB_BASE_PERSISTENT_CLASS, null ); |
121 | 0 | addProperty( pw, MYECLIPSE_HB_RE_SETTINGS_FILE, null ); |
122 | 0 | addProperty( pw, MYECLIPSE_HB_CONFIG_FILE, |
123 | |
makePathToHibernateConfigFile( (String) hibernateConfig.get( "config-file" ) ) ); |
124 | 0 | addProperty( pw, MYECLIPSE_HB_CREATE_CONFIG_FILE, "false" ); |
125 | 0 | addProperty( pw, MYECLIPSE_HB_ADD_LIBS_TO_CLASSPATH, "false" ); |
126 | 0 | addProperty( pw, MYECLIPSE_HB_BASE_DAO_CLASS, null ); |
127 | |
|
128 | 0 | IOUtil.close( w ); |
129 | 0 | } |
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
private void addProperty( PrintWriter pw, String propName, String propValue ) |
139 | |
{ |
140 | 0 | StringBuffer sb = new StringBuffer( 64 ); |
141 | |
|
142 | 0 | sb.append( propName ).append( "=" ); |
143 | |
|
144 | 0 | if ( propValue != null ) |
145 | |
{ |
146 | 0 | sb.append( propValue ); |
147 | |
} |
148 | |
|
149 | 0 | pw.println( sb.toString() ); |
150 | 0 | } |
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
private String getHibernateVersion() |
158 | |
{ |
159 | 0 | String version = |
160 | |
IdeUtils.getArtifactVersion( new String[] { "hibernate" }, config.getProject().getDependencies(), 3 ); |
161 | |
|
162 | 0 | return version != null ? version : "3.2"; |
163 | |
} |
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
private String makePathToHibernateConfigFile( String configFile ) |
171 | |
{ |
172 | 0 | StringBuffer sb = new StringBuffer( 64 ); |
173 | |
|
174 | 0 | sb.append( "/" ).append( config.getProject().getArtifactId() ).append( "/" ).append( configFile ); |
175 | |
|
176 | 0 | return sb.toString(); |
177 | |
} |
178 | |
} |