1 package net.sourceforge.blogentis.utils.tools;
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
27 import net.sourceforge.blogentis.om.Blog;
28 import net.sourceforge.blogentis.storage.VelocityFragment;
29 import net.sourceforge.blogentis.turbine.BlogRunDataService;
30
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33 import org.apache.turbine.services.pull.ApplicationTool;
34
35 /***
36 * @author abas
37 */
38 public class FragmentTool implements ApplicationTool {
39 private static final char PACKAGE_SEPARATOR = '.';
40
41 private static final char PATH_SEPARATOR = '/';
42
43 public static final String DEFAULT_CLASS = VelocityFragment.class.getName();
44
45 public static final String SEARCH_PACKAGE = "net.sourceforge.blogentis.modules";
46
47 public static final String DEFAULT_PACKAGE = "fragments";
48
49 public static final String DEFAUT_PACKAGE_CLASS = "Default";
50
51 private static final Log log = LogFactory.getLog(FragmentTool.class);
52
53 protected HashMap blogs = new HashMap();
54
55 protected VelocityFragment getCachedFragment(Blog blog, String packageName,/package-summary.html">ong> VelocityFragment getCachedFragment(Blog blog, String packageName,
56 String fragmentName) {
57 synchronized (blogs) {
58 HashMap fragments = (HashMap)blogs.get(blog == null ? "NULL"
59 : blog.getName());
60 if (fragments == null)
61 return null;
62 return (VelocityFragment)fragments.get(packageName + PATH_SEPARATOR
63 + fragmentName);
64 }
65 }
66
67 protected void setCacheFragment(Blog blog, VelocityFragment fragment) {
68 String blogName = (blog == null) ? "NULL" : blog.getName();
69 synchronized (blogs) {
70 if (!blogs.containsKey(blogName))
71 blogs.put(blogName, new HashMap());
72 HashMap fragments = (HashMap)blogs.get(blogName);
73 fragments.put(fragment.getName(), fragment);
74 }
75 }
76
77 protected VelocityFragment loadFragment(Blog blog, String packageName,/package-summary.html">ong> VelocityFragment loadFragment(Blog blog, String packageName,
78 String name) {
79 VelocityFragment f = null;
80
81 String path = packageName + PATH_SEPARATOR + name;
82
83 try {
84 Class c = null;
85 try {
86 c = Class.forName(SEARCH_PACKAGE
87 + PACKAGE_SEPARATOR
88 + path.replace(PATH_SEPARATOR,
89 PACKAGE_SEPARATOR));
90 } catch (ClassNotFoundException ignored) {
91 try {
92 c = Class.forName(SEARCH_PACKAGE + PACKAGE_SEPARATOR
93 + packageName + PACKAGE_SEPARATOR
94 + DEFAUT_PACKAGE_CLASS);
95 } catch (ClassNotFoundException ignored2) {
96 c = Class.forName(DEFAULT_CLASS);
97 }
98 }
99 f = (VelocityFragment)c.newInstance();
100 f.init(blog, path);
101 setCacheFragment(blog, f);
102 } catch (Exception e) {
103 log.error(e);
104 }
105 return f;
106 }
107
108 public VelocityFragment getFragment(String pack, String name) {
109 Blog b = BlogRunDataService.getCurrentRunData().getBlog();
110 VelocityFragment f = getCachedFragment(b, pack, name);
111 if (f != null)
112 return f;
113 return loadFragment(b, pack, name);
114 }
115
116 public VelocityFragment getFragment(String name) {
117 return getFragment(DEFAULT_PACKAGE, name);
118 }
119
120 public void init(Object data) {}
121
122 public void refresh() {}
123 }