001: /*
002: * hgcommons 7
003: * Hammurapi Group Common Library
004: * Copyright (C) 2003 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.config;
024:
025: import biz.hammurapi.metrics.MeasurementCollector;
026: import biz.hammurapi.metrics.MeasurementConsumer;
027:
028: /**
029: * Base class for components. Implements some standard functions.
030: * Measurement collection is delegated if measurement consumer is set.
031: * @author Pavel Vlasov
032: * @revision $Revision$
033: */
034: public abstract class ComponentBase implements Component, Context,
035: MeasurementConsumer, MeasurementCollector {
036:
037: protected Object owner;
038:
039: private PathNavigator pathNavigator = new PathNavigator(this ) {
040:
041: protected Object getParent() {
042: return owner;
043: }
044:
045: protected Object getChild(String name) {
046: return ComponentBase.this .getChild(name);
047: }
048:
049: };
050:
051: /**
052: * Override this method if component has subcomponents.
053: * @param name
054: * @return
055: */
056: protected Object getChild(String name) {
057: return null;
058: }
059:
060: public void setOwner(Object owner) {
061: this .owner = owner;
062: }
063:
064: public Object get(String name) {
065: return pathNavigator.get(name);
066: }
067:
068: private MeasurementConsumer measurementConsumer;
069:
070: public void setMeasurementConsumer(
071: MeasurementConsumer measurementConsumer) {
072: this .measurementConsumer = measurementConsumer;
073: if (measurementConsumer instanceof Component) {
074: ((Component) getMeasurementConsumer()).setOwner(this );
075: }
076: }
077:
078: public MeasurementConsumer getMeasurementConsumer() {
079: return measurementConsumer;
080: }
081:
082: public void addMeasurement(String name, double value, long time) {
083: if (measurementConsumer != null) {
084: measurementConsumer.addMeasurement(name, value,
085: time == 0 ? System.currentTimeMillis() : time);
086: }
087: }
088:
089: /**
090: * Finds component owner of particular type.
091: * @param ownerType
092: * @return Owner which is an instance of specified type or null if no such owner is found.
093: */
094: public Object getOwner(Class ownerType) {
095: if (owner == null || ownerType.isInstance(owner)) {
096: return owner;
097: }
098:
099: if (owner instanceof ComponentBase) {
100: return ((ComponentBase) owner).getOwner(ownerType);
101: }
102:
103: if (owner instanceof GenericContainer) {
104: return ((GenericContainer) owner).getOwner(ownerType);
105: }
106:
107: return null;
108: }
109: }
|