KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Paul Mahar
22  *
23  */

24 package org.enhydra.tool.configure;
25
26 // ToolBox imports
27
import org.enhydra.tool.ToolBoxInfo;
28 import org.enhydra.tool.common.FileUtil;
29 import org.enhydra.tool.common.PathHandle;
30 import org.enhydra.tool.common.ResUtil;
31 import org.enhydra.tool.common.ExtensionFilter;
32 import org.enhydra.tool.common.ToolException;
33
34 // Standard imports
35
import java.io.File JavaDoc;
36 import java.util.ResourceBundle JavaDoc;
37 import java.util.ArrayList JavaDoc;
38 import java.util.Arrays JavaDoc;
39
40 //
41
public class Main implements Constants {
42     static ResourceBundle JavaDoc res =
43         ResourceBundle.getBundle("org.enhydra.tool.configure.Res"); // nores
44

45     //
46
private final String JavaDoc PARAM_JAVA = "-java"; // nores
47
private final String JavaDoc PARAM_ENHYDRA = "-enhydra"; // nores
48
private final String JavaDoc PARAM_APP = "-app"; // nores
49
private final String JavaDoc PARAM_ALL = "-all"; // nores
50

51     //
52
private ConfigTool tool = null;
53     private boolean copyNonTemplates = false;
54
55     public Main() {
56         System.out.println("Kelp Configuration Tool " +
57            ToolBoxInfo.getToolBoxVersion());
58         System.out.println(ToolBoxInfo.getCopyright());
59         tool = new ConfigTool();
60         tool.setOwner(null);
61         tool.setEcho(true);
62     }
63
64     public static void main(String JavaDoc[] args) {
65         Main main = new Main();
66
67         main.init(args);
68     }
69
70     //
71
//
72
private void init(String JavaDoc[] args) {
73         try {
74             parseArgs(args);
75             tool.configure();
76             if (copyNonTemplates) {
77                 doCopy();
78             }
79             System.out.println(new String JavaDoc());
80             System.out.println(ResUtil.format(res.getString("Enhydra_home_0_"),
81                                               tool.getEnhydraRoot()));
82             System.out.println(ResUtil.format(res.getString("Java_home_0_"),
83                                               tool.getJavaPath()));
84             if (tool.getAppPath() != null) {
85                 System.out.println(ResUtil.format(res.getString("Application_home_0_"),
86                                                   tool.getAppPath()));
87             }
88             System.out.println(new String JavaDoc());
89             System.out.println(res.getString("Configuration"));
90             System.out.println(new String JavaDoc());
91         } catch (ParameterException e) {
92             showUsage();
93             System.out.println(ResUtil.format(res.getString("Error_0_"),
94                                               e.getMessage()));
95         } catch (ConfigException e) {
96             System.out.println(ResUtil.format(res.getString("Error_0_"),
97                                               e.getMessage()));
98         }
99     }
100
101     private void doCopy() {
102         File JavaDoc sourceDir = null;
103         ExtensionFilter copyFilter = null;
104
105         sourceDir = new File JavaDoc(tool.getAppPath() + File.separator + DIR_INPUT);
106         copyFilter = new ExtensionFilter();
107         copyFilter.addExclusion(".in");
108         copyFilter.setExcludeOnly(true);
109         recurseCopy(sourceDir, copyFilter);
110     }
111
112     private void recurseCopy(File JavaDoc sourceDir, ExtensionFilter copyFilter) {
113         File JavaDoc[] sourceFiles = new File JavaDoc[0];
114         PathHandle dest = null;
115
116         sourceFiles = sourceDir.listFiles(copyFilter);
117         for (int i = 0; i < sourceFiles.length; i++) {
118             if (sourceFiles[i].isDirectory()) {
119                 recurseCopy(sourceFiles[i], copyFilter);
120             } else {
121                 dest = getDestPath(sourceFiles[i]);
122                 try {
123                     FileUtil.copy(sourceFiles[i], dest.getFile());
124                     System.out.println(res.getString("Copying_input_to")
125                                        + dest.getPath());
126                 } catch (ToolException e) {
127                     e.printStackTrace();
128                 }
129             }
130         }
131     }
132
133     private PathHandle getDestPath(File JavaDoc inFile) {
134         PathHandle inputPath = null;
135         PathHandle filePath = null;
136         PathHandle rootPath = null;
137         PathHandle passPath = null;
138         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
139         String JavaDoc suffix = new String JavaDoc();
140
141         inputPath = PathHandle.createPathHandle(tool.getAppPath()
142                                                 + File.separator + DIR_INPUT);
143         filePath = PathHandle.createPathHandle(inFile);
144         rootPath = PathHandle.createPathHandle(tool.getAppPath()
145                                                + File.separator + DIR_OUTPUT);
146         buf.append(rootPath.getPath());
147         if (inputPath.parentOf(filePath)) {
148             suffix =
149                 filePath.getPath().substring(inputPath.getPath().length());
150         } else {
151             suffix = filePath.getFile().getName();
152         }
153         if ((suffix.length() == 0) || (suffix.charAt(0) != '/')) {
154             buf.append('/');
155         }
156         buf.append(suffix);
157         passPath = PathHandle.createPathHandle(buf.toString());
158         return passPath;
159     }
160
161     private String JavaDoc[] extractAllOption(String JavaDoc[] args) {
162         boolean all = false;
163         boolean app = false;
164         ArrayList JavaDoc list = null;
165
166         list = new ArrayList JavaDoc(Arrays.asList(args));
167         for (int i = 0; i < args.length; i++) {
168             if (args[i].equalsIgnoreCase(PARAM_ALL)) {
169                 all = true;
170                 list.remove(args[i]);
171             }
172             if (args[i].equalsIgnoreCase(PARAM_APP)) {
173                 app = true;
174             }
175         }
176         copyNonTemplates = app && all;
177         list.trimToSize();
178         args = new String JavaDoc[list.size()];
179         args = (String JavaDoc[]) list.toArray(args);
180         return args;
181     }
182
183     private void parseArgs(String JavaDoc[] args) throws ParameterException {
184         args = extractAllOption(args);
185         if (args.length == 0) {
186
187             // done
188
} else if ((args.length % 2) == 1) {
189             throw new ParameterException(res.getString("Wrong_number_of"));
190         } else if (args.length > 6) {
191             throw new ParameterException(res.getString("Too_many_parameters"));
192         } else {
193             for (int i = 0; i < args.length; i += 2) {
194                 if (!isKey(args[i])) {
195                     throw new ParameterException(ResUtil.format(res.getString("Invalid_parameter_0_"),
196                                                                 args[i]));
197                 }
198                 int dirIndex = i + 1;
199
200                 if (dirIndex < args.length) {
201                     if ((!FileUtil.isDirectory(args[dirIndex]))) {
202                         throw new ParameterException(ResUtil.format(res.getString("Directory_not_found_0"),
203                                                                     args[dirIndex]));
204                     } else {
205                         initProperty(args[i], args[dirIndex]);
206                     }
207                 }
208             }
209         }
210     }
211
212     private void initProperty(String JavaDoc key, String JavaDoc in) {
213         String JavaDoc path = FileUtil.toCanonicalPath(in);
214
215         path = FileUtil.toJavaPath(path);
216         if (key.equalsIgnoreCase(PARAM_JAVA)) {
217             tool.setJavaPath(path);
218         } else if (key.equalsIgnoreCase(PARAM_ENHYDRA)) {
219             tool.setEnhydraRoot(path);
220         } else if (key.equalsIgnoreCase(PARAM_APP)) {
221             tool.setAppPath(path);
222         }
223     }
224
225     private boolean isKey(String JavaDoc key) {
226         return (key.equalsIgnoreCase(PARAM_JAVA)
227                 || key.equalsIgnoreCase(PARAM_ENHYDRA)
228                 || key.equalsIgnoreCase(PARAM_APP));
229     }
230
231     private void showUsage() {
232         System.out.println(res.getString("Usage01"));
233         System.out.println(res.getString("Usage02"));
234         System.out.println(res.getString("Usage03"));
235         System.out.println(new String JavaDoc());
236         System.out.println(ResUtil.format(res.getString("Usage04"),
237                                           PARAM_APP));
238         System.out.println(res.getString("Usage05"));
239         System.out.println(new String JavaDoc());
240         System.out.println(res.getString("Usage06"));
241         System.out.println(res.getString("Usage07"));
242         System.out.print(ResUtil.format(res.getString("Usage08"),
243                                         PARAM_JAVA));
244         System.out.print(ResUtil.format(res.getString("Usage09"),
245                                         PARAM_ENHYDRA));
246         System.out.print(ResUtil.format(res.getString("Usage10"), PARAM_APP,
247                                         PARAM_ALL));
248         System.out.println(new String JavaDoc());
249         System.out.println(new String JavaDoc());
250     }
251
252     private class ParameterException extends ConfigException {
253         public ParameterException(String JavaDoc msg) {
254             super(msg);
255         }
256
257     }
258 }
259
Popular Tags