KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > deployment > runtime > RuntimeDescriptor


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24
25 package com.sun.enterprise.deployment.runtime;
26
27 import java.beans.*;
28 import java.util.List JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import com.sun.enterprise.deployment.Descriptor;
31
32 /**
33  * This base class defines common behaviour and data for all runtime
34  * descriptors.
35  *
36  * @author Jerome Dochez
37  */

38 public abstract class RuntimeDescriptor extends Descriptor {
39     
40     protected PropertyChangeSupport propListeners;
41
42     /** Creates a new instance of RuntimeDescriptor */
43     public RuntimeDescriptor(RuntimeDescriptor other) {
44     super(other);
45     propListeners = new PropertyChangeSupport(this); // not copied
46
}
47
48     /** Creates a new instance of RuntimeDescriptor */
49     public RuntimeDescriptor() {
50     propListeners = new PropertyChangeSupport(this);
51     }
52     
53     /**
54      * Add a property listener for this bean
55      * @param the property listener
56      */

57     public void addPropertyChangeListener(PropertyChangeListener l) {
58         propListeners.addPropertyChangeListener(l);
59     }
60     
61     /**
62      * removes a property listener for this bean
63      * @param the property listener to remove
64      */

65     public void removePropertyChangeListener(PropertyChangeListener l) {
66         propListeners.removePropertyChangeListener(l);
67     }
68     
69     /**
70      * Add a property listener for a specific property name
71      * @param the property name
72      * @param the property listener
73      */

74     public void addPropertyChangeListener(String JavaDoc n, PropertyChangeListener l) {
75         propListeners.addPropertyChangeListener(n, l);
76     }
77     
78     /**
79      * Remover a property listener for specific property name
80      * @param the property name
81      * @param the property listener
82      */

83     public void removePropertyChangeListener(String JavaDoc n, PropertyChangeListener l) {
84         propListeners.removePropertyChangeListener(n, l);
85     }
86     
87     /**
88      * Sets a property value
89      * @param the property name
90      * @param the property value
91      */

92     public void setValue(String JavaDoc name, Object JavaDoc value) {
93         Object JavaDoc oldValue = getExtraAttribute(name);
94         addExtraAttribute(name, value);
95         propListeners.firePropertyChange(name, oldValue, value);
96     }
97     
98     /**
99      * @return a property value
100      */

101     public Object JavaDoc getValue(String JavaDoc name) {
102         return getExtraAttribute(name);
103     }
104     
105     /**
106      * indexed property support
107      */

108     protected void setValue(String JavaDoc name, int index, Object JavaDoc value) {
109     List JavaDoc list = getIndexedProperty(name);
110     list.set(index, value);
111     setValue(name, list);
112     }
113     
114     protected Object JavaDoc getValue(String JavaDoc name, int index) {
115     List JavaDoc list = getIndexedProperty(name);
116     return list.get(index);
117     }
118     
119     protected int addValue(String JavaDoc name, Object JavaDoc value) {
120     List JavaDoc list = getIndexedProperty(name);
121     list.add(value);
122     setValue(name, list);
123     return list.indexOf(value);
124     }
125     
126     protected int removeValue(String JavaDoc name, Object JavaDoc value) {
127     List JavaDoc list = getIndexedProperty(name);
128     int index = list.indexOf(value);
129     list.remove(index);
130     return index;
131     }
132     
133     protected void removeValue(String JavaDoc name, int index) {
134     List JavaDoc list = getIndexedProperty(name);
135     list.remove(index);
136     }
137     
138     protected void setValues(String JavaDoc name, Object JavaDoc[] values) {
139     List JavaDoc list = getIndexedProperty(name);
140     for (int i=0;i<values.length;) {
141         list.add(values[i]);
142     }
143     }
144     
145     protected Object JavaDoc[] getValues(String JavaDoc name) {
146     List JavaDoc list = (List JavaDoc) getValue(name);
147     if (list!=null && list.size()>0) {
148         Class JavaDoc c = list.get(0).getClass();
149         Object JavaDoc array = java.lang.reflect.Array.newInstance(c, list.size());
150         return list.toArray((Object JavaDoc[]) array);
151     }
152     else
153         return null;
154     }
155     
156     protected int size(String JavaDoc name) {
157     List JavaDoc list = (List JavaDoc) getValue(name);
158     if (list!=null)
159         return list.size();
160     else
161         return 0;
162     }
163     
164     private List JavaDoc getIndexedProperty(String JavaDoc name) {
165     Object JavaDoc o = getValue(name);
166     if (o==null) {
167         return new ArrayList JavaDoc();
168     } else {
169         return (List JavaDoc) o;
170     }
171     }
172     
173     // xml attributes support
174
public void setAttributeValue(String JavaDoc elementName, String JavaDoc attributeName, Object JavaDoc value) {
175     // here we have to play games...
176
// the nodes cannot know if the property scalar is 0,1 or n
177
// so we look if the key name is already used (means property*
178
// DTD langua) and find the last one entered
179

180     
181     int index = 0;
182     while (getValue(elementName + "-" + index + "-" + attributeName)!=null) {
183         index++;
184     }
185     if (index==0) {
186         // we had no value yet
187
setValue(elementName + "-0-" + attributeName, value);
188     } else {
189         setValue(elementName + "-" + (index+1) + "-" + attributeName, value);
190     }
191     }
192     
193     public String JavaDoc getAttributeValue(String JavaDoc elementName, String JavaDoc attributeName) {
194     return getAttributeValue(elementName,0, attributeName);
195     }
196     
197     // attribute stored at the descriptor level are treated like elements
198
public void setAttributeValue(String JavaDoc attributeName, String JavaDoc value) {
199     setValue(attributeName, value);
200     }
201     
202     public String JavaDoc getAttributeValue(String JavaDoc attributeName) {
203     return (String JavaDoc) getValue(attributeName);
204     }
205     
206     // indexed xml attributes support
207
public void setAttributeValue(String JavaDoc elementName, int index, String JavaDoc attributeName, Object JavaDoc value) {
208     setValue(elementName + "-" + index + "-" + attributeName, value);
209     }
210     
211     public String JavaDoc getAttributeValue(String JavaDoc elementName, int index, String JavaDoc attributeName) {
212     return (String JavaDoc) getValue(elementName + "-" + index + "-" + attributeName);
213     }
214 }
215
Popular Tags