KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > iplanet > 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 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 JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.ArrayList JavaDoc;
34 import java.util.Map JavaDoc;
35 import java.util.HashMap JavaDoc;
36 import java.util.Iterator JavaDoc;
37
38 public abstract class ComponentAdmin extends IasAdmin {
39     private Component component;
40     private List JavaDoc components = new ArrayList JavaDoc();
41     private List JavaDoc filesets = new ArrayList JavaDoc();
42
43     protected static final String JavaDoc TYPE_APP = "application";
44     protected static final String JavaDoc TYPE_EJB = "ejb";
45     protected static final String JavaDoc TYPE_WEB = "web";
46     protected static final String JavaDoc TYPE_CONN = "connector";
47     protected static final String JavaDoc TYPE_CLIENT = "client";
48
49     protected static final java.util.Map JavaDoc TYPE_MAP = new HashMap JavaDoc(4); // FIXME (4 or 5 elements?)
50
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 // TYPE_MAP.put("???", TYPE_CLIENT); // FIXME
56
};
57
58     public void setFile(File JavaDoc file) {
59         getComponent().setFile(file);
60     }
61
62     public void setName(String JavaDoc name) {
63         getComponent().setName(name);
64     }
65
66     public void setType(String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc iterator = components.iterator();
114         while (iterator.hasNext()) {
115             Component comp = (Component)iterator.next();
116             String JavaDoc 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 JavaDoc baseDir = scanner.getBasedir();
126
127             String JavaDoc[] files = scanner.getIncludedFiles();
128             for (int j = 0; j < files.length; j++) {
129                 Component archive = new Component();
130                 archive.setFile(new File JavaDoc(baseDir, files[j]));
131                 components.add(archive);
132             }
133
134             String JavaDoc[] dirs = scanner.getIncludedDirectories();
135             for (int j = 0; j < dirs.length; j++) {
136                 Component expandedArchive = new Component();
137                 expandedArchive.setFile(new File JavaDoc(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 JavaDoc 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         // if specified, file must exist (either directory or file)
163
File JavaDoc theFile = comp.getFile();
164         if ((theFile != null) && (!theFile.exists())) {
165             String JavaDoc msg = "The file specified (" + theFile + ") could not be found.";
166             throw new BuildException(msg, getLocation());
167         }
168
169         // name must be >0 characters
170
String JavaDoc theName = comp.getName();
171         if ((theName == null) || (theName.length() == 0)) {
172             String JavaDoc msg = "A valid name for the component could not be determined.";
173             throw new BuildException(msg, getLocation());
174         }
175
176         // type must be valid
177
String JavaDoc theType = comp.getType();
178         if ((theType != null) && (!TYPE_MAP.values().contains(theType))) {
179             String JavaDoc msg = "The type specified (" + theType + ") is not valid.";
180             throw new BuildException(msg, getLocation());
181         }
182     }
183
184     protected abstract String JavaDoc getCommandString(Server server, Component comp);
185
186     public class Component {
187         private Component parent;
188         private File JavaDoc file;
189         private String JavaDoc name;
190         private String JavaDoc type;
191         private boolean force;
192         private boolean upload;
193         private String JavaDoc contextroot;
194
195         private boolean forceIsSet = false;
196         private boolean uploadIsSet = false;
197         private boolean contextRootIsSet = false;
198
199         private static final String JavaDoc 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 JavaDoc file) {
217             this.file = file;
218         }
219
220         protected File JavaDoc getFile() {
221             return file;
222         }
223
224         public void setName(String JavaDoc name) {
225             this.name = name;
226         }
227
228         protected String JavaDoc getName() {
229             if (name == null) {
230                 String JavaDoc 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 JavaDoc type) {
246             this.type = type;
247         }
248
249         /**
250          *
251          */

252         protected String JavaDoc getType() {
253             if (type == null) {
254                 String JavaDoc fileName = null;
255                 String JavaDoc 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 JavaDoc) 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 JavaDoc contextroot) {
300             contextRootIsSet = true;
301             this.contextroot = contextroot;
302         }
303
304         protected String JavaDoc getContextroot() {
305             return (contextroot != null) ? contextroot : getName();
306         }
307
308         protected boolean contextRootIsSet() {
309             return contextRootIsSet;
310         }
311     };
312 }
Popular Tags