KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > server > core > servlet > AdminAPIEntryServlet


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.admin.server.core.servlet;
25
26 //j2ee imports
27
import javax.servlet.http.*;
28 import javax.servlet.ServletException JavaDoc;
29
30 //jdk imports
31
import java.io.Serializable JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.io.ObjectInputStream JavaDoc;
34 import java.io.ObjectOutputStream JavaDoc;
35 import java.io.BufferedOutputStream JavaDoc;
36 import java.io.ByteArrayOutputStream JavaDoc;
37 import java.io.BufferedInputStream JavaDoc;
38 import java.util.logging.Level JavaDoc;
39 import java.util.logging.Logger JavaDoc;
40 //JMX imports
41
import javax.management.ObjectName JavaDoc;
42 import javax.management.MBeanServer JavaDoc;
43 import javax.management.Attribute JavaDoc;
44 import javax.management.AttributeList JavaDoc;
45 import javax.management.MBeanException JavaDoc;
46
47 //Admin imports
48
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 //i18n import
58
import com.sun.enterprise.util.i18n.StringManager;
59
60 /**
61     This is the entry point to the Admin Framework's API layer.
62     For each HTTP Request it receives, its principal functionality is to
63     <li>
64         deserialize the serialized java objects on its input stream sent by the client.
65     <li>
66         send the deserialized java objects to dispatcher available to it.
67     <li>
68         <strong> wait </strong> on dispatcher to return with response.
69     <li>
70         send the response on output stream after serializing it.
71
72     Hence it is to be noted that this class deals with serialized java objects.
73 */

74
75 public class AdminAPIEntryServlet extends HttpServlet
76 {
77     // i18n StringManager
78
private static StringManager localStrings =
79         StringManager.getManager( AdminAPIEntryServlet.class );
80
81     private MBeanServer JavaDoc mMBeanServer = null;
82     private Logger JavaDoc 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     /**
87         Populates the recipients in the dispatcher. These are the recipients
88         to whom the <strong> admin request </strong> will be forwarded. This needs
89         to be done only when the Servlet is loaded by the servlet container.
90
91         @throws ServletException when some configuration error occurs.
92     */

93     public void init() throws ServletException JavaDoc
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 JavaDoc, IOException JavaDoc
102     {
103         doGet(request, response);
104     }
105
106     public void doGet(HttpServletRequest request, HttpServletResponse response)
107         throws ServletException JavaDoc, IOException JavaDoc
108     {
109         Serializable JavaDoc resultObject = "TEST";
110         try
111         {
112             mLogger.log(Level.FINE, "comm.recd_request");
113             ObjectInputStream JavaDoc inpStream = new ObjectInputStream JavaDoc (
114             new BufferedInputStream JavaDoc(
115                 request.getInputStream()));
116             Object JavaDoc obj = inpStream.readObject();
117             AdminRequest adminRequest = (AdminRequest)obj;
118             
119             resultObject = clientVersionCheck(adminRequest);
120             if(resultObject == null)
121                 resultObject = callMBean(adminRequest);
122
123             ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc (
124                    new BufferedOutputStream JavaDoc (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 JavaDoc e)
136         {
137             throw new ServletException JavaDoc(e.getMessage());
138         }
139     }
140
141     private AdminResponse clientVersionCheck(AdminRequest req)
142     {
143         AdminRequestConfigurator requestConfig = new
144             AdminRequestConfigurator(req);
145         String JavaDoc 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 /* backward compartibility */)
154                 return null;
155         }
156         catch (Exception JavaDoc e)
157         {
158         }
159         
160         // here we are only in case of non-matching version
161
// prepare and wrap exception
162
AdminResponse response = new AdminResponse();
163         AdminResponseConfigurator config = new AdminResponseConfigurator(response);
164         String JavaDoc 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 JavaDoc 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 JavaDoc("No Such Type"));
204         }
205         return ( response );
206     }
207     private synchronized AdminResponse callInvoke(AdminRequest req)
208     {
209         Object JavaDoc invokeResult = null;
210         AdminResponse response = new AdminResponse();
211         AdminResponseConfigurator responseConfig = new
212             AdminResponseConfigurator(response);
213         AdminRequestConfigurator requestConfig = new
214             AdminRequestConfigurator(req);
215         ObjectName JavaDoc objName = requestConfig.getObjectName();
216         String JavaDoc oprName = requestConfig.getOperationName();
217         Object JavaDoc[] params = requestConfig.getOperationParams();
218         String JavaDoc[] signature = requestConfig.getOperationSignature();
219         try
220         {
221             invokeResult =
222                 mMBeanServer.invoke(objName, oprName, params, signature);
223             responseConfig.setReturnValue((Serializable JavaDoc)invokeResult);
224             mLogger.log(Level.FINE, "comm.remote_invoke_ok", objName);
225         }
226         catch(Exception JavaDoc 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 JavaDoc invokeResult = null;
237         AdminResponse response = new AdminResponse();
238         AdminResponseConfigurator responseConfig = new
239             AdminResponseConfigurator(response);
240         AdminRequestConfigurator requestConfig = new
241             AdminRequestConfigurator(req);
242         ObjectName JavaDoc objName = requestConfig.getObjectName();
243         String JavaDoc attrName = requestConfig.getAttributeName();
244         try
245         {
246             invokeResult = mMBeanServer.getAttribute(objName, attrName);
247             responseConfig.setReturnValue((Serializable JavaDoc)invokeResult);
248             mLogger.log(Level.FINE, "comm.get_attr_ok", objName);
249         }
250         catch(Exception JavaDoc 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 JavaDoc invokeResult = null;
260         AdminResponse response = new AdminResponse();
261         AdminResponseConfigurator responseConfig = new
262             AdminResponseConfigurator(response);
263         AdminRequestConfigurator requestConfig = new
264             AdminRequestConfigurator(req);
265         ObjectName JavaDoc objName = requestConfig.getObjectName();
266         Attribute JavaDoc attribute = requestConfig.getAttribute();
267         try
268         {
269             mMBeanServer.setAttribute(objName, attribute);
270             String JavaDoc setValue = "value set: " + attribute.getValue();
271             responseConfig.setReturnValue(setValue);
272             mLogger.log(Level.FINE, "comm.set_attr_ok", objName);
273         }
274         catch(Exception JavaDoc 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 JavaDoc invokeResult = null;
284         AdminResponse response = new AdminResponse();
285         AdminResponseConfigurator responseConfig = new
286             AdminResponseConfigurator(response);
287         AdminRequestConfigurator requestConfig = new
288             AdminRequestConfigurator(req);
289         ObjectName JavaDoc mbeanName = requestConfig.getObjectName();
290         String JavaDoc[] attributes = requestConfig.getAttributeNames();
291         try
292         {
293             AttributeList JavaDoc values = mMBeanServer.getAttributes(mbeanName,
294                                                               attributes);
295             responseConfig.setReturnValue(values);
296             mLogger.log(Level.FINE, "comm.get_attrs_ok", mbeanName);
297         }
298         catch(Exception JavaDoc 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 JavaDoc invokeResult = null;
309         AdminResponse response = new AdminResponse();
310         AdminResponseConfigurator responseConfig = new
311             AdminResponseConfigurator(response);
312         AdminRequestConfigurator requestConfig = new
313             AdminRequestConfigurator(req);
314         ObjectName JavaDoc mbeanName = requestConfig.getObjectName();
315         AttributeList JavaDoc attributes = requestConfig.getAttributeList();
316         try
317         {
318             AttributeList JavaDoc values = mMBeanServer.setAttributes(mbeanName,
319                                                               attributes);
320             responseConfig.setReturnValue(values);
321             mLogger.log(Level.FINE, "comm.set_attrs_ok", mbeanName);
322         }
323         catch(Exception JavaDoc 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     /** Returns the size of given seialized object in bytes.
333         The size is calculated from the underlying ByteArrayOutputStream
334         backing an ObjectStream, onto which the Object is written.
335     */

336     private int getContentLength(Serializable JavaDoc serObject)
337     {
338         int size = 0;
339         ObjectOutputStream JavaDoc oos = null;
340
341         try
342         {
343             ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
344             oos = new ObjectOutputStream JavaDoc(baos);
345             oos.writeObject(serObject);
346             size = baos.size();
347         }
348         catch (Exception JavaDoc 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 JavaDoc e)
362             {
363                 e.printStackTrace();
364             }
365         }
366         return size;
367     }
368
369     
370     private Exception JavaDoc unwrapMBeanException(Exception JavaDoc e)
371     {
372         while(e instanceof MBeanException JavaDoc)
373         {
374             e = ((MBeanException JavaDoc)e).getTargetException();
375         }
376         return e;
377     }
378 }
379
Popular Tags