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: // Kelp imports
026: import org.enhydra.kelp.common.AbstractEchoBuilder;
027: import org.enhydra.kelp.common.PathUtil;
028: import org.enhydra.kelp.common.event.WriteListener;
029: import org.enhydra.kelp.common.node.OtterTemplateNode;
030: import org.enhydra.kelp.common.node.OtterFileNode;
031: import org.enhydra.kelp.common.node.OtterNode;
032: import org.enhydra.kelp.common.node.OtterNodeFactory;
033:
034: // ToolBox imports
035: import org.enhydra.tool.common.FileUtil;
036: import org.enhydra.tool.common.PathHandle;
037: import org.enhydra.tool.common.Template;
038: import org.enhydra.tool.common.ToolException;
039: import org.enhydra.tool.configure.ConfigTool;
040:
041: // Standard imports
042: import java.awt.Component;
043: import java.io.File;
044: import java.util.ArrayList;
045: import java.util.Arrays;
046: import java.util.ResourceBundle;
047:
048: //
049: public class InputBuilder extends AbstractEchoBuilder {
050: static ResourceBundle res = ResourceBundle
051: .getBundle("org.enhydra.kelp.common.Res"); // nores
052: private File[] builtTemplates = new File[0];
053: private File[] builtPassthrough = new File[0];
054: private OtterNode[] nodes = null;
055: private Component owner = null;
056:
057: public InputBuilder(WriteListener listener) {
058: super (listener);
059: }
060:
061: public File[] getBuiltTemplates() {
062: return builtTemplates;
063: }
064:
065: public File[] getBuiltPassthrough() {
066: return builtPassthrough;
067: }
068:
069: public void setNodes(OtterNode[] n) {
070: nodes = n;
071: }
072:
073: public OtterNode[] getNodes() {
074: OtterNode[] n = new OtterTemplateNode[0];
075:
076: if (nodes == null) {
077: n = getProject().getAllInput();
078: } else {
079: n = nodes;
080: }
081: return n;
082: }
083:
084: protected void buildImpl() throws ToolException {
085: File[] out = new File[0];
086: ArrayList outList = new ArrayList();
087:
088: if (isInputEnabled()) {
089: builtTemplates = makeTemplates();
090: if (!getProject().isDeployInputFilter()) {
091: builtPassthrough = makePassthrough();
092: }
093: outList.addAll(Arrays.asList(builtTemplates));
094: outList.addAll(Arrays.asList(builtPassthrough));
095: out = (File[]) outList.toArray(out);
096: outList.clear();
097: addFilesToProject(out);
098: }
099: }
100:
101: public void setOwner(Component c) {
102: owner = c;
103: }
104:
105: public Component getOwner() {
106: return owner;
107: }
108:
109: //
110: private boolean isInputEnabled() {
111: boolean enabled = false;
112:
113: if (getNodes().length > 0) {
114: enabled = getNodes()[0].getProject().isDeployInput();
115: }
116: return enabled;
117: }
118:
119: private void addFilesToProject(File[] files) {
120: OtterNodeFactory factory = null;
121:
122: factory = getProject().getNodeFactory();
123: for (int i = 0; i < files.length; i++) {
124: if (!isFresh()) {
125: break;
126: }
127: factory.importFile(getProject(), files[i]);
128: }
129:
130: }
131:
132: private File[] makePassthrough() throws ToolException {
133: File[] out = new File[0];
134: OtterFileNode[] pass = new OtterFileNode[0];
135: PathHandle outPath = null;
136: File inFile = null;
137:
138: pass = PathUtil.getInputPassthrough(getProject());
139: for (int i = 0; i < pass.length; i++) {
140: if (pass[i].isSelected()) {
141: outPath = getPassthroughPath(pass[i]);
142: inFile = new File(pass[i].getFilePath());
143: try {
144: FileUtil.copy(inFile, outPath.getFile());
145: echo(res.getString("Copying_input_to")
146: + outPath.getPath());
147: } catch (ToolException e) {
148: echo(e);
149: e.printStackTrace();
150: }
151: }
152: }
153: return out;
154: }
155:
156: private File[] makeTemplates() throws ToolException {
157: File[] output = new File[0];
158: ConfigTool tool = new ConfigTool();
159:
160: tool.setOwner(getOwner());
161: tool.setEcho(isEcho());
162: tool.setEchoWriter(getEchoWriter());
163: tool.setTransformPath(true);
164: tool.setTemplates(getTemplates());
165: tool.setOverwrite(getProject().isDeployOverwrite());
166: tool.initReplacements(PathUtil.expandReplacementTable(
167: getProject(), getProject().getReplacementTable()));
168: output = tool.createOutput();
169: return output;
170: }
171:
172: private Template[] getTemplates() {
173: Template[] templates = new Template[0];
174: ArrayList list = new ArrayList();
175: OtterTemplateNode[] templateNodes = new OtterTemplateNode[0];
176:
177: templateNodes = PathUtil.getInputTemplates(getProject());
178: for (int i = 0; i < templateNodes.length; i++) {
179: OtterTemplateNode cursor = null;
180: Template temp = null;
181: File outFile = null;
182: String outPath = new String();
183:
184: cursor = templateNodes[i];
185: if (cursor.isSelected()) {
186: temp = new Template(new File(cursor.getFilePath()),
187: getProject().getDeployInputPath());
188: outPath = cursor.getOutputPath();
189: outFile = new File(outPath);
190: temp.setOutput(outFile);
191: list.add(temp);
192: }
193: if (!isFresh()) {
194: break;
195: }
196: }
197: list.trimToSize();
198: templates = new Template[list.size()];
199: templates = (Template[]) list.toArray(templates);
200: list.clear();
201: return templates;
202: }
203:
204: private PathHandle getPassthroughPath(OtterFileNode node) {
205: PathHandle inputPath = null;
206: PathHandle filePath = null;
207: PathHandle rootPath = null;
208: PathHandle passPath = null;
209: StringBuffer buf = new StringBuffer();
210: String suffix = new String();
211:
212: inputPath = PathHandle.createPathHandle(getProject()
213: .getDeployInputPath());
214: filePath = PathHandle.createPathHandle(node.getFilePath());
215: rootPath = PathHandle.createPathHandle(getProject()
216: .getDeployRootPath());
217: buf.append(rootPath.getPath());
218: if (inputPath.parentOf(filePath)) {
219: suffix = filePath.getPath().substring(
220: inputPath.getPath().length());
221: } else {
222: suffix = filePath.getFile().getName();
223: }
224: if ((suffix.length() == 0) || (suffix.charAt(0) != '/')) {
225: buf.append('/');
226: }
227: buf.append(suffix);
228: passPath = PathHandle.createPathHandle(buf.toString());
229: return passPath;
230: }
231:
232: }
|