1 package net.sourceforge.blogentis.utils;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
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 }