KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > tool > configure > ConfigTool


1
2 /*
3  * Enhydra Java Application Server Project
4  *
5  * The contents of this file are subject to the Enhydra Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License on
8  * the Enhydra web site ( http://www.enhydra.org/ ).
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific terms governing rights and limitations
13  * under the License.
14  *
15  * The Initial Developer of the Enhydra Application Server is Lutris
16  * Technologies, Inc. The Enhydra Application Server and portions created
17  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
18  * All Rights Reserved.
19  *
20  * Contributor(s):
21  *
22  */

23 package org.enhydra.tool.configure;
24
25 // ToolBox imports
26
import org.enhydra.tool.ToolBoxInfo;
27 import org.enhydra.tool.common.FileUtil;
28 import org.enhydra.tool.common.PathHandle;
29 import org.enhydra.tool.common.Replacement;
30 import org.enhydra.tool.common.ReplacementSet;
31 import org.enhydra.tool.common.TemplateTool;
32 import org.enhydra.tool.common.ToolException;
33
34 // Standard imports
35
import java.io.BufferedReader JavaDoc;
36 import java.io.IOException JavaDoc;
37 import java.io.File JavaDoc;
38 import java.io.FileFilter JavaDoc;
39 import java.util.Vector JavaDoc;
40 import java.util.ResourceBundle JavaDoc;
41
42 //
43
public class ConfigTool extends TemplateTool implements Constants {
44     private static ResourceBundle JavaDoc localRes =
45         ResourceBundle.getBundle("org.enhydra.tool.configure.Res"); // nores
46

47     //
48
private boolean transformPath = true;
49     private String JavaDoc javaPath = ToolBoxInfo.getJavaPath();
50     private String JavaDoc enhydraRoot = ToolBoxInfo.getEnhydraRoot();
51     private String JavaDoc appPath = null;
52     private FileFilter JavaDoc fillFilter = null;
53
54     //
55
private final String JavaDoc DIR_DIST = "dist"; // nores
56
private final String JavaDoc DIR_LIB = "lib"; // nores
57

58 // Compliance with WEBDOCWF begin
59
// WEBDOCWF line - has no sense in Enhydra 5.1
60
// private final String FILE_ENHYDRA_JAR = "webdocwf.jar"; // nores
61
// Compliance with WEBDOCWF end
62
private final String JavaDoc FILE_ENHYDRA_JAR = "enhydra.jar"; // nores
63

64     //
65
private final String JavaDoc AT_ENHYDRA_DIR_AT = "@ENHYDRA_DIR@"; // nores
66
private final String JavaDoc AT_JDKDIR_AT = "@JDKDIR@"; // nores
67
private final String JavaDoc AT_OUTPUT_AT = "@OUTPUT@"; // nores
68
private final String JavaDoc AT_ENHYDRA_CLASSES_AT =
69         "@ENHYDRA_CLASSES@"; // nores
70

71     //
72
public static final String JavaDoc PROJECT_PATH = "@PROJECT_PATH@"; // nores
73
public static final String JavaDoc JAVA_PATH = "@JAVA_PATH@"; // nores
74
public static final String JavaDoc DEPLOY_PATH = "@DEPLOY_PATH@"; // nores
75
public static final String JavaDoc ENHYDRA_PATH = "@ENHYDRA_PATH@"; // nores
76
public static final String JavaDoc[] PATHS = {
77         PROJECT_PATH, JAVA_PATH, DEPLOY_PATH, ENHYDRA_PATH
78     };
79
80     public static Replacement[] createReplacements(
81             String JavaDoc appRoot,
82             String JavaDoc deployPath,
83             String JavaDoc javaHome) throws ConfigException {
84         Replacement[] defRep = new Replacement[4];
85
86         try {
87             defRep[0] = new Replacement(ConfigTool.PROJECT_PATH,
88               PathHandle.createPathString(appRoot));
89             defRep[1] = new Replacement(ConfigTool.DEPLOY_PATH,
90               PathHandle.createPathString(deployPath));
91             defRep[2] = new Replacement(ConfigTool.JAVA_PATH,
92               PathHandle.createPathString(javaHome));
93             defRep[3] = new Replacement(ConfigTool.ENHYDRA_PATH,
94                                         ToolBoxInfo.getEnhydraRoot());
95         } catch (ToolException e) {
96             throw new ConfigException(e,
97                                       localRes.getString("Unable_to_create"));
98         }
99         return defRep;
100     }
101
102     public static String JavaDoc[] getSuffixArray() {
103         String JavaDoc[] sufs = new String JavaDoc[PATHS.length];
104
105         for (int i = 0; i < PATHS.length; i++) {
106             sufs[i] = PATHS[i].substring(1);
107         }
108         return sufs;
109     }
110
111     public static Replacement[] createReplacements(String JavaDoc deployPath)
112             throws ConfigException {
113         File JavaDoc f = new File JavaDoc(deployPath);
114         return ConfigTool.createReplacements(f.getParent(),
115                                              deployPath,
116                                              ToolBoxInfo.getJavaPath());
117     }
118
119     public static String JavaDoc[][] createReplacementStringArray(String JavaDoc appRoot,
120                                                           String JavaDoc deployPath,
121                                                           String JavaDoc javaP)
122             throws ConfigException {
123         Replacement[] reps = ConfigTool.createReplacements(appRoot, deployPath, javaP);
124         ReplacementSet set = new ReplacementSet(reps);
125
126         return set.toStringArray();
127     }
128
129     public ConfigTool() {}
130
131     public File JavaDoc[] createOutput() throws ToolException {
132         if (isTransformPath()) {
133             setReplacements(getTransformedReplacements());
134         }
135         return super.createOutput();
136     }
137
138     public boolean isTransformPath() {
139         return transformPath;
140     }
141
142     public void setTransformPath(boolean b) {
143         transformPath = b;
144     }
145
146     public String JavaDoc getAppPath() {
147         return appPath;
148     }
149
150     public void setAppPath(String JavaDoc p) {
151         appPath = p;
152     }
153
154     public String JavaDoc getEnhydraRoot() {
155         if (enhydraRoot == null) {
156             enhydraRoot = ToolBoxInfo.getEnhydraRoot();
157         }
158         return enhydraRoot;
159     }
160
161     public void setEnhydraRoot(String JavaDoc p) {
162         enhydraRoot = p;
163     }
164
165     public String JavaDoc getJavaPath() {
166         if (javaPath == null) {
167             javaPath = ToolBoxInfo.getJavaPath();
168         }
169         return javaPath;
170     }
171
172     public void setJavaPath(String JavaDoc p) {
173         javaPath = p;
174     }
175
176     public void configure() throws ConfigException {
177         File JavaDoc dest = null;
178         File JavaDoc source = null;
179         File JavaDoc[] output = new File JavaDoc[0];
180         String JavaDoc[] types = new String JavaDoc[1];
181
182         types[0] = TYPE_IN;
183
184         try {
185             if (getAppPath() == null) {
186                 source = new File JavaDoc(getEnhydraRoot() + File.separator
187                                   + DIR_DIST);
188                 dest = new File JavaDoc(getEnhydraRoot());
189                 initEnhydraReplacements();
190             } else {
191                 source = new File JavaDoc(getAppPath() + File.separator + DIR_INPUT);
192                 dest = new File JavaDoc(getAppPath() + File.separator + DIR_OUTPUT);
193                 initAppReplacements(dest);
194             }
195             setOverwrite(!isSwing());
196             initTemplates(source, dest, types);
197             output = createOutput();
198         } catch (ToolException e) {
199             throw (new ConfigException(e,
200                                        localRes.getString("Unable_to_complete")));
201         }
202         if (output.length == 0 && isEcho()) {
203             System.out.println(localRes.getString("No_templates"));
204         }
205     }
206
207     public void initReplacements(String JavaDoc[][] strings) throws ConfigException {
208         Replacement[] reps = new Replacement[strings.length];
209
210         try {
211             for (int i = 0; i < reps.length; i++) {
212                 reps[i] = new Replacement(strings[i][0], strings[i][1]);
213             }
214         } catch (ToolException e) {
215             throw new ConfigException(e,
216                                       localRes.getString("Unable_to_initialize"));
217         }
218         setReplacements(reps);
219     }
220
221     private void initAppReplacements(File JavaDoc deployRoot)
222             throws ConfigException {
223         Replacement[] newReps = new Replacement[0];
224
225         newReps = ConfigTool.createReplacements(
226            deployRoot.getParent(),
227            deployRoot.getAbsolutePath(),
228            getJavaPath());
229         setReplacements(newReps);
230     }
231
232     private void initEnhydraReplacements() throws ConfigException {
233         Replacement[] newReps = new Replacement[4];
234
235         try {
236             newReps[0] = new Replacement(AT_ENHYDRA_DIR_AT, getEnhydraRoot());
237             newReps[1] = new Replacement(AT_JDKDIR_AT, getJavaPath());
238             newReps[2] = new Replacement(AT_OUTPUT_AT, getEnhydraRoot());
239             newReps[3] = new Replacement(AT_ENHYDRA_CLASSES_AT,
240                                          getEnhydraRoot() + '/' + DIR_LIB
241                                          + '/' + FILE_ENHYDRA_JAR);
242             setReplacements(newReps);
243         } catch (ToolException e) {
244             throw new ConfigException(e,
245                                       localRes.getString("Unable_to_initialize"));
246         }
247     }
248
249     private Replacement[] getTransformedReplacements() {
250         final String JavaDoc AT = (new String JavaDoc()) + '@';
251         final String JavaDoc _PATH = "_PATH@"; // nores
252
final String JavaDoc OS_ = "@OS_"; // nores
253
final String JavaDoc SHELL_ = "@SHELL_"; // nores
254
final String JavaDoc JAVA_ = "@JAVA_"; // nores
255

256         //
257
Vector JavaDoc transVector = new Vector JavaDoc();
258         Replacement[] trans = new Replacement[0];
259         String JavaDoc cursorFind = new String JavaDoc();
260         String JavaDoc newFind = new String JavaDoc();
261         String JavaDoc path = new String JavaDoc();
262
263         for (int i = 0; i < getReplacements().length; i++) {
264             transVector.addElement(getReplacements()[i]);
265             cursorFind = getReplacements()[i].getFind();
266             if (cursorFind.startsWith(AT) && cursorFind.endsWith(_PATH)) {
267                 if (!cursorFind.startsWith(OS_)) {
268                     path = getReplacements()[i].getReplaceWith()[0];
269                     path = FileUtil.toCurrentPath(path);
270                     try {
271                         newFind = OS_ + cursorFind.substring(1);
272                         Replacement osPath = new Replacement(newFind, path);
273
274                         transVector.addElement(osPath);
275                     } catch (ToolException e) {
276                         echoWrite(e);
277                     }
278                 }
279                 if (!cursorFind.startsWith(SHELL_)) {
280                     newFind = SHELL_ + cursorFind.substring(1);
281                     path = getReplacements()[i].getReplaceWith()[0];
282                     path = FileUtil.toShellPath(path);
283                     try {
284                         Replacement shellPath = new Replacement(newFind,
285                                                                 path);
286
287                         transVector.addElement(shellPath);
288                     } catch (ToolException e) {
289                         echoWrite(e);
290                     }
291                 }
292                 if ((!cursorFind.startsWith(JAVA_)) || cursorFind.equals(JAVA_PATH)) {
293                     newFind = JAVA_ + cursorFind.substring(1);
294                     path = getReplacements()[i].getReplaceWith()[0];
295                     path = FileUtil.toJavaPath(path);
296                     try {
297                         Replacement javaRep = new Replacement(newFind, path);
298                         transVector.addElement(javaRep);
299                     } catch (ToolException e) {
300                         echoWrite(e);
301                     }
302                 }
303             }
304         }
305         trans = (Replacement[]) transVector.toArray(trans);
306         transVector.removeAllElements();
307         return trans;
308     }
309
310 }
311
Popular Tags