1 45 package org.openejb.webadmin.ejbgen; 46 47 import java.io.File ; 48 import java.io.IOException ; 49 import java.io.PrintWriter ; 50 import java.util.StringTokenizer ; 51 52 import org.openejb.util.FileUtils; 53 import org.openejb.util.HtmlUtilities; 54 import org.openejb.webadmin.HttpRequest; 55 import org.openejb.webadmin.HttpResponse; 56 import org.openejb.webadmin.WebAdminBean; 57 58 61 public class EJBGenBean extends WebAdminBean 62 { 63 private String psep = System.getProperty("file.separator"); private String oehp = System.getProperty("openejb.home"); private String surl = ""; 66 67 70 public void ejbCreate() 71 { 72 section = "/EJBGenerator/CreateEJB"; 75 } 76 77 82 public void postProcess(HttpRequest request, HttpResponse response) throws IOException 83 { 84 } 85 86 91 public void preProcess(HttpRequest request, HttpResponse response) throws IOException 92 { 93 } 94 95 100 public void writeBody(PrintWriter body) throws IOException 101 { 102 String ejbname = request.getFormParameter("ejbname"); 103 String ejbdesc = request.getFormParameter("ejbdesc"); 104 String ejbauth = request.getFormParameter("ejbauth"); 105 String ejbpack = request.getFormParameter("ejbpack"); 106 String ejbtype = request.getFormParameter("ejbtype"); 107 String ejbsloc = request.getFormParameter("ejbsloc"); 108 String generate = request.getFormParameter("generate"); 109 String ejbstyp = request.getFormParameter("ejbstyp"); 110 boolean error = false; 111 112 if(generate != null) 114 { 115 if(checkSourceDir(ejbsloc)) 116 { 117 error = false; 118 } 119 else 120 { 121 error = true; 122 } 123 124 if(error) 125 { 126 writeGUI(body,true); 127 } 128 else 129 { 130 if(ejbstyp.equals("local")) 131 { 132 surl = ejbsloc + psep; 133 } 134 else 135 { 136 surl = "/BeanSources/"; 137 } 138 139 if(ejbtype.equals("BMP")) 140 { 141 BMPBean newEJB = new BMPBean(ejbname,ejbdesc,ejbauth,ejbpack,ejbsloc,ejbstyp); 142 showBeanFiles(body); 143 System.out.println("EJB Generator has created " + ejbname + "!"); 144 } 145 else if(ejbtype.equals("CMP")) 146 { 147 CMPBean newEJB = new CMPBean(ejbname,ejbdesc,ejbauth,ejbpack,ejbsloc,ejbstyp); 148 showBeanFiles(body); 149 System.out.println("EJB Generator has created " + ejbname + "!"); 150 } 151 else if(ejbtype.equals("Stateful")) 152 { 153 StatefulBean newEJB = new StatefulBean(ejbname,ejbdesc,ejbauth,ejbpack,ejbsloc,ejbstyp); 154 showBeanFiles(body); 155 System.out.println("EJB Generator has created " + ejbname + "!"); 156 } 157 else 158 { 159 StatelessBean newEJB = new StatelessBean(ejbname,ejbdesc,ejbauth,ejbpack,ejbsloc,ejbstyp); 160 showBeanFiles(body); 161 System.out.println("EJB Generator has created " + ejbname + "!"); 162 } 163 164 if(ejbstyp.equals("remote")) 165 { 166 File nsdir = new File (oehp + psep + "beangen" + psep + "EJBGenerator" + psep + "BeanSources"); 167 168 if(!nsdir.exists()) 169 { 170 nsdir.mkdirs(); 171 } 172 173 File nzdir = new File (nsdir,ejbname); 174 175 if(!nzdir.exists()) 176 { 177 nzdir.mkdirs(); 178 } 179 180 FileUtils.copyFile(new File (nzdir, ejbname + ".zip"), 181 new File (ejbsloc + psep + ejbname + psep + ejbname + ".zip")); 182 } 183 } 184 } 185 else 186 { 187 writeGUI(body,false); 188 } 189 } 190 191 199 public void writeHtmlTitle(PrintWriter body) throws IOException 200 { 201 body.print(HTML_TITLE); 202 } 203 204 210 public void writePageTitle(PrintWriter body) throws IOException 211 { 212 body.print("EJB Generator"); 213 } 214 215 235 public void writeSubMenuItems(PrintWriter body) throws IOException 236 { 237 } 238 239 249 private boolean checkSourceDir(String path) 250 { 251 boolean goodDir = false; 252 File srcDir = new File (path); 253 254 if(srcDir.exists() && srcDir.isDirectory() && srcDir.canWrite()) 255 { 256 goodDir = true; 257 } 258 else 259 { 260 goodDir = false; 261 } 262 return goodDir; 263 } 264 265 273 private void writeGUI(PrintWriter body, boolean error) 274 { 275 String ejbname = request.getFormParameter("ejbname"); 276 String ejbdesc = request.getFormParameter("ejbdesc"); 277 String ejbauth = request.getFormParameter("ejbauth"); 278 String ejbpack = request.getFormParameter("ejbpack"); 279 String ejbtype = request.getFormParameter("ejbtype"); 280 String ejbsloc = request.getFormParameter("ejbsloc"); 281 String ejbstyp = request.getFormParameter("ejbstyp"); 282 String generate = request.getFormParameter("generate"); 283 String namehelp = "This is the name of your EJB. It will be appended to all of your source " + 284 "files. For example, if your EJB Name was Foo, your Bean.java file would be " + 285 ""FooBean.java"."; 286 String deschelp = "This has not been implemented yet but it will end up being in your JavaDoc " + 287 "information describing your bean and the bean’s purpose."; 288 String authhelp = "This puts your name in the JavaDoc information."; 289 String packhelp = "This will build the proper package for your EJB. For example. If your package " + 290 " was foo.bar, your .java files for your EJB would be put in the foo/bar/ directory."; 291 String typehelp = "This is to determine which EJB to build for you."; 292 String styphelp = "This is very important as it can keep you from reaching your source if not used " + 293 "properly. If you are accessing the WebAdmin module from http://localhost:4203, you " + 294 "should always select "local". If the server you are accessing has been setup to " + 295 "be accessed remotely the you should select remote."; 296 String slochelp = "This is where your actual files will be created. If you are accessing locally, your " + 297 "directory structure is already setup and there is no need to download the " + 298 "zip file. Just go to your EJB Save Location and you’ll see a folder with your EJB " + 299 "Name. Inside of it will be your META-INF folder and the top level of your EJB’s " + 300 "package."; 301 302 if(ejbname == null) 303 { 304 ejbname = ""; 305 } 306 if(ejbdesc == null) 307 { 308 ejbdesc = ""; 309 } 310 if(ejbauth == null) 311 { 312 ejbauth = ""; 313 } 314 if(ejbpack == null) 315 { 316 ejbpack = ""; 317 } 318 if(ejbtype == null) 319 { 320 ejbtype = ""; 321 } 322 if(ejbsloc == null) 323 { 324 ejbsloc = ""; 325 } 326 if(ejbstyp == null) 327 { 328 ejbstyp = ""; 329 } 330 331 body.println("Welcome to the EJB Generator! This tool will help trim development"); 332 body.println("time by building the skeleton for your EJB. Fill out all "); 333 body.println("information and submit. For a brief explanation, click the column with the name of the field."); 334 body.println("<br>"); 335 body.println("<br>"); 336 body.println("<h2>General EJB Information</h2>"); 337 body.println("<form action=\""+section+"\" method=\"post\" enctype=\"multipart/form-data\" onsubmit=\"return validate(this)\">"); 338 body.println("<table width=\"100%\" border=\"1\">"); 339 body.println("<tr bgcolor=\"#5A5CB8\">"); 340 body.println("<td onclick=\"popupMsg(\'" + namehelp + "\')\"><font face=\"arial\" color=\"white\">EJB Name: </font></td><td>" + 341 " " + 342 HtmlUtilities.createTextFormField("ejbname",ejbname,40,0) + "</td>"); 343 body.println("</tr>"); 344 body.println("<tr bgcolor=\"#5A5CB8\">"); 345 body.println("<td onclick=\"popupMsg(\'" + deschelp + "\')\"><font face=\"arial\" color=\"white\">EJB <br> Description: </font></td><td> " 346 + " " + 347 HtmlUtilities.createTextArea("ejbdesc",ejbdesc,3,30,"","","") + "</td>"); 348 body.println("</tr>"); 349 body.println("<tr bgcolor=\"#5A5CB8\">"); 350 body.println("<td onclick=\"popupMsg(\'" + authhelp + "\')\"><font face=\"arial\" color=\"white\">Author: </font></td><td>" + 351 " " + 352 HtmlUtilities.createTextFormField("ejbauth",ejbauth,40,0) + "</td>"); 353 body.println("</tr>"); 354 body.println("<tr bgcolor=\"#5A5CB8\">"); 355 body.println("<td onclick=\"popupMsg(\'" + packhelp + "\')\"><font face=\"arial\" color=\"white\">Package: </font></td><td>" + 356 " " + 357 HtmlUtilities.createTextFormField("ejbpack",ejbpack,40,0) + "</td>"); 358 body.println("</tr>"); 359 body.println("<tr bgcolor=\"#5A5CB8\">"); 360 body.println("<td onclick=\"popupMsg(\'" + typehelp + "\')\"><font face=\"arial\" color=\"white\">EJB Type: </font></td><td>" + 361 " " + 362 HtmlUtilities.createSelectFormField("ejbtype","")); 363 if(ejbtype.equals("BMP") || ejbtype.equals("")) 364 { 365 body.println(HtmlUtilities.createSelectOption("BMP","Entity (BMP)",true)); 366 body.println(HtmlUtilities.createSelectOption("CMP","Entity (CMP)",false)); 367 body.println(HtmlUtilities.createSelectOption("Stateful","Session (Stateful)",false)); 368 body.println(HtmlUtilities.createSelectOption("Stateless","Session (Stateless)",false)); 369 } 370 else if(ejbtype.equals("CMP")) 371 { 372 body.println(HtmlUtilities.createSelectOption("BMP","Entity (BMP)",false)); 373 body.println(HtmlUtilities.createSelectOption("CMP","Entity (CMP)",true)); 374 body.println(HtmlUtilities.createSelectOption("Stateful","Session (Stateful)",false)); 375 body.println(HtmlUtilities.createSelectOption("Stateless","Session (Stateless)",false)); 376 } 377 else if(ejbtype.equals("Stateful")) 378 { 379 body.println(HtmlUtilities.createSelectOption("BMP","Entity (BMP)",false)); 380 body.println(HtmlUtilities.createSelectOption("CMP","Entity (CMP)",false)); 381 body.println(HtmlUtilities.createSelectOption("Stateful","Session (Stateful)",true)); 382 body.println(HtmlUtilities.createSelectOption("Stateless","Session (Stateless)",false)); 383 } 384 else 385 { 386 body.println(HtmlUtilities.createSelectOption("BMP","Entity (BMP)",false)); 387 body.println(HtmlUtilities.createSelectOption("CMP","Entity (CMP)",false)); 388 body.println(HtmlUtilities.createSelectOption("Stateful","Session (Stateful)",false)); 389 body.println(HtmlUtilities.createSelectOption("Stateless","Session (Stateless)",true)); 390 } 391 body.println("</select> "); 396 body.println(" "); 397 body.println("</td>"); 398 body.println("</tr>"); 399 body.println("<tr bgcolor=\"#5A5CB8\">"); 400 body.println("<td onclick=\"popupMsg(\'" + styphelp + "\')\"><font face=\"arial\" color=\"white\">Access Type:</font></td>"); 401 body.println("<td><center><font face=\"arial\" color=\"white\">"); 402 body.println("Local " + HtmlUtilities.createInputFormField("radio","ejbstyp","local",0,0,"","","","",true,false,true) + " "); 403 body.println("Remote " + HtmlUtilities.createInputFormField("radio","ejbstyp","remote",0,0,"","","","",false,false,true)); 404 body.println("</font></center></td>"); 405 body.println("<tr bgcolor=\"#5A5CB8\">"); 406 body.println("<td onclick=\"popupMsg(\'" + slochelp + "\')\"><font face=\"arial\" color=\"white\">Source<br>Location: </font></td><td>" + 407 " " + 408 HtmlUtilities.createTextFormField("ejbsloc",ejbsloc,40,0) + "</td>"); 409 body.println("</tr>"); 410 if(error) 411 { 412 body.println("<tr bgcolor=\"#5A5CB8\">"); 413 body.println("<td colspan=\"2\"><font face=\"arial\" color=\"red\"><strong><center>Please enter a valid Source Location!</center></strong></font></td></tr>"); 414 } 415 body.println("<tr bgcolor=\"#5A5CB8\">"); 416 body.println("<td colspan=\"2\"><center>" + HtmlUtilities.createSubmitFormButton("generate","Generate") + "</center></td>"); 417 body.println("</tr>"); 418 body.println("</table>"); 419 body.println("</form>"); 420 } 421 422 427 public String getBeanDir() 428 { 429 StringTokenizer dirs = new StringTokenizer (request.getFormParameter("ejbpack"),"\\."); 431 String beandir = request.getFormParameter("ejbsloc") + psep + request.getFormParameter("ejbname"); 432 433 438 while (dirs.hasMoreTokens()) 439 { 440 beandir = beandir + psep + dirs.nextToken(); 441 } 442 443 444 return beandir; 446 } 447 448 454 public String getBeanURL() 455 { 456 StringTokenizer dirs = new StringTokenizer (request.getFormParameter("ejbpack"),"\\."); 458 String beanurl = request.getFormParameter("ejbname"); 459 460 465 while (dirs.hasMoreTokens()) 466 { 467 beanurl = beanurl + "/" + dirs.nextToken(); 468 } 469 470 return beanurl; 472 } 473 474 478 public void showBeanFiles(PrintWriter body) 479 { 480 String beanstr = getBeanDir(); 481 String beanurl = getBeanURL(); 482 File beanDir = new File (beanstr); 483 File metaDir = new File (request.getFormParameter("ejbsloc") + psep + request.getFormParameter("ejbname") + 484 psep + "META-INF"); 485 File [] beanFiles = beanDir.listFiles(); 486 File [] metaFiles = metaDir.listFiles(); 487 488 body.println("<h1>Your Bean Skeleton Has Been Created</h1><br>"); 489 body.println("If you are accessing this tool remotely, where you "); 490 body.println("aren\'t accessing it from the server running this instance of OpenEJB, "); 491 body.println("the links are provided so you can copy and paste the source to your local "); 492 body.println("computer. Otherwise, your full directory structure for your EJB is in tact "); 493 body.println("and you can edit the sources with your favorite editor!<br><br>"); 494 body.println("<table width=\"50%\" border=\"1\" align=\"center\">"); 495 body.println("<tr bgcolor=\"#5A5CB8\">"); 496 body.println("<td><center><font face=\"arial\" color=\"white\">Bean Files</font></center></td>"); 497 body.println("</tr>"); 498 514 530 535 body.println("<tr bgcolor=\"#ffffff\">"); 536 537 if(request.getFormParameter("ejbstyp").equals("remote")) 538 { 539 body.println("<td><center><font face=\"arial\" color=\"white\"><center>" + 540 "<a HREF=\"" + request.getURI().getFile() + surl + request.getFormParameter("ejbname") + "/" + 541 request.getFormParameter("ejbname") + ".zip" + "\">" + 542 request.getFormParameter("ejbname") + ".zip" + "</a></center></font></center></td>"); 543 } 544 else 545 { 546 body.println("<td><center><font face=\"arial\" color=\"white\"><center>" + 547 "<a HREF=\"" + request.getFormParameter("ejbsloc") + "/" + request.getFormParameter("ejbname") + 548 "/" + request.getFormParameter("ejbname") + ".zip" + "\">" + 549 request.getFormParameter("ejbname") + ".zip" + "</a></center></font></center></td>"); 550 } 551 552 body.println("</tr>"); 553 body.println("</table>"); 554 body.println("<br>"); 555 body.println("As you can see, there are all the source files needed for your skeleton."); 556 body.println(" To save time, a .zip file has been created also to give you the ability"); 557 body.println(" to extract the files and the proper directory structure to fit your EJB "); 558 body.println("and it's packaging!"); 559 } 560 } 561 | Popular Tags |