1 48 49 package org.jpublish; 50 51 import java.io.IOException ; 52 import java.util.Iterator ; 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 72 public abstract class ManagerBase implements Manager { 73 74 private static final String ATTRIBUTE_CLASSNAME = "classname"; 75 private static final String ATTRIBUTE_SCHEME = "scheme"; 76 private static final String 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 configurationSuffix = null; 83 84 90 public FileSystemManager getFileSystemManager() throws IOException { 91 return fileSystemManager; 92 } 93 94 99 public void setFileSystemManager(FileSystemManager fileSystemManager) { 100 this.fileSystemManager = fileSystemManager; 101 } 102 103 108 public String getConfigurationSuffix() { 109 return configurationSuffix; 110 } 111 112 117 public void setConfigurationSuffix(String configurationSuffix) { 118 if (log.isDebugEnabled()) { 119 log.debug("Configuration suffix: " + configurationSuffix); 120 } 121 this.configurationSuffix = configurationSuffix; 122 } 123 124 130 protected String getActualConfigurationSuffix() { 131 String configurationSuffix = getConfigurationSuffix(); 132 if (configurationSuffix == null) { 133 return siteContext.getConfigurationSuffix(); 134 } 135 return configurationSuffix; 136 } 137 138 143 public SiteContext getSiteContext() { 144 return siteContext; 145 } 146 147 152 public void setSiteContext(SiteContext siteContext) { 153 log.debug("Site context set."); 154 this.siteContext = siteContext; 155 } 156 157 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 181 protected void configureFileSystemManager(Configuration configuration) 182 throws ConfigurationException { 183 log.debug("Configuring FileSystemManager"); 184 185 DefaultFileSystemManager fileSystemManager = 186 new DefaultFileSystemManager(); 187 188 Iterator providerIter = configuration.getChildren("provider").iterator(); 189 while (providerIter.hasNext()) { 190 Configuration providerConfiguration = 191 (Configuration) providerIter.next(); 192 String className = providerConfiguration.getAttribute(ATTRIBUTE_CLASSNAME); 193 String 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 e) { 201 log.error("Error loading provider: " + className); 202 if (log.isDebugEnabled()) { 203 e.printStackTrace(); 204 } 205 } 206 } 207 208 String 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 [] args = {base}; 215 String 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 230 protected void configureDefaultFileSystemManager(String base) 231 throws FileSystemException { 232 if (log.isDebugEnabled()) { 233 log.debug("Configuring default file system manager (base=" 234 + base + ")"); 235 } 236 237 String 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 |