1 package org.codehaus.mojo.weblogic.util;
2
3 /*
4 * Copyright 2006 The Apache Software Foundation.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 import org.apache.maven.artifact.Artifact;
20
21 import java.io.File;
22 import java.util.Iterator;
23 import java.util.List;
24 import java.util.Set;
25
26 /**
27 * This class contains some utilities that are useful during use of the Weblogic Mojo.
28 *
29 * @author <a href="mailto:scott@theryansplace.com">Scott Ryan</a>
30 * @author <a href="mailto:josborn@belltracy.com">Jon Osborn</a>
31 * @version $Id: WeblogicMojoUtilities.java 7025 2008-05-21 01:50:58Z jonnio $
32 */
33 public class WeblogicMojoUtilities
34 {
35
36 /**
37 * Creates a new WeblogicMojoUtilities object.
38 */
39 private WeblogicMojoUtilities()
40 {
41 super();
42 }
43
44 /**
45 * This method will contstruct the Admin URL to the given server.
46 *
47 * @param inProtocol The protocol to contact the server with (i.e. t3 or http)
48 * @param inServerName The name of the server to contact.
49 * @param inServerPort The listen port for the server to contact.
50 * @return The value of admin url.
51 */
52 public static String getAdminUrl( final String inProtocol, final String inServerName, final String inServerPort )
53 {
54 StringBuffer buffer = new StringBuffer();
55 buffer.append( inProtocol ).append( "://" );
56 buffer.append( inServerName );
57 buffer.append( ":" ).append( inServerPort );
58
59 return buffer.toString();
60 }
61
62 /**
63 * This method will make sure there is a type appended to the file name and if it is the appropriate type for the
64 * project packaging. If the project packaging is ear the artifact must end in .ear. If the project packaging is war
65 * then the artifact must end in .war. If the project packaging is ejb then the artifact must end in .jar.
66 *
67 * @param inName The name of the artifact.
68 * @param inProjectPackaging The type of packaging for this project.
69 * @return The updated artifact name.
70 */
71 public static String updateArtifactName( final String inName, final String inProjectPackaging )
72 {
73 String newName = inName;
74 // If project type is ear then artifact name must end in .ear
75 if ( inProjectPackaging.equalsIgnoreCase( "ear" ) )
76 {
77 if ( !inName.endsWith( ".ear" ) )
78 {
79 newName = inName.concat( ".ear" );
80 }
81 }
82 // If project type is war then artifact name must end in .war
83 else if ( inProjectPackaging.equalsIgnoreCase( "war" ) )
84 {
85 if ( !inName.endsWith( ".war" ) )
86 {
87 newName = inName.concat( ".war" );
88 }
89 }
90
91 // If project type is ejb then artifact name must end in .jar
92 else if ( inProjectPackaging.equalsIgnoreCase( "ejb" ) )
93 {
94 if ( inName.endsWith( ".ejb" ) )
95 {
96 newName = inName.replaceAll( "\\.ejb", ".jar" );
97 }
98 else if ( !inName.endsWith( ".jar" ) )
99 {
100 newName = inName.concat( ".jar" );
101 }
102 }
103 // Unsupported project type
104 else
105 {
106 throw new IllegalArgumentException( "Unsupported project packaging " + inProjectPackaging );
107 }
108 return newName;
109
110 }
111
112 /**
113 * This method will get the dependencies from the pom and construct a classpath string to be used to run a mojo
114 * where a classpath is required.
115 *
116 * @param inArtifacts The Set of artifacts for the pom being run.
117 * @return A string representing the current classpath for the pom.
118 */
119 public static String getDependencies( final Set inArtifacts )
120 {
121
122 if ( inArtifacts == null || inArtifacts.isEmpty() )
123 {
124 return "";
125 }
126 // Loop over all the artifacts and create a classpath string.
127 Iterator iter = inArtifacts.iterator();
128
129 StringBuffer buffer = new StringBuffer();
130 if ( iter.hasNext() )
131 {
132 Artifact artifact = (Artifact) iter.next();
133 buffer.append( artifact.getFile() );
134
135 while ( iter.hasNext() )
136 {
137 artifact = (Artifact) iter.next();
138 buffer.append( System.getProperty( "path.separator" ) );
139 buffer.append( artifact.getFile() );
140 }
141 }
142
143 return buffer.toString();
144 }
145
146 /**
147 * Returns the fully qualified path to an ear file int the artifact list.
148 *
149 * @param inArtifacts - the set of artifacts
150 * @return the fully qualified path to an ear file int the artifact list.
151 */
152 public static File getEarFileName( final Set inArtifacts )
153 {
154 if ( inArtifacts == null || inArtifacts.isEmpty() )
155 {
156 throw new IllegalArgumentException( "EAR not found in artifact list." );
157 }
158
159 final Iterator iter = inArtifacts.iterator();
160 while ( iter.hasNext() )
161 {
162 Artifact artifact = (Artifact) iter.next();
163 if ( "ear".equals( artifact.getType() ) )
164 {
165 return artifact.getFile();
166 }
167 }
168 throw new IllegalArgumentException( "EAR not found in artifact list." );
169 }
170
171 /**
172 * Returns the fully qualified path to a war file in the artifact list.
173 *
174 * @param inArtifacts - the set of artifacts
175 * @return the fully qualified path to an war file in the artifact list.
176 * @throws IllegalArgumentException - when a war is not found
177 */
178 public static File getWarFileName( final Set inArtifacts )
179 {
180 if ( inArtifacts == null || inArtifacts.isEmpty() )
181 {
182 throw new IllegalArgumentException( "WAR not found in artifact list." );
183 }
184
185 final Iterator iter = inArtifacts.iterator();
186 while ( iter.hasNext() )
187 {
188 Artifact artifact = (Artifact) iter.next();
189 if ( "war".equals( artifact.getType() ) )
190 {
191 return artifact.getFile();
192 }
193 }
194 throw new IllegalArgumentException( "WAR not found in artifact list." );
195 }
196
197 /**
198 * Returns the fully qualified path to an war file in the artifact list.
199 *
200 * @param inArtifacts - the set of artifacts
201 * @param fileName - the file name we are looking for in the aftifact list
202 * @return the fully qualified path to an war file in the artifact list.
203 */
204 public static File getWarFileName( final Set inArtifacts, String fileName )
205 {
206 if ( inArtifacts == null || inArtifacts.isEmpty() )
207 {
208 throw new IllegalArgumentException( "WAR not found in artifact list." );
209 }
210
211 final Iterator iter = inArtifacts.iterator();
212 while ( iter.hasNext() )
213 {
214 Artifact artifact = (Artifact) iter.next();
215
216 if ( "war".equals( artifact.getType() ) && artifact.getFile().getName().contains( fileName ) )
217 {
218 return artifact.getFile();
219 }
220 }
221 throw new IllegalArgumentException( "WAR not found in artifact list." );
222 }
223
224 /**
225 * Returns the ejb file type from the artifact list
226 *
227 * @param inArtifacts - the dependency artifacts
228 * @return the File object corresponding to the ejb jar type from the artifact list
229 */
230 public static File getEjbJarFileName( final Set inArtifacts )
231 {
232 if ( inArtifacts == null || inArtifacts.isEmpty() )
233 {
234 throw new IllegalArgumentException( "EJB jar not found in artifact list." );
235 }
236
237 final Iterator iter = inArtifacts.iterator();
238 while ( iter.hasNext() )
239 {
240 Artifact artifact = (Artifact) iter.next();
241 if ( "ejb".equals( artifact.getType() ) )
242 {
243 return artifact.getFile();
244 }
245 }
246 throw new IllegalArgumentException( "EJB jar not found in artifact list." );
247 }
248
249 /**
250 * This method will get the PLUGIN dependencies from the pom and construct a
251 * classpath string to be used to run a mojo where a classpath is required.
252 * <p>The plugin dependencies are placed after the project dependencies in
253 * the classpath.</p>
254 *
255 * @param artifacts The Set of artifacts for the pom being run.
256 * @param pluginArtifacts the plugin artifacts
257 * @return A string representing the current classpath for the pom.
258 */
259 public static String getDependencies( final Set artifacts, final List pluginArtifacts )
260 {
261
262 if ( ( artifacts == null || artifacts.isEmpty() ) &&
263 ( pluginArtifacts == null || pluginArtifacts.size() == 0 ) )
264 {
265 return "";
266 }
267 // Loop over all the artifacts and create a classpath string.
268 final Iterator iter = artifacts.iterator();
269
270 final StringBuffer buffer = new StringBuffer( 1024 );
271 if ( iter.hasNext() )
272 {
273 Artifact artifact = (Artifact) iter.next();
274 buffer.append( artifact.getFile() );
275
276 while ( iter.hasNext() )
277 {
278 artifact = (Artifact) iter.next();
279 buffer.append( System.getProperty( "path.separator" ) );
280 buffer.append( artifact.getFile() );
281 }
282 }
283 //now get the plugin artifacts into the list
284 final Iterator pluginIter = pluginArtifacts.iterator();
285 if ( pluginIter.hasNext() )
286 {
287 Artifact artifact = (Artifact) pluginIter.next();
288 if ( buffer.length() > 0 )
289 {
290 buffer.append( System.getProperty( "path.separator" ) );
291 }
292 buffer.append( artifact.getFile() );
293
294 while ( pluginIter.hasNext() )
295 {
296 artifact = (Artifact) pluginIter.next();
297 buffer.append( System.getProperty( "path.separator" ) );
298 buffer.append( artifact.getFile() );
299 }
300 }
301
302 return buffer.toString();
303 }
304
305 }