KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > deployment > DynamicAttributesDescriptor


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 package com.sun.enterprise.deployment;
25
26 import java.util.*;
27
28 /**
29  * This class is a value holder for dynamic attributes. Dynamic attributes
30  * can be added, queried and removed from this value holder. Attributes are
31  * identified by a string key.
32  *
33  * @author Jerome Dochez
34  */

35 public class DynamicAttributesDescriptor extends Observable {
36     
37     private Map dynamicAttributes;
38     
39     /**
40     * Direct acess to the dynamic attributes repository
41     * @return the Map of dynamic attributes
42     */

43
44     public Map getExtraAttributes() {
45     if (dynamicAttributes == null) {
46         dynamicAttributes = new Hashtable();
47     }
48     return dynamicAttributes;
49     }
50     
51     /**
52      * Add a new dynamic attribte
53      * @param name the attribute name
54      * @param value the attribute value
55      */

56     public void addExtraAttribute(String JavaDoc name, Object JavaDoc value) {
57         if (value==null) {
58             return;
59         }
60     if (dynamicAttributes == null) {
61         dynamicAttributes = new Hashtable();
62     }
63         dynamicAttributes.put(name, value);
64         changed();
65     }
66     
67     /**
68      * Obtain a dynamic attribute from the repository
69      * @param name the attribute name
70      * @return the attribute value of null of non existent
71      */

72     public Object JavaDoc getExtraAttribute(String JavaDoc name) {
73         if (dynamicAttributes == null) {
74             return null;
75         }
76         return dynamicAttributes.get(name);
77     }
78     
79     /**
80      * Removes a dynamic attribute from the repository
81      * @param name the attribute name
82      */

83     public void removeExtraAttribute(String JavaDoc name) {
84         if (dynamicAttributes == null) {
85             return;
86         }
87         dynamicAttributes.remove(name);
88         changed();
89     }
90     
91     /**
92      * @return a meaningfull string about ourself
93      * This method is invoked by toString() method. This method can be overwritten by any descriptor which inherits this class
94      * to self describe. This pattern is adopted to imporve the performance of S1AS 8.0 which avoids creation of several String objects
95      * in toString() method.
96      * When toString() method is called on a Descriptor object, the toString method of this class is called.
97      * The toString method of this class invokes print(StringBuffer) method. If the Descriptor object overrides print method, its method
98      * will be invoked.
99      * For better performance, care should be taken to use print method on all descriptors instead of printing object itself (which calls to toString).
100      * For example
101      * Iterator itr = getDeploymentExtensions();
102             if (itr!=null && itr.hasNext()) {
103                do {
104                    sb.append("\n Deployment Extension : ").append(itr.next());
105                } while (itr.hasNext());
106            }
107
108
109             should probably read as below.
110
111            Iterator itr = getDeploymentExtensions();
112            if (itr!=null && itr.hasNext()) {
113                do {
114                    sb.append("\n Deployment Extension : ");
115             ((Descriptor) itr.next()).print(sb);
116                } while (itr.hasNext());
117            }
118      */

119     public void print(StringBuffer JavaDoc toStringBuffer) {
120         if (dynamicAttributes==null) {
121             toStringBuffer.append("<== No attribute ==>");
122         } else {
123            toStringBuffer.append("==>Dynamic Attribute");
124            Set keys = dynamicAttributes.keySet();
125            for (Iterator itr = keys.iterator();itr.hasNext();) {
126                String JavaDoc keyName = (String JavaDoc) itr.next();
127                Object JavaDoc o = getExtraAttribute(keyName);
128            if (o instanceof Object JavaDoc[]) {
129            Object JavaDoc[] objects = (Object JavaDoc[]) o;
130            for (int i=0;i<objects.length;i++) {
131                toStringBuffer.append("\n Indexed prop name ").append(keyName).append("[").append(i).append("] = ");
132                        if(objects[i] instanceof DynamicAttributesDescriptor)
133                             ((DynamicAttributesDescriptor)objects[i]).print(toStringBuffer);
134                        else
135                             toStringBuffer.append(objects[i]);
136            }
137            } else {
138            toStringBuffer.append("\n Property name = ").append(keyName).append(" value = ");
139                    if(o instanceof DynamicAttributesDescriptor)
140                             ((DynamicAttributesDescriptor)o).print(toStringBuffer);
141                        else
142                             toStringBuffer.append(o);
143                }
144            }
145        toStringBuffer.append("\n<==End");
146            return ;
147         }
148     }
149     /**
150      * @return a meaningfull string about ourself
151      * No Descriptor class which inherits this class should override this method. Rather print() method which is defined in this class
152      * should be overridden to describe itself. Refer to the comments on print() method for more details.
153      * This method is optimized for persformance reasons.
154      */

155     public String JavaDoc toString() {
156         StringBuffer JavaDoc toStringBuf = new StringBuffer JavaDoc();
157         this.print(toStringBuf);
158         return toStringBuf.toString();
159     }
160     
161     /**
162      * notify our observers we have changed
163      */

164     private void changed() {
165         setChanged();
166         notifyObservers();
167     }
168 }
Popular Tags