1 15 package org.apache.tapestry.resolver; 16 17 import org.apache.commons.logging.Log; 18 import org.apache.hivemind.ApplicationRuntimeException; 19 import org.apache.hivemind.Resource; 20 import org.apache.tapestry.INamespace; 21 import org.apache.tapestry.IRequestCycle; 22 import org.apache.tapestry.PageNotFoundException; 23 import org.apache.tapestry.Tapestry; 24 import org.apache.tapestry.services.ComponentPropertySource; 25 import org.apache.tapestry.spec.ComponentSpecification; 26 import org.apache.tapestry.spec.IComponentSpecification; 27 28 59 60 public class PageSpecificationResolverImpl extends AbstractSpecificationResolver implements 61 PageSpecificationResolver 62 { 63 64 private Log _log; 65 66 67 private String _simpleName; 68 69 70 private INamespace _applicationNamespace; 71 72 73 private INamespace _frameworkNamespace; 74 75 76 77 private ComponentPropertySource _componentPropertySource; 78 79 public void initializeService() 80 { 81 _applicationNamespace = getSpecificationSource().getApplicationNamespace(); 82 _frameworkNamespace = getSpecificationSource().getFrameworkNamespace(); 83 84 super.initializeService(); 85 } 86 87 protected void reset() 88 { 89 _simpleName = null; 90 91 super.reset(); 92 } 93 94 101 102 public void resolve(IRequestCycle cycle, String prefixedName) 103 { 104 reset(); 105 106 INamespace namespace = null; 107 108 int colonx = prefixedName.indexOf(':'); 109 110 if (colonx > 0) 111 { 112 _simpleName = prefixedName.substring(colonx + 1); 113 String namespaceId = prefixedName.substring(0, colonx); 114 115 if (namespaceId.equals(INamespace.FRAMEWORK_NAMESPACE)) 116 namespace = _frameworkNamespace; 117 else 118 namespace = _applicationNamespace.getChildNamespace(namespaceId); 119 } 120 else 121 { 122 _simpleName = prefixedName; 123 124 namespace = _applicationNamespace; 125 } 126 127 setNamespace(namespace); 128 129 if (namespace.containsPage(_simpleName)) 130 { 131 setSpecification(namespace.getPageSpecification(_simpleName)); 132 return; 133 } 134 135 137 searchForPage(cycle); 138 139 if (getSpecification() == null) 140 throw new PageNotFoundException(ResolverMessages.noSuchPage(_simpleName, namespace)); 141 } 142 143 public String getSimplePageName() 144 { 145 return _simpleName; 146 } 147 148 private void searchForPage(IRequestCycle cycle) 149 { 150 INamespace namespace = getNamespace(); 151 152 if (_log.isDebugEnabled()) 153 _log.debug(ResolverMessages.resolvingPage(_simpleName, namespace)); 154 155 String expectedName = _simpleName + ".page"; 156 157 Resource namespaceLocation = namespace.getSpecificationLocation(); 158 159 163 if (found(namespaceLocation.getRelativeResource(expectedName))) 164 return; 165 166 if (namespace.isApplicationNamespace()) 167 { 168 169 171 if (found(getWebInfAppLocation().getRelativeResource(expectedName))) 172 return; 173 174 if (found(getWebInfLocation().getRelativeResource(expectedName))) 175 return; 176 177 if (found(getContextRoot().getRelativeResource(expectedName))) 178 return; 179 180 183 String templateName = _simpleName + "." + getTemplateExtension(); 184 185 Resource templateResource = getContextRoot().getRelativeResource(templateName); 186 187 if (_log.isDebugEnabled()) 188 _log.debug(ResolverMessages.checkingResource(templateResource)); 189 190 if (templateResource.getResourceURL() != null) 191 { 192 setupImplicitPage(templateResource); 193 return; 194 } 195 196 198 if (_frameworkNamespace.containsPage(_simpleName)) 199 { 200 if (_log.isDebugEnabled()) 201 _log.debug(ResolverMessages.foundFrameworkPage(_simpleName)); 202 203 setNamespace(_frameworkNamespace); 204 205 209 setSpecification(_frameworkNamespace.getPageSpecification(_simpleName)); 210 return; 211 } 212 } 213 214 217 IComponentSpecification specification = getDelegate().findPageSpecification( 218 cycle, 219 namespace, 220 _simpleName); 221 222 if (specification != null) 223 { 224 setSpecification(specification); 225 install(); 226 } 227 } 228 229 private void setupImplicitPage(Resource resource) 230 { 231 if (_log.isDebugEnabled()) 232 _log.debug(ResolverMessages.foundHTMLTemplate(resource)); 233 234 237 IComponentSpecification specification = new ComponentSpecification(); 238 specification.setPageSpecification(true); 239 specification.setSpecificationLocation(resource); 240 241 setSpecification(specification); 242 243 install(); 244 } 245 246 private boolean found(Resource resource) 247 { 248 if (_log.isDebugEnabled()) 249 _log.debug(ResolverMessages.checkingResource(resource)); 250 251 if (resource.getResourceURL() == null) 252 return false; 253 254 setSpecification(getSpecificationSource().getPageSpecification(resource)); 255 256 install(); 257 258 return true; 259 } 260 261 private void install() 262 { 263 INamespace namespace = getNamespace(); 264 IComponentSpecification specification = getSpecification(); 265 266 if (_log.isDebugEnabled()) 267 _log.debug(ResolverMessages.installingPage(_simpleName, namespace, specification)); 268 269 namespace.installPageSpecification(_simpleName, specification); 270 } 271 272 277 278 private String getTemplateExtension() 279 { 280 return _componentPropertySource.getNamespaceProperty( 281 getNamespace(), 282 Tapestry.TEMPLATE_EXTENSION_PROPERTY); 283 } 284 285 286 287 public void setLog(Log log) 288 { 289 _log = log; 290 } 291 292 293 public void setComponentPropertySource(ComponentPropertySource componentPropertySource) 294 { 295 _componentPropertySource = componentPropertySource; 296 } 297 } | Popular Tags |