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