KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > sun > appserv > ComponentAdmin


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * The Apache Software License, Version 1.1
26  *
27  * Copyright (c) 1999 The Apache Software Foundation. All rights
28  * reserved.
29  *
30  * Redistribution and use in source and binary forms, with or without
31  * modification, are permitted provided that the following conditions
32  * are met:
33  *
34  * 1. Redistributions of source code must retain the above copyright
35  * notice, this list of conditions and the following disclaimer.
36  *
37  * 2. Redistributions in binary form must reproduce the above copyright
38  * notice, this list of conditions and the following disclaimer in
39  * the documentation and/or other materials provided with the
40  * distribution.
41  *
42  * 3. The end-user documentation included with the redistribution, if
43  * any, must include the following acknowlegement:
44  * "This product includes software developed by the
45  * Apache Software Foundation (http://www.apache.org/)."
46  * Alternately, this acknowlegement may appear in the software itself,
47  * if and wherever such third-party acknowlegements normally appear.
48  *
49  * 4. The names "The Jakarta Project", "Ant", and "Apache Software
50  * Foundation" must not be used to endorse or promote products derived
51  * from this software without prior written permission. For written
52  * permission, please contact apache@apache.org.
53  *
54  * 5. Products derived from this software may not be called "Apache"
55  * nor may "Apache" appear in their names without prior written
56  * permission of the Apache Group.
57  *
58  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
59  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
60  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
61  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
62  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
63  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
64  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
65  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
66  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
67  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
68  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69  * SUCH DAMAGE.
70  * ====================================================================
71  *
72  * This software consists of voluntary contributions made by many
73  * individuals on behalf of the Apache Software Foundation. For more
74  * information on the Apache Software Foundation, please see
75  * <http://www.apache.org/>.
76  */

77
78 package org.apache.tools.ant.taskdefs.optional.sun.appserv;
79
80 import org.apache.tools.ant.Project;
81 import org.apache.tools.ant.DirectoryScanner;
82 import org.apache.tools.ant.BuildException;
83 import org.apache.tools.ant.types.FileSet;
84
85 import java.io.File JavaDoc;
86 import java.util.List JavaDoc;
87 import java.util.ArrayList JavaDoc;
88 import java.util.Map JavaDoc;
89 import java.util.HashMap JavaDoc;
90 import java.util.Iterator JavaDoc;
91
92 /**
93  * Abstract class which includes base functionality for Tasks which utilize J2EE
94  * components. This includes support for the following component-related
95  * attributes:
96  *
97  * <ul>
98  * <li><i>file</i> -- The filename of the component. Although this is often
99  * refers to a J2EE archive file, some commands may allow
100  * this attribute to refer to a directory where a J2EE
101  * archive has been exploded
102  * <li><i>name</i> -- The display-name for the J2EE component. This is the
103  * "friendly name" which will appear in the admin GUI and
104  * administrative commands
105  * <li><i>type</i> -- The component type. Valid types are "application",
106  * "ejb", "web", "connector" and "client"
107  * <li><i>force</i> -- A boolean attribute which indicates if the command
108  * should overwrite existing values or components.
109  * Defaults to <code>true</code>
110  * <li><i>upload</i> -- A boolean attribute which indicates if the component
111  * should be transferred to the (potentially) remote
112  * server before executing the command. If the
113  * application server is running on the local machine,
114  * this may be set to <code>false</code> to reduce
115  * execution time. Defaults to <code>true</code>
116  * <li><i>contextroot</i> -- The context root for a web module (WAR file).
117  * This attribute is only used when deploying WAR
118  * files to the application server
119  * </ul>
120  * <p>
121  * In addition, this abstract class provides support for nested &lt;component&gt;
122  * elements. These nested elements enable commands to be executed using multiple
123  * components in a single Ant command. Attributes may be specified in the
124  * "root" command element and those values will be used ("inherited") by all of
125  * the nested &lt;component&gt; components. If an attribute is specified in
126  * both elements, the value specified in the &lt;component&gt; element will be
127  * used.
128  *
129  * Furthermore, this abstract class provides support for nested &lt;fileset&gt;
130  * elements. These may be used to match multiple components which will be used
131  * for the respective application server admin commands.
132  *
133  * @author Greg Nelson <a HREF="mailto:gn@sun.com">gn@sun.com</a>
134  */

135 public abstract class ComponentAdmin extends AppServerAdmin {
136
137     LocalStringsManager lsm = new LocalStringsManager();
138
139     /**
140      * This Component instance will store user settings from the "root" command
141      * element.
142      */

143     protected Component component;
144
145     /**
146      * The list of components will store user settings from nested
147      * &lt;component&gt; elements.
148      */

149     protected List JavaDoc components = new ArrayList JavaDoc();
150
151     /**
152      * The list of component archives which match a nested <fileset> element.
153      */

154     private List JavaDoc filesets = new ArrayList JavaDoc();
155
156     protected static final String JavaDoc TYPE_APP = "application";
157     protected static final String JavaDoc TYPE_EJB = "ejb";
158     protected static final String JavaDoc TYPE_WEB = "web";
159     protected static final String JavaDoc TYPE_CONN = "connector";
160     protected static final String JavaDoc TYPE_CLIENT = "client";
161
162     /**
163      * Constants for each of the component types. In addition, the archive file
164      * extensions are mapped to their appropriate component types. Note that
165      * ".jar" maps only to EJB although this is a valid file extension for both
166      * EJB JARs and application clients.
167      */

168     protected static final java.util.Map JavaDoc TYPE_MAP = new HashMap JavaDoc(4);
169     static {
170         TYPE_MAP.put("ear", TYPE_APP);
171         TYPE_MAP.put("jar", TYPE_EJB);
172         TYPE_MAP.put("war", TYPE_WEB);
173         TYPE_MAP.put("rar", TYPE_CONN);
174     };
175
176     /**
177      * Creates a new instance with the infrastructure for running admin commands
178      * which use components (EARs, WARs, JARs, and RARs).
179      */

180     public ComponentAdmin() {
181         super();
182         component = getNewComponent();
183     }
184
185     /**
186      * Builds a new component instance. This method is intended to be overridden
187      * by subclasses which implement their own subclass of Component.
188      *
189      * @return new Component instance.
190      */

191     protected Component getNewComponent() {
192          return new Component(component);
193     }
194
195     /**
196      * Sets the filename for the component file (or directory) which is used by
197      * the administrative command.
198      *
199      * @param file The component file archive or directory
200      */

201     public void setFile(File JavaDoc file) {
202         component.setFile(file); // Delegates to component object
203
}
204
205     /**
206      * Sets the display-name for the J2EE component. This is the "friendly name"
207      * which will appear in the admin GUI and administrative commands
208      *
209      * @param name The component display-name
210      */

211     public void setName(String JavaDoc name) {
212         component.setName(name); // Delegates to component object
213
}
214
215     /**
216      * Sets the component type. Valid types are "application", "ejb", "web",
217      * "connector" and "client"
218      *
219      * @param type The component type
220      */

221     public void setType(String JavaDoc type) {
222         component.setType(type); // Delegates to component object
223
}
224
225     /**
226      * Sets the deployment target.
227      *
228      * @param target The deployment target
229      */

230     public void setTarget(String JavaDoc target) {
231         component.setTarget(target); // Delegates to component object
232
}
233
234     /**
235      * Adds a nested <code>fileset</code> element.
236      *
237      * @param fileset The nested fileset component.
238      */

239     public void addFileset(FileSet fileset) {
240         filesets.add(fileset);
241     }
242
243     /**
244      * Creates a nested <code>component</code> element.
245      *
246      * @return Component which has been added
247      */

248     public Component createComponent() {
249         Component newComponent = getNewComponent();
250         components.add(newComponent);
251         return newComponent;
252     }
253
254     protected void prepareToExecute() throws BuildException {
255         super.prepareToExecute();
256         processFilesets();
257         if (components.size() == 0) {
258             components.add(component);
259         }
260
261     }
262
263     protected void execute(Server aServer) throws BuildException {
264         Iterator JavaDoc iterator = components.iterator();
265         while (iterator.hasNext()) { // Execute command using each component
266
Component comp = (Component)iterator.next();
267             String JavaDoc cmdString = getCommandString(aServer, comp);
268             execAdminCommand(cmdString);
269         }
270     }
271
272     /**
273      * Examines each nested fileset and adds the matching files and directories
274      * to the "components" List object.
275      */

276     private void processFilesets() {
277         for (int i = 0; i < filesets.size(); i++) {
278             FileSet fileset = (FileSet) filesets.get(i);
279             DirectoryScanner scanner = fileset.getDirectoryScanner(project);
280             File JavaDoc baseDir = scanner.getBasedir();
281
282             String JavaDoc[] files = scanner.getIncludedFiles();
283             for (int j = 0; j < files.length; j++) {
284                 Component archive = getNewComponent();
285                 archive.setFile(new File JavaDoc(baseDir, files[j]));
286                 components.add(archive);
287             }
288
289             String JavaDoc[] dirs = scanner.getIncludedDirectories();
290             for (int j = 0; j < dirs.length; j++) {
291                 Component expandedArchive = getNewComponent();
292                 expandedArchive.setFile(new File JavaDoc(baseDir, dirs[j]));
293                 components.add(expandedArchive);
294             }
295         }
296     }
297
298     protected void checkConfiguration() throws BuildException {
299         super.checkConfiguration();
300
301         log(components.size() + " components were found.", Project.MSG_DEBUG);
302
303         if (components.size() == 0) { // This isn't necessarily a failure
304
log(lsm.getString("NoComponentsSpecified"), Project.MSG_WARN);
305         }
306
307     }
308
309     /**
310      * Verifies that the options and parameters selected by the user are valid
311      * and consistent for a given server.
312      *
313      * @param aServer The server whose configuration is to be validated.
314      * @throws BuildException If the user selections are invalid.
315      */

316     protected void checkConfiguration(Server aServer) throws BuildException {
317         String JavaDoc hostname = aServer.getHost();
318         if (hostname == null) {
319             hostname = "localhost";
320         }
321         log("Checking server config for " + hostname, Project.MSG_DEBUG);
322
323         if (!aServer.hasPassword()) {
324             throw new BuildException(lsm.getString("PasswordMustBeSpecified",
325                                    new Object JavaDoc[] {getTaskName(),
326                                    hostname}), getLocation());
327         }
328
329         Iterator JavaDoc iterator = components.iterator();
330         while (iterator.hasNext()) { // Check the config on each component found
331
Component comp = (Component)iterator.next();
332             checkComponentConfig(aServer, comp);
333         }
334     }
335
336     /**
337      * Verifies the options and parameters selected by the user are valid and
338      * consistent for a given server and given component.
339      *
340      * @param aServer The server where the command will be executed.
341      * @param comp The component which is the target of the command.
342      */

343     protected void checkComponentConfig(Server aServer, Component comp) {
344         log("Checking config for server \"" + aServer + "\" and component \""
345                 + comp + "\"", Project.MSG_DEBUG);
346
347         // if specified, file must exist (either directory or file)
348
File JavaDoc theFile = comp.getFile();
349         log("The file for this component: " + theFile, Project.MSG_DEBUG);
350         if ((theFile != null) && (!theFile.exists())) {
351             throw new BuildException(lsm.getString("FileCouldNotBeFound",
352                                    new Object JavaDoc[] {theFile}),
353                                    getLocation());
354         }
355
356         // name must be >0 characters
357
String JavaDoc theName = comp.getName();
358         if ((theName == null) || (theName.length() == 0)) {
359             throw new BuildException(lsm.getString("CouldNotDetermineComponentName"),
360                          getLocation());
361         }
362
363         // type must be valid
364
String JavaDoc theType = comp.getType();
365         if ((theType != null) && (!TYPE_MAP.values().contains(theType))) {
366             throw new BuildException(lsm.getString("TypeNotValid",
367                                    new Object JavaDoc[] {theType}),
368                          getLocation());
369         }
370
371     }
372
373     /**
374      * Gets the Sun ONE Application Server command to be executed on the
375      * specified server for the given component.
376      *
377      * @param aServer The server where the command will be executed.
378      * @param comp The component which is the target of the command.
379      */

380     protected abstract String JavaDoc getCommandString(Server aServer, Component comp);
381
382     /**
383      * This inner class is used to represent J2EE components used with the
384      * Sun ONE Application Server.
385      *
386      * @author Greg Nelson <a HREF="mailto:gn@sun.com">gn@sun.com</a>
387      */

388     public class Component {
389         protected Component parent; // Attr values are inherited from parent
390
private File JavaDoc file; // Component archive or directory
391
private String JavaDoc name; // Display-name for the component
392
private String JavaDoc type; // Component type -- app, web, ejb, etc
393
private String JavaDoc target; // Target application server entity
394

395         /*
396          * Default values for some attributes.
397          */

398         private static final String JavaDoc DEFAULT_TYPE = TYPE_APP;
399
400         /**
401          * Constructs a new Component object without specifying a parent
402          * component from which values are inherited.
403          */

404         public Component() {
405             this(null);
406         }
407
408         /**
409          * Constructs a new Component object and specifies the parent component
410          * from which attribute values are inherited.
411          *
412          * @param component The parent component for this object.
413          */

414         public Component(Component parent) {
415             this.parent = parent;
416         }
417
418         /**
419          * Sets the parent server for this component. If attribute values are not
420          * explicitly set for this object, they may be inherited from the parent
421          * object.
422          *
423          * @param parent The parent server for this object.
424          */

425         public void setParent(Component parent) {
426             this.parent = parent;
427         }
428
429         /**
430          * Sets the filename for the component file (or directory) which is used
431          * by the administrative command.
432          *
433          * @param file The component file archive or directory
434          */

435         public void setFile(File JavaDoc file) {
436             this.file = file;
437         }
438
439         /**
440          * Returns the filename for the component file (or directory) which is
441          * used by the administrative command.
442          *
443          * @return The component file archive or directory
444          */

445         protected File JavaDoc getFile() {
446             return file;
447         }
448
449         /**
450          * Sets the display-name for the J2EE component. This is the "friendly
451          * name" which will appear in the admin GUI and administrative commands.
452          *
453          * @param name The component display-name
454          */

455         public void setName(String JavaDoc name) {
456             this.name = name;
457         }
458
459         /**
460          * Returns the display-name for the J2EE component. This is the
461          * "friendly name" which will appear in the admin GUI and administrative
462          * commands.
463          *
464          * @return The component display-name
465          */

466         protected String JavaDoc getName() {
467             if (name == null) {
468                 /* Use the file name (before the last '.') as the default name */
469                 String JavaDoc fileName = null;
470
471                 if (file != null) {
472                     fileName = file.getName();
473                 } else {
474                     return null;
475                 }
476
477                 int index = fileName.indexOf('.');
478                 name = (index < 0) ? fileName : fileName.substring(0, index);
479             }
480
481             return name;
482         }
483
484         /**
485          * Sets the component type. Valid types are "application", "ejb", "web",
486          * "connector" and "client"
487          *
488          * @param type The component type
489          */

490         public void setType(String JavaDoc type) {
491             this.type = type;
492         }
493
494         /**
495          * Returns the component type. Valid types are "application", "ejb",
496          * "web", "connector" and "client"
497          *
498          * @return The component type
499          */

500         protected String JavaDoc getType() {
501             if (type == null) {
502                 return null;
503             }
504             return type;
505         }
506
507         /**
508          * Sets the deployment target name,
509          * ex. valid server instance name or cluster name
510          *
511          * @param target Valid target name
512          */

513         public void setTarget(String JavaDoc target) {
514             this.target = target;
515         }
516
517         /**
518          * Returns the name of deployment target
519          *
520          * @return The target name
521          */

522         protected String JavaDoc getTarget() {
523             if (target == null) {
524                 return null;
525             }
526             return target;
527         }
528
529         public String JavaDoc toString() {
530             return getName();
531         }
532     }; // end of Component inner class
533
}
534
Popular Tags