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: * Paul Mahar
021: *
022: */
023: package org.enhydra.kelp.common.importer;
024:
025: // ToolBox
026: import org.enhydra.tool.common.FileUtil;
027: import org.enhydra.tool.common.FilenameFilter;
028: import org.enhydra.tool.common.PathHandle;
029:
030: // AddinCore
031: import org.enhydra.kelp.common.Constants;
032: import org.enhydra.kelp.common.PathUtil;
033: import org.enhydra.kelp.common.ValidationException;
034: import org.enhydra.kelp.common.ValidationUtil;
035: import org.enhydra.kelp.common.map.MapEntry;
036: import org.enhydra.kelp.common.map.MapUtil;
037: import org.enhydra.kelp.common.node.OtterProject;
038: import org.enhydra.kelp.common.node.PropertyKeys;
039:
040: // JDK
041: import java.io.File;
042: import java.util.ResourceBundle;
043:
044: //
045: public class ImportPaths implements PropertyKeys {
046:
047: // strings not to be translated
048: static ResourceBundle res = ResourceBundle
049: .getBundle("org.enhydra.kelp.common.Res"); // nores
050: private final String PRESENTATION = "presentation"; // nores
051: private final String[] PACKAGE_KEYS = { ".business", ".data",
052: '.' + PRESENTATION }; // nores
053:
054: //
055: private OtterProject project = null;
056: private String pack = new String();
057: private String[][] packMap = new String[0][2];
058: private String deployRootPath = null;
059: private String resourcePath = null;
060: private String sourcePath = null;
061: private String rootPath = null;
062: private String defaultSourcePath = null;
063: private String defaultPackage = null;
064: private ResourceFilter resourceFilter = new ResourceFilter();
065: private JavaFilter javaFilter = new JavaFilter();
066: private MakefileReader makefileReader = new MakefileReader();
067:
068: public ImportPaths() {
069: }
070:
071: protected void initProject() {
072: FilenameFilter filter = null;
073: File firstFound = null;
074:
075: getProject().setWorkingPath(getRootPath()); // 1st set
076: getProject().setDeployRootPath(getDeployRootPath());
077: getProject().setDeployResourcePath(getResourcePath());
078: getProject().setClassOutputPath(getClassOutputPath());
079: getProject().setPackageMap(getPackageMap());
080:
081: // determine type
082: filter = new FilenameFilter();
083: filter.setDirectoryValid(false);
084: filter.setIncludeName(Constants.FILE_WEB_XML);
085: firstFound = FileUtil.findFirst(filter, getSourcePath());
086: if (firstFound == null) {
087: filter.setIncludeName(Constants.FILE_ENHYDRA_SERVICE_XML);
088: firstFound = FileUtil.findFirst(filter, getSourcePath());
089: if (firstFound == null) {
090: getProject().setDeployType(OtterProject.TYPE_EN3APP);
091: } else {
092: //Dusan
093: // getProject().setDeployType(OtterProject.TYPE_SERVICE);
094: }
095: } else {
096: getProject().setDeployType(OtterProject.TYPE_WEBAPP);
097: }
098: getProject().setProperty(PropertyKeys.NAME_LIBRARIES,
099: PropertyKeys.VALUE_ENHYDRA);
100: }
101:
102: public OtterProject getProject() {
103: return project;
104: }
105:
106: public MakefileReader getMakefileReader() {
107: return makefileReader;
108: }
109:
110: public JavaFilter getJavaFilter() {
111: return javaFilter;
112: }
113:
114: public void setProject(OtterProject p) {
115: project = p;
116: resourceFilter.setProject(project);
117: makefileReader.setProject(project);
118: }
119:
120: /**
121: * Method declaration
122: *
123: *
124: * @return
125: */
126: public String getPackage() {
127: return pack;
128: }
129:
130: /**
131: * Method declaration
132: *
133: *
134: * @param pac
135: *
136: * @exception ValidationException
137: */
138: public void setPackage(String pac) throws ValidationException {
139: if (ValidationUtil.isJavaPackage(pac)) {
140: pack = pac;
141: } else {
142: throw new ValidationException(res
143: .getString("Not_a_valid_package")
144: + pac);
145: }
146: }
147:
148: public void initPackageMap(boolean write) {
149: String[][] readMap = makefileReader.getPackageMap();
150:
151: makefileReader.invalidate();
152: String[][] defMap = getDefaultPackageMap();
153: MapEntry[] entries = MapUtil.combine(MapUtil
154: .toEntryArray(readMap), MapUtil.toEntryArray(defMap));
155:
156: packMap = MapUtil.toStringArray(entries);
157: if (write) {
158: getProject().setPackageMap(packMap);
159: }
160: }
161:
162: /**
163: * Method declaration
164: *
165: *
166: * @return
167: */
168: public String[][] getPackageMap() {
169: return packMap;
170: }
171:
172: /**
173: * Method declaration
174: *
175: *
176: * @param m
177: */
178: public void setPackageMap(String[][] m) {
179: packMap = m;
180: }
181:
182: /**
183: *
184: */
185: protected String getPackagePath() {
186: return createPackagePath(getPackage());
187: }
188:
189: /**
190: *
191: */
192: private String createPackagePath(String packPath) {
193: String path = new String();
194:
195: if (packPath != null) {
196: path = packPath.replace('.', '/');
197: }
198: return path;
199: }
200:
201: /**
202: * Method declaration
203: *
204: *
205: * @return
206: */
207: public String getResourcePath() {
208: if (resourcePath == null) {
209: resourcePath = getDefaultResourcePath();
210: }
211: return resourcePath;
212: }
213:
214: /**
215: * Method declaration
216: *
217: *
218: * @param dir
219: *
220: * @exception ValidationException
221: */
222: public void setResourcePath(String path) throws ValidationException {
223: if (ValidationUtil.isDirectory(path)) {
224: resourcePath = PathHandle.createPathString(path);
225: } else {
226: throw new ValidationException(res
227: .getString("Resource_not_found")
228: + path);
229: }
230: }
231:
232: /**
233: * Method declaration
234: *
235: *
236: * @return
237: */
238: public String getRootPath() {
239: return rootPath;
240: }
241:
242: /**
243: * Method declaration
244: *
245: *
246: * @param dir
247: *
248: * @exception ValidationException
249: */
250: public void setRootPath(String path) throws ValidationException {
251: if (ValidationUtil.isDirectory(path)) {
252: rootPath = PathHandle.createPathString(path);
253: defaultSourcePath = null;
254: } else {
255: throw new ValidationException(res
256: .getString("Project_root_not_found"));
257: }
258: }
259:
260: /**
261: * Method declaration
262: *
263: *
264: * @return
265: */
266: public String getSourcePath() {
267: return sourcePath;
268: }
269:
270: /**
271: * Method declaration
272: *
273: *
274: * @param dir
275: *
276: * @exception ValidationException
277: */
278: public void setSourcePath(String path) throws ValidationException {
279: File dir = null;
280:
281: if ((path == null) || (path.trim().length() == 0)) {
282: throw new ValidationException(res
283: .getString("Source_path_empty"));
284: } else {
285: dir = new File(path);
286: }
287: if (ValidationUtil.isDirectory(dir)) {
288: sourcePath = PathHandle.createPathString(dir);
289: defaultPackage = null;
290: } else {
291: StringBuffer message = new StringBuffer();
292:
293: if (dir == null) {
294: message.append(res.getString("Source_null"));
295: } else {
296: message.append(res.getString("Source_not_found"));
297: message.append(dir.getAbsolutePath());
298: }
299: throw new ValidationException(message.toString());
300: }
301: }
302:
303: /**
304: * Method declaration
305: *
306: *
307: * @return
308: */
309: public String getDefaultPackage() {
310: File firstFound = null;
311: int index = 0;
312:
313: if (defaultPackage == null) {
314: if (getSourcePath() != null) {
315: firstFound = FileUtil.findFirst(javaFilter,
316: getSourcePath());
317: }
318: if (firstFound != null) {
319: defaultPackage = ImportTool.readPackage(firstFound);
320: for (int i = 0; i < PACKAGE_KEYS.length; i++) {
321: index = defaultPackage.indexOf(PACKAGE_KEYS[i]);
322: if (index > 0) {
323: defaultPackage = defaultPackage.substring(0,
324: index);
325: break;
326: }
327: }
328: }
329: }
330: return defaultPackage;
331: }
332:
333: /**
334: * Method declaration
335: *
336: *
337: * @return
338: */
339: public String[][] getDefaultPackageMap() {
340: String[][] defaultMap = new String[1][2];
341: StringBuffer buf = new StringBuffer();
342: File firstFound = null;
343:
344: if (getPackage().length() == 0) {
345: buf.append(getDefaultPackage());
346: } else {
347: buf.append(getPackage());
348: }
349: if (buf.toString().trim().length() > 0) {
350: buf.append('.');
351: }
352: buf.append(PRESENTATION);
353: defaultMap[0][0] = getResourcePath();
354: defaultMap[0][1] = buf.toString();
355:
356: // That normally works, but lets check a different parallel
357: // html to java structure.
358: if (getResourcePath() != null) {
359: File resource = new File(getResourcePath());
360:
361: firstFound = FileUtil
362: .findFirst(resourceFilter, resource, 0);
363: }
364: if (firstFound != null) {
365: File parent = firstFound.getParentFile().getParentFile();
366: File[] dirList = parent.listFiles();
367: File[] javaList = null;
368: String baseName = firstFound.getName();
369:
370: baseName = baseName.substring(0, baseName.indexOf('.'));
371: for (int i = 0; i < dirList.length; i++) {
372: if (dirList[i].isDirectory()) {
373: javaList = dirList[i].listFiles(javaFilter);
374: for (int j = 0; j < javaList.length; j++) {
375: if (javaList[j].getName().startsWith(baseName)) {
376: defaultMap[0][0] = PathHandle
377: .createPathString(firstFound
378: .getParentFile());
379: defaultMap[0][1] = ImportTool
380: .readPackage(javaList[j]);
381: break;
382: }
383: }
384: }
385: }
386: }
387: return defaultMap;
388: }
389:
390: public String getDefaultResourcePath() {
391: String srcPath = new String();
392: String appPackagePath = new String();
393: String path = new String();
394:
395: if (getPackage() == null) {
396: appPackagePath = createPackagePath(getDefaultPackage());
397: } else {
398: appPackagePath = getPackagePath();
399: }
400: if (getSourcePath() != null) {
401: srcPath = getSourcePath();
402: } else if (getDefaultSourcePath() != null) {
403: srcPath = getDefaultSourcePath();
404: } else if (getRootPath() != null) {
405: srcPath = getRootPath();
406: }
407: path = PathUtil.getDefaultDeployResourcePath(project,
408: appPackagePath, srcPath);
409: return path;
410: }
411:
412: /**
413: * Method declaration
414: *
415: *
416: * @return
417: */
418: public String getDefaultSourcePath() {
419: File firstFound = null;
420: String inPack = new String();
421: int depth = 0;
422:
423: if (defaultSourcePath == null) {
424: if (rootPath != null) {
425: defaultSourcePath = rootPath;
426: firstFound = FileUtil.findFirst(javaFilter, rootPath);
427: }
428: if (firstFound != null) {
429: inPack = ImportTool.readPackage(firstFound);
430: if (ValidationUtil.isJavaPackage(inPack)) {
431: if (inPack.length() > 0) {
432: depth = charCount(inPack, '.') + 1;
433: }
434: }
435: File foundParent = firstFound.getParentFile();
436:
437: while (depth > 0) {
438: foundParent = foundParent.getParentFile();
439: depth--;
440: }
441: defaultSourcePath = foundParent.getAbsolutePath();
442: }
443: }
444: defaultSourcePath = PathHandle
445: .createPathString(defaultSourcePath);
446: return defaultSourcePath;
447: }
448:
449: public void setDeployRootPath(String p) {
450: deployRootPath = p;
451: }
452:
453: public String getDeployRootPath() {
454: String p = deployRootPath;
455:
456: if (p == null) {
457: StringBuffer buf = new StringBuffer();
458:
459: buf.append(getRootPath());
460: buf.append(File.separator);
461: buf.append(Constants.DIR_OUTPUT);
462: p = PathHandle.createPathString(buf.toString());
463: }
464: return p;
465: }
466:
467: // /
468: // / PROTECTED
469: // /
470: protected String getClassOutputPath() {
471: StringBuffer buf = new StringBuffer();
472:
473: buf.append(getRootPath());
474: buf.append(File.separator);
475: buf.append(Constants.DIR_CLASSES);
476: return PathHandle.createPathString(buf.toString());
477: }
478:
479: /**
480: * Method declaration
481: *
482: *
483: * @param in
484: * @param lookFor
485: *
486: * @return
487: */
488: private int charCount(String in, char lookFor) {
489: String search = new String(in);
490: int index = search.indexOf(lookFor);
491: int count = 0;
492:
493: while (index > 0) {
494: count++;
495: search = search.substring(index + 1);
496: index = search.indexOf(lookFor);
497: }
498: return count;
499: }
500:
501: }
|