001: /*
002: * Enhydra Java Application Server Project
003: *
004: * The contents of this file are subject to the Enhydra Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License on
007: * the Enhydra web site ( http://www.enhydra.org/ ).
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
011: * the License for the specific terms governing rights and limitations
012: * under the license.
013: *
014: * The Initial Developer of the Enhydra Application Server is Lutris
015: * Technologies, Inc. The Enhydra Application Server and portions created
016: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
017: * All Rights Reserved.
018: *
019: * Contributor(s):
020: *
021: */
022: package org.enhydra.kelp.common;
023:
024: // ToolBox imports
025: import org.enhydra.tool.common.PathHandle;
026: import org.enhydra.tool.common.FileUtil;
027: import org.enhydra.tool.common.ExtensionFilter;
028: import org.enhydra.tool.configure.ConfigTool;
029:
030: // Kelp imports
031: import org.enhydra.kelp.common.Constants;
032: import org.enhydra.kelp.common.importer.JavaFilter;
033: import org.enhydra.kelp.common.node.OtterTemplateNode;
034: import org.enhydra.kelp.common.node.OtterProject;
035:
036: // Standard imports
037: import java.io.BufferedReader;
038: import java.io.IOException;
039: import java.io.File;
040: import java.io.FileFilter;
041: import java.io.FileNotFoundException;
042: import java.io.FileReader;
043: import java.util.Vector;
044:
045: //
046: public class Backward {
047:
048: // strings not to be resourced
049: private final static String RELATIVE_CLASSES = "= ../classes";
050:
051: //
052: private Vector phVector = new Vector();
053:
054: public static boolean createIsWebApp(String[] sourcePaths) {
055: boolean webApp = true;
056: PresentationFilter filter = new PresentationFilter();
057:
058: for (int i = 0; i < sourcePaths.length; i++) {
059: File found = FileUtil.findFirst(filter, sourcePaths[i]);
060:
061: if (found != null) {
062: webApp = filter.isWebApp();
063: break;
064: }
065: }
066: return webApp;
067: }
068:
069: public static String createDefaultInputPath(String in,
070: String[] sourcePaths) {
071: File file = null;
072: String out = new String(in);
073:
074: file = new File(out);
075: if (file.isDirectory()) {
076:
077: // done
078: } else if (file.getParentFile().exists()) {
079: file = new File(file.getParentFile(), Constants.DIR_INPUT);
080: if (file.exists()) {
081: out = file.getAbsolutePath();
082:
083: // done
084: } else {
085: ExtensionFilter filter = new ExtensionFilter();
086: StringBuffer mkIn = new StringBuffer();
087:
088: mkIn.append('.');
089: mkIn.append(Constants.TYPE_MK);
090: mkIn.append('.');
091: mkIn.append(Constants.TYPE_IN);
092: filter.setDirectoryValid(false);
093: filter.addExtension(Constants.TYPE_IN);
094: filter.addExclusion(mkIn.toString());
095: for (int i = 0; i < sourcePaths.length; i++) {
096: file = FileUtil.findFirst(filter, sourcePaths[i]);
097: if (file != null) {
098: out = file.getParent();
099: break;
100: }
101: }
102: }
103: }
104: out = PathHandle.createPathString(out);
105: return out;
106: }
107:
108: public static String createDefaultBootstrapPath(String in,
109: OtterProject project) {
110: String out = new String(in);
111: String deployPath = new String();
112: String inputPath = new String();
113: File file = new File(out);
114: BootstrapFilter filter = null;
115:
116: if (file.isFile()) {
117: out = PathHandle.createPathString(file);
118:
119: // done
120: } else {
121: deployPath = project.getDeployRootPath();
122: filter = new BootstrapFilter(false);
123: file = FileUtil.findFirst(filter, deployPath);
124: if (file != null && file.isFile()) {
125: out = PathHandle.createPathString(file);
126:
127: // done
128: } else {
129: inputPath = project.getDeployInputPath();
130: filter = new BootstrapFilter(true);
131: file = FileUtil.findFirst(filter, inputPath);
132: if (file != null && file.isFile()) {
133: out = PathHandle.createPathString(file);
134: out = deployPath
135: + out.substring(inputPath.length(), out
136: .length() - 3);
137: }
138: }
139: }
140: return out;
141: }
142:
143: public static String[][] createReplacementTable(String[][] tableIn,
144: OtterTemplateNode[] templates) {
145: String[][] tableOut = new String[0][2];
146: String[] placeholders = new String[0];
147: Backward back = null;
148:
149: back = new Backward();
150: placeholders = back.findPlaceholders(templates);
151: if (placeholders.length == 0) {
152: tableOut = tableIn;
153: } else {
154: tableOut = new String[tableIn.length + placeholders.length][2];
155: for (int i = 0; i < placeholders.length; i++) {
156: tableOut[i][0] = placeholders[i];
157:
158: // strings not to be resourced
159: if (placeholders[i].equals(Backward.RELATIVE_CLASSES)) {
160: tableOut[i][1] = "= @JAVA_DEPLOY_PATH@/../classes";
161: } else if (placeholders[i].equals("@CLASSES@")) {
162: tableOut[i][1] = "@JAVA_DEPLOY_PATH@/../classes";
163: } else if (placeholders[i].equals("@SERVLET_CONF_DIR@")) {
164: tableOut[i][1] = "@JAVA_DEPLOY_PATH@/servlet";
165: } else if (placeholders[i].equals("@OUTPUT@")) {
166: tableOut[i][1] = "@JAVA_DEPLOY_PATH@";
167: } else {
168: tableOut[i][1] = placeholders[i];
169: }
170: }
171: for (int i = 0; i < tableIn.length; i++) {
172: tableOut[i + placeholders.length][0] = tableIn[i][0];
173: tableOut[i + placeholders.length][1] = tableIn[i][1];
174: }
175: }
176: return tableOut;
177: }
178:
179: private String[] findPlaceholders(OtterTemplateNode[] templates) {
180: String placeholders[] = new String[0];
181:
182: phVector.clear();
183: for (int i = 0; i < templates.length; i++) {
184: findPlaceholdersInFile(templates[i].getFilePath());
185: }
186: placeholders = new String[phVector.size()];
187: placeholders = (String[]) phVector.toArray(placeholders);
188: phVector.clear();
189: return placeholders;
190: }
191:
192: private void findPlaceholdersInFile(String path) {
193: BufferedReader reader = null;
194: File file = null;
195: String line = null;
196:
197: file = new File(path);
198: if (file.isFile()) {
199: try {
200: reader = new BufferedReader(new FileReader(file));
201: line = reader.readLine();
202: while (line != null) {
203: findRelativeClasses(line);
204: findPlaceholdersInLine(line);
205: line = reader.readLine();
206: }
207: reader.close();
208: } catch (FileNotFoundException e) {
209: e.printStackTrace();
210: } catch (IOException e) {
211: e.printStackTrace();
212: }
213: }
214: }
215:
216: private void findRelativeClasses(String line) {
217: int index = line.indexOf(Backward.RELATIVE_CLASSES);
218:
219: if (index > 0) {
220: if (!phVector.contains(Backward.RELATIVE_CLASSES)) {
221: phVector.insertElementAt(Backward.RELATIVE_CLASSES, 0);
222: }
223: }
224: }
225:
226: private void findPlaceholdersInLine(String line) {
227: String search = new String(line);
228: String[] exclude = ConfigTool.getSuffixArray();
229: StringBuffer ph = new StringBuffer();
230: int index = -1;
231: boolean start = false;
232: boolean include = true;
233:
234: index = search.indexOf('@');
235: while (index > -1) {
236: if (start) {
237: ph.setLength(0);
238: ph.append('@');
239: ph.append(search.substring(0, index));
240: ph.append('@');
241: if (!phVector.contains(ph.toString())) {
242: include = true;
243: for (int i = 0; i < exclude.length; i++) {
244: if (ph.toString().endsWith(exclude[i])) {
245: include = false;
246: break;
247: }
248: }
249: if (include) {
250: phVector.addElement(ph.toString());
251: }
252: }
253: }
254: if (search.length() > index) {
255: search = search.substring(index + 1);
256: } else {
257: search = new String();
258: }
259: start = (!start);
260: index = search.indexOf('@');
261: }
262: }
263:
264: }
265:
266: //
267: class BootstrapFilter implements FileFilter {
268: private boolean template = false;
269:
270: public BootstrapFilter(boolean t) {
271: template = true;
272: }
273:
274: public boolean accept(File file) {
275: boolean accept = false;
276: String path = file.getAbsolutePath().toLowerCase();
277: StringBuffer multiserverBuf = new StringBuffer();
278: StringBuffer servletBuf = new StringBuffer();
279: StringBuffer kernelBuf = new StringBuffer();
280: StringBuffer bootstrapBuf = new StringBuffer();
281: StringBuffer bootstrap4Buf = new StringBuffer();
282:
283: //
284: multiserverBuf.append(Constants.FILE_MULTISERVER_CONF);
285: servletBuf.append(Constants.FILE_SERVLET_CONF);
286: kernelBuf.append(Constants.FILE_KERNEL_CONF);
287: bootstrapBuf.append(Constants.FILE_BOOTSTRAP_CONF);
288: bootstrap4Buf.append(Constants.FILE_BOOTSTRAP4_CONF);
289: if (template) {
290: multiserverBuf.append('.');
291: multiserverBuf.append(Constants.TYPE_IN);
292: servletBuf.append('.');
293: servletBuf.append(Constants.TYPE_IN);
294: kernelBuf.append('.');
295: kernelBuf.append(Constants.TYPE_IN);
296: bootstrapBuf.append('.');
297: bootstrapBuf.append(Constants.TYPE_IN);
298: bootstrap4Buf.append('.');
299: bootstrap4Buf.append(Constants.TYPE_IN);
300: }
301: if (file.isFile()) {
302: if (path.endsWith(kernelBuf.toString())
303: || path.endsWith(bootstrapBuf.toString())
304: || path.endsWith(bootstrap4Buf.toString())) {
305: accept = true;
306: }
307: }
308: return accept;
309: }
310:
311: }
312:
313: //
314: class PresentationFilter extends JavaFilter {
315:
316: // strings not to be resourced
317: private final static String HTTP_PRESENTATION = "HttpPresentation";
318: private final static String HTTP_SERVLET = "HttpServlet";
319:
320: //
321: private boolean webApp = true;
322:
323: public boolean accept(File f) {
324: boolean accept = super .accept(f);
325:
326: if (accept && f.canRead()) {
327: accept = false;
328: BufferedReader reader = null;
329: String line = new String();
330:
331: try {
332: reader = new BufferedReader(new FileReader(f));
333: line = reader.readLine();
334: while (line != null) {
335: if (line.indexOf(HTTP_PRESENTATION) > -1) {
336: webApp = false;
337: accept = true;
338: break;
339: } else if (line.indexOf(HTTP_SERVLET) > -1) {
340: webApp = true;
341: accept = true;
342: break;
343: }
344: line = reader.readLine();
345: }
346: reader.close();
347: } catch (IOException e) {
348: e.printStackTrace();
349: accept = false;
350: }
351: }
352: return accept;
353: }
354:
355: protected boolean isWebApp() {
356: return webApp;
357: }
358:
359: }
|