KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > page > manage > PageSettingsManager


1 /*
2  * Created on Jul 21, 2004
3  */

4 package com.openedit.page.manage;
5
6 import java.util.HashMap JavaDoc;
7 import java.util.Map JavaDoc;
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 /**
24  * @author Matthew Avery, mavery@einnovation.com
25  *
26  * Can we come up with a better name for this class.... please!?
27  *
28  * Extending PageManager is a shortcut, not a valid case for inheritance.
29  * It is possible that PageManager and MetaDataConfigurator have a common
30  * abstract superclass.
31  */

32 public class PageSettingsManager
33 {
34     /** The default path (_default.xconf). */
35     public static final String JavaDoc DEFAULT_PATH = "_default.xconf";
36     public static final String JavaDoc SITE_PATH = "_site.xconf";
37     private static final Log log = LogFactory.getLog(PageSettingsManager.class);
38     
39     protected Map JavaDoc fieldGeneratorMap;
40     protected ModuleManager fieldModuleManager;
41     protected Generator fieldDefaultGenerator;
42     protected Filter fieldDefaultEditPageFilter;
43     protected Repository fieldRepository;
44     protected Map JavaDoc fieldCache;
45     protected MimeTypeMap fieldMimeTypeMap;
46
47     protected XConfToPageSettingsConverter fieldXconfReader;
48     protected PageSettingsToXconfWriter fieldPageSettingsWriter;
49     
50     public PageSettings getPageSettings( String JavaDoc 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 JavaDoc getCache()
62     {
63         if( fieldCache == null)
64         {
65             //HARD means even if the object goes out of scope we still keep it in the hashmap
66
//until the memory runs low then things get dumped randomly
67
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 JavaDoc inName ) throws OpenEditException
81     {
82         String JavaDoc 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 JavaDoc inUrlPath ) throws OpenEditException
93     {
94         //This managager deals with xconf files only
95
String JavaDoc xconfPath = inUrlPath;
96         xconfPath = toXconfPath(xconfPath);
97         
98         //debug( "Getting page meta data for " + inMetaDataPath );
99
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         //As long as it does not end with _default.xconf nor _site.xconf ir must be a real page
115
if ( !xconfPath.equals("/_site.xconf"))
116         {
117             String JavaDoc path = PathUtilities.extractDirectoryPath( xconfPath ); //go up a level
118
if ( inUrlPath.endsWith("_site.xconf"))
119             {
120                 path = PathUtilities.extractDirectoryPath( path ); //go up a level
121
}
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 JavaDoc mimeType = getMimeTypeMap().getPathMimeType( inUrlPath );
128             settings.setMimeType(mimeType);
129             
130             //insert a _default.xconf in here
131
String JavaDoc dir = PathUtilities.extractDirectoryPath( xconfPath ); //go up a level
132
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 JavaDoc toXconfPath(String JavaDoc xconfPath)
141     {
142         if( xconfPath.endsWith("/"))
143         {
144             return xconfPath + SITE_PATH;
145         }
146         if ( !xconfPath.endsWith("xconf"))
147         {
148             String JavaDoc 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 JavaDoc getGeneratorMap()
164     {
165         if ( fieldGeneratorMap == null )
166         {
167             fieldGeneratorMap = new HashMap JavaDoc();
168         }
169         return fieldGeneratorMap;
170     }
171     public void setGeneratorMap( Map JavaDoc 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     /**
194      * @param inXconf
195      */

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()); //this also should remove the *.html version
215
}
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     /**
239      * @param inPath such as index.xconf
240      */

241     public void clearCache(String JavaDoc inPath)
242     {
243         getCache().remove(inPath);
244         String JavaDoc path = toXconfPath(inPath);
245         getCache().remove(path);
246     }
247 }
248
Popular Tags