KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > webadmin > main > DeployBean


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "OpenEJB" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of The OpenEJB Group. For written permission,
18  * please contact dev@openejb.org.
19  *
20  * 4. Products derived from this Software may not be called "OpenEJB"
21  * nor may "OpenEJB" appear in their names without prior written
22  * permission of The OpenEJB Group. OpenEJB is a registered
23  * trademark of The OpenEJB Group.
24  *
25  * 5. Due credit should be given to the OpenEJB Project
26  * (http://www.openejb.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE OPENEJB GROUP AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * THE OPENEJB GROUP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 2001 (C) The OpenEJB Group. All Rights Reserved.
42  *
43  * $Id: DeployBean.java 1912 2005-06-16 22:29:56Z jlaskowski $
44  */

45 package org.openejb.webadmin.main;
46
47 import java.io.File JavaDoc;
48 import java.io.FileInputStream JavaDoc;
49 import java.io.FileOutputStream JavaDoc;
50 import java.io.IOException JavaDoc;
51 import java.io.ObjectInputStream JavaDoc;
52 import java.io.ObjectOutputStream JavaDoc;
53 import java.io.PrintWriter JavaDoc;
54 import java.lang.reflect.UndeclaredThrowableException JavaDoc;
55 import java.util.Properties JavaDoc;
56 import java.util.StringTokenizer JavaDoc;
57
58 import javax.ejb.Handle JavaDoc;
59 import javax.naming.Context JavaDoc;
60 import javax.naming.InitialContext JavaDoc;
61 import javax.rmi.PortableRemoteObject JavaDoc;
62
63 import org.openejb.webadmin.HttpRequest;
64 import org.openejb.webadmin.HttpResponse;
65 import org.openejb.webadmin.WebAdminBean;
66 import org.openejb.util.FileUtils;
67 import org.openejb.util.HtmlUtilities;
68 import org.openejb.util.Logger;
69
70 /**
71  * This class takes care of deploying a bean in the web administration.
72  *
73  * timu:
74  * 1. Add better error handling
75  * 2. Finish implementing the writeForm function
76  * 3. Add documentation
77  * 4. Fix force overwrite error
78  * 5. Internationalize all text
79  *
80  * @author <a HREF="mailto:tim_urberg@yahoo.com">Tim Urberg</a>
81  */

82 public class DeployBean extends WebAdminBean {
83     private static final String JavaDoc HANDLE_FILE = System.getProperty("file.separator") + "deployerHandle.obj";
84     private DeployerObject deployer = null;
85     private Logger logger = Logger.getInstance("OpenEJB", "org.openejb.util.resources");
86
87     /* key for boolean values:
88      * AUTO_ASSIGN 0
89      * MOVE_JAR 1
90      * FORCE_OVERWRITE_JAR 2
91      * COPY_JAR 3
92      * AUTO_CONFIG 4
93      * GENERATE_DEPLOYMENT_ID 5
94      * GENERATE_STUBS 6
95      */

96     private boolean[] options = new boolean[7];
97
98     /** Creates a new instance of DeployBean */
99     public void ejbCreate() {
100         this.section = "Deployment";
101     }
102
103     /** called after all content is written to the browser
104      * @param request the http request
105      * @param response the http response
106      * @throws IOException if an exception is thrown
107      *
108      */

109     public void postProcess(HttpRequest request, HttpResponse response) throws IOException JavaDoc {}
110
111     /** called before any content is written to the browser
112      * @param request the http request
113      * @param response the http response
114      * @throws IOException if an exception is thrown
115      *
116      */

117     public void preProcess(HttpRequest request, HttpResponse response) throws IOException JavaDoc {}
118
119     /** writes the main body content to the broswer. This content is inside a <code>&lt;p&gt;</code> block
120      *
121      * @param body the output to write to
122      * @exception IOException if an exception is thrown
123      *
124      */

125     public void writeBody(PrintWriter JavaDoc body) throws IOException JavaDoc {
126         String JavaDoc deploy = request.getFormParameter("deploy");
127         String JavaDoc submitDeployment = request.getFormParameter("submitDeploymentAndContainerIds");
128
129         try {
130             //the user has hit the deploy button
131
if (deploy != null) {
132                 String JavaDoc deployerHandleString = createDeployerHandle();
133                 setOptions();
134                 deployer.startDeployment();
135                 body.println(
136                     "Below is a list of beans in the jar which you have chosen to deploy. Some of the methods in your beans may require "
137                         + "OQL statements. If so, form fields will be displayed for the methods which require these statements. "
138                         + "In this case <b>OQL statements are required.</b> "
139                         + "Please enter the information requested in the form fields and click \"Continue &gt;&gt;\" "
140                         + "to continue.<br>");
141                 body.println("<form action=\"Deployment\" method=\"post\" onSubmit=\"return checkDeployValues(this)\">");
142                 body.print(deployer.createIdTable());
143                 body.println(HtmlUtilities.createHiddenFormField("deployerHandle", deployerHandleString));
144                 body.println("</form>");
145             } else if (submitDeployment != null) {
146                 deployPartTwo(body);
147             } else {
148                 writeForm(body);
149             }
150         } catch (Exception JavaDoc e) {
151             //timu - Create a generic error screen
152
handleException(e, body);
153         }
154     }
155
156     /** handles exceptions for the page */
157     private void handleException(Exception JavaDoc e, PrintWriter JavaDoc body) {
158         if (e instanceof UndeclaredThrowableException JavaDoc) {
159             UndeclaredThrowableException JavaDoc ue = (UndeclaredThrowableException JavaDoc) e;
160             Throwable JavaDoc t = ue.getUndeclaredThrowable();
161             if (t != null) {
162                 body.println(t.getMessage());
163                 logger.error("Error on web deployment", t);
164             } else {
165                 body.println("An unknown system error occured.");
166             }
167         } else {
168             if (e != null) {
169                 body.println(e.getMessage());
170                 logger.error("Error on web deployment", e);
171             } else {
172                 body.println("An unknown system error occured.");
173             }
174         }
175     }
176
177     /** the second part of the deployment which loops through the
178      * array of deployments and sets the values
179      */

180     private void deployPartTwo(PrintWriter JavaDoc body) throws Exception JavaDoc {
181         ReferenceData[] referenceDataArray;
182         OQLData[] oqlDataArray;
183         StringTokenizer JavaDoc oqlParameterToken;
184
185         String JavaDoc deployerHandleString = request.getFormParameter("deployerHandle");
186         getDeployerHandle(deployerHandleString); //gets the deployment handle
187
DeployData[] deployDataArray = deployer.getDeployDataArray();
188
189         //loop through all the beans and set the ids
190
for (int i = 0; i < deployDataArray.length; i++) {
191             //set the containerId value and the deployment id value
192
deployDataArray[i].setContainerIdValue(request.getFormParameter(deployDataArray[i].getContainerIdName()));
193             deployDataArray[i].setDeploymentIdValue(request.getFormParameter(deployDataArray[i].getDeploymentIdName()));
194
195             //get the ejb and resource references and loop through them
196
referenceDataArray = deployDataArray[i].getReferenceDataArray();
197             for (int j = 0; j < referenceDataArray.length; j++) {
198                 referenceDataArray[j].setReferenceIdValue(
199                     request.getFormParameter(referenceDataArray[j].getReferenceIdName()));
200                 referenceDataArray[j].setReferenceValue(request.getFormParameter(referenceDataArray[j].getReferenceName()));
201             }
202
203             //get the oql methods and set them
204
oqlDataArray = deployDataArray[i].getOqlDataArray();
205             for (int j = 0; j < oqlDataArray.length; j++) {
206                 oqlDataArray[j].setOqlStatementValue(request.getFormParameter(oqlDataArray[j].getOqlStatementName()));
207
208                 //next set up the tokens for the OQL parameters
209
if (request.getFormParameter(oqlDataArray[j].getOqlParameterName()) != null) {
210                     oqlParameterToken =
211                         new StringTokenizer JavaDoc(request.getFormParameter(oqlDataArray[j].getOqlParameterName()), ",");
212                     //loop through the tokens
213
while (oqlParameterToken.hasMoreTokens()) {
214                         oqlDataArray[j].getOqlParameterValueList().add(oqlParameterToken.nextToken());
215                     }
216
217                 }
218             }
219         }
220
221         deployer.setDeployAndContainerIds(deployDataArray);
222
223         //print out a message to the user to let them know thier bean was deployed
224
body.println(
225             "You jar is now deployed. If you chose to move or copy your jar"
226                 + "from it's original location, you will now find it in: "
227                 + System.getProperty("openejb.home")
228                 + System.getProperty("file.separator")
229                 + "beans. You will need to restart OpenEJB for this "
230                 + "deployment to take affect. Once you restart, you should see your bean(s) in the "
231                 + HtmlUtilities.createAnchor("DeploymentList", "list of beans", HtmlUtilities.ANCHOR_HREF_TYPE)
232                 + "on this console. Below is a table of "
233                 + "the bean(s) you deployed.<br><br>");
234
235         printDeploymentHtml(body);
236         deployer.remove();
237     }
238
239     /** prints a table of deployment HTML */
240     private void printDeploymentHtml(PrintWriter JavaDoc body) throws Exception JavaDoc {
241         deployer.finishDeployment();
242         body.println("<table cellspacing=\"1\" cellpadding=\"2\" border=\"1\">\n");
243         body.println("<tr align=\"left\">\n");
244         body.println("<th>Bean Name</th>\n");
245         body.println("<th>Deployment Id</th>\n");
246         body.println("<th>Container Id</th>\n");
247         body.println("<th>Resource References</th>\n");
248         body.println("</tr>\n");
249         body.println(deployer.getDeploymentHTML());
250         body.println("</table>");
251     }
252
253     /** gets an object reference and handle */
254     private String JavaDoc createDeployerHandle() throws Exception JavaDoc {
255         Properties JavaDoc p = new Properties JavaDoc();
256         p.put(Context.INITIAL_CONTEXT_FACTORY, "org.openejb.core.ivm.naming.InitContextFactory");
257
258         //lookup the bean
259
InitialContext JavaDoc ctx = new InitialContext JavaDoc(p);
260         Object JavaDoc obj = ctx.lookup("deploy/webadmin/Deployer");
261         //create a new instance
262
DeployerHome home = (DeployerHome) PortableRemoteObject.narrow(obj, DeployerHome.class);
263         deployer = home.create();
264
265         //get the handle for that instance
266
Handle JavaDoc deployerHandle = deployer.getHandle();
267
268         //write the handle out to a file
269
File JavaDoc myHandleFile = new File JavaDoc(FileUtils.createTempDirectory().getAbsolutePath() + HANDLE_FILE);
270         if (!myHandleFile.exists()) {
271             myHandleFile.createNewFile();
272         }
273
274         ObjectOutputStream JavaDoc objectOut = new ObjectOutputStream JavaDoc(new FileOutputStream JavaDoc(myHandleFile));
275         objectOut.writeObject(deployerHandle); //writes the handle to the file
276
objectOut.flush();
277         objectOut.close();
278
279         return myHandleFile.getAbsolutePath();
280     }
281
282     /** this function gets the deployer handle */
283     private void getDeployerHandle(String JavaDoc handleFile) throws Exception JavaDoc {
284         File JavaDoc myHandleFile = new File JavaDoc(handleFile);
285
286         //get the object
287
ObjectInputStream JavaDoc objectIn = new ObjectInputStream JavaDoc(new FileInputStream JavaDoc(myHandleFile));
288         //get the handle
289
Handle JavaDoc deployerHandle = (Handle JavaDoc) objectIn.readObject();
290         this.deployer = (DeployerObject) deployerHandle.getEJBObject();
291     }
292
293     /** starts the deployment process */
294     private void setOptions() throws Exception JavaDoc {
295         //the the form values
296
String JavaDoc jarFile = request.getFormParameter("jarFile");
297         String JavaDoc force = request.getFormParameter("force");
298         File JavaDoc testForValidFile = null;
299
300         if (jarFile == null) {
301             //do this for now, needs better exception handling
302
throw new IOException JavaDoc("No jar file was provided, please try again.");
303         }
304         //set the jar file
305
this.deployer.setJarFile(jarFile);
306
307         //force overwrite
308
if (force != null) {
309             options[2] = true;
310         }
311
312         testForValidFile = null;
313         this.deployer.setBooleanValues(options);
314     }
315
316     /** writes the form for this page
317      *
318      * TODO - finish the sections that are not implemented
319      */

320     private void writeForm(PrintWriter JavaDoc body) throws IOException JavaDoc {
321         //the form decleration
322
body.println(
323             "<form action=\"Deployment\" method=\"post\" enctype=\"multipart/form-data\" onsubmit=\"return checkDeploy(this)\">");
324         //the start table
325
body.println("<table border=\"0\" cellspacing=\"0\" cellpadding=\"2\">");
326
327         //info about CMP mapping - not yet implemented
328
/*body.println("<tr>");
329         body.println("<td colspan=\"2\">");
330         body.println("<strong>Important Note:</strong> If you are deploying a Container Managed Persistance");
331         body.println("bean, you must first <a HREF=\"\">map the fields</a> and then deploy. Once that step is completed");
332         body.println("you will be sent to this page and your configuration files will be set up for you.");
333         body.println("(see the help section for more information).");
334         body.println("</td>");
335         body.println("</tr>");
336         body.println("<tr>");
337         body.println("<td colspan=\"2\">&nbsp;</td>");
338         body.println("</tr>"); */

339
340         //info about step 1
341
body.println("<tr>\n<td colspan=\"2\">");
342         body.println("<strong>Step 1:</strong> Browse your file system and select your bean to deploy.");
343         body.println("</td>\n</tr>\n<tr>\n<td colspan=\"2\">&nbsp;</td>\n</tr>");
344
345         //the file upload for the jar file (this may need to be changed)
346
body.println("<tr>");
347         body.println("<td><nobr>Jar File</nobr></td>\n<td>");
348         body.println(HtmlUtilities.createFileFormField("jarFile", "", 35));
349         body.println("</td>\n</tr>\n<tr>\n<td colspan=\"2\">&nbsp;</td>\n</tr>");
350
351         /***************************
352          * Deployment options
353          ***************************/

354 // body.println("<tr>\n<td colspan=\"2\"><strong>Step 2:</strong> Choose options for deployment.");
355
// body.println("</td>\n</tr>\n<tr>\n<td colspan=\"2\">&nbsp;</td>\n</tr>");
356

357         /* force over write of the bean - this will have to wait for now until the bug gets fixed
358         body.println("<tr>");
359         body.println("<td colspan=\"2\">");
360         body.println("<input type=\"checkbox\" name=\"force\" value=\"-f\">");
361         body.println(
362             "Forces a move or a copy, overwriting any previously existing jar with the same name.");
363         body.println("</td>");
364         body.println("</tr>");
365         body.println("<tr>");
366         body.println("<td colspan=\"2\">&nbsp;</td>");
367         body.println("</tr>");
368         */

369
370         // sets the OpenEJB configuration file
371
// body.println("<tr>");
372
// body.println(
373
// "<td colspan=\"2\">Sets the OpenEJB configuration to the specified file. (leave blank for non-use) "
374
// + "Note: you will need to make sure the configuration files are in this location on the server.</td>");
375
// body.println("</tr>\n<tr>\n<td><nobr>Config File</nobr></td>\n<td>");
376
// body.println(HtmlUtilities.createTextFormField("configFile", "", 35, 0));
377
// body.println("</td>\n</tr>\n<tr>\n<td colspan=\"2\">&nbsp;</td>\n</tr>");
378
//
379
//deploy the bean
380
body.println("<tr>\n<td colspan=\"2\">&nbsp;</td>\n</tr>");
381         body.println("<tr><td colspan=\"2\">");
382         body.println(HtmlUtilities.createSubmitFormButton("deploy", "Deploy"));
383         body.println("</td>\n</tr>");
384
385         //the end...
386
/* we don't have help yet
387         body.println("<tr>");
388         body.println("<td colspan=\"2\">Note: see the help section for examples on how to deploy beans.</td>");
389         body.println("</tr>"); */

390         body.println("</table>");
391         //the handle file name
392
body.println(HtmlUtilities.createHiddenFormField("handleFile", ""));
393         body.println("</form>");
394     }
395
396     /** Write the TITLE of the HTML document. This is the part
397      * that goes into the <code>&lt;head&gt;&lt;title&gt;
398      * &lt;/title&gt;&lt;/head&gt;</code> tags
399      *
400      * @param body the output to write to
401      * @exception IOException of an exception is thrown
402      *
403      */

404     public void writeHtmlTitle(PrintWriter JavaDoc body) throws IOException JavaDoc {
405         body.println(HTML_TITLE);
406     }
407
408     /** Write the title of the page. This is displayed right
409      * above the main block of content.
410      *
411      * @param body the output to write to
412      * @exception IOException if an exception is thrown
413      *
414      */

415     public void writePageTitle(PrintWriter JavaDoc body) throws IOException JavaDoc {
416         body.println("EJB Deployment");
417     }
418
419     /** Write the sub items for this bean in the left navigation bar of
420     * the page. This should look somthing like the one below:
421     *
422     * <code>
423     * &lt;tr&gt;
424     * &lt;td valign="top" align="left"&gt;
425     * &lt;a HREF="system?show=deployments"&gt;&lt;span class="subMenuOff"&gt;
426     * &nbsp;&nbsp;&nbsp;Deployments
427     * &lt;/span&gt;
428     * &lt;/a&gt;&lt;/td&gt;
429     * &lt;/tr&gt;
430     * </code>
431     *
432     * Alternately, the bean can use the method formatSubMenuItem(..) which
433     * will create HTML like the one above
434     *
435     * @param body the output to write to
436     * @exception IOException if an exception is thrown
437     *
438     */

439     public void writeSubMenuItems(PrintWriter JavaDoc body) throws IOException JavaDoc {}
440 }
Popular Tags