1 15 package org.apache.tapestry.services.impl; 16 17 import java.util.HashMap ; 18 import java.util.Map ; 19 20 import org.apache.hivemind.ApplicationRuntimeException; 21 import org.apache.hivemind.ClassResolver; 22 import org.apache.hivemind.Resource; 23 import org.apache.hivemind.util.ClasspathResource; 24 import org.apache.tapestry.INamespace; 25 import org.apache.tapestry.engine.ISpecificationSource; 26 import org.apache.tapestry.engine.Namespace; 27 import org.apache.tapestry.event.ResetEventListener; 28 import org.apache.tapestry.parse.ISpecificationParser; 29 import org.apache.tapestry.spec.IApplicationSpecification; 30 import org.apache.tapestry.spec.IComponentSpecification; 31 import org.apache.tapestry.spec.ILibrarySpecification; 32 import org.apache.tapestry.spec.LibrarySpecification; 33 import org.apache.tapestry.util.xml.DocumentParseException; 34 35 46 47 public class SpecificationSourceImpl implements ISpecificationSource, ResetEventListener 48 { 49 private ClassResolver _classResolver; 50 51 private IApplicationSpecification _specification; 52 private ISpecificationParser _parser; 53 54 private INamespace _applicationNamespace; 55 private INamespace _frameworkNamespace; 56 57 61 62 private Map _componentCache = new HashMap (); 63 64 70 71 private Map _pageCache = new HashMap (); 72 73 80 81 private Map _libraryCache = new HashMap (); 82 83 88 89 private Map _namespaceCache = new HashMap (); 90 91 95 96 public synchronized void resetEventDidOccur() 97 { 98 _componentCache.clear(); 99 _pageCache.clear(); 100 _libraryCache.clear(); 101 _namespaceCache.clear(); 102 103 _applicationNamespace = null; 104 _frameworkNamespace = null; 105 } 106 107 protected IComponentSpecification parseSpecification(Resource resource, boolean asPage) 108 { 109 IComponentSpecification result = null; 110 111 try 112 { 113 if (asPage) 114 result = _parser.parsePageSpecification(resource); 115 else 116 result = _parser.parseComponentSpecification(resource); 117 } 118 catch (DocumentParseException ex) 119 { 120 throw new ApplicationRuntimeException( 121 ImplMessages.unableToParseSpecification(resource), 122 ex); 123 } 124 125 return result; 126 } 127 128 protected ILibrarySpecification parseLibrarySpecification(Resource resource) 129 { 130 try 131 { 132 return _parser.parseLibrarySpecification(resource); 133 } 134 catch (DocumentParseException ex) 135 { 136 throw new ApplicationRuntimeException( 137 ImplMessages.unableToParseSpecification(resource), 138 ex); 139 } 140 141 } 142 143 150 151 public synchronized IComponentSpecification getComponentSpecification(Resource resourceLocation) 152 { 153 IComponentSpecification result = 154 (IComponentSpecification) _componentCache.get(resourceLocation); 155 156 if (result == null) 157 { 158 result = parseSpecification(resourceLocation, false); 159 160 _componentCache.put(resourceLocation, result); 161 } 162 163 return result; 164 } 165 166 public synchronized IComponentSpecification getPageSpecification(Resource resourceLocation) 167 { 168 IComponentSpecification result = (IComponentSpecification) _pageCache.get(resourceLocation); 169 170 if (result == null) 171 { 172 result = parseSpecification(resourceLocation, true); 173 174 _pageCache.put(resourceLocation, result); 175 } 176 177 return result; 178 } 179 180 public synchronized ILibrarySpecification getLibrarySpecification(Resource resourceLocation) 181 { 182 ILibrarySpecification result = (LibrarySpecification) _libraryCache.get(resourceLocation); 183 184 if (result == null) 185 { 186 result = parseLibrarySpecification(resourceLocation); 187 _libraryCache.put(resourceLocation, result); 188 } 189 190 return result; 191 } 192 193 public synchronized INamespace getApplicationNamespace() 194 { 195 if (_applicationNamespace == null) 196 _applicationNamespace = new Namespace(null, null, _specification, this, _classResolver); 197 198 return _applicationNamespace; 199 } 200 201 public synchronized INamespace getFrameworkNamespace() 202 { 203 if (_frameworkNamespace == null) 204 { 205 Resource frameworkLocation = 206 new ClasspathResource(_classResolver, "/org/apache/tapestry/Framework.library"); 207 208 ILibrarySpecification ls = getLibrarySpecification(frameworkLocation); 209 210 _frameworkNamespace = new Namespace(INamespace.FRAMEWORK_NAMESPACE, null, ls, this, _classResolver); 211 } 212 213 return _frameworkNamespace; 214 } 215 216 public void setParser(ISpecificationParser parser) 217 { 218 _parser = parser; 219 } 220 221 public void setClassResolver(ClassResolver resolver) 222 { 223 _classResolver = resolver; 224 } 225 226 public void setSpecification(IApplicationSpecification specification) 227 { 228 _specification = specification; 229 } 230 } | Popular Tags |