1 16 17 package org.apache.webapp.admin.realm; 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 import org.apache.webapp.admin.valve.ValveUtil; 48 49 56 57 public final class SaveUserDatabaseRealmAction extends Action { 58 59 60 62 65 private String createUserDatabaseRealmTypes[] = 66 { "java.lang.String", "java.lang.String", }; 69 70 71 74 private MBeanServer mBServer = null; 75 76 77 79 80 95 public ActionForward execute(ActionMapping mapping, 96 ActionForm form, 97 HttpServletRequest request, 98 HttpServletResponse response) 99 throws IOException , ServletException { 100 101 HttpSession session = request.getSession(); 103 Locale locale = getLocale(request); 104 MessageResources resources = getResources(request); 105 106 try { 108 mBServer = ((ApplicationServlet) getServlet()).getServer(); 109 } catch (Throwable t) { 110 throw new ServletException 111 ("Cannot acquire MBeanServer reference", t); 112 } 113 114 UserDatabaseRealmForm rform = (UserDatabaseRealmForm) form; 116 String adminAction = rform.getAdminAction(); 117 String rObjectName = rform.getObjectName(); 118 119 if ("Create".equals(adminAction)) { 121 122 String operation = null; 123 String values[] = null; 124 125 try { 126 127 String parent = rform.getParentObjectName(); 128 String objectName = ValveUtil.getObjectName(parent, 129 TomcatTreeBuilder.REALM_TYPE); 130 131 ObjectName pname = new ObjectName (parent); 132 StringBuffer sb = new StringBuffer (pname.getDomain()); 133 134 try { 137 if ("Service".equalsIgnoreCase(pname.getKeyProperty("type"))) { 138 sb.append(":type=Engine"); 139 parent = sb.toString(); 140 } 141 } catch (Exception e) { 142 String message = 143 resources.getMessage(locale, "error.engineName.bad", 144 sb.toString()); 145 getServlet().log(message); 146 response.sendError(HttpServletResponse.SC_BAD_REQUEST, message); 147 return (null); 148 } 149 150 ObjectName oname = 152 new ObjectName (objectName); 153 if (mBServer.isRegistered(oname)) { 154 ActionErrors errors = new ActionErrors(); 155 errors.add("realmName", 156 new ActionError("error.realmName.exists")); 157 saveErrors(request, errors); 158 return (new ActionForward(mapping.getInput())); 159 } 160 161 String domain = oname.getDomain(); 162 ObjectName fname = TomcatTreeBuilder.getMBeanFactory(); 164 165 values = new String [2]; 167 values[0] = parent; 168 values[1] = rform.getResource(); 169 operation = "createUserDatabaseRealm"; 170 rObjectName = (String ) 171 mBServer.invoke(fname, operation, 172 values, createUserDatabaseRealmTypes); 173 if (rObjectName==null) { 174 request.setAttribute("warning", "error.userdbrealm"); 175 return (mapping.findForward("Save Unsuccessful")); 176 } 177 178 TreeControl control = (TreeControl) 180 session.getAttribute("treeControlTest"); 181 if (control != null) { 182 TreeControlNode parentNode = control.findNode(rform.getParentObjectName()); 183 if (parentNode != null) { 184 String nodeLabel = rform.getNodeLabel(); 185 String encodedName = 186 URLEncoder.encode(rObjectName,TomcatTreeBuilder.URL_ENCODING); 187 TreeControlNode childNode = 188 new TreeControlNode(rObjectName, 189 "Realm.gif", 190 nodeLabel, 191 "EditRealm.do?select=" + 192 encodedName, 193 "content", 194 true, domain); 195 parentNode.addChild(childNode); 196 } else { 198 getServlet().log 199 ("Cannot find parent node '" + parent + "'"); 200 } 201 } else { 202 getServlet().log 203 ("Cannot find TreeControlNode!"); 204 } 205 206 } catch (Exception e) { 207 208 getServlet().log 209 (resources.getMessage(locale, "users.error.invoke", 210 operation), e); 211 response.sendError 212 (HttpServletResponse.SC_INTERNAL_SERVER_ERROR, 213 resources.getMessage(locale, "users.error.invoke", 214 operation)); 215 return (null); 216 217 } 218 219 } 220 221 String attribute = null; 223 try { 224 225 ObjectName roname = new ObjectName (rObjectName); 226 227 attribute = "resourceName"; 228 mBServer.setAttribute(roname, 229 new Attribute ("resourceName", rform.getResource())); 230 231 } catch (Exception e) { 232 233 getServlet().log 234 (resources.getMessage(locale, "users.error.attribute.set", 235 attribute), e); 236 response.sendError 237 (HttpServletResponse.SC_INTERNAL_SERVER_ERROR, 238 resources.getMessage(locale, "users.error.attribute.set", 239 attribute)); 240 return (null); 241 } 242 243 session.removeAttribute(mapping.getAttribute()); 245 return (mapping.findForward("Save Successful")); 246 247 } 248 249 } 250 | Popular Tags |