1 16 17 18 package org.apache.webapp.admin.resources; 19 20 import java.io.IOException ; 21 import java.net.URLEncoder ; 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 SaveDataSourceAction extends Action { 60 61 62 64 67 private MBeanServer mserver = null; 68 69 71 72 87 public ActionForward execute(ActionMapping mapping, 88 ActionForm form, 89 HttpServletRequest request, 90 HttpServletResponse response) 91 throws IOException , ServletException { 92 93 if (mserver == null) { 95 mserver = ((ApplicationServlet) getServlet()).getServer(); 96 } 97 MessageResources resources = getResources(request); 98 HttpSession session = request.getSession(); 99 Locale locale = getLocale(request); 100 101 if (isCancelled(request)) { 103 return (mapping.findForward("List DataSources Setup")); 104 } 105 106 if (!isTokenValid(request)) { 108 response.sendError 109 (HttpServletResponse.SC_BAD_REQUEST, 110 resources.getMessage(locale, "users.error.token")); 111 return (null); 112 } 113 114 DataSourceForm dataSourceForm = (DataSourceForm) form; 116 String objectName = dataSourceForm.getObjectName(); 117 118 if (objectName == null) { 120 121 String signature[] = new String [2]; 122 signature[0] = "java.lang.String"; 123 signature[1] = "java.lang.String"; 124 125 Object params[] = new Object [2]; 126 params[0] = dataSourceForm.getJndiName(); 127 params[1] = ResourceUtils.DATASOURCE_CLASS; 128 String encodedJndiName = URLEncoder.encode(params[0].toString(), 129 "UTF-8"); 130 131 String resourcetype = dataSourceForm.getResourcetype(); 132 String path = dataSourceForm.getPath(); 133 String host = dataSourceForm.getHost(); 134 String domain = dataSourceForm.getDomain(); 135 136 ObjectName oname = null; 137 ObjectName encodedOName = null; 138 139 try { 140 141 if (resourcetype.equals("Global")) { 142 oname = new ObjectName ( domain + ResourceUtils.RESOURCE_TYPE + 143 ResourceUtils.GLOBAL_TYPE + 144 ",class=" + params[1] + 145 ",name=" + params[0]); 146 encodedOName = new ObjectName ( domain + 147 ResourceUtils.RESOURCE_TYPE + 148 ResourceUtils.GLOBAL_TYPE + 149 ",class=" + params[1] + 150 ",name=" + encodedJndiName); 151 } else if (resourcetype.equals("Context")) { 152 oname = new ObjectName ( domain + ResourceUtils.RESOURCE_TYPE + 153 ResourceUtils.CONTEXT_TYPE + 154 ",path=" + path + ",host=" + host + 155 ",class=" + params[1] + 156 ",name=" + params[0]); 157 encodedOName = new ObjectName ( domain + 158 ResourceUtils.RESOURCE_TYPE + 159 ResourceUtils.CONTEXT_TYPE + 160 ",path=" + path + ",host=" + host + 161 ",class=" + params[1] + 162 ",name=" + encodedJndiName); 163 } 164 165 if (mserver.isRegistered(oname) || 166 mserver.isRegistered(encodedOName)) { 167 ActionErrors errors = new ActionErrors(); 168 errors.add("jndiName", 169 new ActionError("resources.invalid.name")); 170 saveErrors(request, errors); 171 return (new ActionForward(mapping.getInput())); 172 } 173 174 oname = ResourceUtils.getNamingResourceObjectName(domain, 175 resourcetype, path, host); 176 177 objectName = (String ) mserver.invoke(oname, "addResource", 179 params, signature); 180 181 } catch (Exception e) { 182 183 getServlet().log 184 (resources.getMessage(locale, "users.error.invoke", 185 "addResource"), e); 186 response.sendError 187 (HttpServletResponse.SC_INTERNAL_SERVER_ERROR, 188 resources.getMessage(locale, "users.error.invoke", 189 "addResource")); 190 return (null); 191 } 192 193 } 194 195 String attribute = null; 197 try { 198 199 ObjectName oname = new ObjectName (objectName); 200 201 attribute = "url"; 202 mserver.setAttribute 203 (oname, 204 new Attribute (attribute, dataSourceForm.getUrl())); 205 attribute = "driverClassName"; 206 mserver.setAttribute 207 (oname, 208 new Attribute (attribute, dataSourceForm.getDriverClass())); 209 attribute = "username"; 210 mserver.setAttribute 211 (oname, 212 new Attribute (attribute, dataSourceForm.getUsername())); 213 attribute = "password"; 214 mserver.setAttribute 215 (oname, 216 new Attribute (attribute, dataSourceForm.getPassword())); 217 attribute = "maxActive"; 218 mserver.setAttribute 219 (oname, 220 new Attribute (attribute, dataSourceForm.getActive())); 221 attribute = "maxIdle"; 222 mserver.setAttribute 223 (oname, 224 new Attribute (attribute, dataSourceForm.getIdle())); 225 attribute = "maxWait"; 226 mserver.setAttribute 227 (oname, 228 new Attribute (attribute, dataSourceForm.getWait())); 229 attribute = "validationQuery"; 230 String validationQuery = dataSourceForm.getQuery(); 231 if ((validationQuery != null) && (validationQuery.length()>0)) { 232 mserver.setAttribute(oname, 233 new Attribute (attribute, validationQuery)); 234 } 235 236 } catch (Exception e) { 237 238 getServlet().log 239 (resources.getMessage(locale, "users.error.set.attribute", 240 attribute), e); 241 response.sendError 242 (HttpServletResponse.SC_INTERNAL_SERVER_ERROR, 243 resources.getMessage(locale, "users.error.set.attribute", 244 attribute)); 245 return (null); 246 247 } 248 249 return (mapping.findForward("DataSources List Setup")); 251 252 } 253 254 255 } 256 | Popular Tags |