1 12 13 package org.ejtools.jmx.browser.web.action; 14 15 16 17 import java.beans.PropertyEditor ; 18 19 import java.io.IOException ; 20 21 import java.io.PrintWriter ; 22 23 import java.io.StringWriter ; 24 25 import java.util.Locale ; 26 27 import java.util.StringTokenizer ; 28 29 import java.util.Vector ; 30 31 32 33 import javax.management.ObjectInstance ; 34 35 import javax.management.ObjectName ; 36 37 import javax.servlet.ServletContext ; 38 39 import javax.servlet.ServletException ; 40 41 import javax.servlet.http.HttpServletRequest ; 42 43 import javax.servlet.http.HttpServletResponse ; 44 45 46 47 import org.apache.log4j.Logger; 48 49 import org.apache.struts.action.Action; 50 51 import org.apache.struts.action.ActionError; 52 53 import org.apache.struts.action.ActionErrors; 54 55 import org.apache.struts.action.ActionForm; 56 57 import org.apache.struts.action.ActionForward; 58 59 import org.apache.struts.action.ActionMapping; 60 61 import org.apache.struts.util.MessageResources; 62 63 import org.ejtools.beans.CustomPropertyEditorManager; 64 65 import org.ejtools.jmx.browser.web.Constants; 66 67 import org.ejtools.jmx.browser.web.JMXContainer; 68 69 import org.ejtools.jmx.browser.web.form.RegisterForm; 70 71 72 73 88 89 public class RegisterAction extends Action 90 91 { 92 93 94 95 private static Logger logger = Logger.getLogger(RegisterAction.class); 96 97 98 99 100 101 102 103 public RegisterAction() { } 104 105 106 107 108 109 130 131 public ActionForward perform(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 132 133 throws IOException , ServletException  134 135 { 136 137 JMXContainer tree = null; 138 139 RegisterForm registerForm = null; 140 141 142 143 145 Locale locale = getLocale(request); 146 147 MessageResources messages = getResources(); 148 149 150 151 153 ActionErrors errors = new ActionErrors(); 154 155 156 157 if (form instanceof RegisterForm) 158 159 { 160 161 registerForm = (RegisterForm) form; 162 163 } 164 165 else 166 167 { 168 169 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("web.error.no.form")); 170 171 } 172 173 174 175 logger.debug("Connecting to JMX Server"); 176 177 178 179 ServletContext context = this.getServlet().getServletContext(); 180 181 tree = (JMXContainer) context.getAttribute(Constants.TREE); 182 183 184 185 if (tree != null) 186 187 { 188 189 logger.debug("Tree root found => " + tree); 190 191 192 193 String className = null; 194 195 ObjectName name = null; 196 197 ObjectName loaderName = null; 198 199 String sParameters = null; 200 201 String sSignature = null; 202 203 204 205 Object [] parameters = null; 206 207 String [] signature = null; 208 209 210 211 StringTokenizer st = null; 212 213 Vector result = null; 214 215 216 217 try 218 219 { 220 221 className = registerForm.getClassName(); 222 223 name = new ObjectName (registerForm.getObjectName()); 224 225 226 227 if ("".equals(registerForm.getClassLoader())) 228 229 { 230 231 loaderName = null; 232 233 } 234 235 else 236 237 { 238 239 loaderName = new ObjectName (registerForm.getClassLoader()); 240 241 } 242 243 244 245 sParameters = registerForm.getParameters(); 246 247 sSignature = registerForm.getSignature(); 248 249 if ((!"".equals(sParameters)) && (!"".equals(sSignature))) 250 251 { 252 253 StringTokenizer stParameters = new StringTokenizer (sParameters, ","); 254 255 StringTokenizer stSignature = new StringTokenizer (sSignature, ","); 256 257 if (stParameters.countTokens() == stSignature.countTokens()) 258 259 { 260 261 try 262 263 { 264 265 parameters = new String [stParameters.countTokens()]; 266 267 signature = new String [stSignature.countTokens()]; 268 269 270 271 for (int i = 0; stParameters.hasMoreTokens(); i++) 272 273 { 274 275 signature[i] = stSignature.nextToken(); 276 277 Class cl = Thread.currentThread().getContextClassLoader().loadClass(signature[i]); 278 279 PropertyEditor pe = CustomPropertyEditorManager.findEditor(cl); 280 281 pe.setAsText(stParameters.nextToken()); 282 283 parameters[i] = pe.getValue(); 284 285 } 286 287 } 288 289 catch (Exception e) 290 291 { 292 293 logger.error("Exception occured " + e.getMessage()); 294 295 throw e; 296 297 } 298 299 } 300 301 } 302 303 304 305 ObjectInstance instance = tree.createMBean(className, name, loaderName, parameters, signature); 306 307 308 309 if (instance == null) 310 311 { 312 313 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("web.error.mbean.register.failed", name)); 314 315 } 316 317 } 318 319 catch (Exception e) 320 321 { 322 323 logger.error("Exception occured " + e.getMessage()); 324 325 326 327 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("web.error.mbean.register.failed", name)); 328 329 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("web.error.exception.message", e.getMessage())); 330 331 332 333 StringWriter w = new StringWriter (); 334 335 PrintWriter pw = new PrintWriter (w); 336 337 e.printStackTrace(pw); 338 339 pw.close(); 340 341 342 343 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("web.error.exception.stack", w.toString())); 344 345 } 346 347 } 348 349 else 350 351 { 352 353 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("web.error.cannot.connect")); 354 355 } 356 357 358 359 361 if (!errors.empty()) 362 363 { 364 365 saveErrors(request, errors); 366 367 return (new ActionForward(mapping.getInput())); 368 369 } 370 371 372 373 return (mapping.findForward("view")); 374 375 } 376 377 378 379 380 381 394 395 protected Vector parseTokenizedString(String toParse, String separator) 396 397 { 398 399 Vector result = new Vector (); 400 401 StringTokenizer st = new StringTokenizer (toParse, separator); 402 403 while (st.hasMoreTokens()) 404 405 { 406 407 result.add(st.nextToken()); 408 409 } 410 411 return result; 412 413 } 414 415 } 416 417
| Popular Tags
|