1 16 17 package org.apache.webapp.admin.service; 18 19 20 import java.net.URLEncoder ; 21 import java.util.Iterator ; 22 import java.util.Locale ; 23 import java.util.Vector ; 24 import java.io.IOException ; 25 import javax.management.Attribute ; 26 import javax.management.MBeanServer ; 27 import javax.management.MBeanServerFactory ; 28 import javax.management.QueryExp ; 29 import javax.management.Query ; 30 import javax.management.ObjectInstance ; 31 import javax.management.ObjectName ; 32 import javax.management.JMException ; 33 import javax.servlet.ServletException ; 34 import javax.servlet.http.HttpServletRequest ; 35 import javax.servlet.http.HttpServletResponse ; 36 import javax.servlet.http.HttpSession ; 37 import org.apache.struts.action.Action; 38 import org.apache.struts.action.ActionError; 39 import org.apache.struts.action.ActionErrors; 40 import org.apache.struts.action.ActionForm; 41 import org.apache.struts.action.ActionForward; 42 import org.apache.struts.action.ActionMapping; 43 import org.apache.struts.util.MessageResources; 44 import org.apache.webapp.admin.ApplicationServlet; 45 import org.apache.webapp.admin.Lists; 46 import org.apache.webapp.admin.TomcatTreeBuilder; 47 import org.apache.webapp.admin.TreeControl; 48 import org.apache.webapp.admin.TreeControlNode; 49 import org.apache.webapp.admin.valve.ValveUtil; 50 51 52 53 61 62 public final class SaveServiceAction extends Action { 63 64 65 67 68 71 private String createStandardEngineTypes[] = 72 { "java.lang.String", "java.lang.String", "java.lang.String", }; 76 77 78 81 private String createStandardServiceTypes[] = 82 { "java.lang.String", "java.lang.String", "java.lang.String" }; 86 87 88 91 private String createStandardEngineServiceTypes[] = 92 { "java.lang.String", "java.lang.String", "java.lang.String", "java.lang.String" }; 97 98 99 102 private String createUserDatabaseRealmTypes[] = 103 { "java.lang.String", "java.lang.String", }; 106 107 108 111 private MBeanServer mBServer = null; 112 113 114 116 117 132 public ActionForward execute(ActionMapping mapping, 133 ActionForm form, 134 HttpServletRequest request, 135 HttpServletResponse response) 136 throws IOException , ServletException { 137 138 HttpSession session = request.getSession(); 140 Locale locale = getLocale(request); 141 MessageResources resources = getResources(request); 142 143 try { 145 mBServer = ((ApplicationServlet) getServlet()).getServer(); 146 } catch (Throwable t) { 147 throw new ServletException 148 ("Cannot acquire MBeanServer reference", t); 149 } 150 151 ServiceForm sform = (ServiceForm) form; 153 String adminAction = sform.getAdminAction(); 154 String sObjectName = sform.getObjectName(); 155 String eObjectName = sform.getEngineObjectName(); 156 String serverObjectName = sform.getServerObjectName(); 157 ObjectName eoname = null; 158 ObjectName soname = null; 159 if ("Create".equals(adminAction)) { 161 162 String operation = null; 163 String values[] = null; 164 165 try { 166 String engineName = sform.getEngineName(); 168 ObjectName oname = 171 new ObjectName ("*" + TomcatTreeBuilder.SERVICE_TYPE + 172 ",serviceName="+sform.getServiceName()); 173 Iterator names = mBServer.queryNames(oname, null).iterator(); 174 while (names.hasNext()) { 175 if (mBServer.isRegistered((ObjectName )names.next())) { 176 ActionErrors errors = new ActionErrors(); 177 errors.add("serviceName", 178 new ActionError("error.serviceName.exists")); 179 saveErrors(request, errors); 180 return (new ActionForward(mapping.getInput())); 181 } 182 } 183 184 oname = new ObjectName (engineName + TomcatTreeBuilder.ENGINE_TYPE); 185 if (mBServer.isRegistered(oname)) { 186 ActionErrors errors = new ActionErrors(); 187 errors.add("serviceName", 188 new ActionError("error.engineName.exists")); 189 saveErrors(request, errors); 190 return (new ActionForward(mapping.getInput())); 191 } 192 193 ObjectName fname = TomcatTreeBuilder.getMBeanFactory(); 195 196 values = new String [4]; 198 values[0] = TomcatTreeBuilder.SERVER_TYPE; 199 values[1] = engineName; 200 values[2] = sform.getDefaultHost(); 201 values[3] = sform.getServiceName(); 202 operation = "createStandardEngineService"; 203 Vector onames = (Vector ) 204 mBServer.invoke(fname, operation, 205 values, createStandardEngineServiceTypes); 206 eoname = (ObjectName )onames.get(0); 207 soname = (ObjectName )onames.get(1); 208 sObjectName = soname.toString(); 209 eObjectName = eoname.toString(); 210 211 String realmOName = ValveUtil.getObjectName( 212 eObjectName, TomcatTreeBuilder.REALM_TYPE); 213 214 ObjectName roname = new ObjectName (realmOName); 215 if (mBServer.isRegistered(roname)) { 216 mBServer.unregisterMBean(roname); 217 } 218 219 values = new String [2]; 221 values[0] = eObjectName; 222 values[1] = "UserDatabase"; 223 operation = "createUserDatabaseRealm"; 224 228 sObjectName = soname.toString(); 233 eObjectName = eoname.toString(); 234 235 245 258 TreeControl control = (TreeControl) 260 session.getAttribute("treeControlTest"); 261 if (control != null) { 262 String parentName = TomcatTreeBuilder.DEFAULT_DOMAIN + 263 TomcatTreeBuilder.SERVER_TYPE; 264 TreeControlNode parentNode = control.findNode(parentName); 265 if (parentNode != null) { 266 String nodeLabel = resources.getMessage(locale, 267 "server.service.treeBuilder.subtreeNode") +" (" + 268 soname.getKeyProperty("serviceName") + ")"; 269 String encodedName = 270 URLEncoder.encode(sObjectName,TomcatTreeBuilder.URL_ENCODING); 271 TreeControlNode childNode = 272 new TreeControlNode(sObjectName, 273 "Service.gif", 274 nodeLabel, 275 "EditService.do?select=" + 276 encodedName, 277 "content", 278 true, engineName); 279 parentNode.addChild(childNode); 280 } else { 302 getServlet().log 303 ("Cannot find parent node '" + parentName + "'"); 304 } 305 } else { 306 getServlet().log 307 ("Cannot find TreeControlNode!"); 308 } 309 310 } catch (Exception e) { 311 312 getServlet().log 313 (resources.getMessage(locale, "users.error.invoke", 314 operation), e); 315 response.sendError 316 (HttpServletResponse.SC_INTERNAL_SERVER_ERROR, 317 resources.getMessage(locale, "users.error.invoke", 318 operation)); 319 return (null); 320 321 } 322 323 } 324 325 String attribute = null; 327 try { 328 329 eoname = new ObjectName (eObjectName); 330 soname = new ObjectName (sObjectName); 331 332 attribute = "defaultHost"; 333 String defaultHost = sform.getDefaultHost(); 334 if ("".equals(defaultHost)) { 335 defaultHost = null; 336 } 337 mBServer.setAttribute(eoname, 338 new Attribute ("defaultHost", defaultHost)); 339 340 } catch (Exception e) { 341 342 getServlet().log 343 (resources.getMessage(locale, "users.error.attribute.set", 344 attribute), e); 345 response.sendError 346 (HttpServletResponse.SC_INTERNAL_SERVER_ERROR, 347 resources.getMessage(locale, "users.error.attribute.set", 348 attribute)); 349 return (null); 350 } 351 352 session.removeAttribute(mapping.getAttribute()); 354 return (mapping.findForward("Save Successful")); 355 356 } 357 358 } 359 | Popular Tags |