001: package com.ibm.webdav.impl;
002:
003: /*
004: * (C) Copyright IBM Corp. 2000 All rights reserved.
005: *
006: * The program is provided "AS IS" without any warranty express or
007: * implied, including the warranty of non-infringement and the implied
008: * warranties of merchantibility and fitness for a particular purpose.
009: * IBM will not be liable for any damages suffered by you as a result
010: * of using the Program. In no event will IBM be liable for any
011: * special, indirect or consequential damages or lost profits even if
012: * IBM has been advised of the possibility of their occurrence. IBM
013: * will not be liable for any third party claims against you.
014: *
015: * Portions Copyright (C) Simulacra Media Ltd, 2004.
016: */
017: import java.util.*;
018:
019: import com.ibm.webdav.*;
020:
021: /** ResourceImplFactory creates implementations of the namespace,
022: * properties, and lock manager for a resource based in its URL.
023: * This allows methods to be dispatch to different underlying repository
024: * managers based on the resource it is operating on.
025: * @author Jim Amsden <jamsden@us.ibm.com>
026: * @see ResourceImpl
027: * @see CollectionImpl
028: * @see NamespaceManager
029: * @see PropertiesManager
030: * @see LockManager
031: */
032: public class ResourceImplFactory extends Object {
033: /*private static String defaultRepository = "fileSystem";
034: private static String lockmanagerClass = "com.ibm.webdav.fileSystem.LockManager";
035: private static String namespacemanagerClass = "com.ibm.webdav.fileSystem.NamespaceManager";
036: private static String propertiesmanagerClass = "com.ibm.webdav.fileSystem.PropertiesManager";*/
037: private static String defaultRepository = "harmonise";
038: private static String searchmanagerClass = "org.openharmonise.dav.server.managers.HarmoniseSearchManager";
039: private static String lockmanagerClass = "org.openharmonise.dav.server.managers.HarmoniseLockManager";
040: private static String namespacemanagerClass = "org.openharmonise.dav.server.managers.VersionedNamespaceManager";
041: private static String propertiesmanagerClass = "org.openharmonise.dav.server.managers.VersionedPropertiesManager";
042: private static String authenticatorClass = "org.openharmonise.dav.server.managers.HarmoniseSessionManager";
043: private static Hashtable namespaceManagers = new Hashtable();
044: private static Hashtable propertiesManagers = new Hashtable();
045: private static Hashtable lockManagers = new Hashtable();
046: private static Hashtable searchManagers = new Hashtable();
047: private static Hashtable authenticators = new Hashtable();
048:
049: /** Create the lock manager for a resource that is specific to a particular
050: * repository manager based on the URL of the resource.
051: * @param resource the client resource
052: * @param namespaceManager its namespace manager
053: * @param propertiesManager its properties manager
054: * @return a LockManager for this resource
055: * @exception com.ibm.webdav.WebDAVException
056: */
057: public static LockManager createLockManager(ResourceImpl resource,
058: NamespaceManager namespaceManager,
059: PropertiesManager propertiesManager) throws WebDAVException {
060: String repositoryManager = getRepositoryManagerFor(resource);
061: boolean usingDefault = false;
062:
063: if (repositoryManager == null) {
064: repositoryManager = defaultRepository;
065:
066: usingDefault = true;
067: }
068:
069: Class theClass = (Class) lockManagers.get(repositoryManager);
070:
071: if (theClass == null) {
072: String classname = ResourceImpl.webdavProperties
073: .getProperty(repositoryManager + ".LockManager");
074:
075: if (classname == null) {
076: if (!usingDefault) {
077: System.err
078: .println("No LockManager for Repository Manager: "
079: + repositoryManager);
080: System.err
081: .println("Using the fileSystem by default");
082: }
083:
084: classname = lockmanagerClass;
085: }
086:
087: try {
088: theClass = Class.forName(classname);
089: } catch (Exception exc) {
090: System.err.println("Cannot create lock manager: "
091: + classname);
092: System.err.println(exc);
093: }
094:
095: lockManagers.put(repositoryManager, theClass);
096: }
097:
098: LockManager lockManager = null;
099:
100: try {
101: lockManager = (LockManager) theClass.newInstance();
102: lockManager.initialize(resource, namespaceManager,
103: propertiesManager);
104: } catch (Exception exc) {
105: System.err.println("Cannot create lock manager: "
106: + theClass.getName());
107: System.err.println(exc);
108: }
109:
110: return lockManager;
111: }
112:
113: /** Create the namespace manager for a resource specific to a particular
114: * repository manager, based on the resource URL.
115: * @param resource the client resource
116: * @return a NamespaceManager for this resource
117: * @exception com.ibm.webdav.WebDAVException
118: */
119: public static NamespaceManager createNamespaceManager(
120: ResourceImpl resource) throws WebDAVException {
121: String repositoryManager = getRepositoryManagerFor(resource);
122: boolean usingDefault = false;
123:
124: if (repositoryManager == null) {
125: repositoryManager = defaultRepository;
126: usingDefault = true;
127: }
128:
129: Class theClass = (Class) namespaceManagers
130: .get(repositoryManager);
131:
132: if (theClass == null) {
133: String classname = ResourceImpl.webdavProperties
134: .getProperty(repositoryManager
135: + ".NamespaceManager");
136:
137: if (classname == null) {
138: if (!usingDefault) {
139: System.err
140: .println("No NamespaceManager for Repository Manager: "
141: + repositoryManager);
142: System.err
143: .println("Using the fileSystem by default");
144: }
145:
146: classname = namespacemanagerClass;
147: }
148:
149: try {
150: theClass = Class.forName(classname);
151: } catch (Exception exc) {
152: System.err.println("Cannot create namespace manager: "
153: + classname);
154: System.err.println(exc);
155: }
156:
157: namespaceManagers.put(repositoryManager, theClass);
158: }
159:
160: NamespaceManager namespaceManager = null;
161:
162: try {
163: namespaceManager = (NamespaceManager) theClass
164: .newInstance();
165: namespaceManager.initialize(resource);
166: } catch (Exception exc) {
167: System.err.println("Cannot create namespace manager: "
168: + theClass.getName());
169: System.err.println(exc);
170: }
171:
172: return namespaceManager;
173: }
174:
175: /** Create the search manager for a resource specific to a particular
176: * repository manager, based on the resource URL.
177: * @param resource the client resource
178: * @return a SearchManager for this resource
179: * @exception com.ibm.webdav.WebDAVException
180: */
181: public static SearchManager createSearchManager(
182: ResourceImpl resource) throws WebDAVException {
183: String repositoryManager = getRepositoryManagerFor(resource);
184: boolean usingDefault = false;
185:
186: if (repositoryManager == null) {
187: repositoryManager = defaultRepository;
188: usingDefault = true;
189: }
190:
191: Class theClass = (Class) searchManagers.get(repositoryManager);
192:
193: if (theClass == null) {
194: String classname = ResourceImpl.webdavProperties
195: .getProperty(repositoryManager + ".SearchManager");
196:
197: if (classname == null) {
198: if (!usingDefault) {
199: System.err
200: .println("No SearchManager for Repository Manager: "
201: + repositoryManager);
202: }
203:
204: classname = searchmanagerClass;
205: }
206:
207: try {
208: theClass = Class.forName(classname);
209: } catch (Exception exc) {
210: System.err.println("Cannot create search manager: "
211: + classname);
212: System.err.println(exc);
213: }
214:
215: searchManagers.put(repositoryManager, theClass);
216: }
217:
218: SearchManager searchManager = null;
219:
220: try {
221: searchManager = (SearchManager) theClass.newInstance();
222: searchManager.initialize();
223: } catch (Exception exc) {
224: System.err.println("Cannot create namespace manager: "
225: + theClass.getName());
226: System.err.println(exc);
227: }
228:
229: return searchManager;
230: }
231:
232: /** Create the properties manager for a resource specific to a particular
233: * repository manager, based on the resource URL.
234: * @param resource the client resource
235: * @param namespaceManager its namespace manager
236: * @return a PropertiesManager for this resource
237: * @exception com.ibm.webdav.WebDAVException
238: */
239: public static PropertiesManager createPropertiesManager(
240: ResourceImpl resource, NamespaceManager namespaceManager)
241: throws WebDAVException {
242: String repositoryManager = getRepositoryManagerFor(resource);
243: boolean usingDefault = false;
244:
245: if (repositoryManager == null) {
246: repositoryManager = defaultRepository;
247: usingDefault = true;
248: }
249:
250: Class theClass = (Class) propertiesManagers
251: .get(repositoryManager);
252:
253: if (theClass == null) {
254: String classname = ResourceImpl.webdavProperties
255: .getProperty(repositoryManager
256: + ".PropertiesManager");
257:
258: if (classname == null) {
259: if (!usingDefault) {
260: System.err
261: .println("No PropertiesManager for Repository Manager: "
262: + repositoryManager);
263: System.err
264: .println("Using the fileSystem by default");
265: }
266:
267: classname = propertiesmanagerClass;
268: }
269:
270: try {
271: theClass = Class.forName(classname);
272: } catch (Exception exc) {
273: System.err.println("Cannot create properties manager: "
274: + classname);
275: System.err.println(exc);
276: }
277:
278: propertiesManagers.put(repositoryManager, theClass);
279: }
280:
281: PropertiesManager propertiesManager = null;
282:
283: try {
284: propertiesManager = (PropertiesManager) theClass
285: .newInstance();
286: propertiesManager.initialize(resource, namespaceManager);
287: } catch (Exception exc) {
288: System.err.println("Cannot create properties manager: "
289: + theClass.getName());
290: System.err.println(exc);
291: }
292:
293: return propertiesManager;
294: }
295:
296: public static UserAuthenticator getAuthenticator(
297: ResourceImpl resource) throws WebDAVException {
298: String repositoryManager = getRepositoryManagerFor(resource);
299: boolean usingDefault = false;
300:
301: if (repositoryManager == null) {
302: repositoryManager = defaultRepository;
303: usingDefault = true;
304: }
305:
306: Class theClass = (Class) authenticators.get(repositoryManager);
307:
308: if (theClass == null) {
309: String classname = ResourceImpl.webdavProperties
310: .getProperty(repositoryManager + ".Authenticator");
311:
312: if (classname == null) {
313: if (!usingDefault) {
314: System.err
315: .println("No Authenticator for Repository Manager: "
316: + repositoryManager);
317: }
318:
319: classname = authenticatorClass;
320: }
321:
322: try {
323: theClass = Class.forName(classname);
324: } catch (Exception exc) {
325: System.err.println("Cannot create authenticator: "
326: + classname);
327: System.err.println(exc);
328: }
329:
330: authenticators.put(repositoryManager, theClass);
331: }
332:
333: UserAuthenticator authenticator = null;
334:
335: try {
336: authenticator = (UserAuthenticator) theClass.newInstance();
337: } catch (Exception exc) {
338: System.err.println("Cannot create authenticator: "
339: + theClass.getName());
340: System.err.println(exc);
341: }
342:
343: return authenticator;
344: }
345:
346: /** Searches the dav4j.properties file for the longest property name
347: * matching a prefix of the file part of the resource URL.
348: *
349: * @parm resource the resource to find a repository manager for
350: * @return the value of the located prefix, the name of a repository
351: * manager to use for this resource, or null if no matching prefix
352: * was found.
353: * @exception com.ibm.webdav.WebDAVException thrown if the resource is not accessible
354: */
355: private static String getRepositoryManagerFor(ResourceImpl resource)
356: throws WebDAVException {
357: String file = null;
358: file = resource.getURL().getFile();
359:
360: String matchingPrefix = new String();
361:
362: // find the longest property name that matches a prefix of the file part of the url
363: Enumeration propertyNames = ResourceImpl.webdavProperties
364: .propertyNames();
365:
366: while (propertyNames.hasMoreElements()) {
367: String propertyName = (String) propertyNames.nextElement();
368:
369: if (file.startsWith(propertyName)
370: && (propertyName.length() > matchingPrefix.length())) {
371: matchingPrefix = propertyName;
372: }
373: }
374:
375: String repositoryManager = ResourceImpl.webdavProperties
376: .getProperty(matchingPrefix);
377:
378: return repositoryManager;
379: }
380: }
|