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.deployer;
024:
025: // Toolbox imports
026: import org.enhydra.tool.common.ToolException;
027: import org.enhydra.tool.common.PathHandle;
028: import org.enhydra.tool.common.FileUtil;
029:
030: // Kelp imports
031: import org.enhydra.kelp.common.AbstractEchoBuilder;
032: import org.enhydra.kelp.common.PathUtil;
033: import org.enhydra.kelp.common.event.WriteListener;
034: import org.enhydra.kelp.common.map.Mapper;
035: import org.enhydra.kelp.common.node.OtterTextFileNode;
036: import org.enhydra.kelp.common.node.OtterDocumentNode;
037: import org.enhydra.kelp.common.node.OtterFileNode;
038: import org.enhydra.kelp.common.node.OtterProject;
039:
040: //
041: import java.io.File;
042: import java.util.ResourceBundle;
043:
044: //
045: public class ContentBuilder extends AbstractEchoBuilder {
046: static ResourceBundle res = ResourceBundle
047: .getBundle("org.enhydra.kelp.common.Res"); // nores
048: private Mapper mapper = null;
049: private PathHandle resourcePath = null;
050: private String copyingTo = res.getString("Copying_to");
051:
052: public ContentBuilder(WriteListener listener) {
053: super (listener);
054: }
055:
056: protected void buildImpl() {
057: makeStaticDocuments();
058: makeNonDocumentContent();
059: makeDeploymentDesc();
060: }
061:
062: public void setProject(OtterProject p) {
063: super .setProject(p);
064: mapper = new Mapper(getProject());
065: resourcePath = PathHandle.createPathHandle(getProject()
066: .getDeployResourcePath());
067: }
068:
069: private void makeStaticDocuments() {
070:
071: // Find all images in project and copy using
072: // the mapping table.
073: OtterDocumentNode[] docs = getProject().getAllDocuments();
074: File source = null;
075: File dest = null;
076: String destPath = new String();
077:
078: for (int i = 0; i < docs.length; i++) {
079: if (docs[i].isStatic()) {
080: destPath = getResourceDestPath(docs[i]);
081: dest = new File(destPath);
082: source = new File(docs[i].getFilePath());
083: try {
084: FileUtil.copy(source, dest);
085: echo(copyingTo + destPath);
086: } catch (ToolException e) {
087: echo(e);
088: e.printStackTrace();
089: }
090: }
091: }
092: }
093:
094: /**
095: * Method declaration
096: *
097: */
098: private void makeNonDocumentContent() {
099:
100: // Find all images in project and copy using
101: // the mapping table.
102: String[] types = new String[0];
103: OtterFileNode[] content = new OtterFileNode[0];
104: PathHandle cursorPath = null;
105:
106: types = getProject().getNonDocumentContentTypes();
107: content = getProject().findFileNodesByType(types);
108: File source = null;
109: File dest = null;
110: String destPath = new String();
111:
112: for (int i = 0; i < content.length; i++) {
113: cursorPath = PathHandle.createPathHandle(content[i]
114: .getFilePath());
115: if (resourcePath.parentOf(cursorPath)) {
116: destPath = getResourceDestPath(content[i]);
117: dest = new File(destPath);
118: source = cursorPath.getFile();
119: try {
120: FileUtil.copy(source, dest);
121: echo(copyingTo + destPath);
122: } catch (ToolException e) {
123: echo(e);
124: e.printStackTrace();
125: }
126: }
127: }
128: }
129:
130: private void makeDeploymentDesc() {
131:
132: // Find all images in project and copy using
133: // the mapping table.
134: OtterTextFileNode[] descs = getProject()
135: .getAllDeploymentDescs();
136: File source = null;
137: File dest = null;
138: String sourcePath = null;
139: String rawFile = null;
140: StringBuffer outPath = new StringBuffer();
141:
142: for (int i = 0; i < descs.length; i++) {
143: source = new File(descs[i].getFilePath());
144: sourcePath = getProject().getSourcePathOf(descs[i]);
145: rawFile = (new File(descs[i].getFilePath()))
146: .getAbsolutePath();
147: outPath.append(PathUtil.getDeployContentPath(getProject()));
148: outPath.append(rawFile.substring(sourcePath.length()));
149: dest = new File(outPath.toString());
150: try {
151: FileUtil.copy(source, dest);
152: echo(copyingTo + PathHandle.createPathString(dest));
153: } catch (ToolException e) {
154: echo(e);
155: e.printStackTrace();
156: }
157: }
158: }
159:
160: /**
161: * Method declaration
162: *
163: * @param resource
164: *
165: * @return
166: */
167: private String getResourceDestPath(OtterFileNode resource) {
168: StringBuffer destPath = new StringBuffer();
169: PathHandle sourceFilePath = null;
170: int type = OtterProject.TYPE_WEBAPP;
171:
172: type = getProject().getDeployType();
173: sourceFilePath = PathHandle.createPathHandle(resource
174: .getFilePath());
175: if (type == OtterProject.TYPE_EN3APP) {
176: destPath.append(mapper.getMappedOutputPath(resource));
177: } else {
178: String suffix = new String();
179:
180: destPath
181: .append(PathUtil.getDeployContentPath(getProject()));
182: suffix = sourceFilePath.getPath().substring(
183: resourcePath.getPath().length());
184: if ((suffix.length() == 0) || (suffix.charAt(0) != '/')) {
185: destPath.append('/');
186: }
187: destPath.append(suffix);
188: }
189: return PathHandle.createPathString(destPath.toString());
190: }
191:
192: }
|