KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibm > webdav > impl > ResourceImplFactory


1 package com.ibm.webdav.impl;
2
3 /*
4  * (C) Copyright IBM Corp. 2000 All rights reserved.
5  *
6  * The program is provided "AS IS" without any warranty express or
7  * implied, including the warranty of non-infringement and the implied
8  * warranties of merchantibility and fitness for a particular purpose.
9  * IBM will not be liable for any damages suffered by you as a result
10  * of using the Program. In no event will IBM be liable for any
11  * special, indirect or consequential damages or lost profits even if
12  * IBM has been advised of the possibility of their occurrence. IBM
13  * will not be liable for any third party claims against you.
14  *
15  * Portions Copyright (C) Simulacra Media Ltd, 2004.
16  */

17 import java.util.*;
18
19 import com.ibm.webdav.*;
20
21
22 /** ResourceImplFactory creates implementations of the namespace,
23  * properties, and lock manager for a resource based in its URL.
24  * This allows methods to be dispatch to different underlying repository
25  * managers based on the resource it is operating on.
26  * @author Jim Amsden <jamsden@us.ibm.com>
27  * @see ResourceImpl
28  * @see CollectionImpl
29  * @see NamespaceManager
30  * @see PropertiesManager
31  * @see LockManager
32  */

33 public class ResourceImplFactory extends Object JavaDoc {
34     /*private static String defaultRepository = "fileSystem";
35     private static String lockmanagerClass = "com.ibm.webdav.fileSystem.LockManager";
36     private static String namespacemanagerClass = "com.ibm.webdav.fileSystem.NamespaceManager";
37     private static String propertiesmanagerClass = "com.ibm.webdav.fileSystem.PropertiesManager";*/

38     private static String JavaDoc defaultRepository = "harmonise";
39     private static String JavaDoc searchmanagerClass = "org.openharmonise.dav.server.managers.HarmoniseSearchManager";
40     private static String JavaDoc lockmanagerClass = "org.openharmonise.dav.server.managers.HarmoniseLockManager";
41     private static String JavaDoc namespacemanagerClass = "org.openharmonise.dav.server.managers.VersionedNamespaceManager";
42     private static String JavaDoc propertiesmanagerClass = "org.openharmonise.dav.server.managers.VersionedPropertiesManager";
43     private static String JavaDoc authenticatorClass = "org.openharmonise.dav.server.managers.HarmoniseSessionManager";
44     private static Hashtable namespaceManagers = new Hashtable();
45     private static Hashtable propertiesManagers = new Hashtable();
46     private static Hashtable lockManagers = new Hashtable();
47     private static Hashtable searchManagers = new Hashtable();
48     private static Hashtable authenticators = new Hashtable();
49     
50     /** Create the lock manager for a resource that is specific to a particular
51     * repository manager based on the URL of the resource.
52     * @param resource the client resource
53     * @param namespaceManager its namespace manager
54     * @param propertiesManager its properties manager
55     * @return a LockManager for this resource
56     * @exception com.ibm.webdav.WebDAVException
57     */

58     public static LockManager createLockManager(ResourceImpl resource,
59                                                 NamespaceManager namespaceManager,
60                                                 PropertiesManager propertiesManager)
61                                          throws WebDAVException {
62         String JavaDoc repositoryManager = getRepositoryManagerFor(resource);
63         boolean usingDefault = false;
64
65         if (repositoryManager == null) {
66             repositoryManager = defaultRepository;
67
68             usingDefault = true;
69         }
70
71         Class JavaDoc theClass = (Class JavaDoc) lockManagers.get(repositoryManager);
72
73         if (theClass == null) {
74             String JavaDoc classname = ResourceImpl.webdavProperties.getProperty(repositoryManager +
75                                                                          ".LockManager");
76
77             if (classname == null) {
78                 if (!usingDefault) {
79                     System.err.println(
80                             "No LockManager for Repository Manager: " +
81                             repositoryManager);
82                     System.err.println("Using the fileSystem by default");
83                 }
84
85                 classname = lockmanagerClass;
86             }
87
88             try {
89                 theClass = Class.forName(classname);
90             } catch (Exception JavaDoc exc) {
91                 System.err.println("Cannot create lock manager: " +
92                                    classname);
93                 System.err.println(exc);
94             }
95
96             lockManagers.put(repositoryManager, theClass);
97         }
98
99         LockManager lockManager = null;
100
101         try {
102             lockManager = (LockManager) theClass.newInstance();
103             lockManager.initialize(resource, namespaceManager,
104                                    propertiesManager);
105         } catch (Exception JavaDoc exc) {
106             System.err.println("Cannot create lock manager: " +
107                                theClass.getName());
108             System.err.println(exc);
109         }
110
111         return lockManager;
112     }
113
114     /** Create the namespace manager for a resource specific to a particular
115     * repository manager, based on the resource URL.
116     * @param resource the client resource
117     * @return a NamespaceManager for this resource
118     * @exception com.ibm.webdav.WebDAVException
119     */

120     public static NamespaceManager createNamespaceManager(ResourceImpl resource)
121                                                    throws WebDAVException {
122         String JavaDoc repositoryManager = getRepositoryManagerFor(resource);
123         boolean usingDefault = false;
124
125         if (repositoryManager == null) {
126             repositoryManager = defaultRepository;
127             usingDefault = true;
128         }
129
130         Class JavaDoc theClass = (Class JavaDoc) namespaceManagers.get(repositoryManager);
131
132         if (theClass == null) {
133             String JavaDoc classname = ResourceImpl.webdavProperties.getProperty(repositoryManager +
134                                                                          ".NamespaceManager");
135
136             if (classname == null) {
137                 if (!usingDefault) {
138                     System.err.println(
139                             "No NamespaceManager for Repository Manager: " +
140                             repositoryManager);
141                     System.err.println("Using the fileSystem by default");
142                 }
143
144                 classname = namespacemanagerClass;
145             }
146
147             try {
148                 theClass = Class.forName(classname);
149             } catch (Exception JavaDoc exc) {
150                 System.err.println("Cannot create namespace manager: " +
151                                    classname);
152                 System.err.println(exc);
153             }
154
155             namespaceManagers.put(repositoryManager, theClass);
156         }
157
158         NamespaceManager namespaceManager = null;
159
160         try {
161             namespaceManager = (NamespaceManager) theClass.newInstance();
162             namespaceManager.initialize(resource);
163         } catch (Exception JavaDoc exc) {
164             System.err.println("Cannot create namespace manager: " +
165                                theClass.getName());
166             System.err.println(exc);
167         }
168
169         return namespaceManager;
170     }
171
172     /** Create the search manager for a resource specific to a particular
173     * repository manager, based on the resource URL.
174     * @param resource the client resource
175     * @return a SearchManager for this resource
176     * @exception com.ibm.webdav.WebDAVException
177     */

178     public static SearchManager createSearchManager(ResourceImpl resource)
179                                              throws WebDAVException {
180         String JavaDoc repositoryManager = getRepositoryManagerFor(resource);
181         boolean usingDefault = false;
182
183         if (repositoryManager == null) {
184             repositoryManager = defaultRepository;
185             usingDefault = true;
186         }
187
188         Class JavaDoc theClass = (Class JavaDoc) searchManagers.get(repositoryManager);
189
190         if (theClass == null) {
191             String JavaDoc classname = ResourceImpl.webdavProperties.getProperty(repositoryManager +
192                                                                          ".SearchManager");
193
194             if (classname == null) {
195                 if (!usingDefault) {
196                     System.err.println(
197                             "No SearchManager for Repository Manager: " +
198                             repositoryManager);
199                 }
200
201                 classname = searchmanagerClass;
202             }
203
204             try {
205                 theClass = Class.forName(classname);
206             } catch (Exception JavaDoc exc) {
207                 System.err.println("Cannot create search manager: " +
208                                    classname);
209                 System.err.println(exc);
210             }
211
212             searchManagers.put(repositoryManager, theClass);
213         }
214
215         SearchManager searchManager = null;
216
217         try {
218             searchManager = (SearchManager) theClass.newInstance();
219             searchManager.initialize();
220         } catch (Exception JavaDoc exc) {
221             System.err.println("Cannot create namespace manager: " +
222                                theClass.getName());
223             System.err.println(exc);
224         }
225
226         return searchManager;
227     }
228
229     /** Create the properties manager for a resource specific to a particular
230     * repository manager, based on the resource URL.
231     * @param resource the client resource
232     * @param namespaceManager its namespace manager
233     * @return a PropertiesManager for this resource
234     * @exception com.ibm.webdav.WebDAVException
235     */

236     public static PropertiesManager createPropertiesManager(ResourceImpl resource,
237                                                             NamespaceManager namespaceManager)
238                                                      throws WebDAVException {
239         String JavaDoc repositoryManager = getRepositoryManagerFor(resource);
240         boolean usingDefault = false;
241
242         if (repositoryManager == null) {
243             repositoryManager = defaultRepository;
244             usingDefault = true;
245         }
246
247         Class JavaDoc theClass = (Class JavaDoc) propertiesManagers.get(repositoryManager);
248
249         if (theClass == null) {
250             String JavaDoc classname = ResourceImpl.webdavProperties.getProperty(repositoryManager +
251                                                                          ".PropertiesManager");
252
253             if (classname == null) {
254                 if (!usingDefault) {
255                     System.err.println(
256                             "No PropertiesManager for Repository Manager: " +
257                             repositoryManager);
258                     System.err.println("Using the fileSystem by default");
259                 }
260
261                 classname = propertiesmanagerClass;
262             }
263
264             try {
265                 theClass = Class.forName(classname);
266             } catch (Exception JavaDoc exc) {
267                 System.err.println("Cannot create properties manager: " +
268                                    classname);
269                 System.err.println(exc);
270             }
271
272             propertiesManagers.put(repositoryManager, theClass);
273         }
274
275         PropertiesManager propertiesManager = null;
276
277         try {
278             propertiesManager = (PropertiesManager) theClass.newInstance();
279             propertiesManager.initialize(resource, namespaceManager);
280         } catch (Exception JavaDoc exc) {
281             System.err.println("Cannot create properties manager: " +
282                                theClass.getName());
283             System.err.println(exc);
284         }
285
286         return propertiesManager;
287     }
288
289     public static UserAuthenticator getAuthenticator(ResourceImpl resource)
290                                               throws WebDAVException {
291         String JavaDoc repositoryManager = getRepositoryManagerFor(resource);
292         boolean usingDefault = false;
293
294         if (repositoryManager == null) {
295             repositoryManager = defaultRepository;
296             usingDefault = true;
297         }
298
299         Class JavaDoc theClass = (Class JavaDoc) authenticators.get(repositoryManager);
300
301         if (theClass == null) {
302             String JavaDoc classname = ResourceImpl.webdavProperties.getProperty(repositoryManager +
303                                                                          ".Authenticator");
304
305             if (classname == null) {
306                 if (!usingDefault) {
307                     System.err.println(
308                             "No Authenticator for Repository Manager: " +
309                             repositoryManager);
310                 }
311
312                 classname = authenticatorClass;
313             }
314
315             try {
316                 theClass = Class.forName(classname);
317             } catch (Exception JavaDoc exc) {
318                 System.err.println("Cannot create authenticator: " +
319                                    classname);
320                 System.err.println(exc);
321             }
322
323             authenticators.put(repositoryManager, theClass);
324         }
325
326         UserAuthenticator authenticator = null;
327
328         try {
329             authenticator = (UserAuthenticator) theClass.newInstance();
330         } catch (Exception JavaDoc exc) {
331             System.err.println("Cannot create authenticator: " +
332                                theClass.getName());
333             System.err.println(exc);
334         }
335
336         return authenticator;
337     }
338
339     /** Searches the dav4j.properties file for the longest property name
340     * matching a prefix of the file part of the resource URL.
341     *
342     * @parm resource the resource to find a repository manager for
343     * @return the value of the located prefix, the name of a repository
344     * manager to use for this resource, or null if no matching prefix
345     * was found.
346     * @exception com.ibm.webdav.WebDAVException thrown if the resource is not accessible
347     */

348     private static String JavaDoc getRepositoryManagerFor(ResourceImpl resource)
349                                            throws WebDAVException {
350         String JavaDoc file = null;
351         file = resource.getURL().getFile();
352
353         String JavaDoc matchingPrefix = new String JavaDoc();
354
355         // find the longest property name that matches a prefix of the file part of the url
356
Enumeration propertyNames = ResourceImpl.webdavProperties.propertyNames();
357
358         while (propertyNames.hasMoreElements()) {
359             String JavaDoc propertyName = (String JavaDoc) propertyNames.nextElement();
360
361             if (file.startsWith(propertyName) &&
362                     (propertyName.length() > matchingPrefix.length())) {
363                 matchingPrefix = propertyName;
364             }
365         }
366
367         String JavaDoc repositoryManager = ResourceImpl.webdavProperties.getProperty(
368                                            matchingPrefix);
369
370         return repositoryManager;
371     }
372 }
Popular Tags