1 16 17 package org.apache.webapp.admin.connector; 18 19 import java.net.URLEncoder ; 20 import java.util.Iterator ; 21 import java.util.Locale ; 22 import java.io.IOException ; 23 import javax.management.Attribute ; 24 import javax.management.MBeanServer ; 25 import javax.management.MBeanServerFactory ; 26 import javax.management.QueryExp ; 27 import javax.management.Query ; 28 import javax.management.ObjectInstance ; 29 import javax.management.ObjectName ; 30 import javax.management.JMException ; 31 import javax.servlet.ServletException ; 32 import javax.servlet.http.HttpServletRequest ; 33 import javax.servlet.http.HttpServletResponse ; 34 import javax.servlet.http.HttpSession ; 35 import org.apache.struts.action.Action; 36 import org.apache.struts.action.ActionError; 37 import org.apache.struts.action.ActionErrors; 38 import org.apache.struts.action.ActionForm; 39 import org.apache.struts.action.ActionForward; 40 import org.apache.struts.action.ActionMapping; 41 import org.apache.struts.util.MessageResources; 42 import org.apache.webapp.admin.ApplicationServlet; 43 import org.apache.webapp.admin.TomcatTreeBuilder; 44 import org.apache.webapp.admin.TreeControl; 45 import org.apache.webapp.admin.TreeControlNode; 46 47 48 55 56 public final class SaveConnectorAction extends Action { 57 58 59 61 64 private String createStandardConnectorTypes[] = 65 { "java.lang.String", "java.lang.String", "int" }; 69 70 73 private MBeanServer mBServer = null; 74 75 77 78 93 public ActionForward execute(ActionMapping mapping, 94 ActionForm form, 95 HttpServletRequest request, 96 HttpServletResponse response) 97 throws IOException , ServletException { 98 99 HttpSession session = request.getSession(); 101 Locale locale = getLocale(request); 102 MessageResources resources = getResources(request); 103 104 try { 106 mBServer = ((ApplicationServlet) getServlet()).getServer(); 107 } catch (Throwable t) { 108 throw new ServletException 109 ("Cannot acquire MBeanServer reference", t); 110 } 111 112 ConnectorForm cform = (ConnectorForm) form; 114 String adminAction = cform.getAdminAction(); 115 String cObjectName = cform.getObjectName(); 116 String connectorType = cform.getConnectorType(); 117 ObjectName coname = null; 118 119 if ("Create".equals(adminAction)) { 121 122 String operation = null; 123 Object values[] = null; 124 125 try { 126 String serviceName = cform.getServiceName(); 128 ObjectName soname = new ObjectName (serviceName); 129 String domain = soname.getDomain(); 130 StringBuffer sb = new StringBuffer (domain); 131 StringBuffer searchSB = new StringBuffer ("*"); 132 sb.append(TomcatTreeBuilder.CONNECTOR_TYPE); 133 searchSB.append(TomcatTreeBuilder.CONNECTOR_TYPE); 134 sb.append(",port=" + cform.getPortText()); 135 searchSB.append(",port=" + cform.getPortText()); 136 137 ObjectName search = new ObjectName (searchSB.toString()+",*"); 138 139 String address = cform.getAddress(); 140 if ((address!=null) && (address.length()>0) && 141 (!address.equalsIgnoreCase(" "))) { 142 sb.append(",address=" + address); 143 } else { 144 address = null; 145 } 146 ObjectName oname = new ObjectName (sb.toString()); 147 148 if (mBServer.isRegistered(oname) || 150 (!mBServer.queryNames(search, null).isEmpty())) { 151 ActionErrors errors = new ActionErrors(); 152 errors.add("connectorName", 153 new ActionError("error.connectorName.exists")); 154 saveErrors(request, errors); 155 return (new ActionForward(mapping.getInput())); 156 } 157 158 ObjectName fname = TomcatTreeBuilder.getMBeanFactory(); 160 161 values = new Object [3]; 163 values[0] = serviceName; values[1] = address; 165 values[2] = new Integer (cform.getPortText()); 166 167 if ("HTTP".equalsIgnoreCase(connectorType)) { 168 operation = "createHttpConnector"; } else if ("HTTPS".equalsIgnoreCase(connectorType)) { 170 operation = "createHttpsConnector"; } else { 172 operation = "createAjpConnector"; } 174 175 cObjectName = (String ) 176 mBServer.invoke(fname, operation, 177 values, createStandardConnectorTypes); 178 179 TreeControl control = (TreeControl) 181 session.getAttribute("treeControlTest"); 182 if (control != null) { 183 String parentName = serviceName; 184 TreeControlNode parentNode = control.findNode(parentName); 185 if (parentNode != null) { 186 String nodeLabel = resources.getMessage(locale, 187 "server.service.treeBuilder.connector") + " (" + 188 cform.getPortText() + ")"; 189 String encodedName = 190 URLEncoder.encode(cObjectName,TomcatTreeBuilder.URL_ENCODING); 191 TreeControlNode childNode = 192 new TreeControlNode(cObjectName, 193 "Connector.gif", 194 nodeLabel, 195 "EditConnector.do?select=" + 196 encodedName, 197 "content", 198 true, domain); 199 parentNode.addChild(childNode); 202 } else { 204 getServlet().log 205 ("Cannot find parent node '" + parentName + "'"); 206 } 207 } else { 208 getServlet().log 209 ("Cannot find TreeControlNode!"); 210 } 211 212 } catch (Exception e) { 213 214 getServlet().log 215 (resources.getMessage(locale, "users.error.invoke", 216 operation), e); 217 response.sendError 218 (HttpServletResponse.SC_INTERNAL_SERVER_ERROR, 219 resources.getMessage(locale, "users.error.invoke", 220 operation)); 221 return (null); 222 223 } 224 225 } 226 227 String attribute = null; 229 try { 230 231 coname = new ObjectName (cObjectName); 232 233 attribute = "acceptCount"; 234 int acceptCount = 60000; 235 try { 236 acceptCount = Integer.parseInt(cform.getAcceptCountText()); 237 } catch (Throwable t) { 238 acceptCount = 60000; 239 } 240 mBServer.setAttribute(coname, 241 new Attribute ("acceptCount", new Integer (acceptCount))); 242 attribute = "compression"; 243 String compression = cform.getCompression(); 244 if ((compression != null) && (compression.length()>0)) { 245 mBServer.setAttribute(coname, 246 new Attribute ("compression", compression)); 247 } 248 attribute = "connectionLinger"; 249 int connectionLinger = -1; 250 try { 251 connectionLinger = Integer.parseInt(cform.getConnLingerText()); 252 } catch (Throwable t) { 253 connectionLinger = 0; 254 } 255 mBServer.setAttribute(coname, 256 new Attribute ("connectionLinger", new Integer (connectionLinger))); 257 attribute = "connectionTimeout"; 258 int connectionTimeout = 0; 259 try { 260 connectionTimeout = Integer.parseInt(cform.getConnTimeOutText()); 261 } catch (Throwable t) { 262 connectionTimeout = 0; 263 } 264 mBServer.setAttribute(coname, 265 new Attribute ("connectionTimeout", new Integer (connectionTimeout))); 266 attribute = "connectionUploadTimeout"; 267 int connectionUploadTimeout = 0; 268 try { 269 connectionUploadTimeout = Integer.parseInt(cform.getConnUploadTimeOutText()); 270 } catch (Throwable t) { 271 connectionUploadTimeout = 0; 272 } 273 mBServer.setAttribute(coname, 274 new Attribute ("connectionUploadTimeout", new Integer (connectionUploadTimeout))); 275 attribute = "bufferSize"; 276 int bufferSize = 2048; 277 try { 278 bufferSize = Integer.parseInt(cform.getBufferSizeText()); 279 } catch (Throwable t) { 280 bufferSize = 2048; 281 } 282 mBServer.setAttribute(coname, 283 new Attribute ("bufferSize", new Integer (bufferSize))); 284 attribute = "disableUploadTimeout"; 285 mBServer.setAttribute(coname, 286 new Attribute ("disableUploadTimeout", new Boolean (cform.getDisableUploadTimeout()))); 287 attribute = "enableLookups"; 288 mBServer.setAttribute(coname, 289 new Attribute ("enableLookups", new Boolean (cform.getEnableLookups()))); 290 291 attribute = "redirectPort"; 292 int redirectPort = 0; 293 try { 294 redirectPort = Integer.parseInt(cform.getRedirectPortText()); 295 } catch (Throwable t) { 296 redirectPort = 0; 297 } 298 mBServer.setAttribute(coname, 299 new Attribute ("redirectPort", new Integer (redirectPort))); 300 attribute = "minProcessors"; 301 int minProcessors = 5; 302 try { 303 minProcessors = Integer.parseInt(cform.getMinProcessorsText()); 304 } catch (Throwable t) { 305 minProcessors = 5; 306 } 307 attribute = "maxProcessors"; 310 int maxProcessors = 20; 311 try { 312 maxProcessors = Integer.parseInt(cform.getMaxProcessorsText()); 313 } catch (Throwable t) { 314 maxProcessors = 20; 315 } 316 319 attribute = "maxKeepAliveRequests"; 320 int maxKeepAliveRequests = 100; 321 try { 322 maxKeepAliveRequests = Integer.parseInt(cform.getMaxKeepAliveText()); 323 } catch (Throwable t) { 324 maxKeepAliveRequests = 100; 325 } 326 mBServer.setAttribute(coname, 327 new Attribute ("maxKeepAliveRequests", new Integer (maxKeepAliveRequests))); 328 attribute = "maxSpareThreads"; 329 int maxSpare = 50; 330 try { 331 maxSpare = Integer.parseInt(cform.getMaxSpare()); 332 } catch (Throwable t) { 333 maxSpare = 50; 334 } 335 mBServer.setAttribute(coname, 336 new Attribute (attribute, (new Integer (maxSpare)).toString())); 337 attribute = "maxThreads"; 338 int maxThreads = 200; 339 try { 340 maxThreads = Integer.parseInt(cform.getMaxThreads()); 341 } catch (Throwable t) { 342 maxThreads = 200; 343 } 344 mBServer.setAttribute(coname, 345 new Attribute (attribute, (new Integer (maxThreads)).toString())); 346 347 attribute = "minSpareThreads"; 348 int minSpare = 4; 349 try { 350 minSpare = Integer.parseInt(cform.getMinSpare()); 351 } catch (Throwable t) { 352 minSpare = 4; 353 } 354 mBServer.setAttribute(coname, 355 new Attribute (attribute, (new Integer (minSpare)).toString())); 356 357 attribute = "threadPriority"; 358 int threadPriority = Thread.NORM_PRIORITY; 359 try { 360 threadPriority = Integer.parseInt(cform.getThreadPriority()); 361 } catch (Throwable t) { 362 threadPriority = Thread.NORM_PRIORITY; 363 } 364 mBServer.setAttribute(coname, 365 new Attribute (attribute, (new Integer (threadPriority)))); 366 367 attribute = "secure"; 368 mBServer.setAttribute(coname, 369 new Attribute ("secure", new Boolean (cform.getSecure()))); 370 attribute = "tcpNoDelay"; 371 mBServer.setAttribute(coname, 372 new Attribute ("tcpNoDelay", new Boolean (cform.getTcpNoDelay()))); 373 374 attribute = "xpoweredBy"; 375 mBServer.setAttribute(coname, 376 new Attribute ("xpoweredBy", new Boolean (cform.getXpoweredBy()))); 377 378 attribute = "URIEncoding"; 379 String uriEnc = cform.getURIEncodingText(); 380 if ((uriEnc != null) && (uriEnc.length()==0)) { 381 uriEnc = null; 382 } 383 mBServer.setAttribute(coname, 384 new Attribute (attribute, uriEnc)); 385 386 attribute = "useBodyEncodingForURI"; 387 mBServer.setAttribute(coname, 388 new Attribute (attribute, new Boolean (cform.getUseBodyEncodingForURIText()))); 389 390 attribute = "allowTrace"; 391 mBServer.setAttribute(coname, 392 new Attribute (attribute, new Boolean (cform.getAllowTraceText()))); 393 394 if (!("AJP".equalsIgnoreCase(connectorType))) { 396 attribute = "proxyName"; 397 String proxyName = cform.getProxyName(); 398 if ((proxyName != null) && (proxyName.length()>0)) { 399 mBServer.setAttribute(coname, 400 new Attribute ("proxyName", proxyName)); 401 } 402 403 attribute = "proxyPort"; 404 int proxyPort = 0; 405 try { 406 proxyPort = Integer.parseInt(cform.getProxyPortText()); 407 } catch (Throwable t) { 408 proxyPort = 0; 409 } 410 mBServer.setAttribute(coname, 411 new Attribute ("proxyPort", new Integer (proxyPort))); 412 } 413 414 if("HTTPS".equalsIgnoreCase(connectorType)) { 416 attribute = "algorithm"; 417 String algorithm = cform.getAlgorithm(); 418 if ((algorithm != null) && (algorithm.length()>0)) 419 mBServer.setAttribute(coname, 420 new Attribute ("algorithm", algorithm)); 421 422 attribute = "clientAuth"; 423 mBServer.setAttribute(coname, 424 new Attribute ("clientAuth", 425 cform.getClientAuthentication())); 426 427 attribute = "ciphers"; 428 String ciphers = cform.getCiphers(); 429 if ((ciphers != null) && (ciphers.length()>0)) 430 mBServer.setAttribute(coname, 431 new Attribute ("ciphers", ciphers)); 432 433 attribute = "keystoreFile"; 434 String keyFile = cform.getKeyStoreFileName(); 435 if ((keyFile != null) && (keyFile.length()>0)) 436 mBServer.setAttribute(coname, 437 new Attribute ("keystoreFile", keyFile)); 438 439 attribute = "keystorePass"; 440 String keyPass = cform.getKeyStorePassword(); 441 if ((keyPass != null) && (keyPass.length()>0)) 442 mBServer.setAttribute(coname, 443 new Attribute ("keystorePass", keyPass)); 444 446 attribute = "keystoreType"; 447 String keyType = cform.getKeyStoreType(); 448 if ((keyType != null) && (keyType.length()>0)) 449 mBServer.setAttribute(coname, 450 new Attribute ("keystoreType", keyType)); 451 452 attribute = "sslProtocol"; 453 String sslProtocol = cform.getSslProtocol(); 454 if ((sslProtocol != null) && (sslProtocol.length()>0)) 455 mBServer.setAttribute(coname, 456 new Attribute ("sslProtocol", sslProtocol)); 457 } 458 459 } catch (Exception e) { 460 461 getServlet().log 462 (resources.getMessage(locale, "users.error.attribute.set", 463 attribute), e); 464 response.sendError 465 (HttpServletResponse.SC_INTERNAL_SERVER_ERROR, 466 resources.getMessage(locale, "users.error.attribute.set", 467 attribute)); 468 return (null); 469 } 470 session.removeAttribute(mapping.getAttribute()); 472 return (mapping.findForward("Save Successful")); 473 474 } 475 476 } 477 | Popular Tags |