MonitoredObjectImpl.java revision 608:7e06bf1dcb09
1/*
2 * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.corba.se.impl.monitoring;
27
28import java.util.Map;
29import java.util.HashMap;
30import java.util.Collection;
31import java.util.Iterator;
32
33import com.sun.corba.se.spi.monitoring.MonitoredObject;
34import com.sun.corba.se.spi.monitoring.MonitoredAttribute;
35
36public class MonitoredObjectImpl implements MonitoredObject {
37    private final String name;
38    private final String description;
39
40    // List of all child Monitored Objects
41    private Map children = new HashMap();
42
43    // All the Attributes of this Monitored Object instance
44    private Map monitoredAttributes = new HashMap();
45
46    private MonitoredObject parent = null;
47
48
49    // Constructor
50    MonitoredObjectImpl( String name, String description ) {
51        this.name = name;
52        this.description = description;
53    }
54
55    public MonitoredObject getChild( String name ) {
56        synchronized( this ) {
57            return (MonitoredObject) children.get( name );
58        }
59    }
60
61    public Collection getChildren( ) {
62        synchronized( this ) {
63            return children.values();
64        }
65    }
66
67    public void addChild( MonitoredObject m ) {
68        if (m != null){
69            synchronized( this ) {
70                children.put( m.getName(), m);
71                m.setParent( this );
72            }
73        }
74    }
75
76    public void removeChild( String name ) {
77        if (name != null){
78            synchronized( this ) {
79                children.remove( name );
80            }
81        }
82    }
83
84    public synchronized MonitoredObject getParent( ) {
85       return parent;
86    }
87
88    public synchronized void setParent( MonitoredObject p ) {
89        parent = p;
90    }
91
92    public MonitoredAttribute getAttribute( String name ) {
93        synchronized( this ) {
94            return (MonitoredAttribute) monitoredAttributes.get( name );
95        }
96    }
97
98    public Collection getAttributes( ) {
99        synchronized( this ) {
100            return monitoredAttributes.values();
101        }
102    }
103
104    public void addAttribute( MonitoredAttribute value ) {
105        if (value != null) {
106            synchronized( this ) {
107                monitoredAttributes.put( value.getName(), value );
108            }
109        }
110    }
111
112    public void removeAttribute( String name ) {
113        if (name != null) {
114            synchronized( this ) {
115                monitoredAttributes.remove( name );
116            }
117        }
118    }
119
120    /**
121     * calls clearState() on all the registered children MonitoredObjects and
122     * MonitoredAttributes.
123     */
124    public void clearState( ) {
125        synchronized( this ) {
126            Iterator i = monitoredAttributes.values().iterator();
127            // First call clearState on all the local attributes
128            while( i.hasNext( ) ) {
129                ((MonitoredAttribute)i.next()).clearState();
130            }
131            i = children.values().iterator();
132            // next call clearState on all the children MonitoredObjects
133            while( i.hasNext() ) {
134                ((MonitoredObject)i.next()).clearState();
135           }
136        }
137    }
138
139    public String getName( ) {
140        return name;
141    }
142
143    public String getDescription( ) {
144        return description;
145    }
146}
147