KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.IOException JavaDoc;
23 import java.io.PrintWriter JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import org.apache.excalibur.instrument.manager.http.server.HTTPRedirect;
27 import org.apache.excalibur.instrument.manager.DefaultInstrumentManager;
28 import org.apache.excalibur.instrument.manager.InstrumentDescriptor;
29 import org.apache.excalibur.instrument.manager.InstrumentSampleDescriptor;
30 import org.apache.excalibur.instrument.manager.NoSuchInstrumentException;
31
32 /**
33  *
34  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
35  * @version CVS $Revision: 1.6 $ $Date: 2004/03/06 14:01:28 $
36  * @since 4.1
37  */

38 public class HTMLInstrumentHandler
39     extends AbstractHTMLHandler
40 {
41     /*---------------------------------------------------------------
42      * Constructors
43      *-------------------------------------------------------------*/

44     /**
45      * Creates a new HTMLInstrumentHandler.
46      *
47      * @param manager Reference to the DefaultInstrumentManager.
48      * @param connector The InstrumentManagerHTTPConnector.
49      */

50     public HTMLInstrumentHandler( DefaultInstrumentManager manager,
51                                   InstrumentManagerHTTPConnector connector )
52     {
53         super( "/instrument.html", manager, connector );
54     }
55     
56     /*---------------------------------------------------------------
57      * AbstractHTTPURLHandler Methods
58      *-------------------------------------------------------------*/

59     /**
60      * Handles the specified request.
61      *
62      * @param The full path being handled.
63      * @param parameters A Map of the parameters in the request.
64      * @param os The PrintWriter to write the result to.
65      */

66     public void doGet( String JavaDoc path, Map JavaDoc parameters, PrintWriter JavaDoc out )
67         throws IOException JavaDoc
68     {
69         String JavaDoc name = getParameter( parameters, "name" );
70         InstrumentDescriptor desc;
71         try
72         {
73             desc = getInstrumentManager().locateInstrumentDescriptor( name );
74         }
75         catch ( NoSuchInstrumentException e )
76         {
77             // Sample no longer exists, go back to the parent instrument.
78
int pos = name.lastIndexOf( '.' );
79             if ( pos >= 0 )
80             {
81                 throw new HTTPRedirect(
82                     "instrumentable.html?name=" + urlEncode( name.substring( 0, pos ) ) );
83             }
84             else
85             {
86                 throw new HTTPRedirect( "instrument-manager.html" );
87             }
88         }
89         
90         String JavaDoc type;
91         StringBuffer JavaDoc types = new StringBuffer JavaDoc();
92         types.append( "<select name='type' onKeyPress=\"javascript:fieldChanged()\">" );
93         StringBuffer JavaDoc presets = new StringBuffer JavaDoc();
94         presets.append( "<select name='preset' onChange=\"javascript:applyPreset(this.options[this.selectedIndex].value)\">" );
95         presets.append( "<option value='0-0' selected>-- Select a Preset --</option>" );
96         switch ( desc.getType() )
97         {
98         case DefaultInstrumentManager.INSTRUMENT_TYPE_COUNTER:
99             type = "Counter";
100             types.append( "<option value='"
101                 + DefaultInstrumentManager.INSTRUMENT_SAMPLE_TYPE_COUNTER
102                 + "' selected>Count</option>" );
103             
104             presets.append( "<option value='"
105                 + DefaultInstrumentManager.INSTRUMENT_SAMPLE_TYPE_COUNTER
106                 + "-0'>Count / Second Over 10 Minutes</option>" );
107             presets.append( "<option value='"
108                 + DefaultInstrumentManager.INSTRUMENT_SAMPLE_TYPE_COUNTER
109                 + "-1'>Count / Minute Over 1 Day</option>" );
110             presets.append( "<option value='"
111                 + DefaultInstrumentManager.INSTRUMENT_SAMPLE_TYPE_COUNTER
112                 + "-2'>Count / Hour Over 1 Month</option>" );
113             break;
114             
115         case DefaultInstrumentManager.INSTRUMENT_TYPE_VALUE:
116             type = "Value";
117             types.append( "<option value='"
118                 + DefaultInstrumentManager.INSTRUMENT_SAMPLE_TYPE_MAXIMUM
119                 + "' selected>Maximum Value</option>" );
120             types.append( "<option value='"
121                 + DefaultInstrumentManager.INSTRUMENT_SAMPLE_TYPE_MINIMUM
122                 + "'>Minimum Value</option>" );
123             types.append( "<option value='"
124                 + DefaultInstrumentManager.INSTRUMENT_SAMPLE_TYPE_MEAN
125                 + "'>Mean Value</option>" );
126             
127             presets.append( "<option value='"
128                 + DefaultInstrumentManager.INSTRUMENT_SAMPLE_TYPE_MAXIMUM
129                 + "-0'>Max Value / Second Over 10 Minutes</option>" );
130             presets.append( "<option value='"
131                 + DefaultInstrumentManager.INSTRUMENT_SAMPLE_TYPE_MAXIMUM
132                 + "-1'>Max Value / Minute Over 1 Day</option>" );
133             presets.append( "<option value='"
134                 + DefaultInstrumentManager.INSTRUMENT_SAMPLE_TYPE_MAXIMUM
135                 + "-2'>Max Value / Hour Over 1 Month</option>" );
136             
137             presets.append( "<option value='"
138                 + DefaultInstrumentManager.INSTRUMENT_SAMPLE_TYPE_MINIMUM
139                 + "-0'>Min Value / Second Over 10 Minutes</option>" );
140             presets.append( "<option value='"
141                 + DefaultInstrumentManager.INSTRUMENT_SAMPLE_TYPE_MINIMUM
142                 + "-1'>Min Value / Minute Over 1 Day</option>" );
143             presets.append( "<option value='"
144                 + DefaultInstrumentManager.INSTRUMENT_SAMPLE_TYPE_MINIMUM
145                 + "-2'>Min Value / Hour Over 1 Month</option>" );
146             
147             presets.append( "<option value='"
148                 + DefaultInstrumentManager.INSTRUMENT_SAMPLE_TYPE_MEAN
149                 + "-0'>Mean Value / Second Over 10 Minutes</option>" );
150             presets.append( "<option value='"
151                 + DefaultInstrumentManager.INSTRUMENT_SAMPLE_TYPE_MEAN
152                 + "-1'>Mean Value / Minute Over 1 Day</option>" );
153             presets.append( "<option value='"
154                 + DefaultInstrumentManager.INSTRUMENT_SAMPLE_TYPE_MEAN
155                 + "-2'>Mean Value / Hour Over 1 Month</option>" );
156             break;
157             
158         default:
159             type = "Unknown";
160             break;
161         }
162         types.append( "</select>" );
163         presets.append( "</select>" );
164         
165         out.println( "<html>" );
166         out.println( "<head><title>" + desc.getDescription() + "</title></head>" );
167         out.println( "<body>" );
168         
169         breadCrumbs( out, desc, false );
170         
171         out.println( "<h2>Instrument</h2>" );
172         startTable( out );
173         tableRow( out, 0, "Name", desc.getName() );
174         tableRow( out, 0, "Description", desc.getDescription() );
175         tableRow( out, 0, "Type", type );
176         endTable( out );
177         
178         InstrumentSampleDescriptor[] samples = desc.getInstrumentSampleDescriptors();
179         if ( samples.length > 0 )
180         {
181             out.println( "<h2>Registered Samples</h2>" );
182             outputInstrumentSamples( out, samples, getConnector().isReadOnly() );
183         }
184         
185         if ( !getConnector().isReadOnly() )
186         {
187             out.println( "<h2>Register Sample</h2>" );
188             out.println( "<SCRIPT LANGUAGE=\"JavaScript\">" );
189             out.println( "function fieldChanged() {" );
190             out.println( " var form = document.forms[0];" );
191             out.println( " form.preset.value=\"0-0\";" );
192             out.println( "}" );
193             out.println( "function applyPreset(preset) {" );
194             out.println( " var form = document.forms[0];" );
195             out.println( " var pos = preset.indexOf('-');" );
196             out.println( " var type = preset.substring(0, pos);" );
197             out.println( " var spec = preset.substring(pos + 1);" );
198             out.println( " var prefix;" );
199             out.println( " if (type == 0) {" );
200             out.println( " return;" );
201             out.println( " } else if (type == " + DefaultInstrumentManager.INSTRUMENT_SAMPLE_TYPE_COUNTER + ") {" );
202             out.println( " typeLbl = \"Count\"" );
203             out.println( " } else if (type == " + DefaultInstrumentManager.INSTRUMENT_SAMPLE_TYPE_MAXIMUM + ") {" );
204             out.println( " typeLbl = \"Max Value\"" );
205             out.println( " } else if (type == " + DefaultInstrumentManager.INSTRUMENT_SAMPLE_TYPE_MINIMUM + ") {" );
206             out.println( " typeLbl = \"Min Value\"" );
207             out.println( " } else if (type == " + DefaultInstrumentManager.INSTRUMENT_SAMPLE_TYPE_MEAN + ") {" );
208             out.println( " typeLbl = \"Mean Value\"" );
209             out.println( " } else {" );
210             out.println( " typeLbl = \"Unknown\"" );
211             out.println( " }" );
212             out.println( " var intervalLbl;" );
213             out.println( " var interval;" );
214             out.println( " var size;" );
215             out.println( " var lease;" );
216             out.println( " if (spec == 1) {" );
217             out.println( " intervalLbl = \"Minute\";" );
218             out.println( " interval = 60000;" );
219             out.println( " size = 1440;" );
220             out.println( " lease = 86400000;" );
221             out.println( " } else if (spec == 2) {" );
222             out.println( " intervalLbl = \"Hour\";" );
223             out.println( " interval = 3600000;" );
224             out.println( " size = 672;" );
225             out.println( " lease = 86400000;" );
226             out.println( " } else {" );
227             out.println( " intervalLbl = \"Second\";" );
228             out.println( " interval = 1000;" );
229             out.println( " size = 600;" );
230             out.println( " lease = 600000;" );
231             out.println( " }" );
232             out.println( " form.description.value = typeLbl + \" / \" + intervalLbl;" );
233             out.println( " form.interval.value = interval;" );
234             out.println( " form.size.value = size;" );
235             out.println( " form.lease.value = lease;" );
236             out.println( " form.type.value = type;" );
237             out.println( "}" );
238             out.println( "</SCRIPT>" );
239     
240             out.println( "<form action='create-sample.html' method='GET' accept-charset='UTF-8'>" );
241             startTable( out );
242             tableRow( out, 0, "Description", "<input name='description' type='text' size='40' value='' onKeyPress=\"javascript:fieldChanged()\">" );
243             tableRow( out, 0, "Interval (ms.)", "<input name='interval' type='text' size='10' value='' onKeyPress=\"javascript:fieldChanged()\">" );
244             tableRow( out, 0, "Size", "<input name='size' type='text' size='10' value='' onKeyPress=\"javascript:fieldChanged()\">" );
245             tableRow( out, 0, "Lease (ms.)", "<input name='lease' type='text' size='10' value='' onKeyPress=\"javascript:fieldChanged()\">" );
246             tableRow( out, 0, "Type", types.toString() );
247             tableRow( out, 0, "Presets", presets.toString() );
248             endTable( out );
249             out.println( "<input type='hidden' name='name' value='" + desc.getName() + "'>" );
250             out.println( "<input type='submit' value='Submit'>" );
251             
252             out.println( "</form>" );
253         }
254         
255         footer( out );
256         
257         out.println( "</body>" );
258         out.println( "</html>" );
259     }
260             
261     /*---------------------------------------------------------------
262      * Methods
263      *-------------------------------------------------------------*/

264 }
265
266
Popular Tags