1 package org.codehaus.mojo.weblogic;
2 /*
3 * Copyright 2008 The Apache Software Foundation.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 import org.apache.maven.plugin.AbstractMojo;
19 import org.apache.maven.plugin.MojoExecutionException;
20 import org.apache.tools.ant.BuildListener;
21 import org.apache.tools.ant.DefaultLogger;
22 import org.apache.tools.ant.Project;
23
24 import java.io.File;
25 import java.util.List;
26 import java.util.Set;
27
28 /**
29 * This class wraps handling for common functions for all weblogic
30 * plugin implementations. Specifically, this class manages the
31 * all important 'weblogic.home' property.
32 *
33 * @author <a href="mailto:josborn@belltracy.com">Jon Osborn</a>
34 * @version $Id: AbstractWeblogicMojo.java 7803 2008-10-03 01:28:10Z jonnio $
35 * @description Abstract base class for weblogic mojo
36 */
37 public abstract class AbstractWeblogicMojo
38 extends AbstractMojo
39 {
40
41 /**
42 * Property key for weblogic.home
43 */
44 public static final String WEBLOGIC_HOME_KEY = "weblogic.home";
45
46 /**
47 * Property to set the weblogic home
48 *
49 * @parameter default-value="${weblogic.home}"
50 */
51 private String weblogicHome;
52
53 /**
54 * This is the set of artifacts that are defined as part of this project's
55 * pom which are active for the compile scope. You should not need to
56 * override this unless your pom file is incomplete.
57 *
58 * @parameter expression="${project.artifacts}"
59 * @required
60 * @readonly
61 */
62 private Set artifacts;
63
64 /**
65 * This is the set of dependencies that are defined as part of this project's
66 * pom which are active for the scope. You should not need to
67 * override this unless your pom file is incomplete.
68 *
69 * @parameter expression="${project.dependencies}"
70 * @required
71 * @readonly
72 */
73 private List dependencies;
74
75 /**
76 * These are the plugin artifacts for the weblogic mojo
77 *
78 * @parameter expression="${plugin.artifacts}"
79 */
80 private List pluginArtifacts;
81
82 /**
83 * This is the output directory for the artifacts. It defaults to
84 * $project.build.directory.
85 *
86 * @parameter expression="${project.build.directory}"
87 */
88 private String outputDirectory;
89
90 /**
91 * Getter for weblogic.home
92 *
93 * @return weblogic.home
94 */
95 public String getWeblogicHome()
96 {
97 return weblogicHome;
98 }
99
100 /**
101 * Setter for weblogic.home
102 *
103 * @param weblogicHome - a fully qualified path to weblogic home directory
104 */
105 public void setWeblogicHome( String weblogicHome )
106 {
107 this.weblogicHome = weblogicHome;
108 }
109
110 /**
111 * Getter for property artifacts.
112 *
113 * @return The value of artifacts.
114 */
115 public Set getArtifacts()
116 {
117 return artifacts;
118 }
119
120 /**
121 * Setter for the artifacts.
122 *
123 * @param inArtifacts The value of artifacts.
124 */
125 public final void setArtifacts( Set inArtifacts )
126 {
127 this.artifacts = inArtifacts;
128 }
129
130 /**
131 * Sets system property for weblogic.home
132 *
133 * @see #weblogicHome
134 */
135 public void execute()
136 throws MojoExecutionException
137 {
138 setProperties();
139 createTargetDirectory();
140 }
141
142 /**
143 * Sets the weblogic.home property
144 *
145 * @see #WEBLOGIC_HOME_KEY
146 * @see System#setProperty(String, String)
147 */
148 protected void setProperties()
149 {
150 if ( System.getProperty( WEBLOGIC_HOME_KEY ) == null ||
151 System.getProperty( WEBLOGIC_HOME_KEY ).trim().length() == 0 )
152 {
153 if ( getLog().isInfoEnabled() )
154 {
155 getLog().info( " Setting " + WEBLOGIC_HOME_KEY + " = " + this.weblogicHome );
156 }
157 if ( this.weblogicHome == null && getLog().isWarnEnabled() )
158 {
159 getLog().warn( " Is the property weblogicHome configured? Users should configure this property " +
160 "to help weblogic functions perform as expected." );
161 }
162 else if ( this.weblogicHome != null )
163 {
164 final File home = new File( this.weblogicHome );
165 if ( !home.exists() )
166 {
167 getLog().warn( "weblogic.home folder '" + this.weblogicHome + "' does not appear to exist. This " +
168 "may cause issues with some weblogic functions." );
169 }
170 System.setProperty( WEBLOGIC_HOME_KEY, this.weblogicHome );
171 }
172 }
173 else
174 {
175 if ( getLog().isDebugEnabled() )
176 {
177 getLog().debug(
178 "weblogicHome property was set externally to '" + System.getProperty( WEBLOGIC_HOME_KEY ) + "'." );
179 }
180 }
181 }
182
183 /**
184 * Generate a logger by using the default settings.
185 *
186 * @return a default logger that logs to System.out and System.err
187 */
188 protected BuildListener getDefaultLogger()
189 {
190 final DefaultLogger antLogger = new DefaultLogger();
191 antLogger.setOutputPrintStream( System.out );
192 antLogger.setErrorPrintStream( System.err );
193 antLogger
194 .setMessageOutputLevel( getLog().isDebugEnabled() ? Project.MSG_DEBUG : Project.MSG_INFO );
195 return antLogger;
196 }
197
198 /**
199 * Creates the target directory if it is missing from the file system.
200 */
201 protected void createTargetDirectory()
202 {
203 final File file = new File( this.outputDirectory );
204 if ( !file.exists() )
205 {
206 file.mkdir();
207 }
208 }
209
210 /**
211 * Getter for the outputDirectory
212 *
213 * @return - this.outputDirectory
214 */
215 public String getOutputDirectory()
216 {
217 return this.outputDirectory;
218 }
219
220 /**
221 * Setter for the output directory
222 *
223 * @param outputDirectory - the directory to write to
224 */
225 public void setOutputDirectory( String outputDirectory )
226 {
227 this.outputDirectory = outputDirectory;
228 }
229
230 /**
231 * Getter for the list of dependencies
232 *
233 * @return the dependencies
234 */
235 public List getDependencies()
236 {
237 return dependencies;
238 }
239
240 /**
241 * Setter for the dependencies
242 *
243 * @param dependencies the dependencies to set
244 */
245 public void setDependencies( List dependencies )
246 {
247 this.dependencies = dependencies;
248 }
249
250 /**
251 * The artifacts managed to the plugin
252 *
253 * @return the list of plugin artifacts
254 */
255 public List getPluginArtifacts()
256 {
257 return pluginArtifacts;
258 }
259
260 /**
261 * Setter for the plugin artifacts
262 *
263 * @param pluginArtifacts the artifacts to set
264 */
265 public void setPluginArtifacts( List pluginArtifacts )
266 {
267 this.pluginArtifacts = pluginArtifacts;
268 }
269
270 /**
271 * Meaningful toString
272 *
273 * @return the string representation of this object
274 */
275 public String toString()
276 {
277 return "AbstractWeblogicMojo{" + "weblogicHome='" + weblogicHome + '\'' + ", artifacts=" + artifacts +
278 ", dependencies=" + dependencies + ", pluginArtifacts=" + pluginArtifacts + ", outputDirectory='" +
279 outputDirectory + '\'' + '}';
280 }
281 }