KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > excalibur > instrument > manager > http > InstrumentManagerHTTPConnector


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14  * implied.
15  *
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19
20 package org.apache.excalibur.instrument.manager.http;
21
22 import java.net.InetAddress JavaDoc;
23 import java.net.UnknownHostException JavaDoc;
24
25 import org.apache.avalon.framework.activity.Startable;
26 import org.apache.avalon.framework.configuration.Configurable;
27 import org.apache.avalon.framework.configuration.Configuration;
28 import org.apache.avalon.framework.configuration.ConfigurationException;
29
30 import org.apache.excalibur.instrument.AbstractLogEnabledInstrumentable;
31
32 import org.apache.excalibur.instrument.manager.DefaultInstrumentManager;
33 import org.apache.excalibur.instrument.manager.DefaultInstrumentManagerConnector;
34 import org.apache.excalibur.instrument.manager.http.server.AbstractHTTPURLHandler;
35 import org.apache.excalibur.instrument.manager.http.server.HTTPServer;
36
37 /**
38  * An HTTP connector which allows a client to connect to the ServiceManager
39  * using the HTTP protocol. This connector makes use of an extremely
40  * lightweight internal HTTP server to provide this access.
41  *
42  * If the application is already running a full blown Servlet Engine, one
43  * alternative to this connector is to make use of the InstrumentManagerServlet.
44  *
45  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
46  * @version CVS $Revision: 1.6 $ $Date: 2004/03/06 14:01:28 $
47  * @since 4.1
48  */

49 public class InstrumentManagerHTTPConnector
50     extends AbstractLogEnabledInstrumentable
51     implements DefaultInstrumentManagerConnector, Configurable, Startable
52 {
53     /** The default port. */
54     public static final int DEFAULT_PORT = 15080;
55     
56     /** The encoding to use when writing out pages and reading in parameters. */
57     public static final String JavaDoc ENCODING = "UTF-8";
58     public static final String JavaDoc XML_BANNER = "<?xml version='1.0' encoding='" + ENCODING + "'?>";
59
60     /** Reference to the actual instrument manager. */
61     private DefaultInstrumentManager m_manager;
62     
63     /** The port to listen on for connections. */
64     private int m_port;
65     
66     /** The address to bind the port server to. Null for any address. */
67     private InetAddress JavaDoc m_bindAddr;
68     
69     /** True if XML handlers should be registered. */
70     private boolean m_xml;
71     
72     /** True if HTML handlers should be registered. */
73     private boolean m_html;
74     
75     /** Default Chart Width. */
76     private int m_chartWidth;
77     
78     /** Default Chart Height. */
79     private int m_chartHeight;
80     
81     /** Antialias flag for images. */
82     private boolean m_antialias;
83     
84     /** The maximum number of leased samples which will be allowed. This is
85      * important to prevent denial of service attacks using connectors. */

86     private int m_maxLeasedSamples;
87     
88     /** The maximum size of a leased sample. This is important to prevent
89      * denial of service attacks using connectors. */

90     private int m_maxLeasedSampleSize;
91     
92     /** The maximum amount of time that a lease will be granted for. This is
93      * important to prevent denial of service attacks using connectors. */

94     private long m_maxLeasedSampleLease;
95     
96     /** True if the connector should only provide read-only access to the
97      * Instrument Manager. */

98     private boolean m_readOnly;
99     
100     /** The root bread crumb URL if configured. */
101     private String JavaDoc m_rootBreadCrumbURL;
102     
103     /** The root bread crumb label if configured. */
104     private String JavaDoc m_rootBreadCrumbLabel;
105     
106     private HTTPServer m_httpServer;
107
108     /*---------------------------------------------------------------
109      * Constructors
110      *-------------------------------------------------------------*/

111     /**
112      * Creates a new InstrumentManagerHTTPConnector.
113      */

114     public InstrumentManagerHTTPConnector()
115     {
116         setInstrumentableName( "http" );
117     }
118
119     /*---------------------------------------------------------------
120      * DefaultInstrumentManagerConnector Methods
121      *-------------------------------------------------------------*/

122     /**
123      * Set the InstrumentManager to which the Connecter will provide
124      * access. This method is called before the new connector is
125      * configured or started.
126      */

127     public void setInstrumentManager( DefaultInstrumentManager manager )
128     {
129         m_manager = manager;
130     }
131
132     /*---------------------------------------------------------------
133      * Configurable Methods
134      *-------------------------------------------------------------*/

135     public void configure( Configuration configuration )
136         throws ConfigurationException
137     {
138         m_port = configuration.getAttributeAsInteger( "port", DEFAULT_PORT );
139         
140         String JavaDoc bindAddress = configuration.getChild( "bind" ).getValue( null );
141         try
142         {
143             if ( null != bindAddress )
144             {
145                 m_bindAddr = InetAddress.getByName( bindAddress );
146             }
147         }
148         catch ( final UnknownHostException JavaDoc e )
149         {
150             throw new ConfigurationException(
151                 "Unable to resolve the bind point: " + bindAddress, e );
152         }
153         
154         m_xml = configuration.getAttributeAsBoolean( "xml", true );
155         m_html = configuration.getAttributeAsBoolean( "html", true );
156         
157         m_chartWidth = configuration.getAttributeAsInteger( "chart-width", 600 );
158         m_chartHeight = configuration.getAttributeAsInteger( "chart-height", 120 );
159         m_antialias = configuration.getAttributeAsBoolean( "antialias", false );
160         
161         // Get configuration values which limit the leases that can be made.
162
// Make sure that none are any less restrictive than the InstrumentManager.
163
int maxSamples = m_manager.getMaxLeasedSamples();
164         m_maxLeasedSamples = Math.min( maxSamples, configuration.getAttributeAsInteger(
165             "max-leased-samples", maxSamples ) );
166         
167         int maxSize = m_manager.getMaxLeasedSampleSize();
168         m_maxLeasedSampleSize = Math.min( maxSize, configuration.getAttributeAsInteger(
169             "max-leased-sample-size", maxSize ) );
170         
171         long maxLease = m_manager.getMaxLeasedSampleLease();
172         m_maxLeasedSampleLease = Math.min( maxLease, 1000L * configuration.getAttributeAsInteger(
173             "max-leased-sample-lease", (int)( maxLease / 1000 ) ) );
174         
175         m_readOnly = configuration.getAttributeAsBoolean( "read-only", false );
176         
177         m_rootBreadCrumbURL = configuration.getAttribute( "root-bread-crumb-url", null );
178         m_rootBreadCrumbLabel = configuration.getAttribute(
179             "root-bread-crumb-label", m_rootBreadCrumbURL );
180         
181         String JavaDoc accessLogFile = configuration.getAttribute( "access-log", null );
182         
183         m_httpServer = new HTTPServer( m_port, m_bindAddr );
184         m_httpServer.enableLogging( getLogger().getChildLogger( "server" ) );
185         m_httpServer.setInstrumentableName( "server" );
186         m_httpServer.setAccessLogFile( accessLogFile );
187         addChildInstrumentable( m_httpServer );
188     }
189
190     /*---------------------------------------------------------------
191      * Startable Methods
192      *-------------------------------------------------------------*/

193     public void start()
194         throws Exception JavaDoc
195     {
196         // Register all of the helpers that we support
197
if ( m_xml )
198         {
199             // XML
200
String JavaDoc nameBase = "xml-";
201             initAndRegisterHandler( new XMLInstrumentManagerHandler( m_manager, this ),
202                 nameBase + "instrument-manager" );
203             initAndRegisterHandler( new XMLInstrumentableHandler( m_manager, this ),
204                 nameBase + "instrumentable" );
205             initAndRegisterHandler( new XMLInstrumentHandler( m_manager, this ),
206                 nameBase + "instrument" );
207             initAndRegisterHandler( new XMLSampleHandler( m_manager, this ), nameBase + "sample" );
208             initAndRegisterHandler( new XMLSnapshotHandler( m_manager, this ), nameBase + "snapshot" );
209             initAndRegisterHandler( new XMLSnapshotsHandler( m_manager, this ), nameBase + "snapshots" );
210             
211             if ( !m_readOnly )
212             {
213                 initAndRegisterHandler(
214                     new XMLSampleLeaseHandler( m_manager, this ), nameBase + "sample-lease" );
215                 initAndRegisterHandler(
216                     new XMLSampleLeasesHandler( m_manager, this ), nameBase + "sample-leases" );
217                 initAndRegisterHandler(
218                     new XMLCreateSampleHandler( m_manager, this ), nameBase + "create-sample" );
219                 initAndRegisterHandler(
220                     new XMLCreateSamplesHandler( m_manager, this ), nameBase + "create-samples" );
221                 initAndRegisterHandler( new XMLGCHandler( m_manager, this ), nameBase + "gc" );
222             }
223         }
224         
225         if ( m_html )
226         {
227             // HTML
228
String JavaDoc nameBase = "html-";
229             initAndRegisterHandler( new HTMLInstrumentManagerHandler( m_manager, this ),
230                 nameBase + "instrument-manager" );
231             initAndRegisterHandler( new HTMLInstrumentableHandler( m_manager, this ),
232                 nameBase + "instrumentable" );
233             initAndRegisterHandler( new HTMLInstrumentHandler( m_manager, this ),
234                 nameBase + "instrument" );
235             initAndRegisterHandler( new HTMLSampleHandler( m_manager, this ),
236                 nameBase + "sample" );
237             initAndRegisterHandler( new SampleChartHandler(
238                 m_manager, m_chartWidth, m_chartHeight, m_antialias ), "sample-chart" );
239             initAndRegisterHandler( new FavIconHandler(), "favicon" );
240             
241             if ( !m_readOnly )
242             {
243                 initAndRegisterHandler(
244                     new HTMLSampleLeaseHandler( m_manager, this ), nameBase + "sample-lease" );
245                 initAndRegisterHandler(
246                     new HTMLCreateSampleHandler( m_manager, this ), nameBase + "create-sample" );
247                 initAndRegisterHandler( new HTMLGCHandler( m_manager, this ), nameBase + "gc" );
248             }
249         
250             // The root handler must be registered last as it will handle any URL.
251
initAndRegisterHandler( new HTMLRootHandler( m_manager, this ), nameBase + "root" );
252         }
253         
254         getLogger().debug( "Starting Instrument Manager HTTP Connector" );
255         m_httpServer.start();
256         getLogger().info( "Instrument Manager HTTP Connector listening on port: " + m_port );
257     }
258
259     public void stop()
260         throws Exception JavaDoc
261     {
262         getLogger().debug( "Stopping Instrument Manager HTTP Connector" );
263         m_httpServer.stop();
264         m_httpServer = null;
265     }
266
267     /*---------------------------------------------------------------
268      * Methods
269      *-------------------------------------------------------------*/

270     /**
271      * Returns the maximum number of leased samples that will be approved.
272      *
273      * @return The maximum number of leased samples.
274      */

275     int getMaxLeasedSamples()
276     {
277         return m_maxLeasedSamples;
278     }
279     
280     /**
281      * Returns the maximum size of a leased sample.
282      *
283      * @return The maximum size of a leased sample.
284      */

285     int getMaxLeasedSampleSize()
286     {
287         return m_maxLeasedSampleSize;
288     }
289     
290     /**
291      * Returns the maximum number of milliseconds that a lease will be granted
292      * for.
293      *
294      * @return The maximum lease length.
295      */

296     long getMaxLeasedSampleLease()
297     {
298         return m_maxLeasedSampleLease;
299     }
300     
301     /**
302      * True if the connector should only provide read-only access to the
303      * Instrument Manager.
304      *
305      * @return The read-only flag.
306      */

307     boolean isReadOnly()
308     {
309         return m_readOnly;
310     }
311     
312     /**
313      * Returns the root bread crumb URL or null if not configured.
314      *
315      * @return The root bread crumb URL or null if not configured.
316      */

317     String JavaDoc getRootBreadCrumbURL()
318     {
319         return m_rootBreadCrumbURL;
320     }
321     
322     /**
323      * Returns the root bread crumb label or null if not configured.
324      *
325      * @return The root bread crumb label or null if not configured.
326      */

327     String JavaDoc getRootBreadCrumbLabel()
328     {
329         return m_rootBreadCrumbLabel;
330     }
331     
332     private void initAndRegisterHandler( AbstractHTTPURLHandler handler, String JavaDoc name )
333         throws Exception JavaDoc
334     {
335         handler.enableLogging( getLogger().getChildLogger( name ) );
336         handler.setInstrumentableName( name );
337         addChildInstrumentable( handler );
338         
339         m_httpServer.registerHandler( handler );
340     }
341 }
342
343
Popular Tags