KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > storeconfig > StoreRegistry


1 /*
2  * Copyright 1999-2001,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.catalina.storeconfig;
18
19 import java.util.HashMap JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import javax.naming.directory.DirContext JavaDoc;
23
24 import org.apache.catalina.LifecycleListener;
25 import org.apache.catalina.Manager;
26 import org.apache.catalina.Realm;
27 import org.apache.catalina.Valve;
28 import org.apache.catalina.cluster.CatalinaCluster;
29 import org.apache.catalina.cluster.ClusterDeployer;
30 import org.apache.catalina.cluster.ClusterReceiver;
31 import org.apache.catalina.cluster.ClusterSender;
32 import org.apache.catalina.cluster.MembershipService;
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35
36 /**
37  * Central StoreRegistry for all server.xml elements
38  *
39  * @author Peter Rossbach
40  *
41  */

42 public class StoreRegistry {
43     private static Log log = LogFactory.getLog(StoreRegistry.class);
44
45     private Map JavaDoc descriptors = new HashMap JavaDoc();
46
47     private String JavaDoc encoding = "UTF-8";
48
49     private String JavaDoc name;
50
51     private String JavaDoc version;
52
53     // Access Information
54
private static Class JavaDoc interfaces[] = { CatalinaCluster.class,
55             ClusterSender.class, ClusterReceiver.class,
56             MembershipService.class, ClusterDeployer.class, Realm.class,
57             Manager.class, DirContext JavaDoc.class, LifecycleListener.class,
58             Valve.class };
59
60     /**
61      * @return Returns the name.
62      */

63     public String JavaDoc getName() {
64         return name;
65     }
66
67     /**
68      * @param name
69      * The name to set.
70      */

71     public void setName(String JavaDoc name) {
72         this.name = name;
73     }
74
75     /**
76      * @return Returns the version.
77      */

78     public String JavaDoc getVersion() {
79         return version;
80     }
81
82     /**
83      * @param version
84      * The version to set.
85      */

86     public void setVersion(String JavaDoc version) {
87         this.version = version;
88     }
89
90     /**
91      * Find a description for id. Handle interface search when no direct match
92      * found.
93      *
94      * @param id
95      * @return
96      */

97     public StoreDescription findDescription(String JavaDoc id) {
98         if (log.isDebugEnabled())
99             log.debug("search descriptor " + id);
100         StoreDescription desc = (StoreDescription) descriptors.get(id);
101         if (desc == null) {
102             Class JavaDoc aClass = null;
103             try {
104                 aClass = Class.forName(id, true, this.getClass()
105                         .getClassLoader());
106             } catch (ClassNotFoundException JavaDoc e) {
107                 log.error("ClassName:" + id, e);
108             }
109             if (aClass != null) {
110                 desc = (StoreDescription) descriptors.get(aClass.getName());
111                 for (int i = 0; desc == null && i < interfaces.length; i++) {
112                     if (interfaces[i].isAssignableFrom(aClass)) {
113                         desc = (StoreDescription) descriptors.get(interfaces[i]
114                                 .getName());
115                     }
116                 }
117             }
118         }
119         if (log.isDebugEnabled())
120             if (desc != null)
121                 log.debug("find descriptor " + id + "#" + desc.getTag() + "#"
122                         + desc.getStoreFactoryClass());
123             else
124                 log.debug(("Can't find descriptor for key " + id));
125         return desc;
126     }
127
128     /**
129      * Find Description by class
130      *
131      * @param aClass
132      * @return
133      */

134     public StoreDescription findDescription(Class JavaDoc aClass) {
135         return findDescription(aClass.getName());
136     }
137
138     /**
139      * Find factory from classname
140      *
141      * @param aClassName
142      * @return
143      */

144     public IStoreFactory findStoreFactory(String JavaDoc aClassName) {
145         StoreDescription desc = findDescription(aClassName);
146         if (desc != null)
147             return desc.getStoreFactory();
148         else
149             return null;
150
151     }
152
153     /**
154      * find factory from class
155      *
156      * @param aClass
157      * @return
158      */

159     public IStoreFactory findStoreFactory(Class JavaDoc aClass) {
160         return findStoreFactory(aClass.getName());
161     }
162
163     /**
164      * Register a new description
165      *
166      * @param desc
167      */

168     public void registerDescription(StoreDescription desc) {
169         String JavaDoc key = desc.getId();
170         if (key == null || "".equals(key))
171             key = desc.getTagClass();
172         descriptors.put(key, desc);
173         if (log.isDebugEnabled())
174             log.debug("register store descriptor " + key + "#" + desc.getTag()
175                     + "#" + desc.getTagClass());
176     }
177
178     public StoreDescription unregisterDescription(StoreDescription desc) {
179         String JavaDoc key = desc.getId();
180         if (key == null || "".equals(key))
181             key = desc.getTagClass();
182         return (StoreDescription) descriptors.remove(key);
183     }
184
185     // Attributes
186

187     /**
188      * @return
189      */

190     public String JavaDoc getEncoding() {
191         return encoding;
192     }
193
194     /**
195      * @param string
196      */

197     public void setEncoding(String JavaDoc string) {
198         encoding = string;
199     }
200
201 }
Popular Tags