KickJava   Java API By Example, From Geeks To Geeks.

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


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: ConfigBean.java 2268 2005-12-02 00:09:35Z jcscoobyrs $
44  */

45 package org.openejb.webadmin.main;
46
47 import java.io.ByteArrayInputStream JavaDoc;
48 import java.io.File JavaDoc;
49 import java.io.FileInputStream JavaDoc;
50 import java.io.FileOutputStream JavaDoc;
51 import java.io.FileWriter JavaDoc;
52 import java.io.IOException JavaDoc;
53 import java.io.ObjectInputStream JavaDoc;
54 import java.io.ObjectOutputStream JavaDoc;
55 import java.io.PrintWriter JavaDoc;
56 import java.util.Properties 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.exolab.castor.xml.ValidationException;
64 import org.openejb.OpenEJBException;
65 import org.openejb.webadmin.HttpRequest;
66 import org.openejb.webadmin.HttpResponse;
67 import org.openejb.webadmin.WebAdminBean;
68 import org.openejb.alt.config.Bean;
69 import org.openejb.alt.config.ConfigUtils;
70 import org.openejb.alt.config.Service;
71 import org.openejb.alt.config.sys.ConnectionManager;
72 import org.openejb.alt.config.sys.Connector;
73 import org.openejb.alt.config.sys.Container;
74 import org.openejb.alt.config.sys.Deployments;
75 import org.openejb.alt.config.sys.JndiProvider;
76 import org.openejb.alt.config.sys.Openejb;
77 import org.openejb.alt.config.sys.ProxyFactory;
78 import org.openejb.alt.config.sys.Resource;
79 import org.openejb.alt.config.sys.SecurityService;
80 import org.openejb.alt.config.sys.TransactionService;
81 import org.openejb.core.EnvProps;
82 import org.openejb.util.FileUtils;
83 import org.openejb.util.StringUtilities;
84
85 /** This bean allows the user to graphicly edit the OpenEJB configuration file usually located at
86  * $OPENEJB_HOME/config/openejb.conf.
87  *
88  * @see org.openejb.alt.config.sys.ConnectionManager
89  * @see org.openejb.alt.config.sys.Connector
90  * @see org.openejb.alt.config.sys.Container
91  * @see org.openejb.alt.config.sys.Deployments
92  * @see org.openejb.alt.config.sys.JndiProvider
93  * @see org.openejb.alt.config.sys.Openejb
94  * @see org.openejb.alt.config.sys.ProxyFactory
95  * @see org.openejb.alt.config.sys.Resource
96  * @see org.openejb.alt.config.sys.SecurityService
97  * @see org.openejb.alt.config.sys.TransactionService
98  * @author <a HREF="mailto:david.blevins@visi.com">David Blevins</a>
99  * @author <a HREF="mailto:tim_urberg@yahoo.com">Tim Urberg</a>
100  */

101 public class ConfigBean extends WebAdminBean {
102     /** the handle file name */
103     private static final String JavaDoc HANDLE_FILE = System.getProperty("file.separator") + "configurationHandle.obj";
104
105     /** Called when the container creates a new instance of this bean */
106     public void ejbCreate() {
107         section = "Configuration";
108     }
109
110     /** Called before content is written to the browser
111      * @param request the HTTP request object
112      * @param response the HTTP response object
113      * @throws IOException if an exception is thrown
114      */

115     public void preProcess(HttpRequest request, HttpResponse response) throws IOException JavaDoc {}
116
117     /** called after content is written to the browser
118      * @param request the HTTP request object
119      * @param response the HTTP response object
120      * @throws IOException if an exception is thrown
121      */

122     public void postProcess(HttpRequest request, HttpResponse response) throws IOException JavaDoc {}
123
124     /** Write the TITLE of the HTML document. This is the part
125      * that goes into the <HEAD><TITLE></TITLE></HEAD> tags
126      * @param body the body to write the title to
127      * @exception IOException if an exception is thrown
128      */

129     public void writeHtmlTitle(PrintWriter JavaDoc body) throws IOException JavaDoc {
130         body.print(HTML_TITLE);
131     }
132
133     /** Write the title of the page. This is displayed right
134      * above the main block of content.
135      * @param body the body to write the page title to
136      * @exception IOException if an exception is thrown
137      */

138     public void writePageTitle(PrintWriter JavaDoc body) throws IOException JavaDoc {
139         body.print("Edit your OpenEJB Configuration");
140     }
141
142     /** Writes sub menu items for this menu item
143      * @param body the output to write to
144      * @throws IOException if an exception is thrown
145      */

146     public void writeSubMenuItems(PrintWriter JavaDoc body) throws IOException JavaDoc {}
147
148     /** Write the main content to the browser
149      * @param body the output to write the content to
150      * @exception IOException if an exception is thrown
151      */

152     public void writeBody(PrintWriter JavaDoc body) throws IOException JavaDoc {
153         Openejb openejb;
154         ConfigurationDataObject configurationData;
155         String JavaDoc configLocation = System.getProperty("openejb.configuration");
156
157         //get all the form parameters
158
String JavaDoc type = request.getQueryParameter(ConfigHTMLWriter.QUERY_PARAMETER_TYPE);
159         String JavaDoc method = request.getQueryParameter(ConfigHTMLWriter.QUERY_PARAMETER_METHOD);
160         String JavaDoc handleFile = request.getFormParameter(ConfigHTMLWriter.FORM_FIELD_HANDLE_FILE);
161         String JavaDoc submitOpenejb = request.getFormParameter(ConfigHTMLWriter.FORM_FIELD_SUBMIT_OPENEJB);
162         String JavaDoc submitService = request.getFormParameter(ConfigHTMLWriter.FORM_FIELD_SUBMIT_SERVICE);
163         String JavaDoc containerType = request.getFormParameter(ConfigHTMLWriter.FORM_FIELD_CONTAINER_TYPE);
164
165         //get the main openejb configuration from ConfigurationData object
166
//also create a handle file to get the configuration next time
167
if (handleFile == null) {
168             configurationData = getConfigurationObject();
169             handleFile = createHandle(configurationData);
170             try {
171                 configLocation = ConfigUtils.searchForConfiguration();
172                 
173                 openejb = ConfigUtils.readConfig(configLocation);
174             } catch (OpenEJBException e) {
175                 throw new IOException JavaDoc(e.getMessage());
176             }
177         } else {
178             configurationData = getHandle(handleFile);
179             openejb = configurationData.getOpenejb();
180         }
181
182         //check for the action we're going to take, these actions are
183
//grouped by "submits" and by "begins"
184
if (submitOpenejb != null) {
185             body.println(submitOpenejb(configLocation, openejb));
186             return;
187         } else if (ConfigHTMLWriter.FORM_VALUE_SUBMIT_CONNECTOR.equals(submitService)) {
188             submitConnector(body, openejb, handleFile, configLocation);
189         } else if (containerType != null) {
190             submitContainer(body, openejb, handleFile, configLocation);
191         } else if (ConfigHTMLWriter.FORM_VALUE_SUBMIT_DEPLOYMENTS.equals(submitService)) {
192             submitDeployments(body, openejb, handleFile, configLocation);
193         } else if (submitService != null) {
194             submitService(body, openejb, handleFile, configLocation, submitService);
195         } else if (ConfigHTMLWriter.TYPE_CONNECTOR.equals(type)) {
196             beginConnector(method, handleFile, body, openejb, configLocation);
197         } else if (ConfigHTMLWriter.TYPE_CONTAINER.equals(type)) {
198             beginContainer(method, handleFile, body, openejb, configLocation);
199         } else if (ConfigHTMLWriter.TYPE_DEPLOYMENTS.equals(type)) {
200             beginDeployments(method, handleFile, body, openejb, configLocation);
201         } else if (type != null) {
202             beginService(method, handleFile, body, openejb, configLocation, type);
203         } else {
204             ConfigHTMLWriter.writeOpenejb(body, openejb, handleFile, configLocation);
205         }
206
207         //set the object onto the stateful bean
208
configurationData.setOpenejb(openejb);
209     }
210
211     /**
212      * Finds the connector from the array based on the id from the form and then
213      * calls a method to create, edit or delete it
214      *
215      * @param method create, edit or delete for the action of this connector
216      * @param handleFile the handle for the ConfigurationDataBean
217      * @param body the PrintWriter to the browser
218      * @param openejb the main configuration object
219      * @param configLocation the location of the configuration object
220      * @throws IOException when an invalid method is passed in
221      */

222     private void beginConnector(String JavaDoc method, String JavaDoc handleFile, PrintWriter JavaDoc body, Openejb openejb, String JavaDoc configLocation)
223         throws IOException JavaDoc {
224         String JavaDoc connectorId = request.getFormParameter(ConfigHTMLWriter.TYPE_CONNECTOR);
225         Connector[] connectors = openejb.getConnector();
226         int connectorIndex = -1;
227
228         //make sure the connectorId is not null and then loop to find a match in the array
229
connectorId = (connectorId == null) ? "" : connectorId;
230         for (int i = 0; i < connectors.length; i++) {
231             if (connectorId.equals(connectors[i].getId())) {
232                 connectorIndex = i;
233                 break;
234             }
235         }
236
237         //check for which method we're performing create, edit or delete
238
if (ConfigHTMLWriter.CREATE.equals(method)) {
239             ConfigHTMLWriter.writeConnector(body, null, handleFile, -1);
240         } else if (ConfigHTMLWriter.EDIT.equals(method)) {
241             ConfigHTMLWriter.writeConnector(
242                 body,
243                 (connectorIndex == -1) ? null : connectors[connectorIndex],
244                 handleFile,
245                 connectorIndex);
246         } else if (ConfigHTMLWriter.DELETE.equals(method)) {
247             if (connectorIndex > -1)
248                 openejb.removeConnector(connectorIndex);
249             ConfigHTMLWriter.writeOpenejb(body, openejb, handleFile, configLocation);
250         } else {
251             throw new IOException JavaDoc("Invalid method");
252         }
253     }
254
255     /**
256      * Finds the container from the array based on the id from the form and then
257      * calls a method to create, edit or delete it. In addition, it uses a ContainerData
258      * object to store the information for the container.
259      *
260      * @param method create, edit or delete for the action of this container
261      * @param handleFile the handle for the ConfigurationDataBean
262      * @param body the PrintWriter to the browser
263      * @param openejb the main configuration object
264      * @param configLocation the location of the configuration object
265      * @see org.openejb.webadmin.main.ContainerData
266      * @throws IOException when an invalid method is passed in
267      */

268     private void beginContainer(String JavaDoc method, String JavaDoc handleFile, PrintWriter JavaDoc body, Openejb openejb, String JavaDoc configLocation)
269         throws IOException JavaDoc {
270         String JavaDoc containerId = request.getFormParameter(ConfigHTMLWriter.TYPE_CONTAINER);
271         Container[] containers = openejb.getContainer();
272         int containerIndex = -1;
273         Properties JavaDoc properties = new Properties JavaDoc();
274
275         //make sure the containerId is not null then loop through the array
276
//to find the matching container
277
containerId = (containerId == null) ? "" : containerId;
278         for (int i = 0; i < containers.length; i++) {
279             if (containerId.equals(containers[i].getId())) {
280                 containerIndex = i;
281                 break;
282             }
283         }
284
285         //based on the method, we want to create a container, edit the existing one
286
//or delete the existing one
287
if (ConfigHTMLWriter.CREATE.equals(method)) {
288             ConfigHTMLWriter.writeContainer(body, new ContainerData(), handleFile);
289         } else if (ConfigHTMLWriter.EDIT.equals(method)) {
290             //create a new container data object and set the contents
291
ContainerData data = new ContainerData();
292             if (containerIndex > -1) {
293                 data.setId(StringUtilities.nullToBlankString(containers[containerIndex].getId()));
294                 data.setJar(StringUtilities.nullToBlankString(containers[containerIndex].getJar()));
295                 data.setProvider(StringUtilities.nullToBlankString(containers[containerIndex].getProvider()));
296                 data.setContainerType(StringUtilities.nullToBlankString(containers[containerIndex].getCtype()));
297                 ByteArrayInputStream JavaDoc in =
298                     new ByteArrayInputStream JavaDoc(
299                         StringUtilities.nullToBlankString(containers[containerIndex].getContent()).getBytes());
300                 properties.load(in);
301
302                 data.setBulkPassivate(properties.getProperty(EnvProps.IM_PASSIVATE_SIZE, ""));
303                 data.setGlobalTxDatabase(properties.getProperty(EnvProps.GLOBAL_TX_DATABASE, ""));
304                 data.setIndex(containerIndex);
305                 data.setLocalTxDatabase(properties.getProperty(EnvProps.LOCAL_TX_DATABASE, ""));
306                 data.setPassivator(properties.getProperty(EnvProps.IM_PASSIVATOR, ""));
307                 data.setPoolSize(properties.getProperty(EnvProps.IM_POOL_SIZE, ""));
308                 data.setStrictPooling(properties.getProperty(EnvProps.IM_STRICT_POOLING, "true"));
309                 data.setTimeOut(properties.getProperty(EnvProps.IM_TIME_OUT, ""));
310                 data.setEdit(true);
311             }
312
313             ConfigHTMLWriter.writeContainer(body, data, handleFile);
314         } else if (ConfigHTMLWriter.DELETE.equals(method)) {
315             if (containerIndex > -1)
316                 openejb.removeContainer(containerIndex);
317             ConfigHTMLWriter.writeOpenejb(body, openejb, handleFile, configLocation);
318         } else {
319             throw new IOException JavaDoc("Invalid method");
320         }
321     }
322
323     /**
324      * Finds the current deployment from the array based on the jar or the directory
325      * from the form and then calls a method to create, edit or delete it.
326      *
327      * @param method create, edit or delete for the action of this deployment
328      * @param handleFile the handle for the ConfigurationDataBean
329      * @param body the PrintWriter to the browser
330      * @param openejb the main configuration object
331      * @param configLocation the location of the configuration object
332      * @throws IOException when an invalid method is passed in
333      */

334     private void beginDeployments(
335         String JavaDoc method,
336         String JavaDoc handleFile,
337         PrintWriter JavaDoc body,
338         Openejb openejb,
339         String JavaDoc configLocation)
340         throws IOException JavaDoc {
341         //get the id for the current deployment, if there is one
342
String JavaDoc deploymentId = request.getFormParameter(ConfigHTMLWriter.TYPE_DEPLOYMENTS);
343         Deployments[] deployments = openejb.getDeployments();
344         int deploymentIndex = -1;
345
346         //loop through the deployment list and grab the jar or directory
347
deploymentId = StringUtilities.nullToBlankString(deploymentId);
348         for (int i = 0; i < deployments.length; i++) {
349             if (deploymentId.equals(deployments[i].getDir()) || deploymentId.equals(deployments[i].getJar())) {
350                 deploymentIndex = i;
351                 break;
352             }
353         }
354
355         //check the method and proceed with a create, edit or delete
356
if (ConfigHTMLWriter.CREATE.equals(method)) {
357             ConfigHTMLWriter.writeDeployments(body, null, handleFile, -1);
358         } else if (ConfigHTMLWriter.EDIT.equals(method)) {
359             ConfigHTMLWriter.writeDeployments(body, deployments[deploymentIndex], handleFile, deploymentIndex);
360         } else if (ConfigHTMLWriter.DELETE.equals(method)) {
361             if (deploymentIndex > -1)
362                 openejb.removeDeployments(deploymentIndex);
363             ConfigHTMLWriter.writeOpenejb(body, openejb, handleFile, configLocation);
364         } else {
365             throw new IOException JavaDoc("Invalid method");
366         }
367     }
368
369     /**
370      * This is a generic begin method for services where all we care about is
371      * id, jar, provider and content. It currently handles any services that
372      * don't have much documentation. In future implementations this method will
373      * be refactored since all services should have a specalized, specific UI
374      *
375      * @param method create, edit or delete for the action of this deployment
376      * @param handleFile the handle for the ConfigurationDataBean
377      * @param body the PrintWriter to the browser
378      * @param openejb the main configuration object
379      * @param configLocation the location of the configuration object
380      * @param type the type of service being passed in (see the "type" variables
381      * in ConfigHTMLWriter)
382      * @see org.openejb.webadmin.main.ConfigHTMLWriter
383      * @throws IOException - when an invalid method is passed in
384      */

385     private void beginService(
386         String JavaDoc method,
387         String JavaDoc handleFile,
388         PrintWriter JavaDoc body,
389         Openejb openejb,
390         String JavaDoc configLocation,
391         String JavaDoc type)
392         throws IOException JavaDoc {
393         //get the current id
394
String JavaDoc serviceId = StringUtilities.nullToBlankString(request.getFormParameter(type));
395         Service service = null;
396         Service[] services = new Service[0];
397         String JavaDoc submit = "";
398
399         /*
400          * TODO: Seperate out the simple services (with just id, jar, provider and content)
401          * into multiple specific services that are specific for the content of each type
402          */

403
404         //instantiate the service type based on the type passed in
405
//also set the value of the submit button
406
if (ConfigHTMLWriter.TYPE_CONNECTION_MANAGER.equals(type)) {
407             service = openejb.getConnectionManager();
408             submit = ConfigHTMLWriter.FORM_VALUE_SUBMIT_CONNECTION_MANAGER;
409         } else if (ConfigHTMLWriter.TYPE_JNDI_PROVIDER.equals(type)) {
410             services = openejb.getJndiProvider();
411             submit = ConfigHTMLWriter.FORM_VALUE_SUBMIT_JNDI_PROVIDER;
412         } else if (ConfigHTMLWriter.TYPE_PROXY_FACTORY.equals(type)) {
413             service = openejb.getProxyFactory();
414             submit = ConfigHTMLWriter.FORM_VALUE_SUBMIT_PROXY_FACTORY;
415         } else if (ConfigHTMLWriter.TYPE_SECURITY_SERVICE.equals(type)) {
416             service = openejb.getSecurityService();
417             submit = ConfigHTMLWriter.FORM_VALUE_SUBMIT_SECURITY_SERVICE;
418         } else if (ConfigHTMLWriter.TYPE_TRANSACTION_SERVICE.equals(type)) {
419             service = openejb.getTransactionService();
420             submit = ConfigHTMLWriter.FORM_VALUE_SUBMIT_TRANSACTION_SERVICE;
421         } else if (ConfigHTMLWriter.TYPE_RESOURCE.equals(type)) {
422             services = openejb.getResource();
423             submit = ConfigHTMLWriter.FORM_VALUE_SUBMIT_RESOURCE;
424         }
425
426         int serviceIndex = -1;
427
428         //if there is an array of services, loop through them
429
for (int i = 0; i < services.length; i++) {
430             if (serviceId.equals(services[i].getId())) {
431                 serviceIndex = i;
432                 service = services[i];
433                 break;
434             }
435         }
436
437         //next check to see if we're doing a create, edit or delete
438
if (ConfigHTMLWriter.CREATE.equals(method)) {
439             ConfigHTMLWriter.writeService(body, null, handleFile, submit, -1);
440         } else if (ConfigHTMLWriter.EDIT.equals(method)) {
441             ConfigHTMLWriter.writeService(body, service, handleFile, submit, serviceIndex);
442         } else if (ConfigHTMLWriter.DELETE.equals(method)) {
443             //here we need to check the type again to remove the proper
444
//service
445
if (ConfigHTMLWriter.TYPE_CONNECTION_MANAGER.equals(type)) {
446                 openejb.setConnectionManager(null);
447             } else if (ConfigHTMLWriter.TYPE_JNDI_PROVIDER.equals(type) && serviceIndex > -1) {
448                 openejb.removeJndiProvider(serviceIndex);
449             } else if (ConfigHTMLWriter.TYPE_PROXY_FACTORY.equals(type)) {
450                 openejb.setProxyFactory(null);
451             } else if (ConfigHTMLWriter.TYPE_SECURITY_SERVICE.equals(type)) {
452                 openejb.setSecurityService(null);
453             } else if (ConfigHTMLWriter.TYPE_TRANSACTION_SERVICE.equals(type)) {
454                 openejb.setTransactionService(null);
455             } else if (ConfigHTMLWriter.TYPE_RESOURCE.equals(type) && serviceIndex > -1) {
456                 openejb.removeResource(serviceIndex);
457             }
458             ConfigHTMLWriter.writeOpenejb(body, openejb, handleFile, configLocation);
459         } else {
460             throw new IOException JavaDoc("Invalid method");
461         }
462     }
463
464     /**
465      * This method takes care of submitting a connector. It grabs the form parameters
466      * and constructs the connector object
467      *
468      * @param body the output to the browser
469      * @param openejb the openejb object
470      * @param handleFile the file of the handle for the ConfigurationData object
471      * @param configLocation the location of the configuration file
472      * @throws IOException when an exception occurs
473      */

474     private void submitConnector(PrintWriter JavaDoc body, Openejb openejb, String JavaDoc handleFile, String JavaDoc configLocation) throws IOException JavaDoc {
475         Connector connector;
476
477         //get all the form variables
478
String JavaDoc id = request.getFormParameter(ConfigHTMLWriter.FORM_FIELD_ID).trim();
479         String JavaDoc jar = request.getFormParameter(ConfigHTMLWriter.FORM_FIELD_JAR).trim();
480         String JavaDoc provider = request.getFormParameter(ConfigHTMLWriter.FORM_FIELD_PROVIDER).trim();
481         String JavaDoc jdbcDriver = request.getFormParameter(EnvProps.JDBC_DRIVER).trim();
482         String JavaDoc jdbcUrl = request.getFormParameter(EnvProps.JDBC_URL).trim();
483         String JavaDoc userName = request.getFormParameter(EnvProps.USER_NAME).trim();
484         String JavaDoc password = request.getFormParameter(EnvProps.PASSWORD).trim();
485         int index = Integer.parseInt(request.getFormParameter(ConfigHTMLWriter.FORM_FIELD_INDEX).trim());
486         StringBuffer JavaDoc contentBuffer = new StringBuffer JavaDoc(125);
487         StringBuffer JavaDoc validationError = new StringBuffer JavaDoc(50);
488
489         //if the index is bigger than -1 then we want to get the current connector
490
//otherwise create a new one
491
if (index > -1) {
492             connector = openejb.getConnector(index);
493         } else {
494             connector = new Connector();
495         }
496
497         //check for blank fields in the different parts of the content
498
if (!"".equals(jdbcDriver))
499             contentBuffer.append(EnvProps.JDBC_DRIVER).append(" ").append(jdbcDriver).append('\n');
500         if (!"".equals(jdbcUrl))
501             contentBuffer.append(EnvProps.JDBC_URL).append(" ").append(jdbcUrl).append('\n');
502         if (!"".equals(userName))
503             contentBuffer.append(EnvProps.USER_NAME).append(" ").append(userName).append('\n');
504         if (!"".equals(password))
505             contentBuffer.append(EnvProps.PASSWORD).append(" ").append(password).append('\n');
506         if (!"".equals(id.trim()))
507             connector.setId(id.trim());
508         if (!"".equals(jar.trim()))
509             connector.setJar(jar);
510         if (!"".equals(provider.trim()))
511             connector.setProvider(provider);
512
513         connector.setContent((contentBuffer.length() > 0) ? contentBuffer.toString() : null);
514
515         try { //perform validation
516
connector.validate();
517         } catch (ValidationException e) {
518             //print the error message
519
body.print("<font color=\"red\">You must fix the following errors before proceeding:<br>\n<b>");
520             body.print(e.getMessage());
521             body.print("</b></font>\n<br><br>");
522
523             ConfigHTMLWriter.writeConnector(body, connector, handleFile, index);
524             return;
525         }
526
527         //if the connector is new, add it after validation
528
if (index == -1) {
529             openejb.addConnector(connector);
530         }
531
532         ConfigHTMLWriter.writeOpenejb(body, openejb, handleFile, configLocation);
533     }
534
535     /**
536      * This method takes care of submitting a container. It constructs a ContainerData
537      * object, puts all the info into it checks to see if we've submitted the form
538      * or just switched the container type
539      *
540      * @see org.openejb.webadmin.main.ContainerData
541      * @param body the output to the browser
542      * @param openejb the openejb object
543      * @param handleFile the file of the handle for the ConfigurationData object
544      * @param configLocation the location of the configuration file
545      * @throws IOException when an exception occurs
546      */

547     private void submitContainer(PrintWriter JavaDoc body, Openejb openejb, String JavaDoc handleFile, String JavaDoc configLocation)
548         throws IOException JavaDoc {
549         ContainerData data = new ContainerData();
550         int index = Integer.parseInt(request.getFormParameter(ConfigHTMLWriter.FORM_FIELD_INDEX));
551         String JavaDoc submit = request.getFormParameter(ConfigHTMLWriter.FORM_FIELD_SUBMIT_SERVICE);
552         Container container;
553         StringBuffer JavaDoc contentBuffer = new StringBuffer JavaDoc(125);
554         StringBuffer JavaDoc errorBuffer = new StringBuffer JavaDoc(100);
555
556         //set all the form data onto the ContainerData object
557
data.setBulkPassivate(
558             StringUtilities.nullToBlankString(request.getFormParameter(EnvProps.IM_PASSIVATE_SIZE)).trim());
559         data.setContainerType(
560             StringUtilities.nullToBlankString(request.getFormParameter(ConfigHTMLWriter.FORM_FIELD_CONTAINER_TYPE)).trim());
561         data.setGlobalTxDatabase(
562             StringUtilities.nullToBlankString(request.getFormParameter(EnvProps.GLOBAL_TX_DATABASE)).trim());
563         data.setId(StringUtilities.nullToBlankString(request.getFormParameter(ConfigHTMLWriter.FORM_FIELD_ID)).trim());
564         data.setIndex(index);
565         data.setJar(StringUtilities.nullToBlankString(request.getFormParameter(ConfigHTMLWriter.FORM_FIELD_JAR)).trim());
566         data.setLocalTxDatabase(
567             StringUtilities.nullToBlankString(request.getFormParameter(EnvProps.LOCAL_TX_DATABASE)).trim());
568         data.setPassivator(StringUtilities.nullToBlankString(request.getFormParameter(EnvProps.IM_PASSIVATOR)).trim());
569         data.setPoolSize(StringUtilities.nullToBlankString(request.getFormParameter(EnvProps.IM_POOL_SIZE)).trim());
570         data.setProvider(
571             StringUtilities.nullToBlankString(request.getFormParameter(ConfigHTMLWriter.FORM_FIELD_PROVIDER)).trim());
572         data.setStrictPooling(
573             StringUtilities.nullToBlankString(request.getFormParameter(EnvProps.IM_STRICT_POOLING)).trim());
574         data.setTimeOut(StringUtilities.nullToBlankString(request.getFormParameter(EnvProps.IM_TIME_OUT)).trim());
575
576         //here we have submitted the form
577
if (ConfigHTMLWriter.FORM_VALUE_SUBMIT_CONTAINER.equals(submit)) {
578             if (index > -1) {
579                 container = openejb.getContainer(index);
580             } else {
581                 container = new Container();
582             }
583
584             //set all the common data
585
container.setCtype(data.getContainerType().trim());
586             if (!"".equals(data.getId().trim()))
587                 container.setId(data.getId().trim());
588             if (!"".equals(data.getJar().trim()))
589                 container.setJar(data.getJar().trim());
590             if ("".equals(data.getProvider().trim()))
591                 container.setProvider(data.getProvider().trim());
592
593             //construct the contents based on type
594
if (Bean.CMP_ENTITY.equals(data.getContainerType())) {
595                 if (!"".equals(data.getPoolSize()))
596                     contentBuffer.append(EnvProps.IM_POOL_SIZE).append(" ").append(data.getPoolSize()).append('\n');
597                 if (!"".equals(data.getGlobalTxDatabase()))
598                     contentBuffer.append(EnvProps.GLOBAL_TX_DATABASE).append(" ").append(data.getGlobalTxDatabase()).append(
599                         '\n');
600                 else
601                     errorBuffer.append("Global Database File is a required field.<br>\n");
602                 if (!"".equals(data.getLocalTxDatabase()))
603                     contentBuffer.append(EnvProps.LOCAL_TX_DATABASE).append(" ").append(data.getLocalTxDatabase()).append(
604                         '\n');
605                 else
606                     errorBuffer.append("Local Database File is a required field.<br>\n");
607             } else if (Bean.STATEFUL.equals(data.getContainerType())) {
608                 if (!"".equals(data.getPassivator()))
609                     contentBuffer.append(EnvProps.IM_PASSIVATOR).append(" ").append(data.getPassivator()).append('\n');
610                 if (!"".equals(data.getTimeOut()))
611                     contentBuffer.append(EnvProps.IM_TIME_OUT).append(" ").append(data.getTimeOut()).append('\n');
612                 if (!"".equals(data.getPoolSize()))
613                     contentBuffer.append(EnvProps.IM_POOL_SIZE).append(" ").append(data.getPoolSize()).append('\n');
614                 if (!"".equals(data.getBulkPassivate()))
615                     contentBuffer.append(EnvProps.IM_PASSIVATE_SIZE).append(" ").append(data.getBulkPassivate()).append(
616                         '\n');
617             } else if (Bean.STATELESS.equals(data.getContainerType())) {
618                 if (!"".equals(data.getTimeOut()))
619                     contentBuffer.append(EnvProps.IM_TIME_OUT).append(" ").append(data.getTimeOut()).append('\n');
620                 if (!"".equals(data.getPoolSize()))
621                     contentBuffer.append(EnvProps.IM_POOL_SIZE).append(" ").append(data.getPoolSize()).append('\n');
622                 if (!"".equals(data.getStrictPooling()))
623                     contentBuffer.append(EnvProps.IM_STRICT_POOLING).append(" ").append(data.getStrictPooling()).append(
624                         '\n');
625
626                 container.setContent((contentBuffer.length() > 0) ? contentBuffer.toString() : null);
627             }
628
629             try { //validate the container
630
container.validate();
631             } catch (ValidationException e) {
632                 errorBuffer.insert(0, e.getMessage() + "<br>");
633             }
634
635             //check for an error message
636
if (errorBuffer.length() > 0) {
637                 errorBuffer.insert(0, "<font color=\"red\">You must fix the following errors: <br><b>").append(
638                     "</b></font><br>");
639                 body.println(errorBuffer.toString());
640                 ConfigHTMLWriter.writeContainer(body, data, handleFile);
641                 return;
642             }
643
644             ConfigHTMLWriter.writeOpenejb(body, openejb, handleFile, configLocation);
645         } else {
646             //in this case we just switched the container type
647
ConfigHTMLWriter.writeContainer(body, data, handleFile);
648         }
649     }
650
651     /**
652      * This method takes care of submitting deployments. It simply sets the jar
653      * or directory based on which one is chosen.
654      *
655      * @param body the output to the browser
656      * @param openejb the openejb object
657      * @param handleFile the file of the handle for the ConfigurationData object
658      * @param configLocation the location of the configuration file
659      */

660     private void submitDeployments(PrintWriter JavaDoc body, Openejb openejb, String JavaDoc handleFile, String JavaDoc configLocation) {
661         String JavaDoc deploymentType = request.getFormParameter(ConfigHTMLWriter.FORM_FIELD_DEPLOYMENT_TYPE);
662         String JavaDoc deploymentText = request.getFormParameter(ConfigHTMLWriter.FORM_FIELD_DEPLOYMENT_TEXT);
663         int index = Integer.parseInt(request.getFormParameter(ConfigHTMLWriter.FORM_FIELD_INDEX));
664         Deployments deployments;
665
666         //get the right deployment from the array
667
if (index > -1) {
668             deployments = openejb.getDeployments(index);
669         } else {
670             deployments = new Deployments();
671             openejb.addDeployments(deployments);
672         }
673
674         //set the directory or jar based on which one was chosen
675
if (ConfigHTMLWriter.DEPLOYMENT_TYPE_DIR.equals(deploymentType.trim())) {
676             deployments.setDir(deploymentText.trim());
677         } else {
678             deployments.setJar(deploymentText.trim());
679         }
680
681         ConfigHTMLWriter.writeOpenejb(body, openejb, handleFile, configLocation);
682     }
683
684     /**
685      * This is a general, "catch all" method for submitting service. It goes through
686      * and checks to see which type of service is being submitted and then goes from
687      * there
688      *
689      * @param body the output to the browser
690      * @param openejb the openejb object
691      * @param handleFile the file of the handle for the ConfigurationData object
692      * @param configLocation the location of the configuration file
693      * @param submit the string to be shown on the submit button
694      * @throws IOException when an exception occurs
695      */

696     private void submitService(
697         PrintWriter JavaDoc body,
698         Openejb openejb,
699         String JavaDoc handleFile,
700         String JavaDoc configLocation,
701         String JavaDoc submit)
702         throws IOException JavaDoc {
703         String JavaDoc id = request.getFormParameter(ConfigHTMLWriter.FORM_FIELD_ID);
704         String JavaDoc jar = request.getFormParameter(ConfigHTMLWriter.FORM_FIELD_JAR);
705         String JavaDoc provider = request.getFormParameter(ConfigHTMLWriter.FORM_FIELD_PROVIDER);
706         String JavaDoc content = request.getFormParameter(ConfigHTMLWriter.FORM_FIELD_CONTENT);
707         int index = Integer.parseInt(request.getFormParameter(ConfigHTMLWriter.FORM_FIELD_INDEX));
708         Service service = null;
709
710         //check to see which type of service we're using then check for null
711
if (ConfigHTMLWriter.FORM_VALUE_SUBMIT_CONNECTION_MANAGER.equals(submit)) {
712             if (openejb.getConnectionManager() == null) {
713                 service = new ConnectionManager();
714                 openejb.setConnectionManager((ConnectionManager) service);
715             } else {
716                 service = openejb.getConnectionManager();
717             }
718         } else if (ConfigHTMLWriter.FORM_VALUE_SUBMIT_PROXY_FACTORY.equals(submit)) {
719             if (openejb.getProxyFactory() == null) {
720                 service = new ProxyFactory();
721                 openejb.setProxyFactory((ProxyFactory) service);
722             } else {
723                 service = openejb.getProxyFactory();
724             }
725         } else if (ConfigHTMLWriter.FORM_VALUE_SUBMIT_SECURITY_SERVICE.equals(submit)) {
726             if (openejb.getSecurityService() == null) {
727                 service = new SecurityService();
728                 openejb.setSecurityService((SecurityService) service);
729             } else {
730                 service = openejb.getSecurityService();
731             }
732         } else if (ConfigHTMLWriter.FORM_VALUE_SUBMIT_TRANSACTION_SERVICE.equals(submit)) {
733             if (openejb.getTransactionService() == null) {
734                 service = new TransactionService();
735                 openejb.setTransactionService((TransactionService) service);
736             } else {
737                 service = openejb.getTransactionService();
738             }
739         } else if (ConfigHTMLWriter.FORM_VALUE_SUBMIT_JNDI_PROVIDER.equals(submit)) {
740             if (index > -1) {
741                 service = openejb.getJndiProvider(index);
742             } else {
743                 service = new JndiProvider();
744             }
745         } else if (ConfigHTMLWriter.FORM_VALUE_SUBMIT_RESOURCE.equals(submit)) {
746             if (index > -1) {
747                 service = openejb.getResource(index);
748             } else {
749                 service = new Resource();
750             }
751         } else {
752             throw new IOException JavaDoc("Invalid Service type");
753         }
754
755         //set the data on the service
756
if (!"".equals(content.trim())) {
757             service.setContent(content.trim());
758         }
759         if (!"".equals(id.trim())) {
760             service.setId(id.trim());
761         }
762         if (!"".equals(jar.trim())) {
763             service.setJar(jar.trim());
764         }
765         if (!"".equals(provider.trim())) {
766             service.setProvider(provider.trim());
767         }
768
769         try { //validate the service
770
service.validate();
771         } catch (ValidationException e) {
772             //print out the error message
773
body.print("<font color=\"red\">You must fix the following errors before continuing.<br>\n<b>");
774             body.print(e.getMessage());
775             body.println("</b></font><br><br>");
776             ConfigHTMLWriter.writeService(body, service, handleFile, submit, index);
777             return;
778         }
779
780         //after validation, add new services to the array
781
if (ConfigHTMLWriter.FORM_VALUE_SUBMIT_JNDI_PROVIDER.equals(submit)) {
782             if (index == -1)
783                 openejb.addJndiProvider((JndiProvider) service);
784         } else if (ConfigHTMLWriter.FORM_VALUE_SUBMIT_RESOURCE.equals(submit)) {
785             if (index == -1)
786                 openejb.addResource((Resource) service);
787         }
788
789         ConfigHTMLWriter.writeOpenejb(body, openejb, handleFile, configLocation);
790     }
791
792     /**
793      * This method submits the main Openejb object and writes it to the file
794      *
795      * @param openejb the openejb object
796      * @param configLocation the location of the configuration file
797      * @throws IOException if the changes could not be written
798      * @return the message of where the changes were written to
799      */

800     private String JavaDoc submitOpenejb(String JavaDoc configLocation, Openejb openejb) throws IOException JavaDoc {
801         FileWriter JavaDoc writer = new FileWriter JavaDoc(configLocation);
802
803         try {
804             openejb.marshal(writer);
805         } catch (Exception JavaDoc e) {
806             throw new IOException JavaDoc(e.getMessage());
807         }
808
809         return "Your changes were written to: " + configLocation;
810     }
811
812     /**
813      * gets an object reference and handle
814      *
815      * @param configurationData the object to create a handle from
816      * @return an absolute path of the handle file
817      * @throws IOException if the file cannot be created
818      */

819     private String JavaDoc createHandle(ConfigurationDataObject configurationData) throws IOException JavaDoc {
820         //write the handle out to a file
821
File JavaDoc myHandleFile = new File JavaDoc(FileUtils.createTempDirectory().getAbsolutePath() + HANDLE_FILE);
822         if (!myHandleFile.exists()) {
823             myHandleFile.createNewFile();
824         }
825
826         ObjectOutputStream JavaDoc objectOut = new ObjectOutputStream JavaDoc(new FileOutputStream JavaDoc(myHandleFile));
827         objectOut.writeObject(configurationData.getHandle()); //writes the handle to the file
828
objectOut.flush();
829         objectOut.close();
830
831         return myHandleFile.getAbsolutePath();
832     }
833
834     /**
835      * creates a new ConfigurationDataObject
836      *
837      * @return a new configuration data object
838      * @throws IOException if the object cannot be created
839      */

840     private ConfigurationDataObject getConfigurationObject() throws IOException JavaDoc {
841         Properties JavaDoc p = new Properties JavaDoc();
842         p.put(Context.INITIAL_CONTEXT_FACTORY, "org.openejb.core.ivm.naming.InitContextFactory");
843
844         //lookup the bean
845
try {
846             InitialContext JavaDoc ctx = new InitialContext JavaDoc(p);
847             Object JavaDoc obj = ctx.lookup("config/webadmin/ConfigurationData");
848             //create a new instance
849
ConfigurationDataHome home =
850                 (ConfigurationDataHome) PortableRemoteObject.narrow(obj, ConfigurationDataHome.class);
851             return home.create();
852         } catch (Exception JavaDoc e) {
853             throw new IOException JavaDoc(e.getMessage());
854         }
855     }
856
857     /**
858      * this method gets the deployer handle
859      *
860      * @param handleFile the handle to the object
861      * @return the configuration data object
862      * @throws IOException if the file is not found
863      */

864     private ConfigurationDataObject getHandle(String JavaDoc handleFile) throws IOException JavaDoc {
865         File JavaDoc myHandleFile = new File JavaDoc(handleFile);
866
867         //get the object
868
ObjectInputStream JavaDoc objectIn = new ObjectInputStream JavaDoc(new FileInputStream JavaDoc(myHandleFile));
869         //get the handle
870
Handle JavaDoc configurationHandle;
871         try {
872             configurationHandle = (Handle JavaDoc) objectIn.readObject();
873             return (ConfigurationDataObject) configurationHandle.getEJBObject();
874         } catch (Exception JavaDoc e) {
875             throw new IOException JavaDoc(e.getMessage());
876         }
877     }
878 }
Popular Tags