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 57 58 public final class SaveJNDIRealmAction extends Action { 59 60 61 63 66 private String createStandardRealmTypes[] = 67 { "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 JNDIRealmForm rform = (JNDIRealmForm) 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("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 = 164 TomcatTreeBuilder.getMBeanFactory(); 165 166 values = new String [1]; 168 values[0] = parent; 169 operation = "createJNDIRealm"; 170 rObjectName = (String ) 171 mBServer.invoke(fname, operation, 172 values, createStandardRealmTypes); 173 174 if (rObjectName==null) { 175 request.setAttribute("warning", "error.jndirealm"); 176 return (mapping.findForward("Save Unsuccessful")); 177 } 178 179 TreeControl control = (TreeControl) 181 session.getAttribute("treeControlTest"); 182 if (control != null) { 183 TreeControlNode parentNode = control.findNode(rform.getParentObjectName()); 184 if (parentNode != null) { 185 String nodeLabel = rform.getNodeLabel(); 186 String encodedName = 187 URLEncoder.encode(rObjectName,TomcatTreeBuilder.URL_ENCODING); 188 TreeControlNode childNode = 189 new TreeControlNode(rObjectName, 190 "Realm.gif", 191 nodeLabel, 192 "EditRealm.do?select=" + 193 encodedName, 194 "content", 195 true, domain); 196 parentNode.addChild(childNode); 197 } else { 199 getServlet().log 200 ("Cannot find parent node '" + parent + "'"); 201 } 202 } else { 203 getServlet().log 204 ("Cannot find TreeControlNode!"); 205 } 206 207 } catch (Exception e) { 208 209 getServlet().log 210 (resources.getMessage(locale, "users.error.invoke", 211 operation), e); 212 response.sendError 213 (HttpServletResponse.SC_INTERNAL_SERVER_ERROR, 214 resources.getMessage(locale, "users.error.invoke", 215 operation)); 216 return (null); 217 218 } 219 220 } 221 222 String attribute = null; 224 try { 225 226 ObjectName roname = new ObjectName (rObjectName); 227 228 attribute = "connectionName"; 229 String connectionName = rform.getConnectionName(); 230 if ((connectionName != null) && (connectionName.length()>0)) { 231 mBServer.setAttribute(roname, 232 new Attribute ("connectionName", connectionName)); 233 } 234 235 attribute = "connectionPassword"; 236 String connectionPassword = rform.getConnectionPassword(); 237 if ((connectionPassword != null) && (connectionPassword.length()>0)) { 238 mBServer.setAttribute(roname, 239 new Attribute ("connectionPassword", connectionPassword)); 240 } 241 242 attribute = "connectionURL"; 243 String connectionURL = rform.getConnectionURL(); 244 if ((connectionURL != null) && (connectionURL.length()>0)) { 245 mBServer.setAttribute(roname, 246 new Attribute ("connectionURL", connectionURL)); 247 } 248 249 attribute = "contextFactory"; 250 String contextFactory = rform.getContextFactory(); 251 if ((contextFactory != null) && (contextFactory.length()>0)) { 252 mBServer.setAttribute(roname, 253 new Attribute ("contextFactory", contextFactory)); 254 } 255 256 attribute = "digest"; 257 String digest = rform.getDigest(); 258 if ((digest != null) && (digest.length()>0)) { 259 mBServer.setAttribute(roname, 260 new Attribute ("digest", digest)); 261 } 262 263 attribute = "roleBase"; 264 String roleBase = rform.getRoleBase(); 265 if ((roleBase != null) && (roleBase.length()>0)) { 266 mBServer.setAttribute(roname, 267 new Attribute ("roleBase", roleBase)); 268 } 269 270 attribute = "roleName"; 271 String roleName = rform.getRoleName(); 272 if ((roleName != null) && (roleName.length()>0)) { 273 mBServer.setAttribute(roname, 274 new Attribute ("roleName", roleName)); 275 } 276 277 attribute = "roleSearch"; 278 String rolePattern = rform.getRolePattern(); 279 if ((rolePattern != null) && (rolePattern.length()>0)) { 280 mBServer.setAttribute(roname, 281 new Attribute ("roleSearch", rolePattern)); 282 } 283 284 attribute = "roleSubtree"; 285 String roleSubtree = rform.getRoleSubtree(); 286 if ((roleSubtree != null) && (roleSubtree.length()>0)) { 287 mBServer.setAttribute(roname, 288 new Attribute ("roleSubtree", new Boolean (roleSubtree))); 289 } 290 291 attribute = "userBase"; 292 String userBase = rform.getUserBase(); 293 if ((userBase != null) && (userBase.length()>0)) { 294 mBServer.setAttribute(roname, 295 new Attribute ("userBase", userBase)); 296 } 297 298 attribute = "userPassword"; 299 String userPassword = rform.getUserPassword(); 300 if ((userPassword != null) && (userPassword.length()>0)) { 301 mBServer.setAttribute(roname, 302 new Attribute ("userPassword", userPassword)); 303 } 304 305 attribute = "userPattern"; 306 String userPattern = rform.getUserPattern(); 307 if ((userPattern != null) && (userPattern.length()>0)) { 308 mBServer.setAttribute(roname, 309 new Attribute ("userPattern", userPattern)); 310 } 311 312 attribute = "userRoleName"; 313 String userRoleName = rform.getUserRoleName(); 314 if ((userRoleName != null) && (userRoleName.length()>0)) { 315 mBServer.setAttribute(roname, 316 new Attribute ("userRoleName", userRoleName)); 317 } 318 319 attribute = "userSearch"; 320 String userSearch = rform.getUserSearch(); 321 if ((userSearch != null) && (userSearch.length()>0)) { 322 mBServer.setAttribute(roname, 323 new Attribute ("userSearch", userSearch)); 324 } 325 326 attribute = "userSubtree"; 327 String userSubtree = rform.getUserSubtree(); 328 if ((userSubtree != null) && (userSubtree.length()>0)) { 329 mBServer.setAttribute(roname, 330 new Attribute ("userSubtree", new Boolean (userSubtree))); 331 } 332 333 } catch (Exception e) { 334 335 getServlet().log 336 (resources.getMessage(locale, "users.error.attribute.set", 337 attribute), e); 338 response.sendError 339 (HttpServletResponse.SC_INTERNAL_SERVER_ERROR, 340 resources.getMessage(locale, "users.error.attribute.set", 341 attribute)); 342 return (null); 343 } 344 345 session.removeAttribute(mapping.getAttribute()); 347 return (mapping.findForward("Save Successful")); 348 349 } 350 351 } 352 | Popular Tags |