1 16 17 package org.apache.webapp.admin.host; 18 19 20 import java.net.URLEncoder ; 21 import java.util.Iterator ; 22 import java.util.Locale ; 23 import java.io.IOException ; 24 import javax.management.Attribute ; 25 import javax.management.MBeanServer ; 26 import javax.management.MBeanServerFactory ; 27 import javax.management.QueryExp ; 28 import javax.management.Query ; 29 import javax.management.ObjectInstance ; 30 import javax.management.ObjectName ; 31 import javax.management.JMException ; 32 import javax.servlet.ServletException ; 33 import javax.servlet.http.HttpServletRequest ; 34 import javax.servlet.http.HttpServletResponse ; 35 import javax.servlet.http.HttpSession ; 36 import org.apache.struts.action.Action; 37 import org.apache.struts.action.ActionError; 38 import org.apache.struts.action.ActionErrors; 39 import org.apache.struts.action.ActionForm; 40 import org.apache.struts.action.ActionForward; 41 import org.apache.struts.action.ActionMapping; 42 import org.apache.struts.util.MessageResources; 43 import org.apache.webapp.admin.ApplicationServlet; 44 import org.apache.webapp.admin.TomcatTreeBuilder; 45 import org.apache.webapp.admin.TreeControl; 46 import org.apache.webapp.admin.TreeControlNode; 47 48 49 50 57 58 public final class SaveHostAction extends Action { 59 60 61 63 66 private String createStandardHostTypes[] = 67 { "java.lang.String", "java.lang.String", "java.lang.String", "boolean", "boolean", "boolean", "boolean", "boolean", "boolean", }; 77 78 81 private MBeanServer mBServer = null; 82 83 84 86 87 102 public ActionForward execute(ActionMapping mapping, 103 ActionForm form, 104 HttpServletRequest request, 105 HttpServletResponse response) 106 throws IOException , ServletException { 107 108 HttpSession session = request.getSession(); 110 Locale locale = getLocale(request); 111 MessageResources resources = getResources(request); 112 113 try { 115 mBServer = ((ApplicationServlet) getServlet()).getServer(); 116 } catch (Throwable t) { 117 throw new ServletException 118 ("Cannot acquire MBeanServer reference", t); 119 } 120 121 HostForm hform = (HostForm) form; 123 String adminAction = hform.getAdminAction(); 124 String hObjectName = hform.getObjectName(); 125 ObjectName honame = null; 126 127 if ("Create".equals(adminAction)) { 129 130 String operation = null; 131 Object values[] = null; 132 133 try { 134 String serviceName = hform.getServiceName(); 135 ObjectName soname = new ObjectName (serviceName); 136 String domain = soname.getDomain(); 137 ObjectName oname = 139 new ObjectName (domain + 140 TomcatTreeBuilder.HOST_TYPE + 141 ",host=" + hform.getHostName()); 142 if (mBServer.isRegistered(oname)) { 143 ActionErrors errors = new ActionErrors(); 144 errors.add("hostName", 145 new ActionError("error.hostName.exists")); 146 saveErrors(request, errors); 147 return (new ActionForward(mapping.getInput())); 148 } 149 150 ObjectName fname = TomcatTreeBuilder.getMBeanFactory(); 152 153 values = new Object [9]; 155 values[0] = domain + TomcatTreeBuilder.ENGINE_TYPE; 156 values[1] = hform.getHostName(); 157 values[2] = hform.getAppBase(); 158 values[3] = new Boolean (hform.getAutoDeploy()); 159 values[4] = new Boolean (hform.getDeployOnStartup()); 160 values[5] = new Boolean (hform.getDeployXML()); 161 values[6] = new Boolean (hform.getUnpackWARs()); 162 values[7] = new Boolean (hform.getXmlNamespaceAware()); 163 values[8] = new Boolean (hform.getXmlValidation()); 164 165 166 operation = "createStandardHost"; 167 hObjectName = (String ) 168 mBServer.invoke(fname, operation, 169 values, createStandardHostTypes); 170 171 TreeControl control = (TreeControl) 173 session.getAttribute("treeControlTest"); 174 if (control != null) { 175 String parentName = serviceName; 176 TreeControlNode parentNode = control.findNode(parentName); 177 if (parentNode != null) { 178 String nodeLabel = 179 resources.getMessage(locale, "server.service.treeBuilder.host") + 180 " (" + hform.getHostName() + ")"; 181 String encodedName = 182 URLEncoder.encode(hObjectName,TomcatTreeBuilder.URL_ENCODING); 183 TreeControlNode childNode = 184 new TreeControlNode(hObjectName, 185 "Host.gif", 186 nodeLabel, 187 "EditHost.do?select=" + 188 encodedName, 189 "content", 190 true, domain); 191 parentNode.addChild(childNode); 192 } else { 194 getServlet().log 195 ("Cannot find parent node '" + parentName + "'"); 196 } 197 } else { 198 getServlet().log 199 ("Cannot find TreeControlNode!"); 200 } 201 202 } catch (Exception e) { 203 getServlet().log 204 (resources.getMessage(locale, "users.error.invoke", 205 operation), e); 206 response.sendError 207 (HttpServletResponse.SC_INTERNAL_SERVER_ERROR, 208 resources.getMessage(locale, "users.error.invoke", 209 operation)); 210 return (null); 211 212 } 213 214 } 215 216 String attribute = null; 218 try { 219 220 honame = new ObjectName (hObjectName); 221 222 attribute = "appBase"; 223 String appBase = ""; 224 try { 225 appBase = hform.getAppBase(); 226 } catch (Throwable t) { 227 appBase = ""; 228 } 229 mBServer.setAttribute(honame, 230 new Attribute ("appBase", appBase)); 231 232 attribute = "autoDeploy"; 233 String autoDeploy = "true"; 234 try { 235 autoDeploy = hform.getAutoDeploy(); 236 } catch (Throwable t) { 237 autoDeploy = "true"; 238 } 239 mBServer.setAttribute(honame, 240 new Attribute ("autoDeploy", new Boolean (autoDeploy))); 241 242 attribute = "deployXML"; 243 String deployXML = "true"; 244 try { 245 deployXML = hform.getDeployXML(); 246 } catch (Throwable t) { 247 deployXML = "true"; 248 } 249 mBServer.setAttribute(honame, 250 new Attribute ("deployXML", new Boolean (deployXML))); 251 252 attribute = "deployOnStartup"; 253 String deployOnStartup = "true"; 254 try { 255 deployOnStartup = hform.getDeployOnStartup(); 256 } catch (Throwable t) { 257 deployOnStartup = "true"; 258 } 259 mBServer.setAttribute(honame, 260 new Attribute ("deployOnStartup", new Boolean (deployOnStartup))); 261 262 attribute = "unpackWARs"; 263 String unpackWARs = "false"; 264 try { 265 unpackWARs = hform.getUnpackWARs(); 266 } catch (Throwable t) { 267 unpackWARs = "false"; 268 } 269 mBServer.setAttribute(honame, 270 new Attribute ("unpackWARs", new Boolean (unpackWARs))); 271 272 attribute = "xmlNamespaceAware"; 273 String xmlNamespaceAware = "false"; 274 try { 275 xmlNamespaceAware = hform.getXmlNamespaceAware(); 276 } catch (Throwable t) { 277 xmlNamespaceAware = "false"; 278 } 279 mBServer.setAttribute(honame, 280 new Attribute ("xmlNamespaceAware", new Boolean (xmlNamespaceAware))); 281 282 attribute = "xmlValidation"; 283 String xmlValidation = "false"; 284 try { 285 xmlValidation = hform.getXmlValidation(); 286 } catch (Throwable t) { 287 xmlValidation = "false"; 288 } 289 mBServer.setAttribute(honame, 290 new Attribute ("xmlValidation", new Boolean (xmlValidation))); 291 292 } catch (Exception e) { 293 294 getServlet().log 295 (resources.getMessage(locale, "users.error.attribute.set", 296 attribute), e); 297 response.sendError 298 (HttpServletResponse.SC_INTERNAL_SERVER_ERROR, 299 resources.getMessage(locale, "users.error.attribute.set", 300 attribute)); 301 return (null); 302 } 303 304 session.removeAttribute(mapping.getAttribute()); 306 return (mapping.findForward("Save Successful")); 307 308 } 309 310 } 311 | Popular Tags |