KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > browser > core > common > DefaultPropertyContainer


1 /*===========================================================================
2
3 ObjectWeb Naming Context Framework
4 Copyright (C) 2002 USTL - LIFL - GOAL
5 Contact: architecture@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Jerome Moroy.
23 Contributor(s): ______________________________________.
24
25 ===========================================================================*/

26
27 package org.objectweb.util.browser.core.common;
28
29 /** The concole's imports */
30 import org.objectweb.util.browser.api.Entry;
31 import org.objectweb.util.browser.core.api.BrowserProperty;
32 import org.objectweb.util.browser.core.api.PropertyContainer;
33 import org.objectweb.util.browser.core.api.Role;
34 import org.objectweb.util.browser.core.api.RoleManagement;
35 import org.objectweb.util.browser.core.api.TypeKey;
36 import org.objectweb.util.browser.core.naming.DefaultContextContainer;
37
38 /** The java AMI's imports */
39 import java.util.List JavaDoc;
40 import java.util.Iterator JavaDoc;
41 import java.util.Map JavaDoc;
42 import java.util.HashMap JavaDoc;
43
44 /**
45  * Basic implementation of the Context interface.
46  *
47  * @author <a HREF="mailto:Jerome.Moroy@lifl.fr">Jérôme Moroy</a>
48  * @version 0.1
49  */

50 public abstract class DefaultPropertyContainer
51     extends DefaultContextContainer
52     implements PropertyContainer {
53
54     // ==================================================================
55
//
56
// Internal state.
57
//
58
// ==================================================================
59

60     /**
61      * The object to use to load new configuration.
62      */

63     protected BrowserProperty loader_;
64
65     /**
66      * This map stores the corresponding class.
67      * Exemple : java.util.ArrayList => java.util.AbstractCollection
68      * Concequently, for java.util.ArrayList object, it directly
69      * look for java.util.AbstractCollection element.
70      */

71     protected Map JavaDoc cache_;
72
73     // ==================================================================
74
//
75
// Constructors.
76
//
77
// ==================================================================
78

79     /**
80      * Default contstructor
81      * @param loader The object to use to load new configuration
82      */

83     public DefaultPropertyContainer(BrowserProperty loader) {
84         super();
85         loader_ = loader;
86         cache_ = new HashMap JavaDoc();
87     }
88
89     // ==================================================================
90
//
91
// Internal methods.
92
//
93
// ==================================================================
94

95     /**
96      * Tries to load an element for the associated class name.
97      * It calls the loader and adds the given entry into the context
98      * @param className The name of the class
99      */

100     protected Entry tryToLoad(String JavaDoc className) {
101         Role[] roles = loader_.getInheritedRoles();
102         // Those variables allow the user to overwrite a node definition.
103
ExtendedBoolean nodeFound = new ExtendedBoolean(false);
104         boolean atLeastOneNodeFound = false;
105         for(int i = 0 ; i < roles.length ; i++){
106             Entry entry = getElement(className, roles[i], nodeFound);
107             if (entry != null) {
108                 if(!(roles[i].getId().equals(RoleManagement.DEFAULT_ROLE) && atLeastOneNodeFound)){
109                     addEntry(entry.getName().toString(), entry.getValue());
110                     return entry;
111                 }
112                 if(nodeFound.getValue())
113                     atLeastOneNodeFound = true;
114             }
115         }
116         return null;
117     }
118
119     /**
120      * Calculates a key depending on the type system
121      * @return The calculated key.
122      */

123     protected String JavaDoc getKey(Class JavaDoc c) {
124         String JavaDoc key = "";
125         if(c!=null){
126             TypeKey typeKey = loader_.getTypeSystem().getTypeKey(c);
127             key = typeKey.getTypeSystem() + "." + typeKey.getTypeName();
128         }
129         return key;
130     }
131
132     /**
133      * Tries to load an element for the associated class name.
134      * @param className The name of the class
135      * @return The founded entry or null
136      */

137     protected abstract Entry getElement(String JavaDoc className, Role role, ExtendedBoolean nodeFound);
138
139     // ==================================================================
140
//
141
// Additional public methods for PropertyTools interface.
142
//
143
// ==================================================================
144

145     /**
146      * Finds the object associated value.
147      * If the key doesn't exist, it looks for in interfaces implemented
148      * by this class and in superclass of the entity
149      * In order to have a string representation of the object,
150      * it takes his name (object.getClass().getName())
151      * @param object The research key
152      * @return The associated value - null is returned if no value exists
153      */

154     public Entry getRecursiveProperty(Object JavaDoc object) {
155         String JavaDoc cKey = getKey(object.getClass());
156         // Look for existing association into the cache
157
if (cache_.containsKey(cKey)) {
158             return getLocalEntry((String JavaDoc) cache_.get(cKey));
159         }
160         ClassesInheritance ci = new ClassesInheritance(object.getClass());
161         List JavaDoc list = ci.getInheritClasses();
162         Iterator JavaDoc it = list.iterator();
163         while (it.hasNext()) {
164             String JavaDoc key = getKey((Class JavaDoc) it.next());;
165             Entry entry = getLocalEntry(key);
166             if (entry == null) {
167                 entry = tryToLoad(key);
168             }
169             if (entry != null) {
170                 cache_.put(cKey, key);
171                 return entry;
172             }
173         }
174         return null;
175     }
176     
177     /**
178      * Removes all of the elements from this context and clears the cache.
179      */

180     public void clear(){
181         super.clear();
182         cache_.clear();
183     }
184 }
185
Popular Tags