1 16 17 18 package org.apache.webapp.admin.resources; 19 20 import java.io.IOException ; 21 import java.net.URLDecoder ; 22 import java.util.ArrayList ; 23 import java.util.Iterator ; 24 import java.util.Locale ; 25 import javax.servlet.ServletException ; 26 import javax.servlet.http.HttpServletRequest ; 27 import javax.servlet.http.HttpServletResponse ; 28 import javax.servlet.http.HttpSession ; 29 import org.apache.struts.action.Action; 30 import org.apache.struts.action.ActionError; 31 import org.apache.struts.action.ActionErrors; 32 import org.apache.struts.action.ActionForm; 33 import org.apache.struts.action.ActionForward; 34 import org.apache.struts.action.ActionMapping; 35 import javax.management.Attribute ; 36 import javax.management.MBeanServer ; 37 import javax.management.MBeanServerFactory ; 38 import javax.management.QueryExp ; 39 import javax.management.Query ; 40 import javax.management.ObjectInstance ; 41 import javax.management.ObjectName ; 42 import javax.management.JMException ; 43 import javax.management.MBeanAttributeInfo ; 44 import javax.management.MBeanOperationInfo ; 45 import javax.management.MBeanInfo ; 46 import org.apache.struts.util.MessageResources; 47 import org.apache.webapp.admin.ApplicationServlet; 48 49 50 58 59 public final class SaveUserDatabaseAction extends Action { 60 61 62 64 67 private MBeanServer mserver = null; 68 69 72 public static String USERDB_FACTORY = "org.apache.catalina.users.MemoryUserDatabaseFactory"; 73 74 76 77 92 public ActionForward execute(ActionMapping mapping, 93 ActionForm form, 94 HttpServletRequest request, 95 HttpServletResponse response) 96 throws IOException , ServletException { 97 98 if (mserver == null) { 100 mserver = ((ApplicationServlet) getServlet()).getServer(); 101 } 102 MessageResources resources = getResources(request); 103 HttpSession session = request.getSession(); 104 Locale locale = getLocale(request); 105 106 if (isCancelled(request)) { 108 return (mapping.findForward("List UserDatabases Setup")); 109 } 110 111 if (!isTokenValid(request)) { 113 response.sendError 114 (HttpServletResponse.SC_BAD_REQUEST, 115 resources.getMessage(locale, "users.error.token")); 116 return (null); 117 } 118 119 UserDatabaseForm userDatabaseForm = (UserDatabaseForm) form; 121 String objectName = userDatabaseForm.getObjectName(); 122 123 if (objectName == null) { 125 126 String signature[] = new String [2]; 127 signature[0] = "java.lang.String"; 128 signature[1] = "java.lang.String"; 129 130 Object params[] = new Object [2]; 131 params[0] = userDatabaseForm.getName(); 132 params[1] = ResourceUtils.USERDB_CLASS; 133 134 ObjectName oname = null; 135 136 try { 137 String domain = userDatabaseForm.getDomain(); 138 oname = new ObjectName ( domain + 139 ResourceUtils.RESOURCE_TYPE + 140 ResourceUtils.GLOBAL_TYPE + 141 ",class=" + ResourceUtils.USERDB_CLASS + 142 ",name=" + params[0]); 143 144 if (mserver.isRegistered(oname)) { 145 ActionErrors errors = new ActionErrors(); 146 errors.add("name", 147 new ActionError("resources.invalid.name")); 148 saveErrors(request, errors); 149 return (new ActionForward(mapping.getInput())); 150 } 151 152 oname = new ObjectName (domain + 154 ResourceUtils.NAMINGRESOURCES_TYPE + 155 ResourceUtils.GLOBAL_TYPE); 156 157 objectName = (String ) mserver.invoke(oname, "addResource", 159 params, signature); 160 161 } catch (Exception e) { 162 163 getServlet().log 164 (resources.getMessage(locale, "users.error.invoke", 165 "addResource"), e); 166 response.sendError 167 (HttpServletResponse.SC_INTERNAL_SERVER_ERROR, 168 resources.getMessage(locale, "users.error.invoke", 169 "addResource")); 170 return (null); 171 } 172 173 } 174 175 String attribute = null; 177 try { 178 179 ObjectName oname = new ObjectName (objectName); 180 181 attribute = "pathname"; 182 mserver.setAttribute 183 (oname, 184 new Attribute (attribute, userDatabaseForm.getPath())); 185 attribute = "factory"; 186 mserver.setAttribute 187 (oname, 188 new Attribute (attribute, userDatabaseForm.getFactory())); 189 attribute = "description"; 190 mserver.setAttribute 191 (oname, 192 new Attribute (attribute, userDatabaseForm.getDescription())); 193 194 } catch (Exception e) { 195 196 getServlet().log 197 (resources.getMessage(locale, "users.error.set.attribute", 198 attribute), e); 199 response.sendError 200 (HttpServletResponse.SC_INTERNAL_SERVER_ERROR, 201 resources.getMessage(locale, "users.error.set.attribute", 202 attribute)); 203 return (null); 204 205 } 206 207 return (mapping.findForward("UserDatabases List Setup")); 209 210 } 211 212 213 } 214 | Popular Tags |