ComponentBase.java

biz/hammurapi/config/ComponentBase.java

Violations

Inspector Message Severity Location
Java Inspector 048 Copyrights information should be present in each file. 1
Java Inspector 089 Undocumented field 2 36:9
Java Inspector 089 Undocumented method 2 40:17
Java Inspector 089 Undocumented method 2 44:17
Java Inspector 089 Parameter name is not documented 2 55:9
Java Inspector 089 Method return value is not properly documented 2 55:9
Java Inspector 089 Undocumented method 2 59:9
Java Inspector 089 Undocumented method 2 63:9
Java Inspector 089 Undocumented method 2 69:9
Java Inspector 089 Undocumented method 2 76:9
Java Inspector 089 Undocumented method 2 80:9
Java Inspector 089 Parameter ownerType is not documented 2 91:9
Java Inspector 089 Method is not properly documented 2 110:9

Source code

1/*
2 * hgcommons 9
3 * Hammurapi Group Common Library
4 * Copyright (C) 2003 Hammurapi Group
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
21 * e-Mail: support@hammurapi.biz
22 */
23package biz.hammurapi.config;
24
25import biz.hammurapi.metrics.MeasurementCollector;
26import biz.hammurapi.metrics.MeasurementConsumer;
27
28/**
29 * Base class for components. Implements some standard functions.
30 * Measurement collection is delegated if measurement consumer is set.
31 * @author Pavel Vlasov
32 * @revision $Revision$
33 */
34public abstract class ComponentBase implements Component, Context, MeasurementConsumer, MeasurementCollector {
35
36 protected Object owner;
37
38 private PathNavigator pathNavigator=new PathNavigator(this) {
39
40 protected Object getParent() {
41 return owner;
42 }
43
44 protected Object getChild(String name) {
45 return ComponentBase.this.getChild(name);
46 }
47
48 };
49
50 /**
51 * Override this method if component has subcomponents.
52 * @param name
53 * @return
54 */
55 protected Object getChild(String name) {
56 return null;
57 }
58
59 public void setOwner(Object owner) {
60 this.owner=owner;
61 }
62
63 public Object get(String name) {
64 return pathNavigator.get(name);
65 }
66
67 private MeasurementConsumer measurementConsumer;
68
69 public void setMeasurementConsumer(MeasurementConsumer measurementConsumer) {
70 this.measurementConsumer = measurementConsumer;
71 if (measurementConsumer instanceof Component) {
72 ((Component) getMeasurementConsumer()).setOwner(this);
73 }
74 }
75
76 public MeasurementConsumer getMeasurementConsumer() {
77 return measurementConsumer;
78 }
79
80 public void addMeasurement(String name, double value, long time) {
81 if (measurementConsumer!=null) {
82 measurementConsumer.addMeasurement(name, value, time==0 ? System.currentTimeMillis() : time);
83 }
84 }
85
86 /**
87 * Finds component owner of particular type.
88 * @param ownerType
89 * @return Owner which is an instance of specified type or null if no such owner is found.
90 */
91 public Object getOwner(Class ownerType) {
92 if (owner==null || ownerType.isInstance(owner)) {
93 return owner;
94 }
95
96 if (owner instanceof ComponentBase) {
97 return ((ComponentBase) owner).getOwner(ownerType);
98 }
99
100 if (owner instanceof GenericContainer) {
101 return ((GenericContainer) owner).getOwner(ownerType);
102 }
103
104 return null;
105 }
106
107 /**
108 * @return Immediate component owner
109 */
110 public Object getOwner() {
111 return owner;
112 }
113}
114