KickJava   Java API By Example, From Geeks To Geeks.

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


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: DeployerBean.java 2082 2005-08-16 04:18:56Z dblevins $
44  */

45 package org.openejb.webadmin.main;
46
47 import org.openejb.DeploymentInfo;
48 import org.openejb.OpenEJB;
49 import org.openejb.OpenEJBException;
50 import org.openejb.alt.config.Bean;
51 import org.openejb.alt.config.ConfigUtils;
52 import org.openejb.alt.config.EjbJarUtils;
53 import org.openejb.alt.config.TempCodebase;
54 import org.openejb.alt.config.ejb11.EjbDeployment;
55 import org.openejb.alt.config.ejb11.EjbJar;
56 import org.openejb.alt.config.ejb11.EjbRef;
57 import org.openejb.alt.config.ejb11.MethodParams;
58 import org.openejb.alt.config.ejb11.OpenejbJar;
59 import org.openejb.alt.config.ejb11.Query;
60 import org.openejb.alt.config.ejb11.QueryMethod;
61 import org.openejb.alt.config.ejb11.ResourceLink;
62 import org.openejb.alt.config.ejb11.ResourceRef;
63 import org.openejb.alt.config.sys.Connector;
64 import org.openejb.alt.config.sys.Container;
65 import org.openejb.alt.config.sys.Openejb;
66 import org.openejb.util.HtmlUtilities;
67 import org.openejb.util.JarUtils;
68 import org.openejb.util.StringUtilities;
69
70 import javax.ejb.CreateException JavaDoc;
71 import javax.ejb.SessionBean JavaDoc;
72 import javax.ejb.SessionContext JavaDoc;
73 import java.io.File JavaDoc;
74 import java.lang.reflect.Method JavaDoc;
75 import java.util.ArrayList JavaDoc;
76 import java.util.List JavaDoc;
77 import java.util.Vector JavaDoc;
78
79 /**
80  * This is a stateful session bean which handles the action of deployment for the
81  * web administration.
82  *
83  * @see org.openejb.webadmin.main.DeployData
84  * @see org.openejb.webadmin.main.OQLData
85  * @see org.openejb.webadmin.main.ReferenceData
86  *
87  * @author <a HREF="mailto:tim_urberg@yahoo.com">Tim Urberg</a>
88  */

89 public class DeployerBean implements SessionBean JavaDoc {
90     public static final String JavaDoc ALL_FIELDS_REQUIRED_ERROR =
91         "All fields (except OQL parameters) are required, "
92             + "please hit your back button and fill out the required fields.";
93
94     //private boolean values
95
/* key for boolean values:
96      * AUTO_ASSIGN 0
97      * MOVE_JAR 1
98      * FORCE_OVERWRITE_JAR 2
99      * COPY_JAR 3
100      * AUTO_CONFIG 4
101      * GENERATE_DEPLOYMENT_ID 5
102      * GENERATE_STUBS 6
103      */

104     private boolean[] options = new boolean[7];
105
106     //private variables
107
private SessionContext JavaDoc context;
108     private Openejb config;
109     private String JavaDoc configFile = null;
110     private boolean configChanged;
111     private boolean autoAssign;
112     private Container[] containers;
113     private Connector[] resources;
114     private Bean[] deployerBeans;
115     private EjbDeployment[] deploymentInfoArray;
116     private String JavaDoc jarFile;
117     private StringBuffer JavaDoc deploymentHTML = new StringBuffer JavaDoc();
118     private String JavaDoc containerDeployIdsHTML = "";
119     private OpenejbJar openejbJar;
120     private Vector JavaDoc beanList = new Vector JavaDoc();
121     private boolean idsWritten = false;
122     private ArrayList JavaDoc usedBeanNames = new ArrayList JavaDoc();
123     private DeployData[] deployDataArray;
124
125     /**
126      * Creates a new instance of DeployerBean
127      * @throws CreateException if this bean cannot be created
128      */

129     public void ejbCreate() throws CreateException JavaDoc {
130         try {
131             if (configFile == null) {
132                 try {
133                     configFile = System.getProperty("openejb.configuration");
134                 } catch (Exception JavaDoc e) {}
135             }
136             if (configFile == null) {
137                 configFile = ConfigUtils.searchForConfiguration();
138             }
139             config = ConfigUtils.readConfig(configFile);
140
141             /* Load container list */
142             containers = config.getContainer();
143
144             /* Load resource list */
145             resources = config.getConnector();
146
147         } catch (Exception JavaDoc e) {
148             // TODO: Better exception handling.
149
e.printStackTrace();
150         }
151     }
152
153     /**
154      * sets the boolean values for options
155      * @param booleanValues the array of boolean values
156      */

157     public void setBooleanValues(boolean[] booleanValues) {
158         options = booleanValues;
159     }
160
161     /**
162      * gets the boolean values for options
163      * @return the array of boolean values
164      */

165     public boolean[] getBooleanValues() {
166         return options;
167     }
168
169     /**
170      * sets the jar file
171      * @param jar the location of the jar file
172      */

173     public void setJarFile(String JavaDoc jar) {
174         jarFile = jar;
175     }
176
177     /**
178      * gets the jar file
179      * @return jar the location of the jar file
180      */

181     public String JavaDoc getJarFile() {
182         return jarFile;
183     }
184
185     /**
186      * gets the HTML for this deployment
187      * @return the HTML for deployment
188      */

189     public String JavaDoc getDeploymentHTML() {
190         return deploymentHTML.toString();
191     }
192
193     /**
194      * gets the array of deploy data objects
195      * @return the array of deploy data objects
196      */

197     public DeployData[] getDeployDataArray() {
198         return this.deployDataArray;
199     }
200
201     /**
202      * starts the deployment process by getting information from the
203      * jar, getting a list of beans and other init functions
204      * @throws OpenEJBException if something goes wrong
205      */

206     public void startDeployment() throws OpenEJBException {
207         EjbJarUtils ejbJarUtils = new EjbJarUtils(this.jarFile);
208         EjbJar jar = null;
209         OpenejbJar initialOpenejbJar = null;
210         try { //test for invalid file
211
jar = ejbJarUtils.getEjbJar();
212
213             //check for an openejb-jar.xml file
214
if (EjbJarUtils.checkForOpenejbJar(this.jarFile)) {
215                 initialOpenejbJar = ejbJarUtils.getOpenejbJar();
216             }
217         } catch (OpenEJBException oe) {
218             throw new OpenEJBException(this.jarFile + " is not a valid jar file. ");
219         }
220
221         //we don't want to perform this check if we're forcing overwrite
222
if (!this.options[2]) {
223             File JavaDoc tempJarFile = new File JavaDoc(this.jarFile);
224             //check for existing jar
225
File JavaDoc beansDir = new File JavaDoc(System.getProperty("openejb.home") + System.getProperty("file.separator") + "beans");
226
227             File JavaDoc[] beans = beansDir.listFiles();
228             for (int i = 0; i < beans.length; i++) {
229                 if (tempJarFile.getName().equalsIgnoreCase(beans[i].getName())) {
230                     throw new OpenEJBException(
231                         System.getProperty("openejb.home")
232                             + System.getProperty("file.separator")
233                             + "beans"
234                             + System.getProperty("file.separator")
235                             + tempJarFile.getName()
236                             + " already exists.");
237                 }
238             }
239         }
240
241         //check for an
242
if (initialOpenejbJar != null) {
243             deploymentInfoArray = initialOpenejbJar.getEjbDeployment();
244         } else {
245             deploymentInfoArray = new EjbDeployment[0];
246         }
247
248         openejbJar = new OpenejbJar();
249         deployerBeans = ejbJarUtils.getBeans();
250     }
251
252     /**
253      * This method takes care of setting the deploy information onto the deployment and
254      * creating the HTML for the second page of the deployment
255      * @param deployDataArray the array of deployment data
256      */

257     public void setDeployAndContainerIds(DeployData[] deployDataArray) throws OpenEJBException {
258         //local variables
259
EjbDeployment deployment;
260         ResourceLink link;
261         ReferenceData[] referenceDataArray;
262         OQLData[] oqlDataArray;
263         Query query;
264         MethodParams methodParams;
265         QueryMethod queryMethod;
266         String JavaDoc[] parameterArray;
267
268         for (int i = 0; i < deployDataArray.length; i++) {
269             if (this.usedBeanNames.contains(deployDataArray[i].getDeploymentIdValue())) {
270                 throw new OpenEJBException(
271                     "The deployment id: "
272                         + deployDataArray[i].getDeploymentIdValue()
273                         + " is already being used by another bean, please choose another deployment id.");
274             } else if ("".equals(deployDataArray[i].getDeploymentIdValue())) {
275                 throw new OpenEJBException(ALL_FIELDS_REQUIRED_ERROR);
276             }
277
278             deployment = new EjbDeployment();
279             this.usedBeanNames.add(deployDataArray[i].getDeploymentIdValue());
280
281             //set the deployment info
282
deployment.setEjbName(deployDataArray[i].getEjbName());
283             deploymentHTML.append("<tr>\n<td>").append(deployerBeans[i].getEjbName()).append("</td>\n");
284             deployment.setDeploymentId(deployDataArray[i].getDeploymentIdValue());
285             deploymentHTML.append("<td>").append(deployDataArray[i].getDeploymentIdValue()).append("</td>\n");
286
287             if ("".equals(deployDataArray[i].getContainerIdValue())) {
288                 throw new OpenEJBException(ALL_FIELDS_REQUIRED_ERROR);
289             }
290             deployment.setContainerId(deployDataArray[i].getContainerIdValue());
291             deploymentHTML.append("<td>").append(deployDataArray[i].getContainerIdValue()).append("</td>\n");
292
293             //check for string lengths
294
if (deployDataArray[i].getReferenceDataArray().length == 0) {
295                 deploymentHTML.append("<td>N/A</td>\n");
296             } else {
297                 deploymentHTML.append("<td>\n");
298                 deploymentHTML.append("<table cellspacing=\"0\" cellpadding=\"2\" border=\"0\" width=\"100%\">\n");
299                 deploymentHTML.append("<tr align=\"left\">\n");
300                 deploymentHTML.append("<th>Name</th>\n");
301                 deploymentHTML.append("<th>Id</th>\n");
302                 deploymentHTML.append("</tr>\n");
303
304                 //set the resource references
305
referenceDataArray = deployDataArray[i].getReferenceDataArray();
306                 for (int j = 0; j < referenceDataArray.length; j++) {
307                     if ("".equals(referenceDataArray[j].getReferenceIdValue())) {
308                         throw new OpenEJBException(ALL_FIELDS_REQUIRED_ERROR);
309                     }
310
311                     if ("".equals(referenceDataArray[j].getReferenceValue())) {
312                         throw new OpenEJBException(ALL_FIELDS_REQUIRED_ERROR);
313                     }
314
315                     link = new ResourceLink();
316                     link.setResId(referenceDataArray[j].getReferenceIdValue());
317
318                     link.setResRefName(referenceDataArray[j].getReferenceValue());
319                     deploymentHTML.append("<tr>\n<td>").append(referenceDataArray[j].getReferenceValue()).append("</td>\n");
320                     deploymentHTML.append("<td>").append(referenceDataArray[j].getReferenceIdValue()).append(
321                         "</td>\n</tr>\n");
322                     deployment.addResourceLink(link);
323                 }
324
325                 deploymentHTML.append("</table>\n</td>\n");
326             }
327
328             deploymentHTML.append("</tr>\n");
329
330             //add in the oql methods
331
oqlDataArray = deployDataArray[i].getOqlDataArray();
332             if (oqlDataArray.length > 0) {
333                 deploymentHTML.append("<tr>\n<td colspan=\"4\">&nbsp;</td>\n</tr>\n");
334                 deploymentHTML.append("<tr>\n<td colspan=\"2\">").append(deployDataArray[i].getEjbName()).append(" - ");
335                 deploymentHTML.append(
336                     "Method</td>\n<td>OQL Statement</td>\n<td>OQL Parameters (comma seperated)</td>\n</tr>\n");
337             }
338
339             for (int j = 0; j < oqlDataArray.length; j++) {
340                 if ("".equals(oqlDataArray[j].getOqlStatementValue())) {
341                     throw new OpenEJBException(ALL_FIELDS_REQUIRED_ERROR);
342                 }
343
344                 //create the new instances
345
query = new Query();
346                 methodParams = new MethodParams();
347                 queryMethod = new QueryMethod();
348
349                 queryMethod.setMethodName(oqlDataArray[j].getMethodName());
350                 query.setObjectQl(oqlDataArray[j].getOqlStatementValue());
351
352                 deploymentHTML.append("<tr>\n<td colspan=\"2\">");
353                 deploymentHTML.append(oqlDataArray[j].getMethodString()).append("</td>\n");
354                 deploymentHTML.append("<td>").append(oqlDataArray[j].getOqlStatementValue()).append("</td>\n");
355                 deploymentHTML.append("<td>");
356
357                 //get the list of parameters
358
parameterArray = (String JavaDoc[]) oqlDataArray[j].getOqlParameterValueList().toArray(new String JavaDoc[0]);
359                 for (int k = 0; k < parameterArray.length; k++) {
360                     deploymentHTML.append(k + 1).append(". ").append(parameterArray[k]).append("<br>");
361                     methodParams.addMethodParam(k, parameterArray[k]);
362                 }
363
364                 if (parameterArray.length == 0) {
365                     deploymentHTML.append("N/A");
366                 }
367
368                 queryMethod.setMethodParams(methodParams);
369                 query.setQueryMethod(queryMethod);
370                 deployment.addQuery(query);
371                 deploymentHTML.append("</td>\n</tr>\n");
372             }
373
374             openejbJar.addEjbDeployment(deployment);
375         }
376     }
377
378     /**
379      * finishes the deployment by adding the deployment entry
380      * and saving the changes
381      * @throws OpenEJBException if something goes wrong
382      */

383     public void finishDeployment() throws OpenEJBException {
384         jarFile = moveJar(jarFile);
385
386         /* TODO: Automatically updating the users
387         config file might not be desireable for
388         some people. We could make this a
389         configurable option.
390         */

391         addDeploymentEntryToConfig(jarFile);
392         saveChanges(jarFile, openejbJar);
393     }
394
395     /**
396      * Assigns a deployment id based on the ejb name
397      * @param bean the EJB which we get the name from
398      * @return the EJB name of this bean
399      */

400     private String JavaDoc autoAssignDeploymentId(Bean bean) {
401         this.resetUsedDeploymentIds();
402         String JavaDoc ejbName = bean.getEjbName();
403         String JavaDoc newEjbName;
404
405         //first check for the deployment id in the list
406
//and make sure that all the bean names are unique
407
if (this.usedBeanNames.contains(ejbName)) {
408             while (true) {
409                 newEjbName = ejbName + (Long.MAX_VALUE * Math.random());
410                 if (!this.usedBeanNames.contains(newEjbName)) {
411                     this.usedBeanNames.add(newEjbName);
412                     return newEjbName;
413                 }
414             }
415         }
416
417         this.usedBeanNames.add(ejbName);
418         return ejbName;
419     }
420
421     /**
422      * Assigns a container id based on the ejb name
423      * @param bean the EJB which we get the name from
424      * @return the the best container for this bean
425      */

426     private String JavaDoc autoAssignContainerId(Bean bean) throws OpenEJBException {
427         Container[] cs = getUsableContainers(bean);
428
429         if (cs.length == 0) {
430             //we'll fix this later
431
throw new OpenEJBException("There are no useable containers for this bean.");
432         }
433
434         return cs[0].getId();
435     }
436
437     /**
438      * Assigns a container id based on the ejb name
439      * @param bean the EJB which we get the name from
440      * @return the the best container for this bean
441      */

442     private void saveChanges(String JavaDoc jarFile, OpenejbJar openejbJar) throws OpenEJBException {
443         EjbJarUtils.writeOpenejbJar("META-INF/openejb-jar.xml", openejbJar);
444         JarUtils.addFileToJar(jarFile, "META-INF/openejb-jar.xml");
445
446         if (configChanged) {
447             ConfigUtils.writeConfig(configFile, config);
448         }
449     }
450
451     /**
452      * creates an HTML table with form fields for deployment
453      * information
454      * @throws OpenEJBException when something goes wrong
455      * @return the HTML table with the form fields
456      */

457     public String JavaDoc createIdTable() throws OpenEJBException {
458         //string that contains all the html
459
StringBuffer JavaDoc htmlString = new StringBuffer JavaDoc();
460         String JavaDoc deploymentId;
461         String JavaDoc containerId;
462         Container[] cs;
463         ResourceRef[] refs;
464         EjbRef[] ejbRefs;
465         Class JavaDoc tempBean;
466         this.deployDataArray = new DeployData[deployerBeans.length];
467         EjbDeployment ejbDeployment = null;
468
469         htmlString.append("<table cellspacing=\"0\" cellpadding=\"0\" border=\"1\">\n");
470         htmlString.append("<tr align=\"left\">\n");
471         htmlString.append("<th>Bean Name</th>\n");
472         htmlString.append("<th>Deployment Id</th>\n");
473         htmlString.append("<th>Container Id</th>\n");
474         htmlString.append("<th>Resource References</th>\n");
475         htmlString.append("</tr>\n");
476
477         for (int i = 0; i < deployerBeans.length; i++) {
478             //set up the data for the next step
479
this.deployDataArray[i] = new DeployData();
480             this.deployDataArray[i].setEjbName(deployerBeans[i].getEjbName());
481             this.deployDataArray[i].setDeploymentIdName("deploymentId" + i);
482             this.deployDataArray[i].setContainerIdName("containerId" + i);
483
484             //in here we check to see if we need to write the different parts or not
485
htmlString.append("<tr>\n");
486             htmlString.append("<td>" + deployerBeans[i].getEjbName() + "</td>\n");
487
488             for (int j = 0; j < this.deploymentInfoArray.length; j++) {
489                 if (deployerBeans[i].getEjbName().equals(deploymentInfoArray[j].getEjbName())) {
490                     ejbDeployment = deploymentInfoArray[j];
491                     break;
492                 }
493             }
494
495             //deployment id
496
if (ejbDeployment != null) {
497                 deploymentId = ejbDeployment.getDeploymentId();
498             } else {
499                 deploymentId = autoAssignDeploymentId(deployerBeans[i]);
500             }
501
502             htmlString
503                 .append("<td>")
504                 .append(HtmlUtilities.createTextFormField(deployDataArray[i].getDeploymentIdName(), deploymentId, 25, 50))
505                 .append("</td>\n");
506
507             //container id
508
if (ejbDeployment != null) {
509                 containerId = ejbDeployment.getContainerId();
510             } else {
511                 containerId = autoAssignContainerId(deployerBeans[i]);
512             }
513
514             htmlString.append("<td>").append(
515                 HtmlUtilities.createSelectFormField(deployDataArray[i].getContainerIdName(), null)).append(
516                 '\n');
517             cs = getUsableContainers(deployerBeans[i]);
518             //loop through the continer
519
for (int j = 0; j < cs.length; j++) {
520                 htmlString.append(
521                     HtmlUtilities.createSelectOption(
522                         cs[j].getId(),
523                         cs[j].getId(),
524                         containerId.equals(cs[j].getId()))).append(
525                     '\n');
526             }
527             htmlString.append("</select></td>\n");
528
529             //outside references go here - put in a seperate method
530
refs = deployerBeans[i].getResourceRef();
531             ejbRefs = deployerBeans[i].getEjbRef();
532
533             if ((refs.length > 0) || (ejbRefs.length > 0)) {
534                 htmlString.append("<td>");
535                 htmlString.append(
536                     createIdTableOutsideRef(
537                         refs,
538                         ejbRefs,
539                         deployerBeans[i].getEjbName(),
540                         deployDataArray[i],
541                         ejbDeployment,
542                         i));
543                 htmlString.append("</td>");
544             } else {
545                 htmlString.append("<td>N/A</td>\n");
546             }
547
548             htmlString.append("</tr>\n");
549         }
550
551         //loop through the beans again to get the OQL statements
552
//(they go in a different part of the table)
553
for (int i = 0; i < deployerBeans.length; i++) {
554             //check for entity beans here
555
if ("CMP_ENTITY".equals(deployerBeans[i].getType())) {
556                 TempCodebase tempCodebase = TempCodebase.getTempCodebase(this.jarFile);
557                 tempBean = tempCodebase.loadClass(deployerBeans[i].getHome());
558                 htmlString.append(
559                     writeOQLForEntityBeansTable(
560                         tempBean,
561                         deployerBeans[i].getEjbName(),
562                         deployDataArray[i],
563                         i,
564                         ejbDeployment));
565             }
566         }
567
568         htmlString.append("<tr><td colspan=\"4\">");
569         htmlString.append(HtmlUtilities.createSubmitFormButton("submitDeploymentAndContainerIds", "Continue &gt;&gt;"));
570         htmlString.append("</td></tr></table>\n");
571
572         this.resetUsedDeploymentIds();
573         return htmlString.toString();
574     }
575
576     /**
577      * creates an HTML table/form for outside references, ejb and resource
578      * @param refs an array of resource references
579      * @param ejbRefs an array of ejb references
580      * @param deploymentName the name of this deployment
581      * @param deployData the DeploymentData object for this deployment
582      * @param index the index of this bean
583      * @return the HTML table/form for these outside references
584      */

585     private String JavaDoc createIdTableOutsideRef(
586         ResourceRef[] refs,
587         EjbRef[] ejbRefs,
588         String JavaDoc deploymentName,
589         DeployData deployData,
590         EjbDeployment ejbDeployment,
591         int index)
592         throws OpenEJBException {
593         StringBuffer JavaDoc htmlString = new StringBuffer JavaDoc();
594         String JavaDoc resourceId = "";
595         String JavaDoc ejbId = "";
596         ResourceLink[] resourceLinks =
597             (ejbDeployment != null && ejbDeployment.getResourceLink() != null)
598                 ? ejbDeployment.getResourceLink()
599                 : new ResourceLink[0];
600         ReferenceData[] referenceDataArray = new ReferenceData[(refs.length + ejbRefs.length)];
601
602         //this will create the html for outside references
603
htmlString.append("<table cellspacing=\"0\" cellpadding=\"2\" border=\"0\" width=\"100%\">\n");
604         htmlString.append("<tr align=\"left\">\n");
605         htmlString.append("<th>Name</th>\n");
606         htmlString.append("<th>Type</th>\n");
607         htmlString.append("<th>Id</th>\n");
608         htmlString.append("</tr>\n");
609
610         //loop through the resource references
611
if (refs.length > 0) {
612             for (int i = 0; i < refs.length; i++) {
613                 //create a new ReferenceData object and set the values
614
referenceDataArray[i] = new ReferenceData();
615                 referenceDataArray[i].setReferenceType(ReferenceData.RESOURCE_REFERENCE);
616                 referenceDataArray[i].setReferenceIdName("resourceRefId_" + index + "_" + i);
617                 referenceDataArray[i].setReferenceName("resourceRefName_" + index + "_" + i);
618
619                 htmlString.append("<tr>\n");
620                 htmlString.append("<td>").append(refs[i].getResRefName()).append("</td>\n");
621                 htmlString.append("<td>").append(refs[i].getResType()).append("</td>\n");
622                 htmlString.append("<td>");
623                 htmlString.append(HtmlUtilities.createSelectFormField(referenceDataArray[i].getReferenceIdName(), null));
624
625                 //loop through the available resources
626
for (int j = 0; j < this.resources.length; j++) {
627                     //check for selected values
628
boolean selected = false;
629                     for (int k = 0; k < resourceLinks.length; k++) {
630                         if (resources[j].getId().equals(resourceLinks[k].getResId())) {
631                             selected = true;
632                             break;
633                         }
634                     }
635                     htmlString.append(
636                         HtmlUtilities.createSelectOption(resources[j].getId(), resources[j].getId(), selected)).append(
637                         '\n');
638                 }
639
640                 htmlString.append("</select>\n");
641                 htmlString.append(
642                     HtmlUtilities.createHiddenFormField(referenceDataArray[i].getReferenceName(), refs[i].getResRefName()));
643                 htmlString.append("</td>\n</tr>\n");
644             }
645         }
646
647         if (ejbRefs.length > 0) {
648             String JavaDoc ejbLink;
649             for (int i = 0; i < ejbRefs.length; i++) {
650                 referenceDataArray[i] = new ReferenceData();
651                 referenceDataArray[i].setReferenceType(ReferenceData.EJB_REFERENCE);
652                 referenceDataArray[i].setReferenceIdName("ejbRefId_" + index + "_" + i);
653                 referenceDataArray[i].setReferenceName("ejbRefName_" + index + "_" + i);
654
655                 htmlString.append("<tr>\n");
656                 htmlString.append("<td>").append(ejbRefs[i].getEjbRefName()).append("</td>\n");
657                 htmlString.append("<td>").append(ejbRefs[i].getEjbRefType()).append("</td>\n");
658
659                 //check for an available link
660
ejbLink = ejbRefs[i].getEjbLink();
661                 if (ejbLink == null) {
662                     htmlString.append("<td>").append(
663                         HtmlUtilities.createSelectFormField("ejbRefId_" + index + "_" + i, null));
664                     //loop through the available beans in the jar
665
for (int j = 0; j < deployerBeans.length; j++) {
666                         if (!deployerBeans[j].getEjbName().equals(deploymentName)) {
667                             //check for selected values
668
boolean selected = false;
669                             for (int k = 0; k < resourceLinks.length; k++) {
670                                 if (deployerBeans[j].getEjbName().equals(resourceLinks[k].getResId())) {
671                                     selected = true;
672                                     break;
673                                 }
674                             }
675                             htmlString.append(
676                                 HtmlUtilities.createSelectOption(
677                                     deployerBeans[j].getEjbName(),
678                                     deployerBeans[j].getEjbName(),
679                                     selected));
680                         }
681                     }
682
683                     htmlString.append("</select>\n");
684                 } else {
685                     htmlString
686                         .append("<td>")
687                         .append(HtmlUtilities.createHiddenFormField(referenceDataArray[i].getReferenceIdName(), ejbLink))
688                         .append(ejbLink)
689                         .append('\n');
690                 }
691
692                 htmlString.append(
693                     HtmlUtilities.createHiddenFormField(
694                         referenceDataArray[i].getReferenceName(),
695                         ejbRefs[i].getEjbRefName()));
696                 htmlString.append("</td>\n</tr>\n");
697             }
698         }
699
700         deployData.setReferenceDataArray(referenceDataArray);
701         htmlString.append("</table>\n");
702
703         return htmlString.toString();
704     }
705
706     private String JavaDoc writeOQLForEntityBeansTable(
707         Class JavaDoc bean,
708         String JavaDoc beanName,
709         DeployData deployData,
710         int index,
711         EjbDeployment ejbDeployment)
712         throws OpenEJBException {
713
714         StringBuffer JavaDoc htmlString = new StringBuffer JavaDoc();
715         int methodCount = 0;
716         Method[] methods = bean.getMethods();
717         List JavaDoc oqlDataList = new ArrayList JavaDoc();
718         OQLData oqlData;
719         String JavaDoc methodString;
720
721         Query[] queries =
722             (ejbDeployment != null && ejbDeployment.getQuery() != null) ? ejbDeployment.getQuery() : new Query[0];
723         Query query = null;
724
725         htmlString.append("<tr>\n<td colspan=\"4\">&nbsp;</td>\n</tr>\n");
726         htmlString.append("<tr>\n<td colspan=\"2\">").append(beanName).append(" - ");
727         htmlString.append("Method</td>\n<td>OQL Statement</td>\n<td>OQL Parameters (comma seperated)</td>\n</tr>\n");
728
729         for (int i = 0; i < methods.length; i++) {
730             if (methods[i].getName().startsWith("find") && !methods[i].getName().equals("findByPrimaryKey")) {
731                 //loop through the queries and get method name
732
for (int j = 0; j < queries.length; j++) {
733                     if (queries[j].getQueryMethod().getMethodName().equals(methods[i].getName())) {
734                         query = queries[j];
735                         break;
736                     }
737                 }
738
739                 methodString = StringUtilities.createMethodString(methods[i], "<br>");
740
741                 methodCount++;
742                 oqlData = new OQLData();
743                 htmlString.append("<tr>\n<td colspan=\"2\">\n");
744                 htmlString.append(methodString);
745                 oqlData.setMethodString(methodString);
746
747                 //put the data into the oqlData object
748
oqlData.setMethodName(methods[i].getName());
749                 oqlData.setOqlStatementName("oqlStatement_" + beanName + "_" + methods[i].getName());
750                 oqlData.setOqlParameterName("oqlParameters_" + beanName + "_" + methods[i].getName());
751
752                 //create the textarea for the OQL statement and parameters
753
htmlString.append("\n</td>\n<td>");
754                 //append the oql statement if there is one
755
htmlString.append(
756                     HtmlUtilities.createTextArea(
757                         oqlData.getOqlStatementName(),
758                         (query == null) ? "" : query.getObjectQl(),
759                         4,
760                         20,
761                         null,
762                         null,
763                         null));
764
765                 htmlString.append("</td>\n<td>");
766
767                 //get the parameters and append them if needed
768
htmlString.append(
769                     HtmlUtilities.createTextArea(
770                         oqlData.getOqlParameterName(),
771                         (query == null)
772                             ? ""
773                             : StringUtilities.stringArrayToCommaDelimitedStringList(
774                                 query.getQueryMethod().getMethodParams().getMethodParam()),
775                         4,
776                         20,
777                         null,
778                         null,
779                         null));
780
781                 htmlString.append("</td>\n</tr>\n");
782
783                 oqlDataList.add(oqlData);
784             }
785         }
786
787         deployData.setOqlDataArray((OQLData[]) oqlDataList.toArray(new OQLData[0]));
788
789         //if there were no methods return a blank string
790
if (methodCount > 0) {
791             return htmlString.toString();
792         } else {
793             return "";
794         }
795     }
796
797     private void resetUsedDeploymentIds() {
798         this.usedBeanNames = new ArrayList JavaDoc();
799
800         //put all the used deployments into the array
801
DeploymentInfo[] deployments = OpenEJB.deployments();
802         for (int i = 0; i < deployments.length; i++) {
803             this.usedBeanNames.add(deployments[i].getDeploymentID());
804         }
805     }
806
807     private Container[] getUsableContainers(Bean bean) {
808         return EjbJarUtils.getUsableContainers(containers, bean);
809     }
810
811     private String JavaDoc moveJar(String JavaDoc jar) throws OpenEJBException {
812         return EjbJarUtils.moveJar(jar, options[2]);
813     }
814
815     private String JavaDoc copyJar(String JavaDoc jar) throws OpenEJBException {
816         return EjbJarUtils.copyJar(jar, options[2]);
817     }
818
819     private void addDeploymentEntryToConfig(String JavaDoc jarLocation) {
820         configChanged = ConfigUtils.addDeploymentEntryToConfig(jarLocation, config);
821     }
822
823     //api callback methods
824
public void ejbActivate() {}
825     public void ejbPassivate() {}
826     public void ejbRemove() {}
827     public void setSessionContext(SessionContext JavaDoc sessionContext) {
828         this.context = sessionContext;
829     }
830 }
Popular Tags