KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.UnsupportedEncodingException JavaDoc;
25 import java.util.Date JavaDoc;
26
27 import org.apache.excalibur.instrument.manager.http.server.URLCoder;
28 import org.apache.excalibur.instrument.manager.InstrumentableDescriptor;
29 import org.apache.excalibur.instrument.manager.InstrumentDescriptor;
30 import org.apache.excalibur.instrument.manager.InstrumentSampleDescriptor;
31 import org.apache.excalibur.instrument.manager.DefaultInstrumentManager;
32
33 /**
34  *
35  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
36  * @version CVS $Revision: 1.7 $ $Date: 2004/03/10 13:56:56 $
37  * @since 4.1
38  */

39 public abstract class AbstractHTMLHandler
40     extends AbstractHandler
41 {
42     /** Version of the package. */
43     private static final String JavaDoc m_packageVersion =
44         AbstractHTMLHandler.class.getPackage().getImplementationVersion();
45     
46     /*---------------------------------------------------------------
47      * Constructors
48      *-------------------------------------------------------------*/

49     /**
50      * Creates a new AbstractHTMLHandler.
51      *
52      * @param path The path handled by this handler.
53      * @param manager Reference to the instrument manager interface.
54      * @param connector The InstrumentManagerHTTPConnector.
55      */

56     public AbstractHTMLHandler( String JavaDoc path,
57                                 DefaultInstrumentManager manager,
58                                 InstrumentManagerHTTPConnector connector )
59     {
60         super( path, CONTENT_TYPE_TEXT_HTML, manager, connector );
61     }
62     
63     /*---------------------------------------------------------------
64      * Methods
65      *-------------------------------------------------------------*/

66     protected String JavaDoc urlEncode( String JavaDoc str )
67     {
68         try
69         {
70             // Starting with Java 1.4, encode takes an encoding, but this needs to
71
// work with 1.3. Use our own version.
72
return URLCoder.encode( str, InstrumentManagerHTTPConnector.ENCODING );
73         }
74         catch ( UnsupportedEncodingException JavaDoc e )
75         {
76             // Should never happen because we control the encoding.
77
throw new IllegalStateException JavaDoc( "Unknown encoding: " + e.toString() );
78         }
79     }
80     
81     protected void breadCrumbs( PrintWriter JavaDoc out, boolean link )
82     {
83         String JavaDoc rootURL = getConnector().getRootBreadCrumbURL();
84         if ( rootURL != null )
85         {
86             // Always shown as a link
87
String JavaDoc rootLabel = getConnector().getRootBreadCrumbLabel();
88             out.println( "<a HREF='" + rootURL + "'>" + rootLabel + "</a>" );
89             out.print( " <b>&gt;</b> " );
90         }
91         
92         if ( link )
93         {
94             out.println( "<a HREF='instrument-manager.html'>"
95                 + getInstrumentManager().getDescription() + "</a>" );
96         }
97         else
98         {
99             out.println( getInstrumentManager().getDescription() );
100         }
101     }
102     
103     protected void breadCrumbs( PrintWriter JavaDoc out, InstrumentableDescriptor desc, boolean link )
104     {
105         InstrumentableDescriptor parent = desc.getParentInstrumentableDescriptor();
106         if ( parent == null )
107         {
108             breadCrumbs( out, true );
109         }
110         else
111         {
112             breadCrumbs( out, parent, true );
113         }
114         out.print( " <b>&gt;</b> " );
115         if ( link )
116         {
117             out.println( "<a HREF='instrumentable.html?name=" + urlEncode( desc.getName() ) + "'>"
118                 + desc.getDescription() + "</a>" );
119         }
120         else
121         {
122             out.println( desc.getDescription() );
123         }
124     }
125     
126     protected void breadCrumbs( PrintWriter JavaDoc out, InstrumentDescriptor desc, boolean link )
127     {
128         breadCrumbs( out, desc.getInstrumentableDescriptor(), true );
129         out.print( " <b>&gt;</b> " );
130         if ( link )
131         {
132             out.println( "<a HREF='instrument.html?name=" + urlEncode( desc.getName() ) + "'>"
133                 + desc.getDescription() + "</a>" );
134         }
135         else
136         {
137             out.println( desc.getDescription() );
138         }
139     }
140     
141     protected void breadCrumbs( PrintWriter JavaDoc out, InstrumentSampleDescriptor desc, boolean link )
142     {
143         breadCrumbs( out, desc.getInstrumentDescriptor(), true );
144         out.print( " <b>&gt;</b> " );
145         if ( link )
146         {
147             out.println( "<a HREF='sample.html?name=" + urlEncode( desc.getName() ) + "'>"
148                 + desc.getDescription() + "</a>" );
149         }
150         else
151         {
152             out.println( desc.getDescription() );
153         }
154     }
155     
156     protected void startTable( PrintWriter JavaDoc out )
157         throws IOException JavaDoc
158     {
159         out.println( "<table cellpadding='1' cellspacing='0'><tr><td bgcolor='#bbbbbb'><table cellpadding='2' cellspacing='1'>" );
160     }
161     protected void endTable( PrintWriter JavaDoc out )
162         throws IOException JavaDoc
163     {
164         out.println( "</table></td></tr></table>" );
165     }
166     
167     protected void startTableHeaderRow( PrintWriter JavaDoc out )
168         throws IOException JavaDoc
169     {
170         out.println( "<tr>" );
171     }
172     
173     protected void endTableHeaderRow( PrintWriter JavaDoc out )
174         throws IOException JavaDoc
175     {
176         out.println( "</tr>" );
177     }
178     
179     protected void tableHeaderCell( PrintWriter JavaDoc out, String JavaDoc value )
180         throws IOException JavaDoc
181     {
182         out.print( "<td bgcolor='#dddddd' nowrap><b>" + value + "</b></td>" );
183     }
184     
185     protected void startTableRow( PrintWriter JavaDoc out, int row )
186         throws IOException JavaDoc
187     {
188         String JavaDoc color;
189         if ( row % 2 == 0 )
190         {
191             color = "#eeeeee";
192         }
193         else
194         {
195             color = "#e4e4e4";
196         }
197         out.println( "<tr bgcolor='" + color + "'>" );
198     }
199     
200     protected void endTableRow( PrintWriter JavaDoc out )
201         throws IOException JavaDoc
202     {
203         out.println( "</tr>" );
204     }
205     
206     protected void tableCell( PrintWriter JavaDoc out, String JavaDoc value )
207         throws IOException JavaDoc
208     {
209         out.print( "<td nowrap>" + value + "</td>" );
210     }
211     
212     protected void tableCellRight( PrintWriter JavaDoc out, String JavaDoc value )
213         throws IOException JavaDoc
214     {
215         out.print( "<td align='right' nowrap>" + value + "</td>" );
216     }
217     
218     protected void tableRow( PrintWriter JavaDoc out, int row, String JavaDoc label, String JavaDoc value )
219         throws IOException JavaDoc
220     {
221         startTableRow( out, row );
222         tableHeaderCell( out, label );
223         tableCell( out, value );
224         endTableRow( out );
225     }
226     
227     protected void footer( PrintWriter JavaDoc out )
228     {
229         out.println( "<br>" );
230         out.print( "<font size='-1' color='#888888'>" );
231         out.print( "<center>" );
232         out.print( "<a HREF='http://excalibur.apache.org/instrument/html-client.html'>" );
233         out.print( "Excalibur Instrument HTTP Client" );
234         out.print( "</a> Version " );
235         out.print( m_packageVersion );
236         out.print( "<br>" );
237         out.print( "Copyright&copy; 2002-2004 The Apache Software Foundation. All rights reserved." );
238         out.print( "</center>" );
239         out.println( "</font>" );
240     }
241     
242     protected void outputInstrumentables( PrintWriter JavaDoc out, InstrumentableDescriptor[] descs )
243         throws IOException JavaDoc
244     {
245         startTable( out );
246         startTableHeaderRow( out );
247         tableHeaderCell( out, "Name" );
248         endTableHeaderRow( out );
249         
250         for ( int i = 0; i < descs.length; i++ )
251         {
252             InstrumentableDescriptor desc = descs[i];
253             
254             startTableRow( out, i );
255             tableCell( out,
256                 "<a HREF='instrumentable.html?name=" + urlEncode( desc.getName() ) + "'>"
257                 + desc.getDescription() + "</a>" );
258             endTableRow( out );
259         }
260         
261         endTable( out );
262     }
263     
264     protected void outputInstruments( PrintWriter JavaDoc out, InstrumentDescriptor[] descs )
265         throws IOException JavaDoc
266     {
267         startTable( out );
268         startTableHeaderRow( out );
269         tableHeaderCell( out, "Name" );
270         endTableHeaderRow( out );
271         
272         for ( int i = 0; i < descs.length; i++ )
273         {
274             InstrumentDescriptor desc = descs[i];
275             
276             startTableRow( out, i );
277             tableCell( out, "<a HREF='instrument.html?name=" + urlEncode( desc.getName() ) + "'>"
278                 + desc.getDescription() + "</a>" );
279             endTableRow( out );
280         }
281         
282         endTable( out );
283     }
284     
285     protected void outputInstrumentSamples( PrintWriter JavaDoc out,
286                                             InstrumentSampleDescriptor[] descs,
287                                             boolean readOnly )
288         throws IOException JavaDoc
289     {
290         startTable( out );
291         startTableHeaderRow( out );
292         tableHeaderCell( out, "Name" );
293         tableHeaderCell( out, "Last Sample" );
294         tableHeaderCell( out, "Last Sample Period" );
295         tableHeaderCell( out, "Interval" );
296         tableHeaderCell( out, "Size" );
297         tableHeaderCell( out, "Expiration Time" );
298         endTableHeaderRow( out );
299         
300         for ( int i = 0; i < descs.length; i++ )
301         {
302             InstrumentSampleDescriptor desc = descs[i];
303             
304             startTableRow( out, i );
305             tableCell( out, "<a HREF='sample.html?name=" + urlEncode( desc.getName() ) + "'>"
306                 + desc.getDescription() + "</a> (<a HREF='sample.html?name="
307                 + urlEncode( desc.getName() ) + "&chart=true'>Chart</a>)" );
308             tableCellRight( out, Integer.toString( desc.getValue() ) );
309             tableCell( out, new Date JavaDoc( desc.getTime() ).toString() );
310             tableCellRight( out, Long.toString( desc.getInterval() ) );
311             tableCellRight( out, Integer.toString( desc.getSize() ) );
312             String JavaDoc value;
313             if ( desc.getLeaseExpirationTime() == 0 )
314             {
315                 value = "<i>Permanent</i>";
316             }
317             else
318             {
319                 String JavaDoc renewUrl =
320                     "sample-lease.html?name=" + urlEncode( desc.getName() ) + "&instrument=true&lease=";
321                 
322                 value = new Date JavaDoc( desc.getLeaseExpirationTime() ).toString();
323                 if ( !readOnly )
324                 {
325                     value = value
326                         + " (Renew <a HREF='" + renewUrl + "600000'>10min</a>, "
327                         + "<a HREF='" + renewUrl + "3600000'>1hr</a>, "
328                         + "<a HREF='" + renewUrl + "86400000'>1day</a>)";
329                 }
330                 
331                 // Make the text red if it is about to expire.
332
if ( desc.getLeaseExpirationTime() - System.currentTimeMillis() < 300000 )
333                 {
334                     value = "<font color='ff0000'>" + value + "</font>";
335                 }
336             }
337             tableCell( out, value );
338             endTableRow( out );
339         }
340         
341         endTable( out );
342     }
343 }
344
345
Popular Tags