KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > kelp > common > deployer > RunBuilder


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  * Paul Mahar
21  *
22  */

23 package org.enhydra.kelp.common.deployer;
24
25 // Toolbox
26
import org.enhydra.tool.ToolBoxInfo;
27 import org.enhydra.tool.common.ToolException;
28 import org.enhydra.tool.common.FileUtil;
29 import org.enhydra.tool.common.PathHandle;
30 import org.enhydra.tool.archive.ArchiveException;
31 import org.enhydra.tool.archive.ArchiveTool;
32 import org.enhydra.tool.archive.JarPlan;
33 import org.enhydra.tool.archive.WarPlan;
34
35 // AddinCore
36
import org.enhydra.kelp.KelpInfo;
37 import org.enhydra.kelp.common.AbstractEchoBuilder;
38 import org.enhydra.kelp.common.Constants;
39 import org.enhydra.kelp.common.PathUtil;
40 import org.enhydra.kelp.common.PropUtil;
41 import org.enhydra.kelp.common.Writer;
42 import org.enhydra.kelp.common.node.OtterProject;
43 import org.enhydra.kelp.common.node.OtterNode;
44 import org.enhydra.kelp.common.node.OtterNodeFactory;
45 import org.enhydra.kelp.common.node.OtterTextFileNode;
46 import org.enhydra.kelp.common.node.OtterFolderNode;
47 import org.enhydra.kelp.common.node.OtterTemplateNode;
48 import org.enhydra.kelp.common.event.WriteListener;
49
50 // JDK
51
import java.awt.Component JavaDoc;
52 import java.io.File JavaDoc;
53 import java.io.FileWriter JavaDoc;
54 import java.io.PrintWriter JavaDoc;
55 import java.util.StringTokenizer JavaDoc;
56 import java.util.ResourceBundle JavaDoc;
57 import java.util.Properties JavaDoc;
58
59 //
60
public class RunBuilder extends AbstractEchoBuilder {
61     static ResourceBundle JavaDoc res =
62         ResourceBundle.getBundle("org.enhydra.kelp.common.Res"); // nores
63

64     //
65
private File JavaDoc startServerDirectory = null;
66
67     /**
68      * Method declaration
69      *
70      *
71      * @param in
72      *
73      * @return
74      */

75     public static File JavaDoc createPolicyFile(String JavaDoc path) {
76
77         // strings not to be resourced
78
final String JavaDoc POLICY_LINE1 = "grant {"; // nores
79
final String JavaDoc POLICY_LINE2 =
80             "permission java.security.AllPermission;"; // nores
81
final String JavaDoc POLICY_LINE3 = "};"; // nores
82
File JavaDoc in = new File JavaDoc(path);
83         File JavaDoc out = null;
84
85         if (in.exists()) {
86             in.delete();
87             in = null;
88         }
89         try {
90             out = new File JavaDoc(path);
91             out.getParentFile().mkdirs();
92             out.createNewFile();
93             PrintWriter JavaDoc writer = new PrintWriter JavaDoc(new FileWriter JavaDoc(out));
94
95             writer.println(POLICY_LINE1);
96             writer.println(POLICY_LINE2);
97             writer.println(POLICY_LINE3);
98             writer.flush();
99             writer.close();
100             out = null;
101             out = new File JavaDoc(path);
102         } catch (java.io.IOException JavaDoc e) {
103             e.printStackTrace();
104         }
105         return out;
106     }
107
108     /**
109      *
110      * @param listeners
111      */

112     public RunBuilder(WriteListener listener) {
113         super(listener);
114     }
115
116     /**
117      * Make content, policy file, process templates, configure project for
118      * running enhydra and create archive.
119      */

120     public void buildImpl() {
121         PathHandle ph = null;
122
123         if (getProject().isDeployRun()) {
124             echo(new String JavaDoc());
125             ph =
126                 PathHandle.createPathHandle(getProject().getDeployBootstrapPath());
127             if (ph.isFile()) {
128
129                 // setup IDE for run
130
if (isFresh()) {
131                     if (!ToolBoxInfo.isEnhydra3()) {
132                         refreshProgress(25,
133                                         res.getString("Deploying_policy_file"));
134                         echo(res.getString("Deploying_policy_file"));
135                         makePolicy();
136                     }
137                 }
138                 if (isFresh()) {
139                     if (getProject().isDeployStartupJava()
140                             || getProject().isDeployStartupRun()) {
141                         refreshProgress(65,
142                                         res.getString("Configuring_project"));
143                         echo(res.getString("Configuring_project"));
144                         makeStartup();
145                     }
146                 }
147             } else if (ph.isDirectory()) {
148
149                 // auto-deploy archive
150
ph =
151                     PathHandle.createPathHandle(getProject().getAutoDeployFilePath());
152                 if (ph.isEmpty()) {
153                     echo("No archive to deploy");
154                 } else if (ph.getFile().isFile()) {
155                     File JavaDoc out = null;
156
157                     out = new File JavaDoc(getProject().getDeployBootstrapPath());
158                     out = new File JavaDoc(out, ph.getFile().getName());
159                     try {
160                         FileUtil.copy(ph.getFile(), out);
161                     } catch (ToolException e) {
162                         echo(e);
163                     }
164                 } else {
165                     echo("Deployment archive not found: " + ph.getPath());
166                 }
167             }
168         }
169         refreshProgress(100, new String JavaDoc());
170     }
171
172     //
173
//
174
//
175

176     /**
177      * Create policy file for security manager
178      */

179     private File JavaDoc createPolicy() {
180         StringBuffer JavaDoc policyPath = new StringBuffer JavaDoc();
181         File JavaDoc policy = null;
182
183         policyPath.append(PathUtil.getDeployConfPath(getProject()));
184         policyPath.append(File.separator);
185         policyPath.append(Constants.FILE_JAVA_POLICY);
186         policy =
187             RunBuilder.createPolicyFile(policyPath.toString().replace('\\',
188                 '/'));
189         return policy;
190     }
191
192     /**
193      * Copy or create policy file as needed.
194      */

195     private void makePolicy() {
196
197         // Find all images in project and copy using
198
// the mapping table.
199
OtterTextFileNode node = getProject().getFirstPolicy();
200
201         if (node == null) {
202             File JavaDoc file = null;
203
204             file = createPolicy();
205             echo(res.getString("Creating_policy_file")
206                  + PathHandle.createPathString(file));
207         } else {
208             File JavaDoc source = null;
209             File JavaDoc dest = null;
210
211             source = new File JavaDoc(node.getFilePath());
212             dest = new File JavaDoc(PathUtil.getDeployConfPath(getProject()),
213                             source.getName());
214             try {
215                 FileUtil.copy(source, dest);
216             } catch (ToolException e) {
217                 echo(e);
218                 e.printStackTrace();
219             }
220         }
221     }
222
223     /**
224      * Configure project for starting server
225      */

226     private void makeStartup() {
227         if (getProject().isDeployStartupJava()) {
228             OtterNodeFactory factory = null;
229             StartServerGenerator start = null;
230             PathHandle startPath = null;
231
232             factory = getProject().getNodeFactory();
233             start = new StartServerGenerator(getProject());
234             try {
235                 start.create();
236                 startPath = PathHandle.createPathHandle(start.getFile());
237                 factory.createJavaFileNode(getProject(), startPath.getPath());
238                 echo(res.getString("Creating_startup") + startPath.getPath());
239             } catch (java.io.IOException JavaDoc e) {
240                 echo(e);
241                 e.printStackTrace();
242             }
243         }
244         if (getProject().isDeployStartupRun()) {
245             getProject().configureRunClass();
246         }
247     }
248
249 }
250
Popular Tags