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 SaveJDBCRealmAction extends Action { 58 59 60 62 65 private String createStandardRealmTypes[] = 66 { "java.lang.String", "java.lang.String", "java.lang.String", "java.lang.String", "java.lang.String", }; 72 73 74 77 private MBeanServer mBServer = null; 78 79 80 82 83 98 public ActionForward execute(ActionMapping mapping, 99 ActionForm form, 100 HttpServletRequest request, 101 HttpServletResponse response) 102 throws IOException , ServletException { 103 104 HttpSession session = request.getSession(); 106 Locale locale = getLocale(request); 107 MessageResources resources = getResources(request); 108 109 try { 111 mBServer = ((ApplicationServlet) getServlet()).getServer(); 112 } catch (Throwable t) { 113 throw new ServletException 114 ("Cannot acquire MBeanServer reference", t); 115 } 116 117 JDBCRealmForm rform = (JDBCRealmForm) form; 119 String adminAction = rform.getAdminAction(); 120 String rObjectName = rform.getObjectName(); 121 122 if ("Create".equals(adminAction)) { 124 125 String operation = null; 126 String values[] = null; 127 128 try { 129 130 String parent = rform.getParentObjectName(); 131 String objectName = ValveUtil.getObjectName(parent, 132 TomcatTreeBuilder.REALM_TYPE); 133 134 ObjectName pname = new ObjectName (parent); 135 StringBuffer sb = new StringBuffer (pname.getDomain()); 136 137 try { 140 if ("Service".equalsIgnoreCase(pname.getKeyProperty("type"))) { 141 sb.append(":type=Engine"); 142 parent = sb.toString(); 143 } 144 } catch (Exception e) { 145 String message = 146 resources.getMessage(locale, "error.engineName.bad", 147 sb.toString()); 148 getServlet().log(message); 149 response.sendError(HttpServletResponse.SC_BAD_REQUEST, message); 150 return (null); 151 } 152 153 ObjectName oname = 155 new ObjectName (objectName); 156 if (mBServer.isRegistered(oname)) { 157 ActionErrors errors = new ActionErrors(); 158 errors.add("realmName", 159 new ActionError("error.realmName.exists")); 160 saveErrors(request, errors); 161 return (new ActionForward(mapping.getInput())); 162 } 163 164 String domain = oname.getDomain(); 165 ObjectName fname = 167 TomcatTreeBuilder.getMBeanFactory(); 168 169 values = new String [5]; 171 values[0] = parent; 172 values[1] = rform.getDriver(); 173 values[2] = rform.getConnectionName(); 174 values[3] = rform.getConnectionPassword(); 175 values[4] = rform.getConnectionURL(); 176 operation = "createJDBCRealm"; 177 rObjectName = (String ) 178 mBServer.invoke(fname, operation, 179 values, createStandardRealmTypes); 180 181 if (rObjectName==null) { 182 request.setAttribute("warning", "error.jdbcrealm"); 183 return (mapping.findForward("Save Unsuccessful")); 184 } 185 186 TreeControl control = (TreeControl) 188 session.getAttribute("treeControlTest"); 189 if (control != null) { 190 TreeControlNode parentNode = control.findNode(rform.getParentObjectName()); 191 if (parentNode != null) { 192 String nodeLabel = rform.getNodeLabel(); 193 String encodedName = 194 URLEncoder.encode(rObjectName,TomcatTreeBuilder.URL_ENCODING); 195 TreeControlNode childNode = 196 new TreeControlNode(rObjectName, 197 "Realm.gif", 198 nodeLabel, 199 "EditRealm.do?select=" + 200 encodedName, 201 "content", 202 true, domain); 203 parentNode.addChild(childNode); 204 } else { 206 getServlet().log 207 ("Cannot find parent node '" + parent + "'"); 208 } 209 } else { 210 getServlet().log 211 ("Cannot find TreeControlNode!"); 212 } 213 214 } catch (Exception e) { 215 216 getServlet().log 217 (resources.getMessage(locale, "users.error.invoke", 218 operation), e); 219 response.sendError 220 (HttpServletResponse.SC_INTERNAL_SERVER_ERROR, 221 resources.getMessage(locale, "users.error.invoke", 222 operation)); 223 return (null); 224 225 } 226 227 } 228 229 String attribute = null; 231 try { 232 233 ObjectName roname = new ObjectName (rObjectName); 234 235 attribute = "digest"; 236 mBServer.setAttribute(roname, 237 new Attribute ("digest", rform.getDigest())); 238 239 attribute = "driverName"; 240 mBServer.setAttribute(roname, 241 new Attribute ("driverName", rform.getDriver())); 242 243 attribute = "roleNameCol"; 244 mBServer.setAttribute(roname, 245 new Attribute ("roleNameCol", rform.getRoleNameCol())); 246 247 attribute = "userNameCol"; 248 mBServer.setAttribute(roname, 249 new Attribute ("userNameCol", rform.getUserNameCol())); 250 251 attribute = "userCredCol"; 252 mBServer.setAttribute(roname, 253 new Attribute ("userCredCol", rform.getPasswordCol())); 254 255 attribute = "userTable"; 256 mBServer.setAttribute(roname, 257 new Attribute ("userTable", rform.getUserTable())); 258 259 attribute = "userRoleTable"; 260 mBServer.setAttribute(roname, 261 new Attribute ("userRoleTable", rform.getRoleTable())); 262 263 attribute = "connectionName"; 264 mBServer.setAttribute(roname, 265 new Attribute ("connectionName", rform.getConnectionName())); 266 267 attribute = "connectionURL"; 268 mBServer.setAttribute(roname, 269 new Attribute ("connectionURL", rform.getConnectionURL())); 270 271 attribute = "connectionPassword"; 272 mBServer.setAttribute(roname, 273 new Attribute ("connectionPassword", rform.getConnectionPassword())); 274 275 } catch (Exception e) { 276 277 getServlet().log 278 (resources.getMessage(locale, "users.error.attribute.set", 279 attribute), e); 280 response.sendError 281 (HttpServletResponse.SC_INTERNAL_SERVER_ERROR, 282 resources.getMessage(locale, "users.error.attribute.set", 283 attribute)); 284 return (null); 285 } 286 287 session.removeAttribute(mapping.getAttribute()); 289 return (mapping.findForward("Save Successful")); 290 291 } 292 293 } 294 | Popular Tags |