1 19 20 package org.apache.excalibur.instrument.manager.http; 21 22 import java.io.IOException ; 23 import java.io.PrintWriter ; 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 37 public abstract class AbstractXMLHandler 38 extends AbstractHandler 39 { 40 protected static final String INDENT = " "; 41 42 45 52 public AbstractXMLHandler( String path, 53 DefaultInstrumentManager manager, 54 InstrumentManagerHTTPConnector connector ) 55 { 56 super( path, CONTENT_TYPE_TEXT_XML, manager, connector ); 57 } 58 59 62 71 protected final String replaceToken( String str, String oldToken, String newToken ) 72 { 73 int len = str.length(); 74 int oldLen = oldToken.length(); 75 if ( oldLen == 0 ) 76 { 77 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 left; 86 String right; 87 int leftLen; 88 int rightLen; 89 90 leftLen = pos; 92 if ( leftLen == 0 ) 93 { 94 left = ""; 95 } 96 else 97 { 98 left = str.substring( 0, pos ); 99 } 100 101 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 str = left + newToken + right; 114 len = leftLen + newLen + rightLen; 115 start = leftLen + newLen; 116 } 117 return str; 118 } 119 120 protected final String makeSafeAttribute( String attribute ) 121 { 122 attribute = replaceToken( attribute, "&", "&" ); attribute = replaceToken( attribute, "<", "<" ); 124 attribute = replaceToken( attribute, ">", ">" ); 125 attribute = replaceToken( attribute, "\"", """ ); 126 127 return attribute; 128 } 129 130 protected void outputLine( PrintWriter out, String indent, boolean packed, String 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 out, 144 DefaultInstrumentManager manager, 145 String indent, 146 boolean recurse, 147 boolean packed, 148 boolean readOnly ) 149 throws IOException 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 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 out, 177 InstrumentableDescriptor instrumentable, 178 String indent, 179 boolean packed ) 180 throws IOException 181 { 182 outputLine( out, indent, packed, "<instrumentable " 183 + "name=\"" + makeSafeAttribute( instrumentable.getName() ) + "\" " 184 + "state-version=\"" + instrumentable.getStateVersion() + "\"/>" ); 185 } 186 187 protected void outputInstrumentable( PrintWriter out, 188 InstrumentableDescriptor instrumentable, 189 String indent, 190 boolean recurse, 191 boolean packed ) 192 throws IOException 193 { 194 InstrumentableDescriptor[] instrumentables = 195 instrumentable.getChildInstrumentableDescriptors(); 196 InstrumentDescriptor[] instruments = instrumentable.getInstrumentDescriptors(); 197 198 String 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 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 out, 250 InstrumentDescriptor instrument, 251 String indent, 252 boolean packed ) 253 throws IOException 254 { 255 outputLine( out, indent, packed, "<instrument " 256 + "name=\"" + makeSafeAttribute( instrument.getName() ) + "\" " 257 + "state-version=\"" + instrument.getStateVersion() + "\"/>" ); 258 } 259 260 protected void outputInstrument( PrintWriter out, 261 InstrumentDescriptor instrument, 262 String indent, 263 boolean recurse, 264 boolean packed ) 265 throws IOException 266 { 267 InstrumentSampleDescriptor[] samples = instrument.getInstrumentSampleDescriptors(); 268 269 String 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 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 out, 309 InstrumentSampleDescriptor sample, 310 String indent, 311 boolean packed ) 312 throws IOException 313 { 314 outputLine( out, indent, packed, "<sample " 315 + "name=\"" + makeSafeAttribute( sample.getName() ) + "\" " 316 + "state-version=\"" + sample.getStateVersion() + "\"/>" ); 317 } 318 319 protected void outputSample( PrintWriter out, 320 InstrumentSampleDescriptor sample, 321 String indent, 322 boolean packed ) 323 throws IOException 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 out, 339 InstrumentSampleDescriptor sample, 340 String indent, 341 long baseTime, 342 boolean packed, 343 boolean compact ) 344 throws IOException 345 { 346 InstrumentSampleSnapshot snapshot = sample.getSnapshot(); 347 int[] values = snapshot.getSamples(); 348 349 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 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 childIndent = indent + INDENT; 387 388 if ( compact ) 389 { 390 StringBuffer sb = new StringBuffer (); 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 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 |