1 23 24 package com.sun.enterprise.admin.server.core.servlet; 25 26 import javax.servlet.http.*; 28 import javax.servlet.ServletException ; 29 30 import java.io.Serializable ; 32 import java.io.IOException ; 33 import java.io.ObjectInputStream ; 34 import java.io.ObjectOutputStream ; 35 import java.io.BufferedOutputStream ; 36 import java.io.ByteArrayOutputStream ; 37 import java.io.BufferedInputStream ; 38 import java.util.logging.Level ; 39 import java.util.logging.Logger ; 40 import javax.management.ObjectName ; 42 import javax.management.MBeanServer ; 43 import javax.management.Attribute ; 44 import javax.management.AttributeList ; 45 import javax.management.MBeanException ; 46 47 import com.sun.enterprise.admin.common.AdminRequest; 49 import com.sun.enterprise.admin.common.AdminRequestConfigurator; 50 import com.sun.enterprise.admin.common.AdminRequestType; 51 import com.sun.enterprise.admin.common.AdminResponse; 52 import com.sun.enterprise.admin.common.AdminResponseConfigurator; 53 import com.sun.enterprise.admin.common.MBeanServerFactory; 54 import com.sun.enterprise.admin.common.constant.AdminConstants; 55 import com.sun.enterprise.admin.common.exception.AFRuntimeException; 56 57 import com.sun.enterprise.util.i18n.StringManager; 59 60 74 75 public class AdminAPIEntryServlet extends HttpServlet 76 { 77 private static StringManager localStrings = 79 StringManager.getManager( AdminAPIEntryServlet.class ); 80 81 private MBeanServer mMBeanServer = null; 82 private Logger mLogger = Logger. 83 getLogger(AdminConstants.kLoggerName); 84 private final int SUPPORTED_CLIENT_MAJOR_VERSION = 2; 85 private final int SUPPORTED_CLIENT_MINOR_VERSION = 0; 86 93 public void init() throws ServletException 94 { 95 super.init(); 96 mMBeanServer = MBeanServerFactory.getMBeanServer(); 97 mLogger.log(Level.FINE, "comm.init_ok"); 98 } 99 100 public void doPost(HttpServletRequest request, HttpServletResponse response) 101 throws ServletException , IOException 102 { 103 doGet(request, response); 104 } 105 106 public void doGet(HttpServletRequest request, HttpServletResponse response) 107 throws ServletException , IOException 108 { 109 Serializable resultObject = "TEST"; 110 try 111 { 112 mLogger.log(Level.FINE, "comm.recd_request"); 113 ObjectInputStream inpStream = new ObjectInputStream ( 114 new BufferedInputStream ( 115 request.getInputStream())); 116 Object obj = inpStream.readObject(); 117 AdminRequest adminRequest = (AdminRequest)obj; 118 119 resultObject = clientVersionCheck(adminRequest); 120 if(resultObject == null) 121 resultObject = callMBean(adminRequest); 122 123 ObjectOutputStream oos = new ObjectOutputStream ( 124 new BufferedOutputStream (response.getOutputStream ())); 125 126 response.setHeader("Content-type", "application/octet-stream"); 127 final int contentLength = getContentLength(resultObject); 128 response.setContentLength(contentLength); 129 response.setStatus(HttpServletResponse.SC_OK); 130 131 oos.writeObject(resultObject); 132 oos.flush(); 133 134 } 135 catch(Exception e) 136 { 137 throw new ServletException (e.getMessage()); 138 } 139 } 140 141 private AdminResponse clientVersionCheck(AdminRequest req) 142 { 143 AdminRequestConfigurator requestConfig = new 144 AdminRequestConfigurator(req); 145 String clientVersion = null; 146 try 147 { 148 clientVersion = requestConfig.getClientVersion(); 149 int dotIdx = clientVersion.indexOf('.'); 150 int majorVersion = (Integer.valueOf(clientVersion.substring(0, dotIdx))).intValue(); 151 int minorVersion = (Integer.valueOf(clientVersion.substring(dotIdx+1))).intValue(); 152 if(majorVersion == SUPPORTED_CLIENT_MAJOR_VERSION && 153 minorVersion <= SUPPORTED_CLIENT_MINOR_VERSION ) 154 return null; 155 } 156 catch (Exception e) 157 { 158 } 159 160 AdminResponse response = new AdminResponse(); 163 AdminResponseConfigurator config = new AdminResponseConfigurator(response); 164 String msg; 165 if(clientVersion == null) 166 msg = localStrings.getString( "admin.server.core.servlet.no_client_version" ); 167 else 168 msg = localStrings.getString( "admin.server.core.servlet.nonsupported_client_version", clientVersion); 169 config.setException(new AFRuntimeException(msg)); 170 return response; 171 } 172 173 private synchronized AdminResponse callMBean(AdminRequest req) 174 { 175 String type = req.getRequestType(); 176 AdminResponse response = null; 177 178 if (type.equals(AdminRequestType.INVOKE)) 179 { 180 response = callInvoke(req); 181 } 182 else if(type.equals(AdminRequestType.GET_ATTRIBUTE)) 183 { 184 response = callGetAttribute(req); 185 } 186 else if(type.equals(AdminRequestType.SET_ATTRIBUTE)) 187 { 188 response = callSetAttribute(req); 189 } 190 else if (type.equals(AdminRequestType.GET_ATTRIBUTES)) 191 { 192 response = callGetAttributes(req); 193 } 194 else if (type.equals(AdminRequestType.SET_ATTRIBUTES)) 195 { 196 response = callSetAttributes(req); 197 } 198 else 199 { 200 response = new AdminResponse(); 201 AdminResponseConfigurator config = new 202 AdminResponseConfigurator(response); 203 config.setException(new Exception ("No Such Type")); 204 } 205 return ( response ); 206 } 207 private synchronized AdminResponse callInvoke(AdminRequest req) 208 { 209 Object invokeResult = null; 210 AdminResponse response = new AdminResponse(); 211 AdminResponseConfigurator responseConfig = new 212 AdminResponseConfigurator(response); 213 AdminRequestConfigurator requestConfig = new 214 AdminRequestConfigurator(req); 215 ObjectName objName = requestConfig.getObjectName(); 216 String oprName = requestConfig.getOperationName(); 217 Object [] params = requestConfig.getOperationParams(); 218 String [] signature = requestConfig.getOperationSignature(); 219 try 220 { 221 invokeResult = 222 mMBeanServer.invoke(objName, oprName, params, signature); 223 responseConfig.setReturnValue((Serializable )invokeResult); 224 mLogger.log(Level.FINE, "comm.remote_invoke_ok", objName); 225 } 226 catch(Exception e) 227 { 228 mLogger.log(Level.WARNING, "comm.remote_invoke_failed", unwrapMBeanException(e)); 229 responseConfig.setException(e); 230 } 231 return ( response ); 232 } 233 234 private synchronized AdminResponse callGetAttribute(AdminRequest req) 235 { 236 Object invokeResult = null; 237 AdminResponse response = new AdminResponse(); 238 AdminResponseConfigurator responseConfig = new 239 AdminResponseConfigurator(response); 240 AdminRequestConfigurator requestConfig = new 241 AdminRequestConfigurator(req); 242 ObjectName objName = requestConfig.getObjectName(); 243 String attrName = requestConfig.getAttributeName(); 244 try 245 { 246 invokeResult = mMBeanServer.getAttribute(objName, attrName); 247 responseConfig.setReturnValue((Serializable )invokeResult); 248 mLogger.log(Level.FINE, "comm.get_attr_ok", objName); 249 } 250 catch(Exception e) 251 { 252 mLogger.log(Level.WARNING, "comm.get_attr_failed", unwrapMBeanException(e)); 253 responseConfig.setException(e); 254 } 255 return ( response ); 256 } 257 private synchronized AdminResponse callSetAttribute(AdminRequest req) 258 { 259 Object invokeResult = null; 260 AdminResponse response = new AdminResponse(); 261 AdminResponseConfigurator responseConfig = new 262 AdminResponseConfigurator(response); 263 AdminRequestConfigurator requestConfig = new 264 AdminRequestConfigurator(req); 265 ObjectName objName = requestConfig.getObjectName(); 266 Attribute attribute = requestConfig.getAttribute(); 267 try 268 { 269 mMBeanServer.setAttribute(objName, attribute); 270 String setValue = "value set: " + attribute.getValue(); 271 responseConfig.setReturnValue(setValue); 272 mLogger.log(Level.FINE, "comm.set_attr_ok", objName); 273 } 274 catch(Exception e) 275 { 276 mLogger.log(Level.WARNING, "comm.set_attr_failed", unwrapMBeanException(e)); 277 responseConfig.setException(e); 278 } 279 return ( response ); 280 } 281 private synchronized AdminResponse callGetAttributes(AdminRequest req) 282 { 283 Object invokeResult = null; 284 AdminResponse response = new AdminResponse(); 285 AdminResponseConfigurator responseConfig = new 286 AdminResponseConfigurator(response); 287 AdminRequestConfigurator requestConfig = new 288 AdminRequestConfigurator(req); 289 ObjectName mbeanName = requestConfig.getObjectName(); 290 String [] attributes = requestConfig.getAttributeNames(); 291 try 292 { 293 AttributeList values = mMBeanServer.getAttributes(mbeanName, 294 attributes); 295 responseConfig.setReturnValue(values); 296 mLogger.log(Level.FINE, "comm.get_attrs_ok", mbeanName); 297 } 298 catch(Exception t) 299 { 300 mLogger.log(Level.WARNING, "comm.get_attrs_failed", unwrapMBeanException(t)); 301 responseConfig.setException(t); 302 } 303 return ( response ); 304 } 305 306 private synchronized AdminResponse callSetAttributes(AdminRequest req) 307 { 308 Object invokeResult = null; 309 AdminResponse response = new AdminResponse(); 310 AdminResponseConfigurator responseConfig = new 311 AdminResponseConfigurator(response); 312 AdminRequestConfigurator requestConfig = new 313 AdminRequestConfigurator(req); 314 ObjectName mbeanName = requestConfig.getObjectName(); 315 AttributeList attributes = requestConfig.getAttributeList(); 316 try 317 { 318 AttributeList values = mMBeanServer.setAttributes(mbeanName, 319 attributes); 320 responseConfig.setReturnValue(values); 321 mLogger.log(Level.FINE, "comm.set_attrs_ok", mbeanName); 322 } 323 catch(Exception e) 324 { 325 mLogger.log(Level.WARNING, "comm.set_attrs_failed", unwrapMBeanException(e)); 326 responseConfig.setException(e); 327 } 328 return ( response ); 329 } 330 331 332 336 private int getContentLength(Serializable serObject) 337 { 338 int size = 0; 339 ObjectOutputStream oos = null; 340 341 try 342 { 343 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 344 oos = new ObjectOutputStream (baos); 345 oos.writeObject(serObject); 346 size = baos.size(); 347 } 348 catch (Exception e) 349 { 350 e.printStackTrace(); 351 } 352 finally 353 { 354 try 355 { 356 if (oos != null) 357 { 358 oos.close(); 359 } 360 } 361 catch (Exception e) 362 { 363 e.printStackTrace(); 364 } 365 } 366 return size; 367 } 368 369 370 private Exception unwrapMBeanException(Exception e) 371 { 372 while(e instanceof MBeanException ) 373 { 374 e = ((MBeanException )e).getTargetException(); 375 } 376 return e; 377 } 378 } 379 | Popular Tags |