KickJava   Java API By Example, From Geeks To Geeks.

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


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
25 import org.apache.excalibur.instrument.manager.InstrumentableDescriptor;
26 import org.apache.excalibur.instrument.manager.InstrumentDescriptor;
27 import org.apache.excalibur.instrument.manager.DefaultInstrumentManager;
28 import org.apache.excalibur.instrument.manager.InstrumentSampleDescriptor;
29 import org.apache.excalibur.instrument.manager.InstrumentSampleSnapshot;
30
31 /**
32  *
33  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
34  * @version CVS $Revision: 1.6 $ $Date: 2004/03/06 14:01:28 $
35  * @since 4.1
36  */

37 public abstract class AbstractXMLHandler
38     extends AbstractHandler
39 {
40     protected static final String JavaDoc INDENT = " ";
41     
42     /*---------------------------------------------------------------
43      * Constructors
44      *-------------------------------------------------------------*/

45     /**
46      * Creates a new AbstractXMLHandler.
47      *
48      * @param path The path handled by this handler.
49      * @param manager Reference to the instrument manager interface.
50      * @param connector The InstrumentManagerHTTPConnector.
51      */

52     public AbstractXMLHandler( String JavaDoc path,
53                                DefaultInstrumentManager manager,
54                                InstrumentManagerHTTPConnector connector )
55     {
56         super( path, CONTENT_TYPE_TEXT_XML, manager, connector );
57     }
58     
59     /*---------------------------------------------------------------
60      * Methods
61      *-------------------------------------------------------------*/

62     /**
63      * Replaces one token with another in a string.
64      *
65      * @param str String with tokens to be replaced.
66      * @param oldToken The token to be replaced.
67      * @param newToken The new token value.
68      *
69      * @return A new String that has had its tokens replaced.
70      */

71     protected final String JavaDoc replaceToken( String JavaDoc str, String JavaDoc oldToken, String JavaDoc newToken )
72     {
73         int len = str.length();
74         int oldLen = oldToken.length();
75         if ( oldLen == 0 )
76         {
77             // Can't replace nothing.
78
return str;
79         }
80         int newLen = newToken.length();
81         int start = 0;
82         int pos;
83         while ( ( pos = str.indexOf( oldToken, start ) ) >= 0 )
84         {
85             String JavaDoc left;
86             String JavaDoc right;
87             int leftLen;
88             int rightLen;
89             
90             // Get the left side of the string
91
leftLen = pos;
92             if ( leftLen == 0 )
93             {
94                 left = "";
95             }
96             else
97             {
98                 left = str.substring( 0, pos );
99             }
100             
101             // Get the right side of the string
102
rightLen = len - pos - oldLen;
103             if ( len - pos - oldLen <= 0 )
104             {
105                 right = "";
106             }
107             else
108             {
109                 right = str.substring( pos + oldLen );
110             }
111             
112             // Rebuild the str variable
113
str = left + newToken + right;
114             len = leftLen + newLen + rightLen;
115             start = leftLen + newLen;
116         }
117         return str;
118     }
119
120     protected final String JavaDoc makeSafeAttribute( String JavaDoc attribute )
121     {
122         attribute = replaceToken( attribute, "&", "&amp;" ); // Must be done first.
123
attribute = replaceToken( attribute, "<", "&lt;" );
124         attribute = replaceToken( attribute, ">", "&gt;" );
125         attribute = replaceToken( attribute, "\"", "&quot;" );
126         
127         return attribute;
128     }
129     
130     protected void outputLine( PrintWriter JavaDoc out, String JavaDoc indent, boolean packed, String JavaDoc line )
131     {
132         if ( !packed )
133         {
134             out.print( indent );
135         }
136         out.print( line );
137         if ( !packed )
138         {
139             out.println();
140         }
141     }
142     
143     protected void outputInstrumentManager( PrintWriter JavaDoc out,
144                                             DefaultInstrumentManager manager,
145                                             String JavaDoc indent,
146                                             boolean recurse,
147                                             boolean packed,
148                                             boolean readOnly )
149         throws IOException JavaDoc
150     {
151         outputLine( out, indent, packed, "<instrument-manager "
152             + "name=\"" + makeSafeAttribute( manager.getName() ) + "\" "
153             + "description=\"" + makeSafeAttribute( manager.getDescription() ) + "\" "
154             + "state-version=\"" + manager.getStateVersion() + "\" "
155             + "batched-updates=\"true\" read-only=\"" + readOnly + "\">" );
156         
157         String JavaDoc childIndent = indent + INDENT;
158         
159         InstrumentableDescriptor[] instrumentables = manager.getInstrumentableDescriptors();
160         for ( int i = 0; i < instrumentables.length; i++ )
161         {
162             InstrumentableDescriptor instrumentable = instrumentables[i];
163             if ( recurse )
164             {
165                 outputInstrumentable( out, instrumentable, childIndent, recurse, packed );
166             }
167             else
168             {
169                 outputInstrumentableBrief( out, instrumentable, childIndent, packed );
170             }
171         }
172         
173         outputLine( out, indent, packed, "</instrument-manager>" );
174     }
175     
176     protected void outputInstrumentableBrief( PrintWriter JavaDoc out,
177                                               InstrumentableDescriptor instrumentable,
178                                               String JavaDoc indent,
179                                               boolean packed )
180         throws IOException JavaDoc
181     {
182         outputLine( out, indent, packed, "<instrumentable "
183             + "name=\"" + makeSafeAttribute( instrumentable.getName() ) + "\" "
184             + "state-version=\"" + instrumentable.getStateVersion() + "\"/>" );
185     }
186     
187     protected void outputInstrumentable( PrintWriter JavaDoc out,
188                                          InstrumentableDescriptor instrumentable,
189                                          String JavaDoc indent,
190                                          boolean recurse,
191                                          boolean packed )
192         throws IOException JavaDoc
193     {
194         InstrumentableDescriptor[] instrumentables =
195             instrumentable.getChildInstrumentableDescriptors();
196         InstrumentDescriptor[] instruments = instrumentable.getInstrumentDescriptors();
197         
198         String JavaDoc terminator;
199         if ( ( instrumentables.length > 0 ) || ( instruments.length > 0 ) )
200         {
201             terminator = ">";
202         }
203         else
204         {
205             terminator = "/>";
206         }
207                                             
208         outputLine( out, indent, packed, "<instrumentable "
209             + "name=\"" + makeSafeAttribute( instrumentable.getName() ) + "\" "
210             + "description=\"" + makeSafeAttribute( instrumentable.getDescription() ) + "\" "
211             + "state-version=\"" + instrumentable.getStateVersion() + "\" "
212             + "registered=\"" + instrumentable.isRegistered() + "\" "
213             + "configured=\"" + instrumentable.isConfigured() + "\"" + terminator );
214         
215         if ( ( instrumentables.length > 0 ) || ( instruments.length > 0 ) )
216         {
217             String JavaDoc childIndent = indent + INDENT;
218             
219             for ( int i = 0; i < instrumentables.length; i++ )
220             {
221                 InstrumentableDescriptor child = instrumentables[i];
222                 if ( recurse )
223                 {
224                     outputInstrumentable( out, child, childIndent, recurse, packed );
225                 }
226                 else
227                 {
228                     outputInstrumentableBrief( out, child, childIndent, packed );
229                 }
230             }
231             
232             for ( int i = 0; i < instruments.length; i++ )
233             {
234                 InstrumentDescriptor instrument = instruments[i];
235                 if ( recurse )
236                 {
237                     outputInstrument( out, instrument, childIndent, recurse, packed );
238                 }
239                 else
240                 {
241                     outputInstrumentBrief( out, instrument, childIndent, packed );
242                 }
243             }
244             
245             outputLine( out, indent, packed, "</instrumentable>" );
246         }
247     }
248     
249     protected void outputInstrumentBrief( PrintWriter JavaDoc out,
250                                           InstrumentDescriptor instrument,
251                                           String JavaDoc indent,
252                                           boolean packed )
253         throws IOException JavaDoc
254     {
255         outputLine( out, indent, packed, "<instrument "
256             + "name=\"" + makeSafeAttribute( instrument.getName() ) + "\" "
257             + "state-version=\"" + instrument.getStateVersion() + "\"/>" );
258     }
259     
260     protected void outputInstrument( PrintWriter JavaDoc out,
261                                      InstrumentDescriptor instrument,
262                                      String JavaDoc indent,
263                                      boolean recurse,
264                                      boolean packed )
265         throws IOException JavaDoc
266     {
267         InstrumentSampleDescriptor[] samples = instrument.getInstrumentSampleDescriptors();
268         
269         String JavaDoc terminator;
270         if ( samples.length > 0 )
271         {
272             terminator = ">";
273         }
274         else
275         {
276             terminator = "/>";
277         }
278         
279         outputLine( out, indent, packed, "<instrument "
280             + "name=\"" + makeSafeAttribute( instrument.getName() ) + "\" "
281             + "description=\"" + makeSafeAttribute( instrument.getDescription() ) + "\" "
282             + "type=\"" + instrument.getType() + "\" "
283             + "state-version=\"" + instrument.getStateVersion() + "\" "
284             + "registered=\"" + instrument.isRegistered() + "\" "
285             + "configured=\"" + instrument.isConfigured() + "\"" + terminator );
286         
287         if ( samples.length > 0 )
288         {
289             String JavaDoc childIndent = indent + INDENT;
290             
291             for ( int i = 0; i < samples.length; i++ )
292             {
293                 InstrumentSampleDescriptor sample = samples[i];
294                 if ( recurse )
295                 {
296                     outputSample( out, sample, childIndent, packed );
297                 }
298                 else
299                 {
300                     outputSampleBrief( out, sample, childIndent, packed );
301                 }
302             }
303             
304             outputLine( out, indent, packed, "</instrument>" );
305         }
306     }
307     
308     protected void outputSampleBrief( PrintWriter JavaDoc out,
309                                       InstrumentSampleDescriptor sample,
310                                       String JavaDoc indent,
311                                       boolean packed )
312         throws IOException JavaDoc
313     {
314         outputLine( out, indent, packed, "<sample "
315             + "name=\"" + makeSafeAttribute( sample.getName() ) + "\" "
316             + "state-version=\"" + sample.getStateVersion() + "\"/>" );
317     }
318     
319     protected void outputSample( PrintWriter JavaDoc out,
320                                  InstrumentSampleDescriptor sample,
321                                  String JavaDoc indent,
322                                  boolean packed )
323         throws IOException JavaDoc
324     {
325         outputLine( out, indent, packed, "<sample "
326             + "name=\"" + makeSafeAttribute( sample.getName() ) + "\" "
327             + "description=\"" + makeSafeAttribute( sample.getDescription() ) + "\" "
328             + "type=\"" + sample.getType() + "\" "
329             + "interval=\"" + sample.getInterval() + "\" "
330             + "size=\"" + sample.getSize() + "\" "
331             + "value=\"" + sample.getValue() + "\" "
332             + "time=\"" + sample.getTime() + "\" "
333             + "expiration-time=\"" + sample.getLeaseExpirationTime() + "\" "
334             + "state-version=\"" + sample.getStateVersion() + "\" "
335             + "configured=\"" + sample.isConfigured() + "\"/>" );
336     }
337     
338     protected void outputSampleHistory( PrintWriter JavaDoc out,
339                                         InstrumentSampleDescriptor sample,
340                                         String JavaDoc indent,
341                                         long baseTime,
342                                         boolean packed,
343                                         boolean compact )
344         throws IOException JavaDoc
345     {
346         InstrumentSampleSnapshot snapshot = sample.getSnapshot();
347         int[] values = snapshot.getSamples();
348         
349         // Given the base time, decide on the first value index and this time which
350
// will be included.
351
long firstTime = snapshot.getTime() - ( snapshot.getSize() - 1 ) * snapshot.getInterval();
352         int firstIndex;
353         if ( baseTime <= firstTime )
354         {
355             firstIndex = 0;
356         }
357         else if ( baseTime >= snapshot.getTime() )
358         {
359             firstTime = snapshot.getTime();
360             firstIndex = values.length - 1;
361         }
362         else
363         {
364             int count = (int)Math.ceil(
365                 ( (double)snapshot.getTime() - baseTime ) / snapshot.getInterval() ) + 1;
366             firstTime = snapshot.getTime() - ( count - 1 ) * snapshot.getInterval();
367             firstIndex = values.length - count;
368         }
369         
370         // Where possible, display values from the snapshot rather than the sample
371
// to avoid any synchronization issues.
372
outputLine( out, indent, packed, "<sample "
373             + "name=\"" + makeSafeAttribute( sample.getName() ) + "\" "
374             + "description=\"" + makeSafeAttribute( sample.getDescription() ) + "\" "
375             + "type=\"" + sample.getType() + "\" "
376             + "interval=\"" + snapshot.getInterval() + "\" "
377             + "size=\"" + snapshot.getSize() + "\" "
378             + "value=\"" + values[values.length - 1] + "\" "
379             + "time=\"" + snapshot.getTime() + "\" "
380             + "first-time=\"" + firstTime + "\" "
381             + "count=\"" + ( values.length - firstIndex ) + "\" "
382             + "expiration-time=\"" + sample.getLeaseExpirationTime() + "\" "
383             + "state-version=\"" + snapshot.getStateVersion() + "\" "
384             + "configured=\"" + sample.isConfigured() + "\">" );
385         
386         String JavaDoc childIndent = indent + INDENT;
387         
388         if ( compact )
389         {
390             // Output the values as a comma separated list.
391
StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
392             sb.append( "<values>" );
393             for ( int i = firstIndex; i < values.length; i++ )
394             {
395                 if ( i > firstIndex )
396                 {
397                     sb.append( "," );
398                 }
399                 sb.append( values[i] );
400             }
401             sb.append( "</values>" );
402             
403             outputLine( out, childIndent, packed, sb.toString() );
404         }
405         else
406         {
407             // Output an element for each value.
408
long interval = snapshot.getInterval();
409             long time = firstTime;
410             for ( int i = firstIndex; i < values.length; i++ )
411             {
412                 outputLine( out, childIndent, packed,
413                     "<value time=\"" + time + "\" value=\"" + values[i] + "\"/>" );
414                 time += interval;
415             }
416         }
417         
418         outputLine( out, indent, packed, "</sample>" );
419     }
420 }
421
422
Popular Tags