KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jpublish > ManagerBase


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;
50
51 import java.io.IOException JavaDoc;
52 import java.util.Iterator JavaDoc;
53
54 import com.anthonyeden.lib.config.Configuration;
55 import com.anthonyeden.lib.config.ConfigurationException;
56 import com.anthonyeden.lib.util.MessageUtilities;
57 import org.apache.commons.logging.Log;
58 import org.apache.commons.logging.LogFactory;
59 import org.apache.commons.vfs.FileObject;
60 import org.apache.commons.vfs.FileSystemException;
61 import org.apache.commons.vfs.FileSystemManager;
62 import org.apache.commons.vfs.impl.DefaultFileSystemManager;
63 import org.apache.commons.vfs.provider.FileProvider;
64 import org.jpublish.vfs.FileProviderFactory;
65
66 /**
67  * An abstract base implementation of the Manager interface which provides behavior common to all Manager
68  * implementations.
69  *
70  * @author Anthony Eden
71  */

72 public abstract class ManagerBase implements Manager {
73
74     private static final String JavaDoc ATTRIBUTE_CLASSNAME = "classname";
75     private static final String JavaDoc ATTRIBUTE_SCHEME = "scheme";
76     private static final String JavaDoc ATTRIBUTE_BASE = "base";
77
78     private Log log = LogFactory.getLog(ManagerBase.class);
79
80     protected FileSystemManager fileSystemManager = null;
81     protected SiteContext siteContext = null;
82     protected String JavaDoc configurationSuffix = null;
83
84     /**
85      * Return the virtual file system manager. This method may return null if the implementation does not support a
86      * virtual file system.
87      *
88      * @return The FileSystemManager
89      */

90     public FileSystemManager getFileSystemManager() throws IOException JavaDoc {
91         return fileSystemManager;
92     }
93
94     /**
95      * Set the virtual file system manager.
96      *
97      * @param fileSystemManager The file system manager
98      */

99     public void setFileSystemManager(FileSystemManager fileSystemManager) {
100         this.fileSystemManager = fileSystemManager;
101     }
102
103     /**
104      * Get the configuration suffix.
105      *
106      * @return The configuration suffix
107      */

108     public String JavaDoc getConfigurationSuffix() {
109         return configurationSuffix;
110     }
111
112     /**
113      * Set the configuration suffix.
114      *
115      * @param configurationSuffix The new configuration suffix
116      */

117     public void setConfigurationSuffix(String JavaDoc configurationSuffix) {
118         if (log.isDebugEnabled()) {
119             log.debug("Configuration suffix: " + configurationSuffix);
120         }
121         this.configurationSuffix = configurationSuffix;
122     }
123
124     /**
125      * Get the configuration suffix, first looking in the manager's configuration suffix value and if that is null
126      * return the default value specified in the SiteContext.
127      *
128      * @return The configuration suffix
129      */

130     protected String JavaDoc getActualConfigurationSuffix() {
131         String JavaDoc configurationSuffix = getConfigurationSuffix();
132         if (configurationSuffix == null) {
133             return siteContext.getConfigurationSuffix();
134         }
135         return configurationSuffix;
136     }
137
138     /**
139      * Get the SiteContext.
140      *
141      * @return The SiteContext
142      */

143     public SiteContext getSiteContext() {
144         return siteContext;
145     }
146
147     /**
148      * Set the SiteContext.
149      *
150      * @param siteContext The site context
151      */

152     public void setSiteContext(SiteContext siteContext) {
153         log.debug("Site context set.");
154         this.siteContext = siteContext;
155     }
156
157     /**
158      * Load the PageManager's configuration.
159      *
160      * @param configuration The Configuration object
161      * @throws ConfigurationException
162      */

163     public void loadConfiguration(Configuration configuration)
164             throws ConfigurationException {
165         setConfigurationSuffix(configuration.getChildValue("configuration-suffix"));
166
167         log.debug("Loading configuration");
168         Configuration fileSystemConfiguration =
169                 configuration.getChild("filesystem");
170         if (fileSystemConfiguration != null) {
171             configureFileSystemManager(fileSystemConfiguration);
172         }
173     }
174
175     /**
176      * Configure the file system manager.
177      *
178      * @param configuration The file system configuration
179      * @throws ConfigurationException
180      */

181     protected void configureFileSystemManager(Configuration configuration)
182             throws ConfigurationException {
183         log.debug("Configuring FileSystemManager");
184
185         DefaultFileSystemManager fileSystemManager =
186                 new DefaultFileSystemManager();
187
188         Iterator JavaDoc providerIter = configuration.getChildren("provider").iterator();
189         while (providerIter.hasNext()) {
190             Configuration providerConfiguration =
191                     (Configuration) providerIter.next();
192             String JavaDoc className = providerConfiguration.getAttribute(ATTRIBUTE_CLASSNAME);
193             String JavaDoc scheme = providerConfiguration.getAttribute(ATTRIBUTE_SCHEME);
194
195             try {
196                 FileProviderFactory factory =
197                         siteContext.getFileProviderFactory();
198                 FileProvider provider = factory.createProvider(className);
199                 fileSystemManager.addProvider(scheme, provider);
200             } catch (Exception JavaDoc e) {
201                 log.error("Error loading provider: " + className);
202                 if (log.isDebugEnabled()) {
203                     e.printStackTrace();
204                 }
205             }
206         }
207
208         String JavaDoc base = configuration.getAttribute(ATTRIBUTE_BASE);
209         if (base != null) {
210             try {
211                 FileObject baseFile = fileSystemManager.resolveFile(base);
212                 fileSystemManager.setBaseFile(baseFile);
213             } catch (FileSystemException e) {
214                 Object JavaDoc[] args = {base};
215                 String JavaDoc msg = MessageUtilities.getMessage(getClass(),
216                         JPublishEngine.MESSAGE_PACKAGE, "baseFileError", args);
217                 throw new ConfigurationException(msg, e, configuration);
218             }
219         }
220
221         setFileSystemManager(fileSystemManager);
222     }
223
224     /**
225      * Configure the default file system manager with the given base URI.
226      *
227      * @param base The base URI
228      * @throws FileSystemException
229      */

230     protected void configureDefaultFileSystemManager(String JavaDoc base)
231             throws FileSystemException {
232         if (log.isDebugEnabled()) {
233             log.debug("Configuring default file system manager (base="
234                     + base + ")");
235         }
236
237         String JavaDoc defaultScheme = siteContext.getDefaultScheme();
238         DefaultFileSystemManager fileSystemManager =
239                 new DefaultFileSystemManager();
240         fileSystemManager.addProvider(defaultScheme,
241                 siteContext.getDefaultFileProvider());
242         FileObject root = fileSystemManager.resolveFile(defaultScheme + ":/");
243         FileObject baseFile = fileSystemManager.resolveFile(root, base);
244         fileSystemManager.setBaseFile(baseFile);
245         setFileSystemManager(fileSystemManager);
246     }
247
248 }
249
Popular Tags