1. /*
  2. * Copyright 2000-2002,2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package org.apache.tools.ant.taskdefs.optional.ejb;
  18. import java.io.File;
  19. import org.apache.tools.ant.BuildException;
  20. import org.apache.tools.ant.Task;
  21. import org.apache.tools.ant.taskdefs.Java;
  22. import org.apache.tools.ant.types.Path;
  23. /**
  24. * Shuts down a WebLogic server.
  25. * To shut down an instance you must supply both a username and
  26. * a password.
  27. *
  28. */
  29. public class WLStop extends Task {
  30. /**
  31. * The classpath to be used. It must contains the weblogic.Admin class.
  32. */
  33. private Path classpath;
  34. /**
  35. * The weblogic username to use to request the shutdown.
  36. */
  37. private String username;
  38. /**
  39. * The password to use to shutdown the weblogic server.
  40. */
  41. private String password;
  42. /**
  43. * The URL which the weblogic server is listening on.
  44. */
  45. private String serverURL;
  46. /**
  47. * The delay (in seconds) to wait before shutting down.
  48. */
  49. private int delay = 0;
  50. /**
  51. * The location of the BEA Home under which this server is run.
  52. * WL6 only
  53. */
  54. private File beaHome = null;
  55. /**
  56. * Do the work.
  57. *
  58. * The work is actually done by creating a separate JVM to run the weblogic admin task
  59. * This approach allows the classpath of the helper task to be set.
  60. *
  61. * @exception BuildException if someting goes wrong with the build
  62. */
  63. public void execute() throws BuildException {
  64. if (username == null || password == null) {
  65. throw new BuildException("weblogic username and password must both be set");
  66. }
  67. if (serverURL == null) {
  68. throw new BuildException("The url of the weblogic server must be provided.");
  69. }
  70. Java weblogicAdmin = (Java) getProject().createTask("java");
  71. weblogicAdmin.setFork(true);
  72. weblogicAdmin.setClassname("weblogic.Admin");
  73. String args;
  74. if (beaHome == null) {
  75. args = serverURL + " SHUTDOWN " + username + " " + password + " " + delay;
  76. } else {
  77. args = " -url " + serverURL
  78. + " -username " + username
  79. + " -password " + password
  80. + " SHUTDOWN " + " " + delay;
  81. }
  82. weblogicAdmin.createArg().setLine(args);
  83. weblogicAdmin.setClasspath(classpath);
  84. weblogicAdmin.execute();
  85. }
  86. /**
  87. * The classpath to be used with the Java Virtual Machine that runs the Weblogic
  88. * Shutdown command;
  89. *
  90. * @param path the classpath to use when executing the weblogic admin task.
  91. */
  92. public void setClasspath(Path path) {
  93. this.classpath = path;
  94. }
  95. /**
  96. * The classpath to be used with the Java Virtual Machine that runs the Weblogic
  97. * Shutdown command;
  98. */
  99. public Path createClasspath() {
  100. if (classpath == null) {
  101. classpath = new Path(getProject());
  102. }
  103. return classpath.createPath();
  104. }
  105. /**
  106. * The username of the account which will be used to shutdown the server;
  107. * required.
  108. *
  109. * @param s the username.
  110. */
  111. public void setUser(String s) {
  112. this.username = s;
  113. }
  114. /**
  115. * The password for the account specified in the
  116. * user parameter; required
  117. *
  118. * @param s the password.
  119. */
  120. public void setPassword(String s) {
  121. this.password = s;
  122. }
  123. /**
  124. * Set the URL to which the weblogic server is listening
  125. * for T3 connections; required.
  126. *
  127. * @param s the url.
  128. */
  129. public void setUrl(String s) {
  130. this.serverURL = s;
  131. }
  132. /**
  133. * Set the delay (in seconds) before shutting down the server;
  134. * optional.
  135. *
  136. * @param s the selay.
  137. */
  138. public void setDelay(String s) {
  139. delay = Integer.parseInt(s);
  140. }
  141. /**
  142. * The location of the BEA Home; implicitly
  143. * selects Weblogic 6.0 shutdown; optional.
  144. *
  145. * @param beaHome the BEA Home directory.
  146. *
  147. */
  148. public void setBEAHome(File beaHome) {
  149. this.beaHome = beaHome;
  150. }
  151. }