KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > management > j2ee > Servlet


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2005, JBoss Inc., and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22 package org.jboss.management.j2ee;
23
24 import org.jboss.logging.Logger;
25 import org.jboss.management.j2ee.statistics.ServletStatsImpl;
26 import org.jboss.management.j2ee.statistics.TimeStatisticImpl;
27
28 import javax.management.MBeanServer JavaDoc;
29 import javax.management.MalformedObjectNameException JavaDoc;
30 import javax.management.ObjectName JavaDoc;
31 import javax.management.j2ee.statistics.Stats JavaDoc;
32 import java.util.Hashtable JavaDoc;
33
34 /**
35  * The JBoss JSR-77.3.17 Servlet model implementation
36  *
37  * @author <a HREF="mailto:andreas@jboss.org">Andreas Schaefer</a>
38  * @author <a HREF="mailto:scott.stark@jboss.org">Scott Stark</a>
39  * @author <a HREF="mailto:thomas.diesler@jboss.org">Thomas Diesler</a>
40  * @version $Revision: 40550 $
41  */

42 public class Servlet extends J2EEManagedObject
43    implements ServletMBean
44 {
45
46    // Attributes ----------------------------------------------------
47
private static Logger log = Logger.getLogger(Servlet.class);
48
49    private ObjectName JavaDoc servletServiceName;
50    private ServletStatsImpl stats;
51
52    // Static --------------------------------------------------------
53
/**
54     * Create a JSR77 Servlet submodel.
55     *
56     * @param mbeanServer the MBeanServer to use for mbean creation
57     * @param webModuleName the name of the JSR77 web module mbean
58     * @param webContainerName the name of the JBoss web container mbean
59     * @param servletName the name of the servlet
60     * @return the ObjectName of the JSR77 Servlet mbean
61     */

62    public static ObjectName JavaDoc create(MBeanServer JavaDoc mbeanServer, ObjectName JavaDoc webModuleName,
63                                    ObjectName JavaDoc webContainerName, ObjectName JavaDoc servletServiceName)
64    {
65       try
66       {
67          Servlet servlet = new Servlet(servletServiceName, webModuleName, webContainerName);
68          ObjectName JavaDoc jsr77Name = servlet.getObjectName();
69          mbeanServer.registerMBean(servlet, jsr77Name);
70          log.debug("Created JSR-77 Servlet: " + jsr77Name);
71          return jsr77Name;
72       }
73       catch (Exception JavaDoc e)
74       {
75          log.debug("Could not create JSR-77 Servlet: " + servletServiceName, e);
76          return null;
77       }
78    }
79
80    public static void destroy(MBeanServer JavaDoc mbeanServer, ObjectName JavaDoc jsr77Name)
81    {
82       try
83       {
84          mbeanServer.unregisterMBean(jsr77Name);
85          log.debug("Destroyed JSR-77 Servlet: " + jsr77Name);
86       }
87       catch (javax.management.InstanceNotFoundException JavaDoc ignore)
88       {
89       }
90       catch (Exception JavaDoc e)
91       {
92          log.debug("Could not destroy JSR-77 Servlet: " + jsr77Name, e);
93       }
94    }
95
96    // Constructors --------------------------------------------------
97

98    /**
99     * @param pName Name of the Servlet
100     * @throws InvalidParameterException If list of nodes or ports was null or empty
101     */

102    public Servlet(ObjectName JavaDoc servletServiceName, ObjectName JavaDoc webModuleName,
103                   ObjectName JavaDoc webContainerName)
104            throws MalformedObjectNameException JavaDoc,
105            InvalidParentException
106    {
107       super(J2EETypeConstants.Servlet, servletServiceName.getKeyProperty("name"), webModuleName);
108       this.servletServiceName = servletServiceName;
109       this.stats = new ServletStatsImpl();
110    }
111
112    /**
113     * StatisticsProvider access to stats.
114     *
115     * @return A ServletStats implementation
116     * @jmx:managed-attribute
117     */

118    public Stats JavaDoc getstats()
119    {
120       try
121       {
122          TimeStatisticImpl serviceTime = (TimeStatisticImpl) stats.getServiceTime();
123          Integer JavaDoc count = (Integer JavaDoc) server.getAttribute(servletServiceName, "requestCount");
124          Long JavaDoc totalTime = (Long JavaDoc) server.getAttribute(servletServiceName, "processingTime");
125          Long JavaDoc minTime = (Long JavaDoc) server.getAttribute(servletServiceName, "minTime");
126          Long JavaDoc maxTime = (Long JavaDoc) server.getAttribute(servletServiceName, "maxTime");
127          serviceTime.set(count.longValue(), minTime.longValue(),
128                  maxTime.longValue(), totalTime.longValue());
129       }
130       catch (Exception JavaDoc e)
131       {
132          log.debug("Failed to retrieve stats", e);
133       }
134       return stats;
135    }
136
137    public void resetStats()
138    {
139       stats.reset();
140    }
141    // java.lang.Object overrides --------------------------------------
142

143    public String JavaDoc toString()
144    {
145       return "Servlet { " + super.toString() + " } []";
146    }
147
148    // Package protected ---------------------------------------------
149

150    // Protected -----------------------------------------------------
151

152    /**
153     * @return A hashtable with the Web-Module, J2EE-Application and J2EE-Server as parent
154     */

155    protected Hashtable JavaDoc getParentKeys(ObjectName JavaDoc pParent)
156    {
157       Hashtable JavaDoc lReturn = new Hashtable JavaDoc();
158       Hashtable JavaDoc lProperties = pParent.getKeyPropertyList();
159       lReturn.put(J2EETypeConstants.WebModule, lProperties.get("name"));
160       // J2EE-Application and J2EE-Server is already parent of J2EE-Application therefore lookup
161
// the name by the J2EE-Server type
162
lReturn.put(J2EETypeConstants.J2EEApplication, lProperties.get(J2EETypeConstants.J2EEApplication));
163       lReturn.put(J2EETypeConstants.J2EEServer, lProperties.get(J2EETypeConstants.J2EEServer));
164
165       return lReturn;
166    }
167
168 }
169
Popular Tags