1 16 package org.apache.cocoon.generation; 17 18 import org.apache.avalon.framework.configuration.Configurable; 19 import org.apache.avalon.framework.configuration.Configuration; 20 import org.apache.avalon.framework.configuration.ConfigurationException; 21 import org.apache.avalon.framework.context.Context; 22 import org.apache.avalon.framework.context.ContextException; 23 import org.apache.avalon.framework.context.Contextualizable; 24 import org.apache.avalon.framework.parameters.Parameters; 25 import org.apache.avalon.framework.service.ServiceException; 26 import org.apache.avalon.framework.service.ServiceManager; 27 28 import org.apache.cocoon.Constants; 29 import org.apache.cocoon.ProcessingException; 30 import org.apache.cocoon.ResourceNotFoundException; 31 import org.apache.cocoon.components.source.SourceUtil; 32 import org.apache.cocoon.environment.SourceResolver; 33 import org.apache.cocoon.xml.AttributesImpl; 34 import org.apache.cocoon.xml.XMLUtils; 35 36 import org.apache.commons.lang.SystemUtils; 37 import org.apache.excalibur.source.Source; 38 import org.apache.excalibur.source.SourceException; 39 import org.apache.excalibur.source.TraversableSource; 40 import org.apache.excalibur.store.Store; 41 import org.apache.excalibur.store.StoreJanitor; 42 import org.xml.sax.Attributes ; 43 import org.xml.sax.SAXException ; 44 45 import java.io.File ; 46 import java.io.IOException ; 47 import java.net.InetAddress ; 48 import java.net.UnknownHostException ; 49 import java.text.DateFormat ; 50 import java.util.ArrayList ; 51 import java.util.Collection ; 52 import java.util.Date ; 53 import java.util.Enumeration ; 54 import java.util.Iterator ; 55 import java.util.List ; 56 import java.util.Map ; 57 import java.util.Properties ; 58 import java.util.Set ; 59 import java.util.StringTokenizer ; 60 import java.util.TreeSet ; 61 62 102 public class StatusGenerator extends ServiceableGenerator 103 implements Contextualizable, Configurable { 104 105 108 public static final String NAMESPACE = "http://apache.org/cocoon/status/2.0"; 109 110 113 protected static final String XLINK_NS = "http://www.w3.org/1999/xlink"; 114 115 118 protected static final String XLINK_PREFIX = "xlink"; 119 120 123 protected Context context; 124 125 128 protected StoreJanitor storeJanitor; 129 130 133 protected Store storePersistent; 134 135 138 private boolean showLibrary; 139 140 143 private Source libDirectory; 144 145 146 public void contextualize(Context context) throws ContextException { 147 this.context = context; 148 } 149 150 public void configure(Configuration configuration) throws ConfigurationException { 151 this.showLibrary = configuration.getChild("show-libraries").getValueAsBoolean(true); 152 } 153 154 159 public void service(ServiceManager manager) throws ServiceException { 160 super.service(manager); 161 162 if (this.manager.hasService(StoreJanitor.ROLE)) { 163 this.storeJanitor = (StoreJanitor) manager.lookup(StoreJanitor.ROLE); 164 } else { 165 getLogger().info("StoreJanitor is not available. Sorry, no cache statistics"); 166 } 167 168 if (this.manager.hasService(Store.PERSISTENT_STORE)) { 169 this.storePersistent = (Store) this.manager.lookup(Store.PERSISTENT_STORE); 170 } else { 171 getLogger().info("Persistent Store is not available. Sorry no cache statistics about it."); 172 } 173 } 174 175 public void setup(SourceResolver resolver, Map objectModel, String src, Parameters par) 176 throws ProcessingException, SAXException , IOException { 177 super.setup(resolver, objectModel, src, par); 178 179 if (this.showLibrary) { 180 try { 181 this.libDirectory = super.resolver.resolveURI("context://WEB-INF/lib"); 182 } catch (SourceException e) { 183 throw SourceUtil.handle(e); 184 } 185 } 186 } 187 188 191 public void dispose() { 192 if (this.manager != null) { 193 this.manager.release(this.storePersistent); 194 this.manager.release(this.storeJanitor); 195 this.storePersistent = null; 196 this.storeJanitor = null; 197 } 198 199 if (this.libDirectory != null) { 200 super.resolver.release(this.libDirectory); 201 this.libDirectory = null; 202 } 203 204 super.dispose(); 205 } 206 207 212 public void generate() throws SAXException , ProcessingException { 213 214 super.contentHandler.startDocument(); 216 super.contentHandler.startPrefixMapping("", NAMESPACE); 217 super.contentHandler.startPrefixMapping(XLINK_PREFIX, XLINK_NS); 218 219 genStatus(); 220 221 super.contentHandler.endPrefixMapping(XLINK_PREFIX); 223 super.contentHandler.endPrefixMapping(""); 224 super.contentHandler.endDocument(); 225 } 226 227 230 private void genStatus() throws SAXException , ProcessingException { 231 233 String dateTime = DateFormat.getDateTimeInstance().format(new Date ()); 235 String localHost; 236 237 try { 239 localHost = InetAddress.getLocalHost().getHostName(); 240 } catch (UnknownHostException e) { 241 getLogger().debug("StatusGenerator:UnknownHost", e); 242 localHost = ""; 243 } catch (SecurityException e) { 244 getLogger().debug("StatusGenerator:Security", e); 245 localHost = ""; 246 } 247 248 AttributesImpl atts = new AttributesImpl(); 249 atts.addCDATAAttribute(NAMESPACE, "date", dateTime); 250 atts.addCDATAAttribute(NAMESPACE, "host", localHost); 251 atts.addCDATAAttribute(NAMESPACE, "cocoon-version", Constants.VERSION); 252 super.contentHandler.startElement(NAMESPACE, "statusinfo", "statusinfo", atts); 253 254 genVMStatus(); 255 genProperties(); 256 if (this.showLibrary) { 257 genLibrarylist(); 258 } 259 260 super.contentHandler.endElement(NAMESPACE, "statusinfo", "statusinfo"); 262 } 263 264 private void genVMStatus() throws SAXException { 265 AttributesImpl atts = new AttributesImpl(); 266 startGroup("VM"); 267 268 String classpath = SystemUtils.JAVA_CLASS_PATH; 270 if (classpath != null) { 271 List paths = new ArrayList (); 272 StringTokenizer tokenizer = new StringTokenizer (classpath, SystemUtils.PATH_SEPARATOR); 273 while (tokenizer.hasMoreTokens()) { 274 paths.add(tokenizer.nextToken()); 275 } 276 addMultilineValue("classpath", paths); 277 } 278 280 String contextClassPath = null; 282 try { 283 contextClassPath = (String ) this.context.get(Constants.CONTEXT_CLASSPATH); 284 } catch (ContextException e) { 285 } 287 if (contextClassPath != null) { 288 List paths = new ArrayList (); 289 StringTokenizer tokenizer = new StringTokenizer (contextClassPath, File.pathSeparator); 290 while (tokenizer.hasMoreTokens()) { 291 paths.add(tokenizer.nextToken()); 292 } 293 addMultilineValue("context-classpath", paths); 294 } 295 297 startGroup("Memory"); 299 final long totalMemory = Runtime.getRuntime().totalMemory(); 300 final long freeMemory = Runtime.getRuntime().freeMemory(); 301 addValue("total", String.valueOf(totalMemory)); 302 addValue("used", String.valueOf(totalMemory - freeMemory)); 303 addValue("free", String.valueOf(freeMemory)); 304 endGroup(); 305 307 startGroup("JRE"); 309 addValue("version", SystemUtils.JAVA_VERSION); 310 atts.clear(); 311 atts.addAttribute(XLINK_NS, "type", XLINK_PREFIX + ":type", "CDATA", "simple"); 313 atts.addAttribute(XLINK_NS, "href", XLINK_PREFIX + ":href", "CDATA", SystemUtils.JAVA_VENDOR_URL); 314 addValue("java-vendor", SystemUtils.JAVA_VENDOR, atts); 315 endGroup(); 316 318 startGroup("Operating System"); 320 addValue("name", SystemUtils.OS_NAME); 321 addValue("architecture", SystemUtils.OS_ARCH); 322 addValue("version", SystemUtils.OS_VERSION); 323 endGroup(); 324 326 if (this.storeJanitor != null) { 328 startGroup("Store Janitor"); 329 330 Iterator i = this.storeJanitor.iterator(); 332 while (i.hasNext()) { 333 Store store = (Store) i.next(); 334 startGroup(store.getClass().getName() + " (hash = 0x" + Integer.toHexString(store.hashCode()) + ")"); 335 int size = 0; 336 int empty = 0; 337 atts.clear(); 338 atts.addAttribute(NAMESPACE, "name", "name", "CDATA", "cached"); 339 super.contentHandler.startElement(NAMESPACE, "value", "value", atts); 340 341 atts.clear(); 342 Enumeration e = store.keys(); 343 while (e.hasMoreElements()) { 344 size++; 345 Object key = e.nextElement(); 346 Object val = store.get(key); 347 String line; 348 if (val == null) { 349 empty++; 350 } else { 351 line = key + " (class: " + val.getClass().getName() + ")"; 352 super.contentHandler.startElement(NAMESPACE, "line", "line", atts); 353 super.contentHandler.characters(line.toCharArray(), 0, line.length()); 354 super.contentHandler.endElement(NAMESPACE, "line", "line"); 355 } 356 } 357 if (size == 0) { 358 super.contentHandler.startElement(NAMESPACE, "line", "line", atts); 359 String value = "[empty]"; 360 super.contentHandler.characters(value.toCharArray(), 0, value.length()); 361 super.contentHandler.endElement(NAMESPACE, "line", "line"); 362 } 363 super.contentHandler.endElement(NAMESPACE, "value", "value"); 364 365 addValue("size", String.valueOf(size) + " items in cache (" + empty + " are empty)"); 366 endGroup(); 367 } 368 endGroup(); 369 } 370 371 if (this.storePersistent != null) { 372 startGroup(storePersistent.getClass().getName() + " (hash = 0x" + Integer.toHexString(storePersistent.hashCode()) + ")"); 373 int size = 0; 374 int empty = 0; 375 atts.clear(); 376 atts.addAttribute(NAMESPACE, "name", "name", "CDATA", "cached"); 377 super.contentHandler.startElement(NAMESPACE, "value", "value", atts); 378 379 atts.clear(); 380 Enumeration e = this.storePersistent.keys(); 381 while (e.hasMoreElements()) { 382 size++; 383 Object key = e.nextElement(); 384 Object val = storePersistent.get(key); 385 String line; 386 if (val == null) { 387 empty++; 388 } else { 389 line = key + " (class: " + val.getClass().getName() + ")"; 390 super.contentHandler.startElement(NAMESPACE, "line", "line", atts); 391 super.contentHandler.characters(line.toCharArray(), 0, line.length()); 392 super.contentHandler.endElement(NAMESPACE, "line", "line"); 393 } 394 } 395 if (size == 0) { 396 super.contentHandler.startElement(NAMESPACE, "line", "line", atts); 397 String value = "[empty]"; 398 super.contentHandler.characters(value.toCharArray(), 0, value.length()); 399 super.contentHandler.endElement(NAMESPACE, "line", "line"); 400 } 401 super.contentHandler.endElement(NAMESPACE, "value", "value"); 402 403 addValue("size", size + " items in cache (" + empty + " are empty)"); 404 endGroup(); 405 } 406 408 endGroup(); 409 } 410 411 private void genProperties() throws SAXException { 412 this.startGroup("System-Properties"); 413 final Properties p = System.getProperties(); 414 final Enumeration e = p.keys(); 415 while ( e.hasMoreElements() ) { 416 final String key = (String )e.nextElement(); 417 final String value = p.getProperty(key); 418 this.addValue(key, value); 419 } 420 this.endGroup(); 421 } 422 423 private void genLibrarylist() throws SAXException ,ProcessingException { 424 try { 425 if (this.libDirectory instanceof TraversableSource) { 426 startGroup("WEB-INF/lib"); 427 428 Set files = new TreeSet (); 429 Collection kids = ((TraversableSource) this.libDirectory).getChildren(); 430 try { 431 for (Iterator i = kids.iterator(); i.hasNext(); ) { 432 final Source lib = (Source) i.next(); 433 final String name = lib.getURI().substring(lib.getURI().lastIndexOf('/')); 434 files.add(name); 435 } 436 } finally { 437 for (Iterator i = kids.iterator(); i.hasNext(); ) { 438 final Source lib = (Source) i.next(); 439 super.resolver.release(lib); 440 } 441 } 442 443 for (Iterator i = files.iterator(); i.hasNext(); ) { 444 addValue("file", (String ) i.next()); 445 } 446 447 endGroup(); 448 } 449 } catch (SourceException e) { 450 throw new ResourceNotFoundException("Could not read directory", e); 451 } 452 } 453 454 455 private void startGroup(String name) throws SAXException { 456 startGroup(name, null); 457 } 458 459 460 private void startGroup(String name, Attributes atts) 461 throws SAXException { 462 AttributesImpl ai = (atts == null) ? new AttributesImpl() : new AttributesImpl(atts); 463 ai.addAttribute(NAMESPACE, "name", "name", "CDATA", name); 464 super.contentHandler.startElement(NAMESPACE, "group", "group", ai); 465 } 466 467 468 private void endGroup() throws SAXException { 469 super.contentHandler.endElement(NAMESPACE, "group", "group"); 470 } 471 472 473 private void addValue(String name, String value) 474 throws SAXException { 475 addValue(name, value, null); 476 } 477 478 479 private void addValue(String name, String value, Attributes atts) 480 throws SAXException { 481 AttributesImpl ai = (atts == null) ? new AttributesImpl() : new AttributesImpl(atts); 482 ai.addAttribute(NAMESPACE, "name", "name", "CDATA", name); 483 super.contentHandler.startElement(NAMESPACE, "value", "value", ai); 484 super.contentHandler.startElement(NAMESPACE, "line", "line", XMLUtils.EMPTY_ATTRIBUTES); 485 486 if (value != null) { 487 super.contentHandler.characters(value.toCharArray(), 0, value.length()); 488 } 489 490 super.contentHandler.endElement(NAMESPACE, "line", "line"); 491 super.contentHandler.endElement(NAMESPACE, "value", "value"); 492 } 493 494 495 private void addMultilineValue(String name, List values) 496 throws SAXException { 497 addMultilineValue(name, values, null); 498 } 499 500 501 private void addMultilineValue(String name, List values, Attributes atts) 502 throws SAXException { 503 AttributesImpl ai = (atts == null) ? new AttributesImpl() : new AttributesImpl(atts); 504 ai.addAttribute(NAMESPACE, "name", "name", "CDATA", name); 505 super.contentHandler.startElement(NAMESPACE, "value", "value", ai); 506 507 for (int i = 0; i < values.size(); i++) { 508 String value = (String ) values.get(i); 509 if (value != null) { 510 super.contentHandler.startElement(NAMESPACE, "line", "line", XMLUtils.EMPTY_ATTRIBUTES); 511 super.contentHandler.characters(value.toCharArray(), 0, value.length()); 512 super.contentHandler.endElement(NAMESPACE, "line", "line"); 513 } 514 } 515 super.contentHandler.endElement(NAMESPACE, "value", "value"); 516 } 517 } 518 | Popular Tags |