1 12 13 package org.ejtools.jmx.browser.web.action; 14 15 16 17 import java.io.IOException; 18 19 import java.io.PrintWriter; 20 21 import java.io.StringWriter; 22 23 import java.util.Enumeration; 24 25 import java.util.Locale; 26 27 import java.util.Vector; 28 29 30 31 import javax.servlet.ServletContext; 32 33 import javax.servlet.ServletException; 34 35 import javax.servlet.http.HttpServletRequest; 36 37 import javax.servlet.http.HttpServletResponse; 38 39 40 41 import org.apache.log4j.Logger; 42 43 import org.apache.struts.action.Action; 44 45 import org.apache.struts.action.ActionError; 46 47 import org.apache.struts.action.ActionErrors; 48 49 import org.apache.struts.action.ActionForm; 50 51 import org.apache.struts.action.ActionForward; 52 53 import org.apache.struts.action.ActionMapping; 54 55 import org.apache.struts.util.MessageResources; 56 57 import org.ejtools.jmx.browser.model.Resource; 58 59 import org.ejtools.jmx.browser.web.Constants; 60 61 import org.ejtools.jmx.browser.web.JMXContainer; 62 63 64 65 82 83 public class NotificationAction extends Action 84 85 { 86 87 88 89 private static Logger logger = Logger.getLogger(NotificationAction.class); 90 91 92 93 94 95 96 97 public NotificationAction() { } 98 99 100 101 102 103 124 125 public ActionForward perform(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 126 127 throws IOException, ServletException 128 129 { 130 131 Vector parameters = new Vector(); 132 133 134 135 137 Locale locale = getLocale(request); 138 139 MessageResources messages = getResources(); 140 141 142 143 145 ActionErrors errors = new ActionErrors(); 146 147 148 149 ServletContext context = this.getServlet().getServletContext(); 150 151 JMXContainer tree = (JMXContainer) context.getAttribute(Constants.TREE); 152 153 154 155 if (tree != null) 156 157 { 158 159 logger.debug("Tree root found => " + tree); 160 161 162 163 Enumeration enum = request.getParameterNames(); 164 165 while (enum.hasMoreElements()) 166 167 { 168 169 String name = (String) enum.nextElement(); 170 171 172 173 logger.debug("Param " + name); 174 175 176 177 if (name.startsWith(":mbean:")) 178 179 { 180 181 String value = request.getParameter(name); 182 183 parameters.add(value); 184 185 logger.debug("Found " + value); 186 187 } 188 189 } 190 191 192 193 logger.debug("Parameters " + parameters.size()); 194 195 196 197 for (int i = 0; i < parameters.size(); i++) 198 199 { 200 201 String reference = (String) parameters.elementAt(i); 202 203 String value = request.getParameter(":state:" + reference); 204 205 206 207 logger.debug("Reference " + reference); 208 209 logger.debug("Value " + value); 210 211 212 213 Resource res = (Resource) tree.searchObjectName(reference); 214 215 216 217 logger.debug("ManagedObject " + res); 218 219 220 221 if (res != null) 222 223 { 224 225 try 226 227 { 228 229 if ("true".equals(value)) 230 231 { 232 233 res.registerForNotifications(); 234 235 } 236 237 else 238 239 { 240 241 res.unregisterForNotifications(); 242 243 } 244 245 } 246 247 catch (Exception e) 248 249 { 250 251 logger.error("Exception occured " + e.getMessage()); 252 253 254 255 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("web.error.exception.message", e.getMessage())); 256 257 258 259 StringWriter w = new StringWriter(); 260 261 PrintWriter pw = new PrintWriter(w); 262 263 e.printStackTrace(pw); 264 265 pw.close(); 266 267 268 269 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("web.error.exception.stack", w.toString())); 270 271 } 272 273 } 274 275 else 276 277 { 278 279 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("web.error.no.mbean")); 280 281 } 282 283 } 284 285 } 286 287 else 288 289 { 290 291 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("web.error.cannot.connect")); 292 293 } 294 295 296 297 299 if (!errors.empty()) 300 301 { 302 303 saveErrors(request, errors); 304 305 return (mapping.findForward("error")); 306 307 } 308 309 310 311 return (mapping.findForward("view")); 312 313 } 314 315 } 316 317
| Popular Tags
|