| 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 | 256 | public class TemplateManagerImpl implements TemplateManager { |
| 41 | 4 | private static final UrumaLogger logger = UrumaLogger |
| 42 | |
.getLogger(TemplateManager.class); |
| 43 | |
|
| 44 | 144 | private Map<String, Template> templateCache = new HashMap<String, Template>(); |
| 45 | |
|
| 46 | 144 | private Map<String, String> idToPathMap = new HashMap<String, String>(); |
| 47 | |
|
| 48 | 144 | private ComponentTreeBuilder builder = new ComponentTreeBuilder(); |
| 49 | |
|
| 50 | 4 | private static boolean initialized = false; |
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
public Template getTemplate(final String path) { |
| 56 | 112 | synchronized (templateCache) { |
| 57 | 112 | if (!initialized) { |
| 58 | 112 | DisposableUtil.add(new Disposable() { |
| 59 | 112 | public void dispose() { |
| 60 | 112 | clear(); |
| 61 | 112 | initialized = false; |
| 62 | 112 | } |
| 63 | |
}); |
| 64 | 112 | initialized = true; |
| 65 | |
} |
| 66 | |
|
| 67 | 112 | Template template = templateCache.get(path); |
| 68 | 112 | if (template == null) { |
| 69 | 112 | logger.log(UrumaMessageCodes.LOAD_TEMPLATE_FROM_FILE, path); |
| 70 | |
|
| 71 | 112 | template = builder.build(path); |
| 72 | 112 | if (template != null) { |
| 73 | 112 | templateCache.put(path, template); |
| 74 | 112 | String id = template.getRootComponent().getId(); |
| 75 | 112 | if (StringUtil.isNotBlank(id)) { |
| 76 | 112 | if (!idToPathMap.containsKey(id)) { |
| 77 | 112 | idToPathMap.put(id, path); |
| 78 | |
|
| 79 | 112 | String type = template.getRootComponent() |
| 80 | |
.getClass().getSimpleName(); |
| 81 | 112 | logger.log(UrumaMessageCodes.TEMPLATE_REGISTERED, |
| 82 | |
id, type, path); |
| 83 | 112 | } else { |
| 84 | 0 | if (!idToPathMap.get(id).equals(path)) { |
| 85 | 0 | throw new DuplicateIdTemplateException(id, path); |
| 86 | |
} |
| 87 | |
} |
| 88 | |
} |
| 89 | 112 | } |
| 90 | |
} else { |
| 91 | 0 | logger.log(UrumaMessageCodes.LOAD_TEMPLATE_FROM_CACHE, path); |
| 92 | |
} |
| 93 | 112 | return template; |
| 94 | 0 | } |
| 95 | |
} |
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
public Template getTemplateById(final String id) { |
| 102 | 0 | String path = idToPathMap.get(id); |
| 103 | 0 | if (path != null) { |
| 104 | 0 | return getTemplate(path); |
| 105 | |
} else { |
| 106 | 0 | 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 | 0 | List<Template> templates = new ArrayList<Template>(); |
| 117 | |
|
| 118 | 0 | for (String path : idToPathMap.values()) { |
| 119 | 0 | Template template = getTemplate(path); |
| 120 | 0 | UIComponentContainer root = template.getRootComponent(); |
| 121 | 0 | if ((root != null) && componentClass.equals(root.getClass())) { |
| 122 | 0 | templates.add(template); |
| 123 | |
} |
| 124 | 0 | } |
| 125 | 0 | return templates; |
| 126 | |
} |
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
public void loadTemplates(final List<String> pathList) { |
| 132 | 0 | for (String path : pathList) { |
| 133 | 0 | getTemplate(path); |
| 134 | |
} |
| 135 | 0 | } |
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
public void clear() { |
| 141 | 112 | logger.log(UrumaMessageCodes.DELETE_ALL_TEMPLATE_FROM_CACHE); |
| 142 | 112 | templateCache.clear(); |
| 143 | 112 | } |
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
public void remove(final String id) { |
| 149 | 0 | String path = idToPathMap.get(id); |
| 150 | 0 | if (path != null) { |
| 151 | 0 | logger.log(UrumaMessageCodes.DELETE_TEMPLATE_FROM_CACHE); |
| 152 | 0 | templateCache.remove(path); |
| 153 | |
} |
| 154 | 0 | } |
| 155 | |
|
| 156 | |
} |