1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.seasar.uruma.core.impl;
17
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22
23 import org.seasar.framework.util.Disposable;
24 import org.seasar.framework.util.DisposableUtil;
25 import org.seasar.framework.util.StringUtil;
26 import org.seasar.uruma.component.Template;
27 import org.seasar.uruma.component.UIComponentContainer;
28 import org.seasar.uruma.component.factory.ComponentTreeBuilder;
29 import org.seasar.uruma.core.TemplateManager;
30 import org.seasar.uruma.core.UrumaMessageCodes;
31 import org.seasar.uruma.exception.DuplicateIdTemplateException;
32 import org.seasar.uruma.exception.NotFoundException;
33 import org.seasar.uruma.log.UrumaLogger;
34
35
36
37
38
39
40 public class TemplateManagerImpl implements TemplateManager {
41 private static final UrumaLogger logger = UrumaLogger
42 .getLogger(TemplateManager.class);
43
44 private Map<String, Template> templateCache = new HashMap<String, Template>();
45
46 private Map<String, String> idToPathMap = new HashMap<String, String>();
47
48 private ComponentTreeBuilder builder = new ComponentTreeBuilder();
49
50 private static boolean initialized = false;
51
52
53
54
55 public Template getTemplate(final String path) {
56 synchronized (templateCache) {
57 if (!initialized) {
58 DisposableUtil.add(new Disposable() {
59 public void dispose() {
60 clear();
61 initialized = false;
62 }
63 });
64 initialized = true;
65 }
66
67 Template template = templateCache.get(path);
68 if (template == null) {
69 logger.log(UrumaMessageCodes.LOAD_TEMPLATE_FROM_FILE, path);
70
71 template = builder.build(path);
72 if (template != null) {
73 templateCache.put(path, template);
74 String id = template.getRootComponent().getId();
75 if (StringUtil.isNotBlank(id)) {
76 if (!idToPathMap.containsKey(id)) {
77 idToPathMap.put(id, path);
78
79 String type = template.getRootComponent()
80 .getClass().getSimpleName();
81 logger.log(UrumaMessageCodes.TEMPLATE_REGISTERED,
82 id, type, path);
83 } else {
84 if (!idToPathMap.get(id).equals(path)) {
85 throw new DuplicateIdTemplateException(id, path);
86 }
87 }
88 }
89 }
90 } else {
91 logger.log(UrumaMessageCodes.LOAD_TEMPLATE_FROM_CACHE, path);
92 }
93 return template;
94 }
95 }
96
97
98
99
100
101 public Template getTemplateById(final String id) {
102 String path = idToPathMap.get(id);
103 if (path != null) {
104 return getTemplate(path);
105 } else {
106 throw new NotFoundException(UrumaMessageCodes.TEMPLATE_NOT_FOUND,
107 id);
108 }
109 }
110
111
112
113
114 public List<Template> getTemplates(
115 final Class<? extends UIComponentContainer> componentClass) {
116 List<Template> templates = new ArrayList<Template>();
117
118 for (String path : idToPathMap.values()) {
119 Template template = getTemplate(path);
120 UIComponentContainer root = template.getRootComponent();
121 if ((root != null) && componentClass.equals(root.getClass())) {
122 templates.add(template);
123 }
124 }
125 return templates;
126 }
127
128
129
130
131 public void loadTemplates(final List<String> pathList) {
132 for (String path : pathList) {
133 getTemplate(path);
134 }
135 }
136
137
138
139
140 public void clear() {
141 logger.log(UrumaMessageCodes.DELETE_ALL_TEMPLATE_FROM_CACHE);
142 templateCache.clear();
143 }
144
145
146
147
148 public void remove(final String id) {
149 String path = idToPathMap.get(id);
150 if (path != null) {
151 logger.log(UrumaMessageCodes.DELETE_TEMPLATE_FROM_CACHE);
152 templateCache.remove(path);
153 }
154 }
155
156 }