KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibm > webdav > ResourceFactory


1 package com.ibm.webdav;
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.io.*;
18 import java.net.*;
19 import java.rmi.*;
20 import java.rmi.UnknownHostException JavaDoc;
21 import java.util.*;
22 import java.util.logging.*;
23 import java.util.logging.Logger JavaDoc;
24
25 import com.ibm.webdav.impl.*;
26 import com.ibm.webdav.protocol.http.*;
27
28 /** A ResourceFactory is used to construct Resources or their subclasses
29  * based on the desired communication protocol, http:, rmi:, or iiop: as
30  * specified in the resource URL.
31  * @author Jim Amsden <jamsden@us.ibm.com>
32  * @see com.ibm.webdav.impl.ResourceHTTPStub
33  * @see com.ibm.webdav.impl.ResourceImpl
34  */

35 public class ResourceFactory {
36     public static String JavaDoc defaultDocRoot = "c:\\www\\html\\dav";
37
38     /** All WebDAV properties from the dav4j.properties file located somewhere
39     * in the classpath. See the dav4j.properties file for details.
40     */

41     public static Properties properties = null;
42     
43     /**
44      * Logger for this class
45      */

46     private static Logger JavaDoc m_logger = Logger.getLogger(ResourceFactory.class.getName());
47
48     // load the webdav properties
49
static {
50         properties = new Properties();
51         String JavaDoc resource = "dav4j.properties";
52         
53         try {
54             // in the current build environment, dav4j.properties will most likely be in the
55
// jar file for harmonise-dav-server, so look in here first
56
ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
57             InputStream istream = loader.getResourceAsStream(resource); // will return null if not found
58
if (istream != null) {
59                 properties.load(loader.getResourceAsStream(resource));
60             }
61             else {
62                 // If that fails, find the dav4j.properties file in the classpath
63
String JavaDoc classpath = System.getProperty("java.class.path");
64                 StringTokenizer paths = new StringTokenizer(classpath, File.pathSeparator);
65                 File propertiesFile = null;
66                 boolean found = false;
67                 while (!found && paths.hasMoreTokens()) {
68                     String JavaDoc path = paths.nextToken();
69                     propertiesFile = new File(path, resource);
70                     found = propertiesFile.exists();
71                 }
72                 if (found) {
73                     try {
74                         properties.load(new FileInputStream(propertiesFile));
75                     } catch (Exception JavaDoc exc) {
76                         exc.printStackTrace();
77                     }
78                 }
79             }
80         } catch (RuntimeException JavaDoc e) {
81             m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
82         } catch (IOException e) {
83             m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
84         }
85     }
86 /** Create a Resource identified by the given URL. The URL
87 * specifies the protocol to use for remote communication, and
88 * the host name. If the host name is "localhost", the communication
89 * is local (typical fat client).
90 * <p>
91 * This method does attempt to determine
92 * if the url is for a collection or a normal resource. At the
93 * moment it does this by asking the server. If it can not
94 * determine what type the resource is by asking the server
95 * (often because the resource doesn't yet exist at the server),
96 * then an exception is thrown.
97 * <p>
98 * The caller should be aware that just because we've
99 * asked the server if this URL is a collection/resouce, that doesn't
100 * insure that it wasn't deleted a moment or replaced a moment later
101 * by something different. Locking is required to do that and this
102 * method doesn't lock.
103 *
104 * @param url the identifier of the resource
105 * @return a Resource, ResourceCollection, or one of its subclasses.
106 * @exception java.io.IOException
107 */

108 public static Resource create(String JavaDoc url) throws java.io.IOException JavaDoc {
109     return create(url, null);
110 }
111 /** Create a Resource identified by the given URL. The URL
112 * specifies the protocol to use for remote communication, and
113 * the host name. If the host name is "localhost", the communication
114 * is local (typical fat client).
115 * <p>
116 * This method does attempt to determine
117 * if the url is for a collection or a normal resource. At the
118 * moment it does this by asking the server. If it can not
119 * determine what type the resource is by asking the server
120 * (often because the resource doesn't yet exist at the server),
121 * then an exception is thrown.
122 * <p>
123 * The caller should be aware that just because we've
124 * asked the server if this URL is a collection/resouce, that doesn't
125 * insure that it wasn't deleted a moment or replaced a moment later
126 * by something different. Locking is required to do that and this
127 * method doesn't lock.
128 *
129 * @param resourceURL the identifier of the resource
130 * @return a Resource, ResourceCollection, or one of its subclasses.
131 * @exception java.io.IOException
132 */

133 public static Resource create(String JavaDoc url, TargetSelector targetSelector) throws java.io.IOException JavaDoc {
134     Resource resource = null;
135     try {
136         resource = new Resource(url, targetSelector);
137         if (resource.isCollection()) {
138             resource = null;
139             resource = new Collection(url, targetSelector);
140         }
141     } catch (WebDAVException exc) {
142         throw exc;
143     }
144     return resource;
145 }
146 /** Create a Collection identified by the given URL. The URL
147 * specifies the protocol to use for remote communication, and
148 * the host name. If the host name is "localhost", the communication
149 * is local (typical fat client).
150 *
151 * @param resourceURL the identifier of the resource
152 * @return a Collection
153 * @exception com.ibm.WebDAVException
154 */

155 public static IRCollection createCollection(URL url, TargetSelector targetSelector) throws WebDAVException {
156    IRCollection resource = null;
157    String JavaDoc protocol = null;
158    try {
159        protocol = url.getProtocol();
160
161        // if the URL host is local, and the port was not specified,
162
// do local access
163
if (isLocalHost(url)) {
164         resource = new CollectionImpl(url, getRealPath(url), targetSelector);
165     } else if (protocol.equals("http")) {
166             resource = new CollectionHTTPStub(url, targetSelector);
167        } else if (protocol.equals("rmi")) {
168         String JavaDoc name = url.toString();
169            name = name.substring(name.indexOf(":") + 1);
170            try {
171             resource = (IRCollection) Naming.lookup(name);
172         } catch (java.rmi.NotBoundException JavaDoc exc) {
173                throw new WebDAVException(WebDAVStatus.SC_NOT_FOUND, "Not bound");
174            }
175        } else {
176          throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "WebDAV: Invalid communication protocol: " + protocol);
177     }
178     } catch (WebDAVException exc) {
179         throw exc;
180    } catch (java.io.IOException JavaDoc exc) {
181       throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "WebDAV: Invalid communication protocol: " + protocol);
182    }
183     return resource;
184 }
185 /** Create a Resource identified by the given URL. The URL
186 * specifies the protocol to use for remote communication, and
187 * the host name. If the host name is "localhost", the communication
188 * is local (typical fat client).
189 *
190 * @param resourceURL the identifier of the resource
191 * @return a Resource
192 * @exception com.ibm.webdav.WebDAVException
193 */

194 public static IRResource createResource(URL url, TargetSelector targetSelector) throws WebDAVException {
195    IRResource resource = null;
196    String JavaDoc protocol = null;
197     try {
198        protocol = url.getProtocol();
199
200     // if the URL host is local, and the port was not specified,
201
// do local access
202
if (isLocalHost(url)) {
203                resource = new ResourceImpl(url, getRealPath(url), targetSelector);
204     } else if (protocol.equals("http")) {
205           //@todo this ResourceHTTPStub won't work with Tomcat - needs work
206
resource = new ResourceHTTPStub(url, targetSelector);
207        } else if (protocol.equals("rmi")) {
208             String JavaDoc name = url.toString();
209         name = name.substring(name.indexOf(":") + 1);
210         try {
211             resource = (IRResource) Naming.lookup(name);
212         } catch (java.rmi.NotBoundException JavaDoc exc) {
213             throw new WebDAVException(WebDAVStatus.SC_NOT_FOUND, "Not bound");
214            }
215     } else {
216            throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "WebDAV: Invalid communication protocol: " + protocol);
217        }
218     } catch (WebDAVException exc) {
219         throw exc;
220   } catch (java.io.IOException JavaDoc exc) {
221       throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "WebDAV: Invalid communication protocol: " + protocol);
222    }
223     return resource;
224 }
225 /** Translate a URL into a local path name using the doc.root property
226 * from the webdav properties. The doc.root property in the dav4j.properties
227 * file is used for local access without a server.
228 *
229 * @param url the URL of the resource
230 * @return the translated pathname
231 */

232
233 public static String JavaDoc getRealPath(URL url) {
234     String JavaDoc docRoot = properties.getProperty("doc.root", defaultDocRoot);
235     String JavaDoc pathName = docRoot + url.getFile().replace('/', File.separatorChar);
236     return pathName;
237 }
238 /** Does the given URL refer to the local host? Used to determine if there doesn't
239 * need to be any RPC at all.
240 * @return true if the host in the URL is the local host, and a port was not specified
241 * @exception UnknownHostException
242 */

243 public static boolean isLocalHost(URL url) throws WebDAVException {
244     /*String thisHost = null;
245
246         System.out.println("RESOURCE FACTORY: isLocalHost check for url " + url.getProtocol() + url.getHost() + url.getPort() + url.getPort() + url.getQuery());
247
248     try {
249         thisHost = InetAddress.getLocalHost().getHostName();
250
251     } catch (java.net.UnknownHostException exc) {
252         throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "Unknown Host");
253     }
254     return (url.getHost().equals("localhost") ||
255             url.getHost().equals(thisHost))*/
/*&& url.getPort() == -1*///;
256
return true;
257 }
258 }
259
Popular Tags