1 23 24 29 30 package com.sun.enterprise.tools.admingui.util; 31 32 import javax.management.MBeanException ; 33 import javax.management.ObjectName ; 34 import javax.management.AttributeList ; 35 import javax.management.Attribute ; 36 import javax.management.MBeanServerConnection ; 37 38 import com.iplanet.jato.RequestContext; 39 import com.iplanet.jato.RequestManager; 40 import com.iplanet.jato.util.NonSyncStringBuffer; 41 import java.util.ArrayList ; 42 import java.util.Map ; 43 import java.util.Set ; 44 import java.util.HashMap ; 45 46 import com.sun.enterprise.admin.common.MBeanServerFactory; 47 import com.sun.enterprise.admin.config.MBeanConfigInstanceNotFoundException; 48 49 import com.sun.enterprise.tools.guiframework.exception.FrameworkException; 50 51 public class MBeanUtil { 52 53 private static MBeanServerConnection remoteConnection = null; 54 55 58 public static Object invoke(String objectName, String operationName, Object [] params, String [] signature) { 59 try { 60 return invoke( 61 new ObjectName (objectName), operationName, params, signature); 62 } catch (Exception ex) { 63 if (Util.isLoggableFINE()) { 64 NonSyncStringBuffer buf = new NonSyncStringBuffer(); 65 buf.append(ex.getMessage()); 66 buf.append("MBUtil.invoke failed:\n"); 67 buf.append(" OBJECT NAME: "+objectName+"\n"); 68 buf.append(" OPERATION NAME: "+operationName+"\n"); 69 Util.logFINE(buf.toString()); 70 } 71 throw new FrameworkException(ex); 72 } 73 } 74 75 public static AttributeList getAttributes(String objectName, String [] attributeNames) { 76 try { 77 return getMBeanServer().getAttributes(new ObjectName (objectName), attributeNames); 78 } catch (Exception ex) { 79 throw new FrameworkException(ex); 80 } 81 } 82 83 public static Object getAttribute(ObjectName objectName, String attributeName) { 84 try { 85 return getMBeanServer().getAttribute(objectName, attributeName); 86 } catch (Exception ex) { 87 if (Util.isLoggableFINE()) { 88 NonSyncStringBuffer buf = new NonSyncStringBuffer(); 89 buf.append(ex.getMessage()); 90 buf.append("Attribute not found on the given Object:\n"); 91 buf.append(" OBJECT NAME: "+objectName+"\n"); 92 buf.append(" ATTRIBUTE NAME: "+attributeName+"\n"); 93 Util.logFINE(buf.toString()); 94 } 95 throw new FrameworkException(ex); 96 } 97 } 98 99 public static Object getAttribute(String objectName, String attributeName) { 100 try { 101 return getMBeanServer().getAttribute(new ObjectName (objectName), attributeName); 102 } catch (Exception ex) { 103 if (Util.isLoggableFINE()) { 104 NonSyncStringBuffer buf = new NonSyncStringBuffer(); 105 buf.append(ex.getMessage()); 106 buf.append("Attribute not found on the given Object:\n"); 107 buf.append(" OBJECT NAME: "+objectName+"\n"); 108 buf.append(" ATTRIBUTE NAME: "+attributeName+"\n"); 109 Util.logFINE(buf.toString()); 110 } 111 throw new FrameworkException(ex); 112 } 113 } 114 115 public static void setAttribute(String objectName, Attribute attributeName) { 116 try { 117 setAttribute(new ObjectName (objectName), attributeName); 118 } catch (Exception ex) { 119 throw new FrameworkException(ex); 120 } 121 } 122 123 public static void setAttribute(ObjectName objectName, Attribute attributeName) { 124 try { 125 getMBeanServer().setAttribute(objectName, attributeName); 126 } catch (Exception ex) { 127 throw new FrameworkException(ex); 128 } 129 } 130 131 public static AttributeList setAttributes(String objectName, AttributeList attributes) { 132 try { 133 return getMBeanServer().setAttributes(new ObjectName (objectName), attributes); 134 } catch (Exception ex) { 135 throw new FrameworkException(ex); 136 } 137 } 138 139 public static Object invoke(ObjectName objectName, String operationName, Object [] params, String [] signature) { 140 if (Util.isLoggableFINE()) { 141 NonSyncStringBuffer buf = new NonSyncStringBuffer(); 143 buf.append("***** Calling MBean Server *****\n"); 144 buf.append("objectName = "+objectName+"\n"); 145 buf.append("operationName = "+operationName+"\n"); 146 if (params != null) { 147 for (int i = 0; i < params.length; i++) { 148 buf.append("params["+i+"] = "+params[i]); 149 } 150 } 151 if (signature != null) { 152 for (int i = 0; i < signature.length; i++) { 153 buf.append("types["+i+"] = "+signature[i]); 154 } 155 } 156 buf.append("params = "+params); 157 buf.append("signature = "+signature); 158 Util.logFINE(buf.toString()); 159 } 160 161 try { 162 return getMBeanServer().invoke(objectName, operationName, params, signature); 163 } catch (Exception ex) { 164 if (ex.getCause() instanceof MBeanConfigInstanceNotFoundException) { 167 return null; 168 } 169 throw new FrameworkException(ex); 170 } 171 } 172 173 174 public static boolean isValidMBean(String objectName) { 175 boolean valid = false; 176 try { 177 Set beans = getMBeanServer().queryMBeans(new ObjectName (objectName), null); 178 if (beans.size() > 0) 179 valid = true; 180 } catch (Exception ex) { 181 } 183 return valid; 184 } 185 216 public static MBeanServerConnection getMBeanServer() { 217 return MBeanServerFactory.getMBeanServer(); 219 } 226 227 230 public static Object [] getParamsAndTypes(ArrayList paramsList, ArrayList typesList) { 231 String [] types = null; 232 Object [] params = null; 233 234 if (paramsList == null) 235 return new Object []{null, null}; 236 237 if (typesList == null) { 238 types = new String [paramsList.size()]; 239 for (int i = 0; i < types.length; i++) { 240 types[i] = "java.lang.String"; 241 } 242 if (paramsList != null) { 243 params = paramsList.toArray(); 244 } 245 return new Object [] {params, types}; 246 } 247 for (int i = 0; i < typesList.size(); i++) { 254 Object type = typesList.get(i); 255 Object param = paramsList.get(i); 256 257 if (type instanceof ArrayList ) { 258 ArrayList typeArrayList = (ArrayList )type; 259 String [] typesArray = (String [])typeArrayList.toArray(new String [typeArrayList.size()]); 260 typesList.remove(i); 261 for (int j = 0; j < typesArray.length; j++) { 262 typesList.add(i+j, typesArray[j]); 263 } 264 if (param != null) { 265 ArrayList paramArrayList = (ArrayList )param; 266 Object [] paramsArray = paramArrayList.toArray(); 267 paramsList.remove(i); 268 for (int j = 0; j < paramsArray.length; j++) { 269 paramsList.add(i+j, paramsArray[j]); 270 } 271 } 272 } 273 } 274 types = (String [])typesList.toArray(new String [typesList.size()]); 276 params = paramsList.toArray(); 277 for(int i = 0; i < types.length; i++) { 278 if(types[i].equals("boolean") || types[i].equals("java.lang.Boolean")) { 279 params[i] = new Boolean (params[i].toString()); 280 } 281 else if(types[i].equals("java.lang.Integer")) { 282 params[i] = new Integer (params[i].toString()); 283 } 284 } 285 return new Object [] {params, types}; 286 } 287 288 public static Object [] getParams(ArrayList params, String [] types) { 289 if (params == null) 290 return null; 291 292 if (types == null) 293 return null; 294 295 return params.toArray(); 296 } 297 298 299 public static void main(String _args[]) { 300 ArrayList typesList = new ArrayList (); 301 ArrayList paramsList = new ArrayList (); 302 typesList.add("java.lang.String"); 303 paramsList.add(null); 304 Object [] paramArray = null; 305 String [] typesArray = null; 306 Object [] paramsAndTypes = getParamsAndTypes(paramsList, typesList); 307 System.out.println(paramsAndTypes[0]); 308 } 309 } 310 | Popular Tags |