1 4 package com.openedit.page.manage; 5 6 import java.util.HashMap ; 7 import java.util.Map ; 8 9 import org.apache.commons.collections.map.ReferenceMap; 10 import org.apache.commons.logging.Log; 11 import org.apache.commons.logging.LogFactory; 12 import org.openedit.repository.ContentItem; 13 import org.openedit.repository.Repository; 14 import org.openedit.repository.RepositoryException; 15 16 import com.openedit.Generator; 17 import com.openedit.ModuleManager; 18 import com.openedit.OpenEditException; 19 import com.openedit.page.PageSettings; 20 import com.openedit.util.PathUtilities; 21 import com.openedit.util.strainer.Filter; 22 23 32 public class PageSettingsManager 33 { 34 35 public static final String DEFAULT_PATH = "_default.xconf"; 36 public static final String SITE_PATH = "_site.xconf"; 37 private static final Log log = LogFactory.getLog(PageSettingsManager.class); 38 39 protected Map fieldGeneratorMap; 40 protected ModuleManager fieldModuleManager; 41 protected Generator fieldDefaultGenerator; 42 protected Filter fieldDefaultEditPageFilter; 43 protected Repository fieldRepository; 44 protected Map fieldCache; 45 protected MimeTypeMap fieldMimeTypeMap; 46 47 protected XConfToPageSettingsConverter fieldXconfReader; 48 protected PageSettingsToXconfWriter fieldPageSettingsWriter; 49 50 public PageSettings getPageSettings( String inPath ) throws OpenEditException 51 { 52 PageSettings page = (PageSettings) getCache().get( inPath ); 53 if ( page != null && page.isCurrent() ) 54 { 55 return page; 56 } 57 page = createPageSettings( inPath ); 58 return page; 59 } 60 61 protected Map getCache() 62 { 63 if( fieldCache == null) 64 { 65 fieldCache = new ReferenceMap(ReferenceMap.HARD,ReferenceMap.SOFT); 68 } 69 return fieldCache; 70 } 71 public Filter getDefaultEditPageFilter() 72 { 73 return fieldDefaultEditPageFilter; 74 } 75 76 public void setDefaultEditPageFilter( Filter inFilter ) 77 { 78 fieldDefaultEditPageFilter = inFilter; 79 } 80 protected Generator getGenerator( String inName ) throws OpenEditException 81 { 82 String id = inName; 83 Generator gen = (Generator)getGeneratorMap().get( id ); 84 if ( gen == null) 85 { 86 gen = (Generator)getModuleManager().getBean(inName); 87 getGeneratorMap().put(id,gen); 88 } 89 return gen; 90 } 91 92 protected PageSettings createPageSettings( String inUrlPath ) throws OpenEditException 93 { 94 String xconfPath = inUrlPath; 96 xconfPath = toXconfPath(xconfPath); 97 98 xconfPath = PathUtilities.resolveRelativePath( xconfPath, "/" ); 100 101 102 PageSettings settings = new PageSettings(); 103 ContentItem content = null; 104 try 105 { 106 content = getRepository().get(xconfPath); 107 settings.setXConf( content ); 108 } 109 catch (RepositoryException ex) 110 { 111 log.error( ex ); 112 throw new OpenEditException(ex); 113 } 114 if ( !xconfPath.equals("/_site.xconf")) 116 { 117 String path = PathUtilities.extractDirectoryPath( xconfPath ); if ( inUrlPath.endsWith("_site.xconf")) 119 { 120 path = PathUtilities.extractDirectoryPath( path ); } 122 PageSettings parent = getPageSettings( path + "/" + SITE_PATH ); 123 settings.setParent(parent); 124 } 125 if( !xconfPath.endsWith("_site.xconf") && !xconfPath.endsWith("_default.xconf")) 126 { 127 String mimeType = getMimeTypeMap().getPathMimeType( inUrlPath ); 128 settings.setMimeType(mimeType); 129 130 String dir = PathUtilities.extractDirectoryPath( xconfPath ); PageSettings parent = getPageSettings(dir + "/" + DEFAULT_PATH); 133 settings.setParent(parent); 134 } 135 getXconfReader().configure( settings, inUrlPath ); 136 getCache().put( inUrlPath, settings ); 137 return settings; 138 } 139 140 public String toXconfPath(String xconfPath) 141 { 142 if( xconfPath.endsWith("/")) 143 { 144 return xconfPath + SITE_PATH; 145 } 146 if ( !xconfPath.endsWith("xconf")) 147 { 148 String path = PathUtilities.extractPagePath( xconfPath ); 149 xconfPath = path + ".xconf"; 150 } 151 return xconfPath; 152 } 153 public MimeTypeMap getMimeTypeMap() 154 { 155 return fieldMimeTypeMap; 156 } 157 158 public void setMimeTypeMap( MimeTypeMap mimeTypeMap ) 159 { 160 fieldMimeTypeMap = mimeTypeMap; 161 } 162 163 public Map getGeneratorMap() 164 { 165 if ( fieldGeneratorMap == null ) 166 { 167 fieldGeneratorMap = new HashMap (); 168 } 169 return fieldGeneratorMap; 170 } 171 public void setGeneratorMap( Map generators ) 172 { 173 fieldGeneratorMap = generators; 174 } 175 public Repository getRepository() 176 { 177 return fieldRepository; 178 } 179 180 public void setRepository(Repository inRepository) 181 { 182 fieldRepository = inRepository; 183 } 184 185 protected ModuleManager getModuleManager() 186 { 187 return fieldModuleManager; 188 } 189 public void setModuleManager(ModuleManager inPluginManager) 190 { 191 fieldModuleManager = inPluginManager; 192 } 193 196 public void saveSetting(PageSettings inSetting) throws OpenEditException 197 { 198 ContentItem item = getPageSettingsWriter().createXConf(inSetting); 199 saveSetting(item); 200 } 201 public void saveSetting(ContentItem inXconf) throws OpenEditException 202 { 203 if ( inXconf.isWritable()) 204 { 205 try 206 { 207 getRepository().put(inXconf); 208 } 209 catch (RepositoryException ex) 210 { 211 log.error( ex ); 212 throw new OpenEditException(ex); 213 } 214 getCache().remove(inXconf.getPath()); } 216 } 217 public void clearCache() 218 { 219 getCache().clear(); 220 } 221 protected XConfToPageSettingsConverter getXconfReader() 222 { 223 return fieldXconfReader; 224 } 225 public void setXconfReader(XConfToPageSettingsConverter inConverter) 226 { 227 fieldXconfReader = inConverter; 228 } 229 protected PageSettingsToXconfWriter getPageSettingsWriter() 230 { 231 return fieldPageSettingsWriter; 232 } 233 public void setPageSettingsWriter(PageSettingsToXconfWriter inPageSettingsWriter) 234 { 235 fieldPageSettingsWriter = inPageSettingsWriter; 236 } 237 238 241 public void clearCache(String inPath) 242 { 243 getCache().remove(inPath); 244 String path = toXconfPath(inPath); 245 getCache().remove(path); 246 } 247 } 248 | Popular Tags |