1 16 17 package org.apache.webapp.admin.valve; 18 19 import java.net.URLEncoder ; 20 import java.util.ArrayList ; 21 import java.util.Iterator ; 22 import java.util.Locale ; 23 import java.io.IOException ; 24 import java.util.regex.Pattern ; 25 import java.util.regex.PatternSyntaxException ; 26 import javax.management.Attribute ; 27 import javax.management.MBeanServer ; 28 import javax.management.MBeanServerFactory ; 29 import javax.management.QueryExp ; 30 import javax.management.Query ; 31 import javax.management.ObjectInstance ; 32 import javax.management.ObjectName ; 33 import javax.management.JMException ; 34 import javax.servlet.ServletException ; 35 import javax.servlet.http.HttpServletRequest ; 36 import javax.servlet.http.HttpServletResponse ; 37 import javax.servlet.http.HttpSession ; 38 import org.apache.struts.Globals; 39 import org.apache.struts.action.Action; 40 import org.apache.struts.action.ActionError; 41 import org.apache.struts.action.ActionErrors; 42 import org.apache.struts.action.ActionForm; 43 import org.apache.struts.action.ActionForward; 44 import org.apache.struts.action.ActionMapping; 45 import org.apache.struts.util.MessageResources; 46 import org.apache.webapp.admin.ApplicationServlet; 47 import org.apache.webapp.admin.TomcatTreeBuilder; 48 import org.apache.webapp.admin.TreeControl; 49 import org.apache.webapp.admin.TreeControlNode; 50 51 57 58 public final class ValveUtil { 59 60 61 63 66 private static String createStandardValveTypes[] = 67 { "java.lang.String", }; 69 70 71 73 public static String createValve(String parent, String valveType, 74 HttpServletResponse response, HttpServletRequest request, 75 ActionMapping mapping, ApplicationServlet servlet) 76 throws IOException , ServletException { 77 78 MessageResources resources = (MessageResources) 79 servlet.getServletContext().getAttribute(Globals.MESSAGES_KEY); 80 HttpSession session = request.getSession(); 81 82 MBeanServer mBServer = null; 83 Locale locale = (Locale ) session.getAttribute(Globals.LOCALE_KEY); 84 try { 86 mBServer = servlet.getServer(); 87 } catch (Throwable t) { 88 throw new ServletException 89 ("Cannot acquire MBeanServer reference", t); 90 } 91 92 String operation = null; 93 String values[] = null; 94 String vObjectName = null; 95 96 try { 97 98 String objectName = ValveUtil.getObjectName(parent, 99 TomcatTreeBuilder.VALVE_TYPE); 100 101 String parentNodeName = parent; 102 ObjectName pname = new ObjectName (parent); 103 StringBuffer sb = new StringBuffer (pname.getDomain()); 104 105 try { 108 if ("Service".equalsIgnoreCase(pname.getKeyProperty("type"))) { 109 sb.append(":type=Engine"); 110 parent = sb.toString(); 111 } 112 } catch (Exception e) { 113 String message = resources.getMessage("error.engineName.bad", 114 sb.toString()); 115 servlet.log(message); 116 response.sendError(HttpServletResponse.SC_BAD_REQUEST, message); 117 return (null); 118 } 119 121 135 136 String domain = pname.getDomain(); 137 ObjectName fname = TomcatTreeBuilder.getMBeanFactory(); 139 140 values = new String [1]; 142 values[0] = parent; 143 144 operation = "create" + valveType; 145 if ("AccessLogValve".equalsIgnoreCase(valveType)) 146 operation = "createAccessLoggerValve"; 147 148 vObjectName = (String ) 149 mBServer.invoke(fname, operation, values, createStandardValveTypes); 150 151 TreeControl control = (TreeControl) 153 session.getAttribute("treeControlTest"); 154 if (control != null) { 155 TreeControlNode parentNode = control.findNode(parentNodeName); 156 if (parentNode != null) { 157 String nodeLabel = 158 "Valve for " + parentNode.getLabel(); 159 String encodedName = 160 URLEncoder.encode(vObjectName,TomcatTreeBuilder.URL_ENCODING); 161 TreeControlNode childNode = 162 new TreeControlNode(vObjectName, 163 "Valve.gif", 164 nodeLabel, 165 "EditValve.do?select=" + encodedName + 166 "&nodeLabel=" + URLEncoder.encode(nodeLabel,TomcatTreeBuilder.URL_ENCODING) + 167 "&parent=" + URLEncoder.encode(parentNodeName,TomcatTreeBuilder.URL_ENCODING), 168 "content", 169 true, domain); 170 parentNode.addChild(childNode); 171 } else { 173 servlet.log 174 ("Cannot find parent node '" + parentNodeName + "'"); 175 } 176 } else { 177 servlet.log 178 ("Cannot find TreeControlNode!"); 179 } 180 181 } catch (Exception e) { 182 183 servlet.log 184 (resources.getMessage(locale, "users.error.invoke", 185 operation), e); 186 response.sendError 187 (HttpServletResponse.SC_INTERNAL_SERVER_ERROR, 188 resources.getMessage(locale, "users.error.invoke", 189 operation)); 190 return (null); 191 192 } 193 194 session.removeAttribute(mapping.getAttribute()); 196 return vObjectName; 197 } 198 199 200 210 public static Pattern [] precalculate(String list) 211 throws IllegalArgumentException { 212 213 if (list == null) 214 return (new Pattern [0]); 215 list = list.trim(); 216 if (list.length() < 1) 217 return (new Pattern [0]); 218 list += ","; 219 220 ArrayList reList = new ArrayList (); 221 while (list.length() > 0) { 222 int comma = list.indexOf(','); 223 if (comma < 0) 224 break; 225 String pattern = list.substring(0, comma).trim(); 226 try { 227 reList.add(Pattern.compile(pattern)); 228 } catch (PatternSyntaxException e) { 229 throw new IllegalArgumentException 230 ("Syntax error in request filter pattern"); 231 } 232 list = list.substring(comma + 1); 233 } 234 235 Pattern reArray[] = new Pattern [reList.size()]; 236 return ((Pattern []) reList.toArray(reArray)); 237 238 } 239 240 public static String getObjectName(String parent, String MBeanType) 241 throws Exception { 242 243 ObjectName poname = new ObjectName (parent); 246 String domain = poname.getDomain(); 247 StringBuffer sb = new StringBuffer (domain+MBeanType); 248 String type = poname.getKeyProperty("type"); 249 String j2eeType = poname.getKeyProperty("j2eeType"); 250 String path = ""; 251 String host = ""; 252 String name = poname.getKeyProperty("name"); 253 if ((name != null) && (name.length() > 0)) { 254 name = name.substring(2); 255 int i = name.indexOf("/"); 256 host = name.substring(0,i); 257 path = name.substring(i); 258 } 259 if ("WebModule".equalsIgnoreCase(j2eeType)) { sb.append(",path="+path); 261 sb.append(",host="+host); 262 } 263 if ("Host".equalsIgnoreCase(type)) { sb.append(",host="); 265 sb.append(poname.getKeyProperty("host")); 266 } 267 if ("Service".equalsIgnoreCase(type)) { } 269 return sb.toString(); 270 } 271 272 } 273 | Popular Tags |