1 23 24 package org.apache.tools.ant.taskdefs.optional.iplanet; 25 26 import org.apache.tools.ant.Project; 27 import org.apache.tools.ant.DirectoryScanner; 28 import org.apache.tools.ant.BuildException; 29 import org.apache.tools.ant.types.FileSet; 30 31 import java.io.File ; 32 import java.util.List ; 33 import java.util.ArrayList ; 34 import java.util.Map ; 35 import java.util.HashMap ; 36 import java.util.Iterator ; 37 38 public abstract class ComponentAdmin extends IasAdmin { 39 private Component component; 40 private List components = new ArrayList (); 41 private List filesets = new ArrayList (); 42 43 protected static final String TYPE_APP = "application"; 44 protected static final String TYPE_EJB = "ejb"; 45 protected static final String TYPE_WEB = "web"; 46 protected static final String TYPE_CONN = "connector"; 47 protected static final String TYPE_CLIENT = "client"; 48 49 protected static final java.util.Map TYPE_MAP = new HashMap (4); static { 51 TYPE_MAP.put("ear", TYPE_APP); 52 TYPE_MAP.put("jar", TYPE_EJB); 53 TYPE_MAP.put("war", TYPE_WEB); 54 TYPE_MAP.put("rar", TYPE_CONN); 55 }; 57 58 public void setFile(File file) { 59 getComponent().setFile(file); 60 } 61 62 public void setName(String name) { 63 getComponent().setName(name); 64 } 65 66 public void setType(String type) { 67 getComponent().setType(type); 68 } 69 70 public void setForce(boolean force) { 71 getComponent().setForce(force); 72 } 73 74 public void setUpload(boolean upload) { 75 getComponent().setUpload(upload); 76 } 77 78 public void setContextroot(String contextroot) { 79 getComponent().setContextroot(contextroot); 80 } 81 82 public void addFileset(FileSet fileset) { 83 filesets.add(fileset); 84 } 85 86 public Component createComponent() { 87 log("createComponent", Project.MSG_DEBUG); 88 Component newComponent = new Component(component); 89 components.add(newComponent); 90 return newComponent; 91 } 92 93 private Component getComponent() { 94 if (component == null) { 95 component = new Component(); 96 Iterator it = components.iterator(); 97 while (it.hasNext()) { 98 Component aComponent = (Component)it.next(); 99 aComponent.setParent(component); 100 } 101 components.add(0, component); 102 } 103 104 return component; 105 } 106 107 protected void prepareToExecute() throws BuildException { 108 super.prepareToExecute(); 109 processFilesets(); 110 } 111 112 protected void execute(Server server) throws BuildException { 113 Iterator iterator = components.iterator(); 114 while (iterator.hasNext()) { 115 Component comp = (Component)iterator.next(); 116 String cmdString = getCommandString(server, comp); 117 execIasCommand(cmdString); 118 } 119 } 120 121 private void processFilesets() { 122 for (int i = 0; i < filesets.size(); i++) { 123 FileSet fileset = (FileSet) filesets.get(i); 124 DirectoryScanner scanner = fileset.getDirectoryScanner(project); 125 File baseDir = scanner.getBasedir(); 126 127 String [] files = scanner.getIncludedFiles(); 128 for (int j = 0; j < files.length; j++) { 129 Component archive = new Component(); 130 archive.setFile(new File (baseDir, files[j])); 131 components.add(archive); 132 } 133 134 String [] dirs = scanner.getIncludedDirectories(); 135 for (int j = 0; j < dirs.length; j++) { 136 Component expandedArchive = new Component(); 137 expandedArchive.setFile(new File (baseDir, dirs[j])); 138 components.add(expandedArchive); 139 } 140 } 141 } 142 143 protected void checkConfiguration() throws BuildException { 144 super.checkConfiguration(); 145 146 if (components.size() == 0) { 147 log("WARNING! No components were specified.", Project.MSG_WARN); 148 } 149 150 log(components.size() + " components were found.", Project.MSG_DEBUG); 151 152 Iterator iterator = components.iterator(); 153 while (iterator.hasNext()) { 154 Component comp = (Component)iterator.next(); 155 checkComponentConfig(comp); 156 } 157 } 158 159 protected void checkComponentConfig(Component comp) throws BuildException { 160 log("Checking configuration for " + comp.getName(), Project.MSG_DEBUG); 161 162 File theFile = comp.getFile(); 164 if ((theFile != null) && (!theFile.exists())) { 165 String msg = "The file specified (" + theFile + ") could not be found."; 166 throw new BuildException(msg, getLocation()); 167 } 168 169 String theName = comp.getName(); 171 if ((theName == null) || (theName.length() == 0)) { 172 String msg = "A valid name for the component could not be determined."; 173 throw new BuildException(msg, getLocation()); 174 } 175 176 String theType = comp.getType(); 178 if ((theType != null) && (!TYPE_MAP.values().contains(theType))) { 179 String msg = "The type specified (" + theType + ") is not valid."; 180 throw new BuildException(msg, getLocation()); 181 } 182 } 183 184 protected abstract String getCommandString(Server server, Component comp); 185 186 public class Component { 187 private Component parent; 188 private File file; 189 private String name; 190 private String type; 191 private boolean force; 192 private boolean upload; 193 private String contextroot; 194 195 private boolean forceIsSet = false; 196 private boolean uploadIsSet = false; 197 private boolean contextRootIsSet = false; 198 199 private static final String DEFAULT_TYPE = TYPE_APP; 200 private static final boolean DEFAULT_FORCE = true; 201 private static final boolean DEFAULT_UPLOAD = true; 202 203 204 public Component() { 205 this(null); 206 } 207 208 public Component(Component parent) { 209 this.parent = parent; 210 } 211 212 public void setParent(Component parent) { 213 this.parent = parent; 214 } 215 216 public void setFile(File file) { 217 this.file = file; 218 } 219 220 protected File getFile() { 221 return file; 222 } 223 224 public void setName(String name) { 225 this.name = name; 226 } 227 228 protected String getName() { 229 if (name == null) { 230 String fileName = null; 231 232 if (file != null) { 233 fileName = file.getName(); 234 } else { 235 return null; 236 } 237 238 int index = fileName.indexOf('.'); 239 name = (index < 0) ? fileName : fileName.substring(0, index); 240 } 241 242 return name; 243 } 244 245 public void setType(String type) { 246 this.type = type; 247 } 248 249 252 protected String getType() { 253 if (type == null) { 254 String fileName = null; 255 String extension = null; 256 int lastIndex; 257 258 if (file != null) { 259 fileName = file.getName(); 260 lastIndex = fileName.lastIndexOf('.'); 261 if (lastIndex >= 0) { 262 extension = fileName.substring(lastIndex + 1); 263 } 264 265 type = (String ) TYPE_MAP.get(extension); 266 } 267 } 268 269 if (type == null) { 270 return (parent == null) ? null : parent.getType(); 271 } 272 return type; 273 } 274 275 public void setForce(boolean force) { 276 this.force = force; 277 forceIsSet = true; 278 } 279 280 protected boolean getForce() { 281 if (!forceIsSet) { 282 return (parent == null) ? DEFAULT_FORCE : parent.getForce(); 283 } 284 return force; 285 } 286 287 public void setUpload(boolean upload) { 288 this.upload = upload; 289 uploadIsSet = true; 290 } 291 292 protected boolean getUpload() { 293 if (!uploadIsSet) { 294 return (parent == null) ? DEFAULT_UPLOAD : parent.getUpload(); 295 } 296 return upload; 297 } 298 299 public void setContextroot(String contextroot) { 300 contextRootIsSet = true; 301 this.contextroot = contextroot; 302 } 303 304 protected String getContextroot() { 305 return (contextroot != null) ? contextroot : getName(); 306 } 307 308 protected boolean contextRootIsSet() { 309 return contextRootIsSet; 310 } 311 }; 312 } | Popular Tags |