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
026: import org.enhydra.tool.common.ToolException;
027: import org.enhydra.tool.common.FileUtil;
028: import org.enhydra.tool.common.PathHandle;
029:
030: // AddinCore
031: import org.enhydra.kelp.common.AbstractEchoBuilder;
032: import org.enhydra.kelp.common.Constants;
033: import org.enhydra.kelp.common.PathUtil;
034: import org.enhydra.kelp.common.node.OtterNodeFactory;
035: import org.enhydra.kelp.common.node.OtterTextFileNode;
036: import org.enhydra.kelp.common.event.WriteListener;
037:
038: // JDK
039: import java.io.File;
040: import java.io.FileWriter;
041: import java.io.PrintWriter;
042: import java.util.ResourceBundle;
043:
044: //
045: public class RunBuilder extends AbstractEchoBuilder {
046: static ResourceBundle res = ResourceBundle
047: .getBundle("org.enhydra.kelp.common.Res"); // nores
048:
049: /**
050: * Method declaration
051: *
052: *
053: * @param in
054: *
055: * @return
056: */
057: public static File createPolicyFile(String path) {
058:
059: // strings not to be resourced
060: final String POLICY_LINE1 = "grant {"; // nores
061: final String POLICY_LINE2 = "permission java.security.AllPermission;"; // nores
062: final String POLICY_LINE3 = "};"; // nores
063: File in = new File(path);
064: File out = null;
065:
066: if (in.exists()) {
067: in.delete();
068: in = null;
069: }
070: try {
071: out = new File(path);
072: out.getParentFile().mkdirs();
073: out.createNewFile();
074: PrintWriter writer = new PrintWriter(new FileWriter(out));
075:
076: writer.println(POLICY_LINE1);
077: writer.println(POLICY_LINE2);
078: writer.println(POLICY_LINE3);
079: writer.flush();
080: writer.close();
081: out = null;
082: out = new File(path);
083: } catch (java.io.IOException e) {
084: e.printStackTrace();
085: }
086: return out;
087: }
088:
089: /**
090: *
091: * @param listeners
092: */
093: public RunBuilder(WriteListener listener) {
094: super (listener);
095: }
096:
097: /**
098: * Make content, policy file, process templates, configure project for
099: * running enhydra and create archive.
100: */
101: public void buildImpl() {
102: PathHandle ph = null;
103:
104: if (getProject().isDeployRun()) {
105: echo(new String());
106: ph = PathHandle.createPathHandle(getProject()
107: .getDeployBootstrapPath());
108: if (ph.isFile()) {
109:
110: // setup IDE for run
111: if (isFresh()) {
112: if (getProject().isDeployStartupJava()
113: || getProject().isDeployStartupRun()) {
114: refreshProgress(65, res
115: .getString("Configuring_project"));
116: echo(res.getString("Configuring_project"));
117: makeStartup();
118: }
119: }
120: } else if (ph.isDirectory()) {
121:
122: // auto-deploy archive
123: ph = PathHandle.createPathHandle(getProject()
124: .getAutoDeployFilePath());
125: if (ph.isEmpty()) {
126: echo("No archive to deploy");
127: } else if (ph.getFile().isFile()) {
128: File out = null;
129:
130: out = new File(getProject()
131: .getDeployBootstrapPath());
132: out = new File(out, ph.getFile().getName());
133: try {
134: FileUtil.copy(ph.getFile(), out);
135: } catch (ToolException e) {
136: echo(e);
137: }
138: } else {
139: echo("Deployment archive not found: "
140: + ph.getPath());
141: }
142: }
143: }
144: refreshProgress(100, new String());
145: }
146:
147: //
148: //
149: //
150:
151: /**
152: * Create policy file for security manager
153: */
154: private File createPolicy() {
155: StringBuffer policyPath = new StringBuffer();
156: File policy = null;
157:
158: policyPath.append(PathUtil.getDeployConfPath(getProject()));
159: policyPath.append(File.separator);
160: policyPath.append(Constants.FILE_JAVA_POLICY);
161: policy = RunBuilder.createPolicyFile(policyPath.toString()
162: .replace('\\', '/'));
163: return policy;
164: }
165:
166: /**
167: * Copy or create policy file as needed.
168: */
169: private void makePolicy() {
170:
171: // Find all images in project and copy using
172: // the mapping table.
173: OtterTextFileNode node = getProject().getFirstPolicy();
174:
175: if (node == null) {
176: File file = null;
177:
178: file = createPolicy();
179: echo(res.getString("Creating_policy_file")
180: + PathHandle.createPathString(file));
181: } else {
182: File source = null;
183: File dest = null;
184:
185: source = new File(node.getFilePath());
186: dest = new File(PathUtil.getDeployConfPath(getProject()),
187: source.getName());
188: try {
189: FileUtil.copy(source, dest);
190: } catch (ToolException e) {
191: echo(e);
192: e.printStackTrace();
193: }
194: }
195: }
196:
197: /**
198: * Configure project for starting server
199: */
200: private void makeStartup() {
201: if (getProject().isDeployStartupJava()) {
202: OtterNodeFactory factory = null;
203: StartServerGenerator start = null;
204: PathHandle startPath = null;
205:
206: factory = getProject().getNodeFactory();
207: start = new StartServerGenerator(getProject());
208: try {
209: start.create();
210: startPath = PathHandle
211: .createPathHandle(start.getFile());
212: factory.createJavaFileNode(getProject(), startPath
213: .getPath());
214: echo(res.getString("Creating_startup")
215: + startPath.getPath());
216: } catch (java.io.IOException e) {
217: echo(e);
218: e.printStackTrace();
219: }
220: }
221: if (getProject().isDeployStartupRun()) {
222: getProject().configureRunClass();
223: }
224: }
225:
226: }
|