View Javadoc

1   package net.sourceforge.blogentis.utils;
2   
3   //-----------------------------------------------------------------------
4   //Blogentis - a blog publishing platform.
5   //Copyright (C) 2004 Tassos Bassoukos <abassouk@gmail.com>
6   //
7   //This library is free software; you can redistribute it and/or
8   //modify it under the terms of the GNU Lesser General Public
9   //License as published by the Free Software Foundation; either
10  //version 2.1 of the License, or (at your option) any later version.
11  //
12  //This library is distributed in the hope that it will be useful,
13  //but WITHOUT ANY WARRANTY; without even the implied warranty of
14  //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  //Lesser General Public License for more details.
16  //
17  //You should have received a copy of the GNU Lesser General Public
18  //License along with this library; if not, write to the Free Software
19  //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  //-----------------------------------------------------------------------
21  //
22  //$Id: BlogPropertyCache.java,v 1.2 2004/10/28 10:45:51 tassos Exp $
23  //
24  
25  import java.util.HashMap;
26  import java.util.Map;
27  
28  import net.sourceforge.blogentis.om.Blog;
29  
30  import org.apache.commons.configuration.SubsetConfiguration;
31  import org.apache.turbine.services.TurbineBaseService;
32  import org.apache.turbine.services.TurbineServices;
33  
34  /***
35   * @author abas
36   */
37  public class BlogPropertyCache
38          extends TurbineBaseService {
39      public static final String SERVICE_NAME = "BlogPropertyService";
40  
41      private static class Entry {
42          BlogProperties blog = null;
43          PresentationalProperties section = null;
44          PresentationalProperties post = null;
45          PresentationalProperties edit = null;
46      }
47  
48      private Map blogConfigurations = new HashMap();
49      private Entry rootProperties;
50  
51      private void clear() {
52          blogConfigurations.clear();
53          rootProperties = null;
54      }
55  
56      public void init() {
57          clear();
58          //rootProperties = makeProperties(null);
59          setInit(true);
60      }
61  
62      public void shutdown() {
63          clear();
64          super.shutdown();
65      }
66  
67      private Entry makeProperties(Blog blog) {
68          Entry e = new Entry();
69  
70          MappedConfiguration conf = blog.getConfiguration();
71  
72          e.blog = new BlogProperties(new SubsetConfiguration(conf, "blog", "."));
73          e.section = new PresentationalProperties(new SubsetConfiguration(conf,
74              "section", "."));
75          e.post = new PresentationalProperties(new SubsetConfiguration(conf,
76              "post", "."));
77          e.edit = new PresentationalProperties(new SubsetConfiguration(conf,
78              "edit", "."));
79          if (blog != null)
80              blogConfigurations.put(blog.getName(), e);
81          return e;
82      }
83  
84      private Entry getProperties(Blog blog) {
85          if (blog == null)
86              return rootProperties;
87          synchronized (blogConfigurations) {
88              Entry e = (Entry)blogConfigurations.get(blog.getName());
89              if (e == null)
90                  return makeProperties(blog);
91              return e;
92          }
93      }
94  
95      public BlogProperties getBlogProperties(Blog blog) {
96          return getProperties(blog).blog;
97      }
98  
99      public PresentationalProperties getSectionProperties(Blog blog) {
100         return getProperties(blog).section;
101     }
102 
103     public PresentationalProperties getPostProperties(Blog blog) {
104         return getProperties(blog).post;
105     }
106 
107     public PresentationalProperties getEditProperties(Blog blog) {
108         return getProperties(blog).post;
109     }
110 
111     public static BlogPropertyCache getInstance() {
112         return (BlogPropertyCache)TurbineServices.getInstance()
113             .getService(SERVICE_NAME);
114     }
115 }