001 /* 002 @license.text@ 003 */ 004 package biz.hammurapi.config; 005 006 import biz.hammurapi.metrics.MeasurementCollector; 007 import biz.hammurapi.metrics.MeasurementConsumer; 008 009 /** 010 * Base class for components. Implements some standard functions. 011 * Measurement collection is delegated if measurement consumer is set. 012 * @author Pavel Vlasov 013 * @revision $Revision$ 014 */ 015 public abstract class ComponentBase implements Component, Context, MeasurementConsumer, MeasurementCollector { 016 017 protected Object owner; 018 019 private PathNavigator pathNavigator=new PathNavigator(this) { 020 021 protected Object getParent() { 022 return owner; 023 } 024 025 protected Object getChild(String name) { 026 return ComponentBase.this.getChild(name); 027 } 028 029 }; 030 031 /** 032 * Override this method if component has subcomponents. 033 * @param name 034 * @return 035 */ 036 protected Object getChild(String name) { 037 return null; 038 } 039 040 public void setOwner(Object owner) { 041 this.owner=owner; 042 } 043 044 public Object get(String name) { 045 return pathNavigator.get(name); 046 } 047 048 private MeasurementConsumer measurementConsumer; 049 050 public void setMeasurementConsumer(MeasurementConsumer measurementConsumer) { 051 this.measurementConsumer = measurementConsumer; 052 if (measurementConsumer instanceof Component) { 053 ((Component) getMeasurementConsumer()).setOwner(this); 054 } 055 } 056 057 public MeasurementConsumer getMeasurementConsumer() { 058 return measurementConsumer; 059 } 060 061 public void addMeasurement(String name, double value, long time) { 062 if (measurementConsumer!=null) { 063 measurementConsumer.addMeasurement(name, value, time==0 ? System.currentTimeMillis() : time); 064 } 065 } 066 067 /** 068 * Finds component owner of particular type. 069 * @param ownerType 070 * @return Owner which is an instance of specified type or null if no such owner is found. 071 */ 072 public Object getOwner(Class ownerType) { 073 if (owner==null || ownerType.isInstance(owner)) { 074 return owner; 075 } 076 077 if (owner instanceof ComponentBase) { 078 return ((ComponentBase) owner).getOwner(ownerType); 079 } 080 081 if (owner instanceof GenericContainer) { 082 return ((GenericContainer) owner).getOwner(ownerType); 083 } 084 085 return null; 086 } 087 088 /** 089 * @return Immediate component owner 090 */ 091 public Object getOwner() { 092 return owner; 093 } 094 }