1 45 package org.openejb.webadmin.main; 46 47 import java.io.File ; 48 import java.io.FileInputStream ; 49 import java.io.FileOutputStream ; 50 import java.io.IOException ; 51 import java.io.ObjectInputStream ; 52 import java.io.ObjectOutputStream ; 53 import java.io.PrintWriter ; 54 import java.lang.reflect.UndeclaredThrowableException ; 55 import java.util.Properties ; 56 import java.util.StringTokenizer ; 57 58 import javax.ejb.Handle ; 59 import javax.naming.Context ; 60 import javax.naming.InitialContext ; 61 import javax.rmi.PortableRemoteObject ; 62 63 import org.openejb.webadmin.HttpRequest; 64 import org.openejb.webadmin.HttpResponse; 65 import org.openejb.webadmin.WebAdminBean; 66 import org.openejb.util.FileUtils; 67 import org.openejb.util.HtmlUtilities; 68 import org.openejb.util.Logger; 69 70 82 public class DeployBean extends WebAdminBean { 83 private static final String HANDLE_FILE = System.getProperty("file.separator") + "deployerHandle.obj"; 84 private DeployerObject deployer = null; 85 private Logger logger = Logger.getInstance("OpenEJB", "org.openejb.util.resources"); 86 87 96 private boolean[] options = new boolean[7]; 97 98 99 public void ejbCreate() { 100 this.section = "Deployment"; 101 } 102 103 109 public void postProcess(HttpRequest request, HttpResponse response) throws IOException {} 110 111 117 public void preProcess(HttpRequest request, HttpResponse response) throws IOException {} 118 119 125 public void writeBody(PrintWriter body) throws IOException { 126 String deploy = request.getFormParameter("deploy"); 127 String submitDeployment = request.getFormParameter("submitDeploymentAndContainerIds"); 128 129 try { 130 if (deploy != null) { 132 String deployerHandleString = createDeployerHandle(); 133 setOptions(); 134 deployer.startDeployment(); 135 body.println( 136 "Below is a list of beans in the jar which you have chosen to deploy. Some of the methods in your beans may require " 137 + "OQL statements. If so, form fields will be displayed for the methods which require these statements. " 138 + "In this case <b>OQL statements are required.</b> " 139 + "Please enter the information requested in the form fields and click \"Continue >>\" " 140 + "to continue.<br>"); 141 body.println("<form action=\"Deployment\" method=\"post\" onSubmit=\"return checkDeployValues(this)\">"); 142 body.print(deployer.createIdTable()); 143 body.println(HtmlUtilities.createHiddenFormField("deployerHandle", deployerHandleString)); 144 body.println("</form>"); 145 } else if (submitDeployment != null) { 146 deployPartTwo(body); 147 } else { 148 writeForm(body); 149 } 150 } catch (Exception e) { 151 handleException(e, body); 153 } 154 } 155 156 157 private void handleException(Exception e, PrintWriter body) { 158 if (e instanceof UndeclaredThrowableException ) { 159 UndeclaredThrowableException ue = (UndeclaredThrowableException ) e; 160 Throwable t = ue.getUndeclaredThrowable(); 161 if (t != null) { 162 body.println(t.getMessage()); 163 logger.error("Error on web deployment", t); 164 } else { 165 body.println("An unknown system error occured."); 166 } 167 } else { 168 if (e != null) { 169 body.println(e.getMessage()); 170 logger.error("Error on web deployment", e); 171 } else { 172 body.println("An unknown system error occured."); 173 } 174 } 175 } 176 177 180 private void deployPartTwo(PrintWriter body) throws Exception { 181 ReferenceData[] referenceDataArray; 182 OQLData[] oqlDataArray; 183 StringTokenizer oqlParameterToken; 184 185 String deployerHandleString = request.getFormParameter("deployerHandle"); 186 getDeployerHandle(deployerHandleString); DeployData[] deployDataArray = deployer.getDeployDataArray(); 188 189 for (int i = 0; i < deployDataArray.length; i++) { 191 deployDataArray[i].setContainerIdValue(request.getFormParameter(deployDataArray[i].getContainerIdName())); 193 deployDataArray[i].setDeploymentIdValue(request.getFormParameter(deployDataArray[i].getDeploymentIdName())); 194 195 referenceDataArray = deployDataArray[i].getReferenceDataArray(); 197 for (int j = 0; j < referenceDataArray.length; j++) { 198 referenceDataArray[j].setReferenceIdValue( 199 request.getFormParameter(referenceDataArray[j].getReferenceIdName())); 200 referenceDataArray[j].setReferenceValue(request.getFormParameter(referenceDataArray[j].getReferenceName())); 201 } 202 203 oqlDataArray = deployDataArray[i].getOqlDataArray(); 205 for (int j = 0; j < oqlDataArray.length; j++) { 206 oqlDataArray[j].setOqlStatementValue(request.getFormParameter(oqlDataArray[j].getOqlStatementName())); 207 208 if (request.getFormParameter(oqlDataArray[j].getOqlParameterName()) != null) { 210 oqlParameterToken = 211 new StringTokenizer (request.getFormParameter(oqlDataArray[j].getOqlParameterName()), ","); 212 while (oqlParameterToken.hasMoreTokens()) { 214 oqlDataArray[j].getOqlParameterValueList().add(oqlParameterToken.nextToken()); 215 } 216 217 } 218 } 219 } 220 221 deployer.setDeployAndContainerIds(deployDataArray); 222 223 body.println( 225 "You jar is now deployed. If you chose to move or copy your jar" 226 + "from it's original location, you will now find it in: " 227 + System.getProperty("openejb.home") 228 + System.getProperty("file.separator") 229 + "beans. You will need to restart OpenEJB for this " 230 + "deployment to take affect. Once you restart, you should see your bean(s) in the " 231 + HtmlUtilities.createAnchor("DeploymentList", "list of beans", HtmlUtilities.ANCHOR_HREF_TYPE) 232 + "on this console. Below is a table of " 233 + "the bean(s) you deployed.<br><br>"); 234 235 printDeploymentHtml(body); 236 deployer.remove(); 237 } 238 239 240 private void printDeploymentHtml(PrintWriter body) throws Exception { 241 deployer.finishDeployment(); 242 body.println("<table cellspacing=\"1\" cellpadding=\"2\" border=\"1\">\n"); 243 body.println("<tr align=\"left\">\n"); 244 body.println("<th>Bean Name</th>\n"); 245 body.println("<th>Deployment Id</th>\n"); 246 body.println("<th>Container Id</th>\n"); 247 body.println("<th>Resource References</th>\n"); 248 body.println("</tr>\n"); 249 body.println(deployer.getDeploymentHTML()); 250 body.println("</table>"); 251 } 252 253 254 private String createDeployerHandle() throws Exception { 255 Properties p = new Properties (); 256 p.put(Context.INITIAL_CONTEXT_FACTORY, "org.openejb.core.ivm.naming.InitContextFactory"); 257 258 InitialContext ctx = new InitialContext (p); 260 Object obj = ctx.lookup("deploy/webadmin/Deployer"); 261 DeployerHome home = (DeployerHome) PortableRemoteObject.narrow(obj, DeployerHome.class); 263 deployer = home.create(); 264 265 Handle deployerHandle = deployer.getHandle(); 267 268 File myHandleFile = new File (FileUtils.createTempDirectory().getAbsolutePath() + HANDLE_FILE); 270 if (!myHandleFile.exists()) { 271 myHandleFile.createNewFile(); 272 } 273 274 ObjectOutputStream objectOut = new ObjectOutputStream (new FileOutputStream (myHandleFile)); 275 objectOut.writeObject(deployerHandle); objectOut.flush(); 277 objectOut.close(); 278 279 return myHandleFile.getAbsolutePath(); 280 } 281 282 283 private void getDeployerHandle(String handleFile) throws Exception { 284 File myHandleFile = new File (handleFile); 285 286 ObjectInputStream objectIn = new ObjectInputStream (new FileInputStream (myHandleFile)); 288 Handle deployerHandle = (Handle ) objectIn.readObject(); 290 this.deployer = (DeployerObject) deployerHandle.getEJBObject(); 291 } 292 293 294 private void setOptions() throws Exception { 295 String jarFile = request.getFormParameter("jarFile"); 297 String force = request.getFormParameter("force"); 298 File testForValidFile = null; 299 300 if (jarFile == null) { 301 throw new IOException ("No jar file was provided, please try again."); 303 } 304 this.deployer.setJarFile(jarFile); 306 307 if (force != null) { 309 options[2] = true; 310 } 311 312 testForValidFile = null; 313 this.deployer.setBooleanValues(options); 314 } 315 316 320 private void writeForm(PrintWriter body) throws IOException { 321 body.println( 323 "<form action=\"Deployment\" method=\"post\" enctype=\"multipart/form-data\" onsubmit=\"return checkDeploy(this)\">"); 324 body.println("<table border=\"0\" cellspacing=\"0\" cellpadding=\"2\">"); 326 327 339 340 body.println("<tr>\n<td colspan=\"2\">"); 342 body.println("<strong>Step 1:</strong> Browse your file system and select your bean to deploy."); 343 body.println("</td>\n</tr>\n<tr>\n<td colspan=\"2\"> </td>\n</tr>"); 344 345 body.println("<tr>"); 347 body.println("<td><nobr>Jar File</nobr></td>\n<td>"); 348 body.println(HtmlUtilities.createFileFormField("jarFile", "", 35)); 349 body.println("</td>\n</tr>\n<tr>\n<td colspan=\"2\"> </td>\n</tr>"); 350 351 354 357 369 370 body.println("<tr>\n<td colspan=\"2\"> </td>\n</tr>"); 381 body.println("<tr><td colspan=\"2\">"); 382 body.println(HtmlUtilities.createSubmitFormButton("deploy", "Deploy")); 383 body.println("</td>\n</tr>"); 384 385 390 body.println("</table>"); 391 body.println(HtmlUtilities.createHiddenFormField("handleFile", "")); 393 body.println("</form>"); 394 } 395 396 404 public void writeHtmlTitle(PrintWriter body) throws IOException { 405 body.println(HTML_TITLE); 406 } 407 408 415 public void writePageTitle(PrintWriter body) throws IOException { 416 body.println("EJB Deployment"); 417 } 418 419 439 public void writeSubMenuItems(PrintWriter body) throws IOException {} 440 } | Popular Tags |