1 48 49 package org.jpublish.page; 50 51 import java.io.IOException ; 52 import java.io.InputStream ; 53 54 import com.anthonyeden.lib.config.Configuration; 55 import com.anthonyeden.lib.config.ConfigurationException; 56 import com.anthonyeden.lib.util.ClassUtilities; 57 import com.anthonyeden.lib.util.IOUtilities; 58 import com.anthonyeden.lib.util.MessageUtilities; 59 import org.apache.commons.logging.Log; 60 import org.apache.commons.logging.LogFactory; 61 import org.apache.commons.vfs.FileContent; 62 import org.apache.commons.vfs.FileObject; 63 import org.apache.commons.vfs.FileSystemManager; 64 import org.jpublish.JPublishEngine; 65 import org.jpublish.JPublishRuntimeException; 66 import org.jpublish.ManagerBase; 67 import org.jpublish.SiteContext; 68 import org.jpublish.cache.CacheEntry; 69 import org.jpublish.cache.CacheStorage; 70 import org.jpublish.cache.HashMapCacheStorage; 71 import org.jpublish.util.PathUtilities; 72 73 82 83 public class DefaultPageManager extends ManagerBase implements PageManager { 84 85 88 public static final String DEFAULT_PATH = "default.xml"; 89 90 private static final String ATTRIBUTE_CLASSNAME = "classname"; 91 92 private Log log = LogFactory.getLog(DefaultPageManager.class); 93 private CacheStorage pageCache = null; 94 private String defaultPath = DEFAULT_PATH; 95 96 102 103 public synchronized FileSystemManager getFileSystemManager() 104 throws IOException { 105 if (fileSystemManager == null) { 106 configureDefaultFileSystemManager(SiteContext.DEFAULT_PAGE_ROOT); 107 } 108 return fileSystemManager; 109 } 110 111 116 117 public synchronized CacheStorage getPageCache() { 118 if (pageCache == null) { 119 pageCache = new HashMapCacheStorage(); 120 } 121 return pageCache; 122 } 123 124 129 130 public void setPageCache(CacheStorage pageCache) { 131 this.pageCache = pageCache; 132 } 133 134 139 140 public String getDefaultPath() { 141 return defaultPath; 142 } 143 144 149 150 public void setDefaultPath(String defaultPath) { 151 if (defaultPath == null) { 152 this.defaultPath = DEFAULT_PATH; 153 } else { 154 this.defaultPath = defaultPath; 155 } 156 } 157 158 167 168 public synchronized PageInstance getPage(String path) 169 throws IOException , PageNotFoundException { 170 FileSystemManager fileSystemManager = getFileSystemManager(); 171 FileObject baseFile = fileSystemManager.getBaseFile(); 172 FileObject file = fileSystemManager.resolveFile(baseFile, 173 PathUtilities.toRelativePath(path)); 174 FileObject parentFolder = file.getParent(); 175 176 String pageName = PathUtilities.extractPageName(path); 177 String pageType = PathUtilities.extractPageType(path); 178 String pagePath = PathUtilities.extractPagePath(path); 179 String pageParent = PathUtilities.extractPageParent(path); 180 181 if (log.isDebugEnabled()) { 182 log.debug("Page name: " + pageName); 183 log.debug("Page type: " + pageType); 184 log.debug("Page path: " + pagePath); 185 log.debug("Page parent: " + pageParent); 186 } 187 188 String suffix = getActualConfigurationSuffix(); 189 if (log.isDebugEnabled()) { 190 log.debug("Resolving page " + pageName + suffix); 191 } 192 FileObject xmlFile = parentFolder.resolveFile(pageName + suffix); 193 194 if (!xmlFile.exists()) { 195 log.debug("Configuration file not found"); 196 xmlFile = baseFile.getChild(getDefaultPath()); 197 if (xmlFile == null || !xmlFile.exists()) { 198 Object [] args = {path}; 199 String msg = MessageUtilities.getMessage(getClass(), 200 JPublishEngine.MESSAGE_PACKAGE, "pageNotFound", args); 201 throw new PageNotFoundException(msg); 202 } 203 } 204 205 if (log.isDebugEnabled()) { 206 log.debug("Looking for page:" + xmlFile); 207 } 208 209 FileContent content = xmlFile.getContent(); 210 211 CacheStorage pageCache = getPageCache(); 213 CacheEntry cacheEntry = (CacheEntry) pageCache.get(pagePath); 214 215 PageInstance page = null; 216 PageDefinition pageDefinition = null; 217 InputStream in = null; 218 try { 219 if (cacheEntry == null) { 220 if (log.isDebugEnabled()) { 221 log.debug("Page definition (" + pagePath + 222 ") not found in cache."); 223 } 224 225 if (log.isDebugEnabled()) { 226 log.debug("Loading page definition configuration: " + xmlFile); 227 } 228 229 in = content.getInputStream(); 230 231 pageDefinition = new PageDefinition(siteContext, pagePath); 232 pageDefinition.loadConfiguration(in); 233 234 pageCache.put(pagePath, new CacheEntry(pageDefinition, 235 content.getLastModifiedTime())); 236 } else { 237 if (log.isDebugEnabled()) { 238 log.debug("Page definition (" + pagePath + 239 ") found in cache."); 240 } 241 242 pageDefinition = (PageDefinition) cacheEntry.getObject(); 243 if (cacheEntry.getLastModified() != 244 content.getLastModifiedTime()) { 245 log.debug("Page modification dates do not match."); 246 log.debug("Reloading page definition."); 247 long lastModified = content.getLastModifiedTime(); 248 249 in = content.getInputStream(); 250 251 pageDefinition = new PageDefinition(siteContext, pagePath); 252 pageDefinition.loadConfiguration(in); 253 254 pageCache.put(pagePath, new CacheEntry(pageDefinition, 255 lastModified)); 256 } 257 } 258 } catch (ConfigurationException e) { 259 throw new JPublishRuntimeException(e.getMessage(), e); 260 } finally { 261 IOUtilities.close(in); 262 } 263 264 if (pageDefinition != null) { 265 try { 266 if (log.isDebugEnabled()) { 267 log.debug("Getting page instance for " + path); 268 } 269 page = pageDefinition.getPageInstance(path); 270 } catch (ConfigurationException e) { 271 throw new JPublishRuntimeException(e.getMessage(), e); 272 } 273 } 274 275 return page; 276 } 277 278 286 287 public long getLastModified(String path) throws IOException { 288 FileSystemManager fileSystemManager = getFileSystemManager(); 289 FileObject baseFile = fileSystemManager.getBaseFile(); 290 FileObject file = fileSystemManager.resolveFile(baseFile, 291 PathUtilities.toRelativePath(path)); 292 FileContent content = file.getContent(); 293 return content.getLastModifiedTime(); 294 } 295 296 302 303 public void loadConfiguration(Configuration configuration) 304 throws ConfigurationException { 305 super.loadConfiguration(configuration); 306 307 setDefaultPath(configuration.getChildValue("default-path")); 309 310 String className = null; 312 try { 313 Configuration cacheStorageElement = configuration.getChild("cache-storage"); 314 if (cacheStorageElement != null) { 315 className = cacheStorageElement.getAttribute(ATTRIBUTE_CLASSNAME, HashMapCacheStorage.class.getName()); 316 CacheStorage cache = (CacheStorage) ClassUtilities.loadClass(className).newInstance(); 317 cache.loadConfiguration(cacheStorageElement); 318 setPageCache(cache); 319 } 320 } catch (ClassNotFoundException e) { 321 Object [] args = {className}; 322 String msg = MessageUtilities.getMessage(getClass(), 323 JPublishEngine.MESSAGE_PACKAGE, "cacheStorageClassNotFound", 324 args); 325 throw new ConfigurationException(msg, e); 326 } catch (InstantiationException e) { 327 Object [] args = {}; 328 String msg = MessageUtilities.getMessage(getClass(), 329 JPublishEngine.MESSAGE_PACKAGE, 330 "cacheStorageInstantiationError", args); 331 throw new ConfigurationException(msg, e); 332 } catch (IllegalAccessException e) { 333 Object [] args = {}; 334 String msg = MessageUtilities.getMessage(getClass(), 335 JPublishEngine.MESSAGE_PACKAGE, 336 "cacheStorageIllegalAccessError", args); 337 throw new ConfigurationException(msg, e); 338 } 339 } 340 341 } 342 | Popular Tags |