001: package org.jacorb.notification.util;
002:
003: /*
004: * JacORB - a free Java ORB
005: *
006: * Copyright (C) 1999-2004 Gerald Brose
007: *
008: * This library is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU Library General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * This library is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * Library General Public License for more details.
017: *
018: * You should have received a copy of the GNU Library General Public
019: * License along with this library; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: *
022: */
023:
024: import java.util.ArrayList;
025: import java.util.Collections;
026: import java.util.HashMap;
027: import java.util.HashSet;
028: import java.util.Iterator;
029: import java.util.List;
030: import java.util.Map;
031: import java.util.Set;
032:
033: import org.apache.avalon.framework.logger.Logger;
034: import org.omg.CORBA.Any;
035: import org.omg.CORBA.ORB;
036: import org.omg.CosNotification.Property;
037: import org.omg.CosNotification.PropertyError;
038: import org.omg.CosNotification.PropertyRange;
039: import org.omg.CosNotification.QoSError_code;
040:
041: /**
042: * @author Alphonse Bendt
043: * @version $Id: PropertySet.java,v 1.8 2005/12/02 21:09:01 alphonse.bendt Exp $
044: */
045:
046: public abstract class PropertySet {
047: protected static final ORB sORB = ORB.init();
048:
049: protected static final PropertyError[] PROPERTY_ERROR_ARRAY_TEMPLATE = new PropertyError[0];
050:
051: protected static final PropertyRange EMPTY_PROPERTY_RANGE = new PropertyRange(
052: sORB.create_any(), sORB.create_any());
053:
054: ////////////////////////////////////////
055:
056: protected final Logger logger_ = LogUtil.getLogger(getClass()
057: .getName());
058:
059: private final Map listeners_ = new HashMap();
060:
061: private boolean modified_ = true;
062:
063: private final Map properties_ = new HashMap();
064:
065: private Property[] arrayView_ = null;
066:
067: ////////////////////////////////////////
068:
069: public void addPropertySetListener(String[] props,
070: PropertySetListener listener) {
071: for (int x = 0; x < props.length; ++x) {
072: addPropertySetListener(props[x], listener);
073: }
074: }
075:
076: public void addPropertySetListener(String property,
077: PropertySetListener listener) {
078: final List _list;
079:
080: if (!listeners_.containsKey(property)) {
081: _list = new ArrayList();
082: listeners_.put(property, _list);
083: } else {
084: _list = (List) listeners_.get(property);
085: }
086:
087: _list.add(listener);
088: }
089:
090: public synchronized Property[] toArray() {
091: if (arrayView_ == null || modified_) {
092: Property[] _props = new Property[properties_.size()];
093:
094: Iterator i = properties_.keySet().iterator();
095: int x = 0;
096: while (i.hasNext()) {
097: String _key = (String) i.next();
098: _props[x++] = new Property(_key, (Any) properties_
099: .get(_key));
100: }
101: arrayView_ = _props;
102: modified_ = false;
103: }
104:
105: return arrayView_;
106: }
107:
108: public Map toMap() {
109: return Collections.unmodifiableMap(properties_);
110: }
111:
112: public String toString() {
113: return properties_.toString();
114: }
115:
116: public boolean containsKey(String propertyName) {
117: return properties_.containsKey(propertyName);
118: }
119:
120: public Any get(String propertyName) {
121: return (Any) properties_.get(propertyName);
122: }
123:
124: protected void set_properties(Property[] props) {
125: final HashSet _toBeNotified = new HashSet();
126:
127: for (int x = 0; x < props.length; ++x) {
128: Any _oldValue = null;
129:
130: if (properties_.containsKey(props[x].name)) {
131: _oldValue = (Any) properties_.get(props[x].name);
132: }
133:
134: properties_.put(props[x].name, props[x].value);
135:
136: if (listeners_.containsKey(props[x].name)) {
137: if (!props[x].value.equals(_oldValue)) {
138: _toBeNotified.addAll((List) listeners_
139: .get(props[x].name));
140: }
141: }
142: }
143:
144: synchronized (this ) {
145: modified_ = true;
146: }
147:
148: Iterator i = _toBeNotified.iterator();
149: while (i.hasNext()) {
150: try {
151: ((PropertySetListener) i.next())
152: .actionPropertySetChanged(this );
153: } catch (Exception e) {
154: logger_.error("exception in listener", e);
155: }
156: }
157: }
158:
159: abstract Set getValidNames();
160:
161: protected void checkPropertyExistence(Property[] props,
162: List errorList) {
163: for (int x = 0; x < props.length; ++x) {
164: if (!getValidNames().contains(props[x].name)) {
165: errorList.add(badProperty(props[x].name));
166: }
167: }
168: }
169:
170: protected PropertyError badProperty(String name) {
171: return new PropertyError(QoSError_code.BAD_PROPERTY, name,
172: EMPTY_PROPERTY_RANGE);
173: }
174:
175: protected PropertyError badType(String name) {
176: return new PropertyError(QoSError_code.BAD_TYPE, name,
177: EMPTY_PROPERTY_RANGE);
178: }
179:
180: ////////////////////////////////////////
181:
182: public static Property[] map2Props(Map props) {
183: Property[] _ps = new Property[props.size()];
184:
185: Iterator i = props.keySet().iterator();
186: int x = 0;
187: while (i.hasNext()) {
188: String _key = (String) i.next();
189: _ps[x++] = new Property(_key, (Any) props.get(_key));
190: }
191: return _ps;
192: }
193: }
|