1 16 17 package org.apache.webapp.admin.context; 18 19 20 import java.net.URLEncoder ; 21 import java.util.Locale ; 22 import java.io.IOException ; 23 import javax.management.Attribute ; 24 import javax.management.MBeanServer ; 25 import javax.management.ObjectName ; 26 import javax.servlet.ServletException ; 27 import javax.servlet.http.HttpServletRequest ; 28 import javax.servlet.http.HttpServletResponse ; 29 import javax.servlet.http.HttpSession ; 30 import org.apache.commons.modeler.Registry; 31 import org.apache.struts.action.Action; 32 import org.apache.struts.action.ActionError; 33 import org.apache.struts.action.ActionErrors; 34 import org.apache.struts.action.ActionForm; 35 import org.apache.struts.action.ActionForward; 36 import org.apache.struts.action.ActionMapping; 37 import org.apache.struts.util.MessageResources; 38 import org.apache.webapp.admin.ApplicationServlet; 39 import org.apache.webapp.admin.TomcatTreeBuilder; 40 import org.apache.webapp.admin.TreeControl; 41 import org.apache.webapp.admin.TreeControlNode; 42 43 44 45 52 53 public final class SaveContextAction extends Action { 54 55 56 58 61 private String createStandardContextTypes[] = 62 { "java.lang.String", "java.lang.String", "java.lang.String", }; 66 67 70 private String createStandardLoaderTypes[] = 71 { "java.lang.String", }; 73 74 77 private String createStandardManagerTypes[] = 78 { "java.lang.String", }; 80 81 84 private String removeContextTypes[] = 85 { "java.lang.String", }; 87 88 91 private MBeanServer mBServer = null; 92 93 94 96 97 112 public ActionForward execute(ActionMapping mapping, 113 ActionForm form, 114 HttpServletRequest request, 115 HttpServletResponse response) 116 throws IOException , ServletException { 117 118 HttpSession session = request.getSession(); 120 Locale locale = getLocale(request); 121 MessageResources resources = getResources(request); 122 123 try { 125 mBServer = ((ApplicationServlet) getServlet()).getServer(); 126 } catch (Throwable t) { 127 throw new ServletException 128 ("Cannot acquire MBeanServer reference", t); 129 } 130 131 ContextForm cform = (ContextForm) form; 133 String adminAction = cform.getAdminAction(); 134 String cObjectName = cform.getObjectName(); 135 String lObjectName = cform.getLoaderObjectName(); 136 String mObjectName = cform.getManagerObjectName(); 137 if ((cform.getPath() == null) || (cform.getPath().length()<1)) { 138 cform.setPath("/"); 139 } 140 141 if ("Create".equals(adminAction)) { 143 144 String operation = null; 145 Object values[] = null; 146 147 try { 148 String parentName = cform.getParentObjectName(); 150 ObjectName honame = new ObjectName (parentName); 151 152 ObjectName oname = 154 new ObjectName (honame.getDomain() + 155 ":j2eeType=WebModule,name=//" + 156 honame.getKeyProperty("host") + 157 cform.getPath() + 158 ",J2EEApplication=none,J2EEServer=none"); 160 161 if (mBServer.isRegistered(oname)) { 162 ActionErrors errors = new ActionErrors(); 163 errors.add("contextName", 164 new ActionError("error.contextName.exists")); 165 saveErrors(request, errors); 166 return (new ActionForward(mapping.getInput())); 167 } 168 169 ObjectName fname = 171 TomcatTreeBuilder.getMBeanFactory(); 172 173 values = new Object [3]; 175 values[0] = parentName; 176 values[1] = cform.getPath(); 177 values[2] = cform.getDocBase(); 178 179 operation = "createStandardContext"; 180 cObjectName = (String ) 181 mBServer.invoke(fname, operation, 182 values, createStandardContextTypes); 183 values = new String [1]; 185 values[0] = cObjectName.toString(); 187 operation = "createWebappLoader"; 188 lObjectName = (String ) 189 mBServer.invoke(fname, operation, 190 values, createStandardLoaderTypes); 191 192 values = new String [1]; 194 values[0] = cObjectName.toString(); 196 operation = "createStandardManager"; 197 mObjectName = (String ) 198 mBServer.invoke(fname, operation, 199 values, createStandardManagerTypes); 200 201 if (mObjectName==null) { 202 operation = "removeLoader"; 203 values[0] = lObjectName; 204 mBServer.invoke(fname, operation, values, 205 removeContextTypes); 206 operation = "removeContext"; 207 values[0] = cObjectName; 208 mBServer.invoke(fname, operation, values, 209 removeContextTypes); 210 Registry.getRegistry().unregisterComponent(new ObjectName (cObjectName)); 211 request.setAttribute("warning", "error.context.directory"); 212 return (mapping.findForward("Save Unsuccessful")); 213 } 214 215 addToTreeControlNode(oname, cObjectName, parentName, 217 resources, session, locale); 218 219 } catch (Exception e) { 220 getServlet().log 221 (resources.getMessage(locale, "users.error.invoke", 222 operation), e); 223 response.sendError 224 (HttpServletResponse.SC_INTERNAL_SERVER_ERROR, 225 resources.getMessage(locale, "users.error.invoke", 226 operation)); 227 return (null); 228 229 } 230 231 } 232 233 String attribute = null; 235 try { 236 237 ObjectName coname = new ObjectName (cObjectName); 238 ObjectName loname = new ObjectName (lObjectName); 239 ObjectName moname = new ObjectName (mObjectName); 240 241 attribute = "path"; 242 String path = ""; 243 try { 244 path = cform.getPath(); 245 } catch (Throwable t) { 246 path = ""; 247 } 248 mBServer.setAttribute(coname, 249 new Attribute ("path", path)); 250 251 attribute = "workDir"; 252 String workDir = ""; 253 workDir = cform.getWorkDir(); 254 if ((workDir!=null) && (workDir.length()>=1)) { 255 mBServer.setAttribute(coname, 256 new Attribute ("workDir", workDir)); 257 } 258 259 attribute = "cookies"; 260 String cookies = "false"; 261 try { 262 cookies = cform.getCookies(); 263 } catch (Throwable t) { 264 cookies = "false"; 265 } 266 mBServer.setAttribute(coname, 267 new Attribute ("cookies", new Boolean (cookies))); 268 269 attribute = "crossContext"; 270 String crossContext = "false"; 271 try { 272 crossContext = cform.getCrossContext(); 273 } catch (Throwable t) { 274 crossContext = "false"; 275 } 276 mBServer.setAttribute(coname, 277 new Attribute ("crossContext", new Boolean (crossContext))); 278 279 attribute = "override"; 280 String override = "false"; 281 try { 282 override = cform.getOverride(); 283 } catch (Throwable t) { 284 override = "false"; 285 } 286 mBServer.setAttribute(coname, 287 new Attribute ("override", new Boolean (override))); 288 289 attribute = "privileged"; 290 String privileged = "false"; 291 try { 292 privileged = cform.getPrivileged(); 293 } catch (Throwable t) { 294 privileged = "false"; 295 } 296 mBServer.setAttribute(coname, 297 new Attribute ("privileged", new Boolean (privileged))); 298 299 attribute = "reloadable"; 300 String reloadable = "false"; 301 try { 302 reloadable = cform.getReloadable(); 303 } catch (Throwable t) { 304 reloadable = "false"; 305 } 306 mBServer.setAttribute(coname, 307 new Attribute ("reloadable", new Boolean (reloadable))); 308 309 attribute = "swallowOutput"; 310 String swallowOutput = "false"; 311 try { 312 swallowOutput = cform.getSwallowOutput(); 313 } catch (Throwable t) { 314 swallowOutput = "false"; 315 } 316 mBServer.setAttribute(coname, 317 new Attribute ("swallowOutput", new Boolean (swallowOutput))); 318 319 attribute = "useNaming"; 320 String useNaming = "false"; 321 try { 322 useNaming = cform.getUseNaming(); 323 } catch (Throwable t) { 324 useNaming = "false"; 325 } 326 mBServer.setAttribute(coname, 327 new Attribute ("useNaming", new Boolean (useNaming))); 328 329 attribute = "antiJARLocking"; 330 String antiJarLocking = cform.getAntiJarLocking(); 331 mBServer.setAttribute(coname, 332 new Attribute ("antiJARLocking", new Boolean (antiJarLocking))); 333 334 attribute = "antiResourceLocking"; 335 String antiResourceLocking = cform.getAntiResourceLocking(); 336 mBServer.setAttribute(coname, 337 new Attribute ("antiResourceLocking", new Boolean (antiResourceLocking))); 338 339 340 attribute = "reloadable"; 342 try { 343 reloadable = cform.getLdrReloadable(); 344 } catch (Throwable t) { 345 reloadable = "false"; 346 } 347 mBServer.setAttribute(loname, 348 new Attribute ("reloadable", new Boolean (reloadable))); 349 350 360 attribute = "entropy"; 362 String entropy = cform.getMgrSessionIDInit(); 363 if ((entropy!=null) && (entropy.length()>=1)) { 364 mBServer.setAttribute(moname, 365 new Attribute ("entropy",entropy)); 366 } 367 368 377 attribute = "maxActiveSessions"; 378 int maxActiveSessions = -1; 379 try { 380 maxActiveSessions = Integer.parseInt(cform.getMgrMaxSessions()); 381 } catch (Throwable t) { 382 maxActiveSessions = -1; 383 } 384 mBServer.setAttribute(moname, 385 new Attribute ("maxActiveSessions", new Integer (maxActiveSessions))); 386 387 } catch (Exception e) { 388 389 getServlet().log 390 (resources.getMessage(locale, "users.error.attribute.set", 391 attribute), e); 392 response.sendError 393 (HttpServletResponse.SC_INTERNAL_SERVER_ERROR, 394 resources.getMessage(locale, "users.error.attribute.set", 395 attribute)); 396 return (null); 397 } 398 399 session.removeAttribute(mapping.getAttribute()); 401 return (mapping.findForward("Save Successful")); 402 403 } 404 405 406 414 public void addToTreeControlNode(ObjectName oname, String containerName, 415 String parentName, MessageResources resources, 416 HttpSession session, Locale locale) 417 throws Exception { 418 419 String domain = oname.getDomain(); 420 TreeControl control = (TreeControl) session.getAttribute("treeControlTest"); 421 if (control != null) { 422 TreeControlNode parentNode = control.findNode(parentName); 423 if (parentNode != null) { 424 String type = "Context"; 425 String path = ""; 426 String host = ""; 427 String name = oname.getKeyProperty("name"); 428 if ((name != null) && (name.length() > 0)) { 429 name = name.substring(2); 430 int i = name.indexOf("/"); 431 host = name.substring(0,i); 432 path = name.substring(i); 433 } 434 String nodeLabel = 435 resources.getMessage(locale, "server.service.treeBuilder.context") + 436 " (" + path + ")"; 437 String encodedName = URLEncoder.encode(oname.toString(),TomcatTreeBuilder.URL_ENCODING); 438 TreeControlNode childNode = 439 new TreeControlNode(oname.toString(), 440 "Context.gif", 441 nodeLabel, 442 "EditContext.do?select=" + 443 encodedName, 444 "content", 445 true, domain); 446 parentNode.addChild(childNode); 447 448 TreeControlNode subtree = new TreeControlNode 450 ("Context Resource Administration " + containerName, 451 "folder_16_pad.gif", 452 resources.getMessage(locale, "resources.treeBuilder.subtreeNode"), 453 null, 454 "content", 455 true, domain); 456 childNode.addChild(subtree); 457 TreeControlNode datasources = new TreeControlNode 458 ("Context Data Sources " + containerName, 459 "Datasource.gif", 460 resources.getMessage(locale, "resources.treeBuilder.datasources"), 461 "resources/listDataSources.do?resourcetype=" + 462 URLEncoder.encode(type,TomcatTreeBuilder.URL_ENCODING) + "&path=" + 463 URLEncoder.encode(path,TomcatTreeBuilder.URL_ENCODING) + "&host=" + 464 URLEncoder.encode(host,TomcatTreeBuilder.URL_ENCODING) + "&forward=" + 465 URLEncoder.encode("DataSources List Setup",TomcatTreeBuilder.URL_ENCODING), 466 "content", 467 false, domain); 468 TreeControlNode mailsessions = new TreeControlNode 469 ("Context Mail Sessions " + containerName, 470 "Mailsession.gif", 471 resources.getMessage(locale, "resources.treeBuilder.mailsessions"), 472 "resources/listMailSessions.do?resourcetype=" + 473 URLEncoder.encode(type,TomcatTreeBuilder.URL_ENCODING) + "&path=" + 474 URLEncoder.encode(path,TomcatTreeBuilder.URL_ENCODING) + "&host=" + 475 URLEncoder.encode(host,TomcatTreeBuilder.URL_ENCODING) + "&forward=" + 476 URLEncoder.encode("MailSessions List Setup",TomcatTreeBuilder.URL_ENCODING), 477 "content", 478 false, domain); 479 TreeControlNode resourcelinks = new TreeControlNode 480 ("Resource Links " + containerName, 481 "ResourceLink.gif", 482 resources.getMessage(locale, "resources.treeBuilder.resourcelinks"), 483 "resources/listResourceLinks.do?resourcetype=" + 484 URLEncoder.encode(type,TomcatTreeBuilder.URL_ENCODING) + "&path=" + 485 URLEncoder.encode(path,TomcatTreeBuilder.URL_ENCODING) + "&host=" + 486 URLEncoder.encode(host,TomcatTreeBuilder.URL_ENCODING) + "&forward=" + 487 URLEncoder.encode("ResourceLinks List Setup",TomcatTreeBuilder.URL_ENCODING), 488 "content", 489 false, domain); 490 TreeControlNode envs = new TreeControlNode 491 ("Context Environment Entries "+ containerName, 492 "EnvironmentEntries.gif", 493 resources.getMessage(locale ,"resources.env.entries"), 494 "resources/listEnvEntries.do?resourcetype=" + 495 URLEncoder.encode(type,TomcatTreeBuilder.URL_ENCODING) + "&path=" + 496 URLEncoder.encode(path,TomcatTreeBuilder.URL_ENCODING) + "&host=" + 497 URLEncoder.encode(host,TomcatTreeBuilder.URL_ENCODING) + "&forward=" + 498 URLEncoder.encode("EnvEntries List Setup",TomcatTreeBuilder.URL_ENCODING), 499 "content", 500 false, domain); 501 subtree.addChild(datasources); 502 subtree.addChild(mailsessions); 503 subtree.addChild(resourcelinks); 504 subtree.addChild(envs); 505 } else { 506 getServlet().log 507 ("Cannot find parent node '" + parentName + "'"); 508 } 509 }else { 510 getServlet().log("Cannot find TreeControlNode!"); 511 } 512 } 513 514 } 515 | Popular Tags |