1 23 24 package org.apache.tools.ant.taskdefs.optional.iplanet; 25 26 import org.apache.tools.ant.Task; 27 import org.apache.tools.ant.BuildException; 28 import org.apache.tools.ant.Project; 29 import org.apache.tools.ant.AntClassLoader; 30 import org.apache.tools.ant.types.Path; 31 32 import java.io.File ; 33 import java.util.List ; 34 import java.util.ArrayList ; 35 import java.util.Iterator ; 36 import java.lang.reflect.Method ; 37 import java.lang.reflect.InvocationTargetException ; 38 39 public abstract class IasAdmin extends Task { 40 private Server server; 41 private List servers = new ArrayList (); 42 private File ias7home; 43 44 private static final boolean ACTUALLY_EXEC_COMMAND = true; 45 46 private static final String [] CLASSPATH_ELEMENTS = { 47 "ias7se/iWS/bin/https/jar/iascli.jar", 48 "ias7se/iWS/bin/https/jar/adminshared.jar", 49 "ias7se/iWS/bin/https/jar/jmxri.jar"}; 50 private static final String CLASS_INPUTS_AND_OUTPUTS = 51 "com.sun.enterprise.tools.cli.framework.InputsAndOutputs"; 52 private static final String CLASS_IAS_ADMIN_MAIN = 53 "com.sun.enterprise.tools.cli.IasAdminMain"; 54 private static final String METHOD_INVOKE_CLI = "invokeCLI"; 55 56 public void setUser(String user) { 57 getServer().setUser(user); 58 } 59 60 public void setPassword(String password) { 61 log("setPassword: " + password, Project.MSG_DEBUG); 62 getServer().setPassword(password); 63 } 64 65 public void setHost(String host) { 66 getServer().setHost(host); 67 } 68 69 public void setPort(int port) { 70 getServer().setPort(port); 71 } 72 73 public void setInstanceport(int instanceport) { 74 getServer().setInstanceport(instanceport); 75 } 76 77 public void setInstance(String instance) { 78 getServer().setInstance(instance); 79 } 80 81 public void setIas7home(File iashome) { 82 this.ias7home = ias7home; 83 } 84 85 89 protected File getIas7home() { 90 if (ias7home == null) { 91 String home = getProject().getProperty("ias7.home"); 92 if (home != null) { 93 ias7home = new File (home); 94 } 95 } 96 97 return ias7home; 98 } 99 100 public Server createServer() { 101 log("createServer", Project.MSG_DEBUG); 102 Server aServer = new Server(server); 103 servers.add(aServer); 104 return aServer; 105 } 106 107 private Server getServer() { 108 if (server == null) { 109 server = new Server(); 110 Iterator it = servers.iterator(); 111 while (it.hasNext()) { 112 Server aServer = (Server)it.next(); 113 aServer.setParent(server); 114 } 115 } 116 return server; 117 } 118 119 public void execute() throws BuildException { 120 prepareToExecute(); 121 checkConfiguration(); 122 123 Iterator it = servers.iterator(); 124 while (it.hasNext()) { 125 Server aServer = (Server)it.next(); 126 execute(aServer); 127 } 128 } 129 130 protected void prepareToExecute() throws BuildException { 131 if ((servers.size() == 0) && (server != null)) { 132 servers.add(server); 133 } 134 } 135 136 protected void checkConfiguration() throws BuildException { 137 138 if (servers.size() == 0) { 139 String msg = "At least one server must be specified."; 140 throw new BuildException(msg, getLocation()); 141 } 142 143 Iterator it = servers.iterator(); 144 while (it.hasNext()) { 145 Server aServer = (Server)it.next(); 146 checkConfiguration(server); 147 } 148 } 149 150 protected void checkConfiguration(Server aServer) throws BuildException { 151 if (aServer.getPassword() == null) { 152 String msg = "A password must be specified for each server. A " 153 + "password for " + aServer.getHost() + " was " 154 + "not specified."; 155 throw new BuildException(msg, getLocation()); 156 } 157 } 158 159 protected abstract void execute(Server server) throws BuildException; 160 161 protected void execIasCommand(String command) { 162 log("Executing: " + command, Project.MSG_INFO); 163 164 try { 165 Path iasClasspath = new Path(getProject(), getIasClasspath()); 166 iasClasspath.append(Path.systemClasspath); 167 log("Using classpath: " + iasClasspath.toString(), Project.MSG_DEBUG); 168 169 ClassLoader antClassLoader = new AntClassLoader(getProject(), iasClasspath); 170 171 Class inputsAndOutputs = 172 Class.forName(CLASS_INPUTS_AND_OUTPUTS, true, antClassLoader); 173 Class iasAdminMain = 174 Class.forName(CLASS_IAS_ADMIN_MAIN, true, antClassLoader); 175 176 Class [] parameterClasses = {String .class, inputsAndOutputs}; 177 Method invokeCLI = 178 iasAdminMain.getDeclaredMethod(METHOD_INVOKE_CLI, parameterClasses); 179 180 Object [] parameters = {command, null}; 181 182 if (ACTUALLY_EXEC_COMMAND) { 183 invokeCLI.invoke(iasAdminMain, parameters); 184 } 185 } catch (ClassNotFoundException e) { 186 String msg = "An iPlanet Application Server 7.0 admin CLI " 187 + "class could not be found (" + e.getMessage() 188 + "). Use the ias7home attribute, set the " 189 + "ias7.home property, or add the appropriate " 190 + "JARs to the classpath."; 191 throw new BuildException(msg, getLocation()); 192 } catch (NoSuchMethodException e) { 193 String msg = "The \"invokeCLI\" method couldn't be found on the " 194 + "IasAdminMain class. The exception message " 195 + "is: " + e.getMessage(); 196 throw new BuildException(msg, getLocation()); 197 } catch (InvocationTargetException e) { 198 String msg = "An exception occurred while running the command. The " 199 + "exception message is: " 200 + e.getTargetException().getMessage(); 201 throw new BuildException(msg, getLocation()); 202 } catch (IllegalAccessException e) { 203 String msg = "An exception occurred while trying to invoke the " 204 + "\"invokeCLI\" method. The exception message " 205 + "is: " + e.getMessage(); 206 throw new BuildException(msg, getLocation()); 207 } 208 } 209 210 private String getIasClasspath() { 211 StringBuffer classpath = new StringBuffer (); 212 String [] elements = getClasspathElements(); 213 for (int i = 0; i < elements.length; i++ ) { 214 classpath.append(new File (getIas7home(), elements[i]).getPath()); 215 classpath.append(':'); 216 } 217 218 return classpath.toString(); 219 } 220 221 222 227 protected String [] getClasspathElements() { 228 return CLASSPATH_ELEMENTS; 229 } 230 231 public class Server { 232 private Server parent; 233 private String user; 234 private String password; 235 private String host; 236 private int port; 237 private int instanceport; 238 private String instance; 239 240 private static final String DEFAULT_USER = "admin"; 241 private static final String DEFAULT_HOST = "localhost"; 242 private static final int DEFAULT_PORT = 8000; 243 244 public Server() { 245 this(null); 246 } 247 248 public Server(Server parent) { 249 this.parent = parent; 250 } 251 252 public void setParent(Server parent) { 253 this.parent = parent; 254 } 255 256 public void setUser(String user) { 257 this.user = user; 258 } 259 260 protected String getUser() { 261 if (user == null) { 262 return (parent == null) ? DEFAULT_USER : parent.getUser(); 263 } 264 return user; 265 } 266 267 public void setPassword(String password) { 268 this.password = password; 269 } 270 271 protected String getPassword() { 272 if (password == null) { 273 return (parent == null) ? null : parent.getPassword(); 274 } 275 return password; 276 } 277 278 public void setHost(String host) { 279 this.host = host; 280 } 281 282 protected String getHost() { 283 if (host == null) { 284 return (parent == null) ? DEFAULT_HOST : parent.getHost(); 285 } 286 return host; 287 } 288 289 public void setPort(int port) { 290 this.port = port; 291 } 292 293 protected int getPort() { 294 if (port == 0) { 295 return (parent == null) ? DEFAULT_PORT : parent.getPort(); 296 } 297 return port; 298 } 299 300 public void setInstanceport(int instanceport) { 301 this.instanceport = instanceport; 302 } 303 304 protected int getInstanceport() { 305 if ((instanceport == 0) && (parent != null)) { 306 return parent.getInstanceport(); 307 } 308 return instanceport; 309 } 310 311 public void setInstance(String instance) { 312 this.instance = instance; 313 } 314 315 protected String getInstance() { 316 if (instance == null) { 317 return (parent == null) ? null : parent.getInstance(); 318 } 319 return instance; 320 } 321 322 protected String getCommandParameters(boolean includeInstance) { 323 StringBuffer cmdString = new StringBuffer (); 324 cmdString.append(" --user ").append(getUser()); 325 cmdString.append(" --password ").append(getPassword()); 326 cmdString.append(" --host ").append(getHost()); 327 cmdString.append(" --port ").append(getPort()); 328 if (includeInstance && (getInstance() != null)) { 329 cmdString.append(" --instance ").append(getInstance()); 330 } 331 332 return cmdString.toString(); 333 } 334 }; 335 } 336 | Popular Tags |