1 23 24 package com.sun.enterprise.admin.comm; 25 26 import java.io.IOException ; 27 28 import javax.management.ObjectName ; 29 import javax.management.Attribute ; 30 import javax.management.AttributeList ; 31 import javax.management.MBeanException ; 32 import javax.management.ReflectionException ; 33 import javax.management.InstanceNotFoundException ; 34 import javax.management.AttributeNotFoundException ; 35 import javax.management.InvalidAttributeValueException ; 36 import javax.management.RuntimeErrorException ; 37 38 import com.sun.enterprise.admin.util.*; 39 import com.sun.enterprise.admin.common.AdminRequest; 40 import com.sun.enterprise.admin.common.AdminRequestType; 41 import com.sun.enterprise.admin.common.AdminResponse; 42 import com.sun.enterprise.admin.common.AdminRequestConfigurator; 43 import com.sun.enterprise.admin.common.AdminResponseConfigurator; 44 import com.sun.enterprise.admin.common.exception.AFRuntimeException; 45 46 48 public class AdminConnectorClient implements ConnectorClient 49 { 50 private final HttpConnectorAddress connectorAddress; 51 private final static String ADMIN_CLIENT_VERSION = "2.0"; 52 53 public AdminConnectorClient(HttpConnectorAddress address) 54 { 55 this.connectorAddress = address; 56 Debug.println("AdminConnectorClient.<init> : " + 57 "host = " + address.getHost() + 58 " port = " + address.getPort() + 59 " user = " + address.getAuthenticationInfo().getUser() ); 60 } 61 62 64 public String getClientVersion() 65 { 66 return ADMIN_CLIENT_VERSION; 67 } 68 69 71 public Object invoke(ObjectName mbeanName, 72 String operationName, 73 Object [] params, 74 String [] signature) 75 throws InstanceNotFoundException , MBeanException , ReflectionException 76 { 77 Object returnValue = null; 78 AdminRequest request = new AdminRequest(AdminRequestType.INVOKE); 79 AdminRequestConfigurator reqConfigurator = 80 new AdminRequestConfigurator(request); 81 reqConfigurator.setObjectName(mbeanName); 82 reqConfigurator.setOperationName(operationName); 83 reqConfigurator.setOperationParams(params); 84 reqConfigurator.setOperationSignature(signature); 85 reqConfigurator.setClientVersion(ADMIN_CLIENT_VERSION); 86 87 AdminResponse response = sendRequest(request); 88 AdminResponseConfigurator resConfigurator = 89 new AdminResponseConfigurator(response); 90 if (resConfigurator.hasException()) 91 { 92 Throwable t = resConfigurator.getException(); 93 if (t instanceof InstanceNotFoundException ) 94 { 95 throw (InstanceNotFoundException )t; 96 } 97 else if (t instanceof MBeanException ) 98 { 99 throw (MBeanException )t; 100 } 101 else if (t instanceof ReflectionException ) 102 { 103 throw (ReflectionException )t; 104 } 105 else if (t instanceof RuntimeException ) 106 { 107 throw (RuntimeException )t; 108 } 109 else if (t instanceof Error ) 110 { 111 throw new RuntimeErrorException ((Error )t); 112 } 113 } 114 else 115 { 116 returnValue = resConfigurator.getReturnValue(); 117 } 118 return returnValue; 119 } 120 121 123 public Object getAttribute(ObjectName mbeanName, String attributeName) 124 throws MBeanException , AttributeNotFoundException , 125 InstanceNotFoundException , ReflectionException 126 { 127 Object returnValue = null; 128 AdminRequest request = new AdminRequest(AdminRequestType.GET_ATTRIBUTE); 129 AdminRequestConfigurator reqConfigurator = 130 new AdminRequestConfigurator(request); 131 reqConfigurator.setObjectName(mbeanName); 132 reqConfigurator.setAttributeName(attributeName); 133 reqConfigurator.setClientVersion(ADMIN_CLIENT_VERSION); 134 AdminResponse response = sendRequest(request); 135 AdminResponseConfigurator resConfigurator = 137 new AdminResponseConfigurator(response); 138 if (resConfigurator.hasException()) 139 { 140 Throwable t = resConfigurator.getException(); 141 if (t instanceof AttributeNotFoundException ) 142 { 143 throw (AttributeNotFoundException )t; 144 } 145 else if (t instanceof InstanceNotFoundException ) 146 { 147 throw (InstanceNotFoundException )t; 148 } 149 else if (t instanceof MBeanException ) 150 { 151 throw (MBeanException )t; 152 } 153 else if (t instanceof ReflectionException ) 154 { 155 throw (ReflectionException )t; 156 } 157 else if (t instanceof RuntimeException ) 158 { 159 throw (RuntimeException )t; 160 } 161 else if (t instanceof Error ) 162 { 163 throw new RuntimeErrorException ((Error )t); 164 } 165 } 166 else 167 { 168 returnValue = resConfigurator.getReturnValue(); 169 } 170 return returnValue; 171 } 172 173 175 public void setAttribute(ObjectName mbeanName, Attribute attribute) 176 throws InstanceNotFoundException , AttributeNotFoundException , 177 InvalidAttributeValueException , MBeanException , 178 ReflectionException 179 { 180 AdminRequest request = new AdminRequest(AdminRequestType.SET_ATTRIBUTE); 181 AdminRequestConfigurator reqConfigurator = 182 new AdminRequestConfigurator(request); 183 reqConfigurator.setObjectName(mbeanName); 184 reqConfigurator.setAttribute(attribute); 185 reqConfigurator.setClientVersion(ADMIN_CLIENT_VERSION); 186 AdminResponse response = sendRequest(request); 187 AdminResponseConfigurator resConfigurator = 189 new AdminResponseConfigurator(response); 190 if (resConfigurator.hasException()) 191 { 192 Throwable t = resConfigurator.getException(); 193 if (t instanceof AttributeNotFoundException ) 194 { 195 throw (AttributeNotFoundException )t; 196 } 197 else if (t instanceof InvalidAttributeValueException ) 198 { 199 throw (InvalidAttributeValueException )t; 200 } 201 else if (t instanceof InstanceNotFoundException ) 202 { 203 throw (InstanceNotFoundException )t; 204 } 205 else if (t instanceof MBeanException ) 206 { 207 throw (MBeanException )t; 208 } 209 else if (t instanceof ReflectionException ) 210 { 211 throw (ReflectionException )t; 212 } 213 else if (t instanceof RuntimeException ) 214 { 215 throw (RuntimeException )t; 216 } 217 else if (t instanceof Error ) 218 { 219 throw new RuntimeErrorException ((Error )t); 220 } 221 } 222 } 223 224 226 public AttributeList getAttributes(ObjectName mbeanName, 227 String [] attributes) 228 throws InstanceNotFoundException , ReflectionException 229 { 230 AttributeList values = null; 231 AdminRequest request = new AdminRequest( 232 AdminRequestType.GET_ATTRIBUTES); 233 AdminRequestConfigurator reqConfigurator = 234 new AdminRequestConfigurator(request); 235 reqConfigurator.setObjectName(mbeanName); 236 reqConfigurator.setAttributeNames(attributes); 237 reqConfigurator.setClientVersion(ADMIN_CLIENT_VERSION); 238 AdminResponse response = sendRequest(request); 239 AdminResponseConfigurator resConfigurator = 241 new AdminResponseConfigurator(response); 242 if (resConfigurator.hasException()) 243 { 244 Throwable t = resConfigurator.getException(); 245 if (t instanceof InstanceNotFoundException ) 246 { 247 throw (InstanceNotFoundException )t; 248 } 249 else if (t instanceof ReflectionException ) 250 { 251 throw (ReflectionException )t; 252 } 253 else if (t instanceof RuntimeException ) 254 { 255 throw (RuntimeException )t; 256 } 257 else if (t instanceof Error ) 258 { 259 throw new RuntimeErrorException ((Error )t); 260 } 261 } 262 else 263 { 264 values = (AttributeList ) resConfigurator.getReturnValue(); 265 } 266 return values; 267 } 268 269 271 public AttributeList setAttributes(ObjectName mbeanName, 272 AttributeList attributes) 273 throws InstanceNotFoundException , ReflectionException 274 { 275 AttributeList values = null; 276 AdminRequest request = new AdminRequest( 277 AdminRequestType.SET_ATTRIBUTES); 278 AdminRequestConfigurator reqConfigurator = 279 new AdminRequestConfigurator(request); 280 reqConfigurator.setObjectName(mbeanName); 281 reqConfigurator.setAttributeList(attributes); 282 reqConfigurator.setClientVersion(ADMIN_CLIENT_VERSION); 283 AdminResponse response = sendRequest(request); 284 AdminResponseConfigurator resConfigurator = 286 new AdminResponseConfigurator(response); 287 if (resConfigurator.hasException()) 288 { 289 Throwable t = resConfigurator.getException(); 290 if (t instanceof InstanceNotFoundException ) 291 { 292 throw (InstanceNotFoundException )t; 293 } 294 else if (t instanceof ReflectionException ) 295 { 296 throw (ReflectionException )t; 297 } 298 else if (t instanceof RuntimeException ) 299 { 300 throw (RuntimeException )t; 301 } 302 else if (t instanceof Error ) 303 { 304 throw new RuntimeErrorException ((Error )t); 305 } 306 } 307 else 308 { 309 values = (AttributeList ) resConfigurator.getReturnValue(); 310 } 311 return values; 312 } 313 314 private AdminResponse sendRequest(AdminRequest request) 315 throws AFConnectionException, AFRuntimeException 316 { 317 AdminResponse response = null; 318 IConnection connection = null; 319 try 320 { 321 connection = ConnectionFactory.createConnection(connectorAddress); 322 connection.send(request); 323 response = (AdminResponse)connection.receive(); 324 } 325 catch (IOException e) 326 { 327 Debug.printStackTrace(e); 328 throw new AFConnectionException(e.getMessage()); 329 } 330 catch (ClassNotFoundException cnfe) 331 { 332 throw new AFRuntimeException(cnfe.toString()); 333 } 334 catch (Exception e) 335 { 336 throw new AFRuntimeException(e.getLocalizedMessage()); 337 } 338 finally 339 { 340 if (connection != null) 341 { 342 try 343 { 344 connection.close(); 345 } 346 catch (Exception e) 347 { 348 ExceptionUtil.ignoreException(e); 349 } 350 } 351 } 352 return response; 353 } 354 } 355 | Popular Tags |