1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.maven.plugin.eclipse.writers.rad; |
20 | |
|
21 | |
import java.io.File; |
22 | |
import java.io.FileInputStream; |
23 | |
import java.io.FileNotFoundException; |
24 | |
import java.io.FileOutputStream; |
25 | |
import java.io.IOException; |
26 | |
import java.io.InputStreamReader; |
27 | |
import java.io.OutputStreamWriter; |
28 | |
import java.io.Reader; |
29 | |
import java.io.Writer; |
30 | |
import java.util.Arrays; |
31 | |
import java.util.Comparator; |
32 | |
|
33 | |
import org.apache.maven.artifact.repository.ArtifactRepository; |
34 | |
import org.apache.maven.plugin.MojoExecutionException; |
35 | |
import org.apache.maven.plugin.eclipse.Constants; |
36 | |
import org.apache.maven.plugin.eclipse.EclipseSourceDir; |
37 | |
import org.apache.maven.plugin.eclipse.Messages; |
38 | |
import org.apache.maven.plugin.eclipse.writers.AbstractEclipseWriter; |
39 | |
import org.apache.maven.plugin.eclipse.writers.wtp.AbstractWtpResourceWriter; |
40 | |
import org.codehaus.plexus.util.IOUtil; |
41 | |
import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter; |
42 | |
import org.codehaus.plexus.util.xml.XMLWriter; |
43 | |
import org.codehaus.plexus.util.xml.Xpp3Dom; |
44 | |
import org.codehaus.plexus.util.xml.Xpp3DomBuilder; |
45 | |
import org.codehaus.plexus.util.xml.Xpp3DomWriter; |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | 0 | public class RadEjbClasspathWriter |
54 | |
extends AbstractEclipseWriter |
55 | |
{ |
56 | |
|
57 | |
private static final String CLASSPATH = "classpath"; |
58 | |
|
59 | |
private static final String CLASSPATH_FILE = ".classpath"; |
60 | |
|
61 | |
private static final String CLASSPATHENTRY = "classpathentry"; |
62 | |
|
63 | |
private static final String CON = "con"; |
64 | |
|
65 | |
private static final String KIND = "kind"; |
66 | |
|
67 | |
private static final String LIB = "lib"; |
68 | |
|
69 | |
private static final String OUTPUT = "output"; |
70 | |
|
71 | |
private static final String PATH = "path"; |
72 | |
|
73 | |
private static final String SRC = "src"; |
74 | |
|
75 | |
private static final String TARGET_WEBSPHERE_CLASSES = "target/websphere/generated-classes"; |
76 | |
|
77 | |
private static final String VAR = "var"; |
78 | |
|
79 | |
private static final String WEBSPHERE6CONTAIGNER = |
80 | |
"com.ibm.wtp.server.java.core.container/com.ibm.ws.ast.st.runtime.core.runtimeTarget.v60/was.base.v6"; |
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
public void write() |
92 | |
throws MojoExecutionException |
93 | |
{ |
94 | 0 | String packaging = config.getPackaging(); |
95 | 0 | if ( Constants.PROJECT_PACKAGING_EJB.equalsIgnoreCase( packaging ) ) |
96 | |
{ |
97 | 0 | new File( config.getEclipseProjectDirectory(), TARGET_WEBSPHERE_CLASSES ).mkdirs(); |
98 | 0 | File classpathFile = new File( config.getEclipseProjectDirectory(), CLASSPATH_FILE ); |
99 | |
|
100 | 0 | if ( !classpathFile.exists() ) |
101 | |
{ |
102 | 0 | return; |
103 | |
} |
104 | 0 | Xpp3Dom classpath = readXMLFile( classpathFile ); |
105 | 0 | Xpp3Dom[] children = classpath.getChildren(); |
106 | 0 | for ( int index = 0; index < children.length; index++ ) |
107 | |
{ |
108 | 0 | if ( LIB.equals( children[index].getAttribute( KIND ) ) && |
109 | |
TARGET_WEBSPHERE_CLASSES.equals( children[index].getAttribute( "path" ) ) ) |
110 | |
{ |
111 | 0 | return; |
112 | |
} |
113 | |
} |
114 | |
|
115 | 0 | Xpp3Dom newEntry = new Xpp3Dom( CLASSPATHENTRY ); |
116 | 0 | newEntry.setAttribute( KIND, LIB ); |
117 | 0 | newEntry.setAttribute( PATH, TARGET_WEBSPHERE_CLASSES ); |
118 | 0 | classpath.addChild( newEntry ); |
119 | |
|
120 | 0 | newEntry = new Xpp3Dom( CLASSPATHENTRY ); |
121 | 0 | newEntry.setAttribute( KIND, CON ); |
122 | 0 | newEntry.setAttribute( PATH, WEBSPHERE6CONTAIGNER ); |
123 | 0 | classpath.addChild( newEntry ); |
124 | |
|
125 | 0 | children = classpath.getChildren(); |
126 | 0 | for ( int index = children.length - 1; index >= 0; index-- ) |
127 | |
{ |
128 | 0 | if ( children[index].getValue() == null ) |
129 | |
{ |
130 | 0 | children[index].setValue( "" ); |
131 | |
} |
132 | |
} |
133 | |
|
134 | 0 | removeDupicateWAS6Libs( classpath ); |
135 | 0 | classpath = orderClasspath( classpath ); |
136 | |
|
137 | |
Writer w; |
138 | |
try |
139 | |
{ |
140 | 0 | w = new OutputStreamWriter( new FileOutputStream( classpathFile ), "UTF-8" ); |
141 | |
} |
142 | 0 | catch ( IOException ex ) |
143 | |
{ |
144 | 0 | throw new MojoExecutionException( Messages.getString( "EclipsePlugin.erroropeningfile" ), ex ); |
145 | 0 | } |
146 | 0 | XMLWriter writer = new PrettyPrintXMLWriter( w, "UTF-8", null ); |
147 | 0 | Xpp3DomWriter.write( writer, classpath ); |
148 | 0 | IOUtil.close( w ); |
149 | |
} |
150 | 0 | } |
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
private int detectClasspathEntryType( Xpp3Dom classpathentry ) |
160 | |
{ |
161 | 0 | String kind = classpathentry.getAttribute( KIND ); |
162 | 0 | String path = classpathentry.getAttribute( PATH ); |
163 | |
|
164 | 0 | if ( kind == null || path == null ) |
165 | |
{ |
166 | 0 | return 6; |
167 | |
} |
168 | |
|
169 | 0 | boolean absolutePath = path.startsWith( "\\" ) || path.startsWith( "/" ); |
170 | 0 | boolean windowsAbsolutePath = path.indexOf( ':' ) >= 0; |
171 | 0 | boolean anyAbsolutePath = absolutePath || windowsAbsolutePath; |
172 | |
|
173 | 0 | if ( kind.equals( SRC ) && !absolutePath ) |
174 | |
{ |
175 | 0 | return 1; |
176 | |
} |
177 | 0 | else if ( kind.equals( LIB ) && !anyAbsolutePath ) |
178 | |
{ |
179 | 0 | return 2; |
180 | |
} |
181 | 0 | else if ( kind.equals( SRC ) ) |
182 | |
{ |
183 | 0 | return 3; |
184 | |
} |
185 | 0 | else if ( kind.equals( VAR ) ) |
186 | |
{ |
187 | 0 | return 4; |
188 | |
} |
189 | 0 | else if ( kind.equals( LIB ) ) |
190 | |
{ |
191 | 0 | return 5; |
192 | |
} |
193 | 0 | else if ( kind.equals( OUTPUT ) ) |
194 | |
{ |
195 | 0 | return 7; |
196 | |
} |
197 | |
else |
198 | |
{ |
199 | 0 | return 6; |
200 | |
} |
201 | |
} |
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
private Xpp3Dom orderClasspath( Xpp3Dom classpath ) |
212 | |
{ |
213 | 0 | Xpp3Dom[] children = classpath.getChildren(); |
214 | 0 | Arrays.sort( children, new Comparator() |
215 | |
{ |
216 | 0 | public int compare( Object o1, Object o2 ) |
217 | |
{ |
218 | 0 | return detectClasspathEntryType( (Xpp3Dom) o1 ) - detectClasspathEntryType( (Xpp3Dom) o2 ); |
219 | |
} |
220 | |
} ); |
221 | 0 | Xpp3Dom resultClasspath = new Xpp3Dom( CLASSPATH ); |
222 | 0 | for ( int index = 0; index < children.length; index++ ) |
223 | |
{ |
224 | 0 | resultClasspath.addChild( children[index] ); |
225 | |
} |
226 | 0 | return resultClasspath; |
227 | |
} |
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
|
235 | |
private Xpp3Dom readXMLFile( File xmlFile ) |
236 | |
{ |
237 | |
try |
238 | |
{ |
239 | 0 | Reader reader = new InputStreamReader( new FileInputStream( xmlFile ), "UTF-8" ); |
240 | 0 | Xpp3Dom applicationXmlDom = Xpp3DomBuilder.build( reader ); |
241 | 0 | return applicationXmlDom; |
242 | |
} |
243 | 0 | catch ( FileNotFoundException e ) |
244 | |
{ |
245 | 0 | return null; |
246 | |
} |
247 | 0 | catch ( Exception e ) |
248 | |
{ |
249 | 0 | log.error( Messages.getString( "EclipsePlugin.cantreadfile", xmlFile.getAbsolutePath() ) ); |
250 | |
|
251 | 0 | return null; |
252 | |
} |
253 | |
} |
254 | |
|
255 | |
|
256 | |
|
257 | |
|
258 | |
|
259 | |
|
260 | |
private void removeDupicateWAS6Libs( Xpp3Dom classpath ) |
261 | |
{ |
262 | |
Xpp3Dom[] children; |
263 | 0 | children = classpath.getChildren(); |
264 | 0 | for ( int index = children.length - 1; index >= 0; index-- ) |
265 | |
{ |
266 | |
try |
267 | |
{ |
268 | 0 | File path = new File( children[index].getAttribute( PATH ) ); |
269 | |
|
270 | 0 | if ( path.exists() && path.getParentFile().getName().equals( LIB ) && |
271 | |
path.getParentFile().getParentFile().getName().equals( "base_v6" ) && |
272 | |
path.getParentFile().getParentFile().getParentFile().getName().equals( "runtimes" ) ) |
273 | |
{ |
274 | 0 | Xpp3Dom[] currentChildren = classpath.getChildren(); |
275 | 0 | for ( int deleteIndex = currentChildren.length - 1; deleteIndex >= 0; deleteIndex-- ) |
276 | |
{ |
277 | 0 | if ( currentChildren[deleteIndex] == children[index] ) |
278 | |
{ |
279 | 0 | classpath.removeChild( deleteIndex ); |
280 | 0 | break; |
281 | |
} |
282 | |
} |
283 | |
} |
284 | |
} |
285 | 0 | catch ( Exception e ) |
286 | |
{ |
287 | 0 | log.debug( e ); |
288 | 0 | } |
289 | |
} |
290 | 0 | } |
291 | |
|
292 | |
} |