KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > notification > util > PropertySet


1 package org.jacorb.notification.util;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 1999-2004 Gerald Brose
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  */

23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.Set JavaDoc;
32
33 import org.apache.avalon.framework.logger.Logger;
34 import org.omg.CORBA.Any JavaDoc;
35 import org.omg.CORBA.ORB JavaDoc;
36 import org.omg.CosNotification.Property;
37 import org.omg.CosNotification.PropertyError;
38 import org.omg.CosNotification.PropertyRange;
39 import org.omg.CosNotification.QoSError_code;
40
41 /**
42  * @author Alphonse Bendt
43  * @version $Id: PropertySet.java,v 1.7 2005/04/10 14:30:31 alphonse.bendt Exp $
44  */

45
46 public abstract class PropertySet
47 {
48     protected static final ORB JavaDoc sORB = ORB.init();
49
50     protected static final PropertyError[] PROPERTY_ERROR_ARRAY_TEMPLATE = new PropertyError[0];
51
52     protected static final PropertyRange EMPTY_PROPERTY_RANGE = new PropertyRange(
53             sORB.create_any(), sORB.create_any());
54
55     ////////////////////////////////////////
56

57     protected final Logger logger_ = LogUtil.getLogger(getClass().getName());
58
59     private final Map JavaDoc listeners_ = new HashMap JavaDoc();
60
61     private boolean modified_ = true;
62
63     private final Map JavaDoc properties_ = new HashMap JavaDoc();
64
65     private Property[] arrayView_ = null;
66
67     ////////////////////////////////////////
68

69     public void addPropertySetListener(String JavaDoc[] props, PropertySetListener listener)
70     {
71         for (int x = 0; x < props.length; ++x)
72         {
73             addPropertySetListener(props[x], listener);
74         }
75     }
76
77     public void addPropertySetListener(String JavaDoc property, PropertySetListener listener)
78     {
79         List JavaDoc _list;
80
81         if (!listeners_.containsKey(property))
82         {
83             _list = new ArrayList JavaDoc();
84             listeners_.put(property, _list);
85         }
86         else
87         {
88             _list = (List JavaDoc) listeners_.get(property);
89         }
90
91         _list.add(listener);
92     }
93
94     public synchronized Property[] toArray()
95     {
96         if (arrayView_ == null || modified_)
97         {
98             Property[] _props = new Property[properties_.size()];
99
100             Iterator JavaDoc i = properties_.keySet().iterator();
101             int x = 0;
102             while (i.hasNext())
103             {
104                 String JavaDoc _key = (String JavaDoc) i.next();
105                 _props[x++] = new Property(_key, (Any JavaDoc) properties_.get(_key));
106             }
107             arrayView_ = _props;
108             modified_ = false;
109         }
110         
111         return arrayView_;
112     }
113
114     public Map JavaDoc toMap()
115     {
116         return Collections.unmodifiableMap(properties_);
117     }
118
119     public String JavaDoc toString()
120     {
121         return properties_.toString();
122     }
123
124     public boolean containsKey(String JavaDoc propertyName)
125     {
126         return properties_.containsKey(propertyName);
127     }
128
129     public Any JavaDoc get(String JavaDoc propertyName)
130     {
131         return (Any JavaDoc) properties_.get(propertyName);
132     }
133
134     protected void set_properties(Property[] props)
135     {
136         HashSet JavaDoc _toBeNotified = new HashSet JavaDoc();
137
138         for (int x = 0; x < props.length; ++x)
139         {
140             Any JavaDoc _oldValue = null;
141
142             if (properties_.containsKey(props[x].name))
143             {
144                 _oldValue = (Any JavaDoc) properties_.get(props[x].name);
145             }
146
147             properties_.put(props[x].name, props[x].value);
148
149             if (listeners_.containsKey(props[x].name))
150             {
151                 if (!props[x].value.equals(_oldValue))
152                 {
153                     _toBeNotified.addAll((List JavaDoc) listeners_.get(props[x].name));
154                 }
155             }
156         }
157
158         synchronized (this)
159         {
160             modified_ = true;
161         }
162
163         Iterator JavaDoc i = _toBeNotified.iterator();
164         while (i.hasNext())
165         {
166             try
167             {
168                 ((PropertySetListener) i.next()).actionPropertySetChanged(this);
169             } catch (Exception JavaDoc e)
170             {
171                 logger_.error("exception in listener", e);
172             }
173         }
174     }
175
176     abstract Set JavaDoc getValidNames();
177
178     protected void checkPropertyExistence(Property[] props, List JavaDoc errorList)
179     {
180         for (int x = 0; x < props.length; ++x)
181         {
182             if (!getValidNames().contains(props[x].name))
183             {
184                 errorList.add(badProperty(props[x].name));
185             }
186         }
187     }
188
189     protected PropertyError badProperty(String JavaDoc name)
190     {
191         return new PropertyError(QoSError_code.BAD_PROPERTY, name, EMPTY_PROPERTY_RANGE);
192     }
193
194     protected PropertyError badType(String JavaDoc name)
195     {
196         return new PropertyError(QoSError_code.BAD_TYPE, name, EMPTY_PROPERTY_RANGE);
197     }
198
199     ////////////////////////////////////////
200

201     public static Property[] map2Props(Map JavaDoc props)
202     {
203         Property[] _ps = new Property[props.size()];
204
205         Iterator JavaDoc i = props.keySet().iterator();
206         int x = 0;
207         while (i.hasNext())
208         {
209             String JavaDoc _key = (String JavaDoc) i.next();
210             _ps[x++] = new Property(_key, (Any JavaDoc) props.get(_key));
211         }
212         return _ps;
213     }
214 }
Popular Tags