1 package org.codehaus.mojo.weblogic;
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.plugin.MojoExecutionException;
20 import org.codehaus.mojo.weblogic.util.WeblogicMojoUtilities;
21 import weblogic.Deployer;
22
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.StringTokenizer;
26
27 /**
28 * This class is a base class for all deployment related mojos.
29 *
30 * @author <a href="mailto:scott@theryansplace.com">Scott Ryan</a>
31 * @version $Id: DeployMojoBase.java 7099 2008-05-31 19:30:04Z jonnio $
32 * @description Deploy an artifact (war, ear, etc) to a target(s) which can be servers or clusters.
33 */
34 public abstract class DeployMojoBase
35 extends AbstractWeblogicMojo
36 {
37
38 /**
39 * The dns hostname of the Weblogic Admin server.
40 *
41 * @parameter expression="${weblogic.adminServer.hostName}" default-value="localhost"
42 */
43 private String adminServerHostName;
44
45 /**
46 * The protocol to use to access the Weblogic Admin server for deployment.
47 *
48 * @parameter expression="${weblogic.adminServer.protocol}" default-value="t3"
49 */
50 private String adminServerProtocol;
51
52 /**
53 * The admin port of the Weblogic Admin Server.
54 *
55 * @parameter expression="${weblogic.adminServer.port}" default-value="7001"
56 */
57 private String adminServerPort;
58
59 /**
60 * The Admin UserId to access the Weblogic Admin server for deployment.
61 *
62 * @parameter expression="${weblogic.user}" default-value="weblogic"
63 */
64 private String userId;
65
66 /**
67 * The admin password to access the Weblogic Admin server for deployment.
68 *
69 * @parameter expression="${weblogic.password}" default-value="weblogic"
70 */
71 private String password;
72
73 /**
74 * The full path to artifact to be deployed.
75 *
76 * @parameter expression="${project.build.directory}/${project.build.finalName}"
77 */
78 private String artifactPath;
79
80 /**
81 * The project packaging used to check the suffix on the artifact.
82 *
83 * @parameter expression="${project.packaging}"
84 */
85 private String projectPackaging;
86
87 /**
88 * The name to use when deploying the object.
89 *
90 * @parameter expression="${project.artifactId}"
91 */
92 private String name;
93
94 /**
95 * A comma seperated list of names of servers or clusters to deploy the artifact onto.
96 *
97 * @parameter expression="${weblogic.targetNames}" default-value="AdminServer"
98 */
99 private String targetNames;
100
101 /**
102 * True if you are running on a machine that is remote to the admin server. If this is a remote deployment and this
103 * is set to false then it is assumed that all source paths are valid paths on the admin server.
104 *
105 * @parameter default-value="false"
106 */
107 private boolean remote;
108
109 /**
110 * True if you want to turn off staging
111 *
112 * @parameter default-value="true"
113 */
114 private boolean stage;
115
116 /**
117 * True to turn on debugging
118 *
119 * @parameter default-value="false"
120 */
121 private boolean debug;
122
123 /**
124 * True to turn on debugging
125 *
126 * @parameter default-value="true"
127 */
128 private boolean verbose;
129
130
131 /**
132 * Set to true to upload the code.
133 *
134 * @parameter default-value="false"
135 */
136 private boolean upload;
137
138 /**
139 * If set to true, the deployer will not stop if a failure is detected.
140 *
141 * @parameter default-value="false"
142 */
143 private boolean continueOnFailure;
144
145 /**
146 * Set this value to true so weblogic will return immediately
147 *
148 * @parameter default-value="false"
149 */
150 private boolean noWait;
151
152
153 /**
154 * Set this value to the timeout seconds. Set to <=0 to disable
155 *
156 * @parameter default-value="-1"
157 */
158 private int timeout;
159
160
161 /**
162 * Print the version for weblogic
163 *
164 * @parameter default-value="false"
165 */
166 private boolean version;
167
168 /**
169 * Tell weblogic to ignore the versions an apply the task to all versions.
170 *
171 * @parameter default-value="false"
172 */
173 private boolean noVersion;
174
175 /**
176 * tell weblogic to not exit if there is a deployment failure.
177 *
178 * @parameter default-value="false"
179 */
180 private boolean noExit;
181
182 /**
183 * Deploy the target as an exploded directory. The target should be the
184 * target directory.
185 *
186 * @parameter expression="${weblogic.exploded}" default-value="false"
187 */
188 private boolean exploded;
189
190 /**
191 * This method will perform the deployment of the object to the proper server url.
192 *
193 * @throws MojoExecutionException Thrown if we fail to obtain a Weblogic deployment instance.
194 */
195 public void execute()
196 throws MojoExecutionException
197 {
198 super.execute();
199
200 if ( getLog().isWarnEnabled() )
201 {
202 getLog().warn( "Running the Deploy Mojo Base without an implementation" );
203 }
204
205 //throw new MojoExecutionException( "Unimplemented Mojo Base" );
206 }
207
208 /**
209 * Returns the value for the admin server host name property.
210 *
211 * @return The value of the admin server host name property.
212 */
213 public String getAdminServerHostName()
214 {
215 return this.adminServerHostName;
216 }
217
218 /**
219 * Sets the value of the admin server host name property.
220 *
221 * @param inAdminServerHostName The new value of the admin server host name property.
222 */
223 public void setAdminServerHostName( final String inAdminServerHostName )
224 {
225 this.adminServerHostName = inAdminServerHostName;
226 }
227
228 /**
229 * Returns the value for the admin server port property.
230 *
231 * @return The value of the admin server port property.
232 */
233 public String getAdminServerPort()
234 {
235 return this.adminServerPort;
236 }
237
238 /**
239 * Sets the value of the admin server port property.
240 *
241 * @param inAdminServerPort The new value of the admin server port property.
242 */
243 public void setAdminServerPort( final String inAdminServerPort )
244 {
245 this.adminServerPort = inAdminServerPort;
246 }
247
248 /**
249 * Returns the value for the name property.
250 *
251 * @return The value of the name property.
252 */
253 public String getName()
254 {
255 return this.name;
256 }
257
258 /**
259 * Sets the value of the name property.
260 *
261 * @param inName The new value of the name property.
262 */
263 public void setName( final String inName )
264 {
265 this.name = inName;
266 }
267
268 /**
269 * Returns the value for the password property.
270 *
271 * @return The value of the password property.
272 */
273 public String getPassword()
274 {
275 return this.password;
276 }
277
278 /**
279 * Sets the value of the password property.
280 *
281 * @param inPassword The new value of the password property.
282 */
283 public void setPassword( final String inPassword )
284 {
285 this.password = inPassword;
286 }
287
288 /**
289 * Returns the value for the target names property.
290 *
291 * @return The value of the target names property.
292 */
293 public String getTargetNames()
294 {
295 return this.targetNames;
296 }
297
298 /**
299 * Sets the value of the target names property.
300 *
301 * @param inTargetNames The new value of the target names property.
302 */
303 public void setTargetNames( final String inTargetNames )
304 {
305 this.targetNames = inTargetNames;
306 }
307
308 /**
309 * Returns the value for the user id property.
310 *
311 * @return The value of the user id property.
312 */
313 public String getUserId()
314 {
315 return this.userId;
316 }
317
318 /**
319 * Sets the value of the user id property.
320 *
321 * @param inUserId The new value of the user id property.
322 */
323 public void setUserId( final String inUserId )
324 {
325 this.userId = inUserId;
326 }
327
328 /**
329 * This method will return a list of target by parsing the comma separated list of targets..
330 *
331 * @return The value of target list.
332 */
333 public List getTargetList()
334 {
335 List targets = new ArrayList();
336 StringTokenizer tokens = new StringTokenizer( this.getTargetNames(), "," );
337
338 while ( tokens.hasMoreTokens() )
339 {
340 targets.add( tokens.nextToken() );
341 }
342
343 return targets;
344 }
345
346 /**
347 * Getter for property remote.
348 *
349 * @return The value of remote.
350 */
351 public boolean isRemote()
352 {
353 return this.remote;
354 }
355
356 /**
357 * Setter for the remote.
358 *
359 * @param inRemote The value of remote.
360 */
361 public void setRemote( final boolean inRemote )
362 {
363 this.remote = inRemote;
364 }
365
366 /**
367 * Getter for property admin server protocol.
368 *
369 * @return The value of admin server protocol.
370 */
371 public String getAdminServerProtocol()
372 {
373 return this.adminServerProtocol;
374 }
375
376 /**
377 * Setter for the admin server protocol.
378 *
379 * @param inAdminServerProtocol The value of admin server protocol.
380 */
381 public void setAdminServerProtocol( final String inAdminServerProtocol )
382 {
383 this.adminServerProtocol = inAdminServerProtocol;
384 }
385
386 /**
387 * This method will build up the parameters required for the various deployment operations.
388 *
389 * @param inOperation The deployment operation to take place.
390 * @return The value of input parameters to be passed to the deployer.
391 */
392 protected String[] getInputParameters( String inOperation )
393 {
394 final List parameterList = new ArrayList();
395
396 // Create the admin URL
397 final String adminURL = WeblogicMojoUtilities.getAdminUrl( this.adminServerProtocol, this.adminServerHostName,
398 this.adminServerPort );
399
400 // Load the admin url
401 parameterList.add( "-adminurl" );
402 parameterList.add( adminURL );
403
404 // add the admin user name
405 parameterList.add( "-username" );
406 parameterList.add( this.getUserId() );
407
408 // add the admin password
409 parameterList.add( "-password" );
410 parameterList.add( this.getPassword() );
411
412 if ( this.verbose )
413 {
414 parameterList.add( "-verbose" );
415 }
416
417 if ( this.debug )
418 {
419 parameterList.add( "-debug" );
420 }
421
422 if ( this.noWait )
423 {
424 parameterList.add( "-nowait" );
425 }
426
427 if ( this.timeout > 0 )
428 {
429 parameterList.add( "-timeout " + this.timeout );
430 }
431
432 if ( this.version )
433 {
434 parameterList.add( "-version" );
435 }
436
437 if ( this.noVersion )
438 {
439 parameterList.add( "-noVersion" );
440 }
441
442 if ( this.noExit )
443 {
444 parameterList.add( "-noexit" );
445 }
446
447 // Not valid for listapps operation
448 if ( !inOperation.equalsIgnoreCase( "listapps" ) )
449 {
450
451 // add the artifact name
452 parameterList.add( "-name" );
453 parameterList.add( this.getName() );
454 }
455
456 // not use in listabpps
457 if ( !inOperation.equalsIgnoreCase( "listapps" ) )
458 {
459
460 // add the target comma seperated list
461 parameterList.add( "-targets" );
462 parameterList.add( this.getTargetNames() );
463 }
464
465 // Only use these parameters for a deploy operation
466 if ( inOperation.equalsIgnoreCase( "deploy" ) )
467 {
468
469 // add if remote
470 if ( this.isRemote() )
471 {
472 parameterList.add( "-upload" );
473 }
474
475 parameterList.add( "-source" );
476 parameterList.add( this.getArtifactPath() );
477
478 }
479
480 // Set the operation
481 String operation = "-" + inOperation;
482 parameterList.add( operation );
483
484 if ( getLog().isInfoEnabled() )
485 {
486 getLog().info( "Weblogic Deployment parameters " + parameterList );
487 }
488
489 return (String[]) parameterList.toArray( new String[parameterList.size()] );
490 }
491
492 /**
493 * Executes the deployer with the given parameters. Returns success or failure.
494 *
495 * @param parameters the parameters to execute
496 * @param errorMessage the error message
497 * @return success
498 * @throws MojoExecutionException when the deployer fails and the continueOnFailure is false
499 */
500 protected boolean executeDeployer( String[] parameters, final String errorMessage )
501 throws MojoExecutionException
502 {
503 try
504 {
505 if ( getLog().isDebugEnabled() )
506 {
507 getLog().debug( "continueOnFailure is " + continueOnFailure );
508 }
509 Deployer deployer = new Deployer( parameters );
510 deployer.run();
511 }
512 catch ( Exception e )
513 {
514 if ( getLog().isDebugEnabled() )
515 {
516 getLog().debug( errorMessage + ": " + e.getMessage() );
517 }
518 if ( this.continueOnFailure )
519 {
520 getLog().info( "Continuing on failure." );
521 getLog().error( e );
522 return false;
523 }
524 else
525 {
526 throw new MojoExecutionException( errorMessage + ": " + e.getMessage(), e );
527 }
528 }
529 return true;
530 }
531
532
533 /**
534 * Getter for property artifact path. If the {@link #exploded} attribute is set
535 * the value will be {@link #artifactPath}.
536 *
537 * @return The value of artifact path.
538 */
539 public String getArtifactPath()
540 {
541 final String fixedArtifactPath;
542 if ( this.exploded )
543 {
544 fixedArtifactPath = this.artifactPath;
545 }
546 else
547 {
548 fixedArtifactPath = WeblogicMojoUtilities.updateArtifactName( this.artifactPath, this.projectPackaging );
549 }
550 if ( getLog().isDebugEnabled() )
551 {
552 getLog().debug( "fixedArtifactPath=" + fixedArtifactPath );
553 }
554 return fixedArtifactPath;
555 }
556
557 /**
558 * Setter for the artifact path.
559 *
560 * @param inArtifactPath The value of artifact path.
561 */
562 public void setArtifactPath( final String inArtifactPath )
563 {
564 this.artifactPath = inArtifactPath;
565 }
566
567 /**
568 * Getter for property project packaging.
569 *
570 * @return The value of project packaging.
571 */
572 public String getProjectPackaging()
573 {
574 return projectPackaging;
575 }
576
577 /**
578 * Setter for the project packaging.
579 *
580 * @param inProjectPackaging The value of project packaging.
581 */
582 public void setProjectPackaging( final String inProjectPackaging )
583 {
584 this.projectPackaging = inProjectPackaging;
585 }
586
587 /**
588 * toString methode: creates a String representation of the object
589 *
590 * @return the String representation
591 */
592 public String toString()
593 {
594 StringBuffer buffer = new StringBuffer();
595 buffer.append( "DeployMojoBase[" );
596 buffer.append( "adminServerHostName = " ).append( adminServerHostName );
597 buffer.append( ", adminServerProtocol = " ).append( adminServerProtocol );
598 buffer.append( ", adminServerPort = " ).append( adminServerPort );
599 buffer.append( ", userId = " ).append( userId );
600 buffer.append( ", password = " ).append( password );
601 buffer.append( ", artifactPath = " ).append( artifactPath );
602 buffer.append( ", projectPackaging = " ).append( projectPackaging );
603 buffer.append( ", name = " ).append( name );
604 buffer.append( ", targetNames = " ).append( targetNames );
605 buffer.append( ", remote = " ).append( remote );
606 buffer.append( "]" );
607 return buffer.toString();
608 }
609
610 /**
611 * @return the stage
612 */
613 public boolean isStage()
614 {
615 return stage;
616 }
617
618 /**
619 * @param stage the stage to set
620 */
621 public void setStage( boolean stage )
622 {
623 this.stage = stage;
624 }
625
626 /**
627 * @return the debug
628 */
629 public boolean isDebug()
630 {
631 return debug;
632 }
633
634 /**
635 * @param debug the debug to set
636 */
637 public void setDebug( boolean debug )
638 {
639 this.debug = debug;
640 }
641
642 /**
643 * @return the verbose
644 */
645 public boolean isVerbose()
646 {
647 return verbose;
648 }
649
650 /**
651 * @param verbose the verbose to set
652 */
653 public void setVerbose( boolean verbose )
654 {
655 this.verbose = verbose;
656 }
657
658 /**
659 * @return the upload
660 */
661 public boolean isUpload()
662 {
663 return upload;
664 }
665
666 /**
667 * @param upload the upload to set
668 */
669 public void setUpload( boolean upload )
670 {
671 this.upload = upload;
672 }
673
674 /**
675 * Getter for continueOnFailure
676 *
677 * @return true if the deployer should continue
678 */
679 public boolean isContinueOnFailure()
680 {
681 return continueOnFailure;
682 }
683
684 /**
685 * Setter for continue on failure. Set to true so the deployer
686 * will not stop if there is a failure.
687 *
688 * @param continueOnFailure the value to set
689 */
690 public void setContinueOnFailure( boolean continueOnFailure )
691 {
692 this.continueOnFailure = continueOnFailure;
693 }
694
695 /**
696 * Getter for noWait
697 *
698 * @return the value of noWait
699 */
700 public boolean isNoWait()
701 {
702 return noWait;
703 }
704
705 /**
706 * Setter for noWait
707 *
708 * @param noWait if set to true, weblogic will not wait for deployment success
709 */
710 public void setNoWait( boolean noWait )
711 {
712 this.noWait = noWait;
713 }
714
715 /**
716 * Getter for deployment timeout
717 *
718 * @return the timeout in seconds
719 */
720 public int getTimeout()
721 {
722 return timeout;
723 }
724
725 /**
726 * Setter for the deployment timeout
727 *
728 * @param timeout in seconds
729 */
730 public void setTimeout( int timeout )
731 {
732 this.timeout = timeout;
733 }
734
735 /**
736 * Getter for version
737 *
738 * @return true if weblogic should print the version
739 */
740 public boolean isVersion()
741 {
742 return version;
743 }
744
745 /**
746 * Setter for version
747 *
748 * @param version set to true to have weblogic print the version
749 */
750 public void setVersion( boolean version )
751 {
752 this.version = version;
753 }
754
755 /**
756 * Getter for the -noversion flag
757 *
758 * @return true if the -noversion flag should be used.
759 */
760 public boolean isNoVersion()
761 {
762 return noVersion;
763 }
764
765 /**
766 * Setter for noVersion.
767 *
768 * @param noVersion set to true if the -noversion flag should be used
769 */
770 public void setNoVersion( boolean noVersion )
771 {
772 this.noVersion = noVersion;
773 }
774
775 /**
776 * getter for this.noExit
777 *
778 * @return true if weblogic should not use System.exit() when problems arise.
779 */
780 public boolean isNoExit()
781 {
782 return noExit;
783 }
784
785 /**
786 * Setter for this.noExit
787 *
788 * @param noExit set to true if weblogic should NOT use System.exit() when problems arise.
789 */
790 public void setNoExit( boolean noExit )
791 {
792 this.noExit = noExit;
793 }
794
795 /**
796 * Getter for {@link #exploded}.
797 *
798 * @return true if the deployer should use the target artifact path as the source parameter
799 */
800 public boolean isExploded()
801 {
802 return exploded;
803 }
804
805 /**
806 * Setter for {@link #exploded}.
807 *
808 * @param exploded true to use the target artifact path as the -source parameter
809 */
810 public void setExploded( boolean exploded )
811 {
812 this.exploded = exploded;
813 }
814 }