Grzegorz Kossakowski pisze:
>
> There is a subtle problem I discovered few days ago but I haven't reported up
> to date because I
> hadn't enough free time to confirm if this problem really exist. Anyway, I
> think that
> cocoon-servlet-service-impl is not really Cocoon-independent because of this
> line:
>
> resolver = (SourceResolver) factory.getBean(SourceResolver.ROLE);
>
> of getResource() method from ServletServiceContext class. The problem I can
> see here is that if
> cocoon-servlet-service-impl is used outside the Cocoon there is no default
> implementation of
> SourceResolver registered as a Spring bean. It's not a big deal, because
> Excalibur provides such
> implementation but we would need to figure out how to register and properly
> set up this component.
>
> Actually, I see whole getResource() method flawed because it calculates
> contextPath when used first
> time and I think this code should belong to the setup (not runtime) phase of
> ServletServiceContext.
> I already tried to move this code (I paste the patch at the end of this
> e-mail) but stumbled across
> some NPEs for specific servlet bean configurations and I haven't had enough
> free time to dig into
> this.
Of course forgot to paste a patch:
diff --git
a/core/cocoon-servlet-service/cocoon-servlet-service-impl/src/main/java/org/apache/cocoon/servletservice/ServletServiceContext.java
b/core/cocoon-servlet-service/cocoon-servlet-service-impl/src/main/java/org/apache/cocoon/servletservice/ServletServiceContext.java
index ca07fbd..eabc5ce 100644
---
a/core/cocoon-servlet-service/cocoon-servlet-service-impl/src/main/java/org/apache/cocoon/servletservice/ServletServiceContext.java
+++
b/core/cocoon-servlet-service/cocoon-servlet-service-impl/src/main/java/org/apache/cocoon/servletservice/ServletServiceContext.java
@@ -102,36 +102,11 @@ public class ServletServiceContext extends
ServletContextWrapper implements Abso
}
public URL getResource(String path) throws MalformedURLException {
- // hack for getting a file protocol or other protocols that can be
used as context
- // path in the getResource method in the servlet context
- if (!(contextPath.startsWith("file:") || contextPath.startsWith("/")
- || contextPath.indexOf(':') == -1)) {
- SourceResolver resolver = null;
- Source source = null;
- try {
- BeanFactory factory =
WebApplicationContextUtils.getRequiredWebApplicationContext(this);
- resolver = (SourceResolver)
factory.getBean(SourceResolver.ROLE);
- source = resolver.resolveURI(contextPath);
- contextPath = source.getURI();
- } catch (IOException e) {
- throw new MalformedURLException("Could not resolve " +
contextPath + " due to " + e);
- } finally {
- if (resolver != null) {
- resolver.release(source);
- }
- }
- }
-
// HACK: allow file:/ URLs for reloading of sitemaps during development
if (this.contextPath.startsWith("file:")) {
return new URL("file", null,
this.contextPath.substring("file:".length()) + path);
}
- if (this.contextPath.length() != 0 && this.contextPath.charAt(0) !=
'/') {
- throw new MalformedURLException("The contextPath must be empty or
start with '/' " +
- this.contextPath);
- }
-
// prefix the path with the servlet context resolve and resolve in the
embedding
// servlet context
return super.getResource(this.contextPath + path);
diff --git
a/core/cocoon-servlet-service/cocoon-servlet-service-impl/src/main/java/org/apache/cocoon/servletservice/spring/ServletFactoryBean.java
b/core/cocoon-servlet-service/cocoon-servlet-service-impl/src/main/java/org/apache/cocoon/servletservice/spring/ServletFactoryBean.java
index 74615b1..eb6e099 100644
---
a/core/cocoon-servlet-service/cocoon-servlet-service-impl/src/main/java/org/apache/cocoon/servletservice/spring/ServletFactoryBean.java
+++
b/core/cocoon-servlet-service/cocoon-servlet-service-impl/src/main/java/org/apache/cocoon/servletservice/spring/ServletFactoryBean.java
@@ -16,6 +16,8 @@
*/
package org.apache.cocoon.servletservice.spring;
+import java.io.IOException;
+import java.net.MalformedURLException;
import java.util.Enumeration;
import java.util.Map;
@@ -30,6 +32,8 @@ import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.cocoon.servletservice.Mountable;
import org.apache.cocoon.servletservice.ServletServiceContext;
+import org.apache.excalibur.source.Source;
+import org.apache.excalibur.source.SourceResolver;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.DefaultIntroductionAdvisor;
import org.springframework.aop.support.DelegatingIntroductionInterceptor;
@@ -76,7 +80,6 @@ public class ServletFactoryBean implements FactoryBean,
ApplicationContextAware,
this.servletServiceContext.setServletContext(this.servletContext);
this.servletServiceContext.setMountPath(this.mountPath);
- this.servletServiceContext.setContextPath(this.contextPath);
this.servletServiceContext.setInitParams(this.initParams);
this.servletServiceContext.setAttributes(this.contextParams);
@@ -90,6 +93,39 @@ public class ServletFactoryBean implements FactoryBean,
ApplicationContextAware,
if (this.parentContainer == null) {
this.parentContainer =
WebApplicationContextUtils.getRequiredWebApplicationContext(this.servletContext);
}
+
+ String contextPath = this.contextPath;
+
+ //FIXME: I'm not sure if there is any better place for this code (GK)
+ //-----------------------------------------------------
+ // hack for getting a file protocol or other protocols that can be
used as context
+ // path in the getResource method in the servlet context
+ int tmp = contextPath.indexOf(':');
+ boolean tmp2 = !(contextPath.startsWith("file:") ||
contextPath.startsWith("/") ||
contextPath.indexOf(':') == -1);
+ if (!(contextPath.startsWith("file:") || contextPath.startsWith("/") ||
contextPath.indexOf(':') == -1)) {
+ SourceResolver resolver = null;
+ Source source = null;
+ try {
+ resolver = (SourceResolver)
parentContainer.getBean(SourceResolver.ROLE);
+ source = resolver.resolveURI(contextPath);
+ contextPath = source.getURI();
+ } catch (IOException e) {
+ throw new MalformedURLException("Could not resolve " +
contextPath + " due to " + e);
+ } finally {
+ if (resolver != null) {
+ resolver.release(source);
+ }
+ }
+ }
+ //----------------------------------------------------
+
+
+ if (contextPath.length() != 0 && contextPath.charAt(0) != '/' &&
!contextPath.startsWith("file:")) {
+ throw new MalformedURLException("The contextPath must be empty or
start with '/' " +
+ contextPath);
+ }
+
+ this.servletServiceContext.setContextPath(contextPath);
GenericWebApplicationContext container = new
GenericWebApplicationContext();
container.setParent(this.parentContainer);
--
Best regards,
Grzegorz Kossakowski