KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jpublish > vfs > FileProviderFactory


1 /*--
2
3  Copyright (C) 2001-2003 Aetrion LLC.
4  All rights reserved.
5  
6  Redistribution and use in source and binary forms, with or without
7  modification, are permitted provided that the following conditions
8  are met:
9  
10  1. Redistributions of source code must retain the above copyright
11     notice, this list of conditions, and the following disclaimer.
12  
13  2. Redistributions in binary form must reproduce the above copyright
14     notice, this list of conditions, and the disclaimer that follows
15     these conditions in the documentation and/or other materials
16     provided with the distribution.
17
18  3. The name "JPublish" must not be used to endorse or promote products
19     derived from this software without prior written permission. For
20     written permission, please contact info@aetrion.com.
21  
22  4. Products derived from this software may not be called "JPublish", nor
23     may "JPublish" appear in their name, without prior written permission
24     from Aetrion LLC (info@aetrion.com).
25  
26  In addition, the authors of this software request (but do not require)
27  that you include in the end-user documentation provided with the
28  redistribution and/or in the software itself an acknowledgement equivalent
29  to the following:
30      "This product includes software developed by
31       Aetrion LLC (http://www.aetrion.com/)."
32
33  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
34  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36  DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
37  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
39  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
42  IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
43  POSSIBILITY OF SUCH DAMAGE.
44
45  For more information on JPublish, please see <http://www.jpublish.org/>.
46  
47  */

48
49 package org.jpublish.vfs;
50
51 import com.anthonyeden.lib.util.ClassUtilities;
52 import org.apache.commons.logging.Log;
53 import org.apache.commons.logging.LogFactory;
54 import org.apache.commons.vfs.provider.FileProvider;
55 import org.apache.commons.vfs.provider.local.DefaultLocalFileProvider;
56 import org.apache.commons.vfs.provider.servletcontext.ServletContextFileProvider;
57 import org.apache.commons.vfs.provider.url.UrlFileProvider;
58 import org.jpublish.SiteContext;
59
60 /**
61  * Factory for constructing FileProvider implementations. This factory allows for pre-defined names to be used for
62  * known file providers as well as fully-qualified classnames for unknown providers.
63  *
64  * @author Anthony Eden
65  */

66
67 public class FileProviderFactory {
68
69     /**
70      * Indicates that the ServletContext is used.
71      */

72     public static final String JavaDoc WEBAPP = "webapp";
73
74     /**
75      * Indicates that the local file system is used.
76      */

77     public static final String JavaDoc FILE = "file";
78
79     /**
80      * Indicates that the a URL file system is used.
81      */

82     public static final String JavaDoc URL = "url";
83
84     private Log log = LogFactory.getLog(FileProviderFactory.class);
85     protected SiteContext siteContext;
86
87     /**
88      * Construct a new FileProviderFactory.
89      *
90      * @param siteContext The SiteContext
91      */

92
93     public FileProviderFactory(SiteContext siteContext) {
94         this.siteContext = siteContext;
95     }
96
97     /**
98      * Create the named provider. The name may either be one of the predefined names (webapp, local) or it may be the
99      * fully qualified classname of the provider.
100      *
101      * @param name The name
102      * @return The FileProvider
103      * @throws ClassNotFoundException
104      * @throws InsantiationException
105      * @throws IllegalAccessException
106      */

107
108     public FileProvider createProvider(String JavaDoc name)
109             throws ClassNotFoundException JavaDoc,
110             InstantiationException JavaDoc,
111             IllegalAccessException JavaDoc {
112         if (log.isDebugEnabled()) {
113             log.debug("createProvider(" + name + ")");
114         }
115         if (WEBAPP.equals(name) ||
116                 ServletContextFileProvider.class.getName().equals(name)) {
117             log.debug("Constructing ServletContextFileProvider");
118             return new ServletContextFileProvider(siteContext.getServletContext());
119         } else if (FILE.equals(name)) {
120             log.debug("Constructing DefaultLocalFileProvider");
121             return new DefaultLocalFileProvider();
122         } else if (URL.equals(name)) {
123             log.debug("Constructing URLProvider");
124             return new UrlFileProvider();
125         } else {
126             log.debug("Constructing by class name");
127             return (FileProvider) ClassUtilities.loadClass(name).newInstance();
128         }
129     }
130
131 }
132
Popular Tags