cocoon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Leszek Gawron <lgaw...@mobilebox.pl>
Subject Re: Spring integration: accessing servlet context
Date Wed, 11 Aug 2004 18:34:37 GMT
Ugo Cei wrote:

> Leszek Gawron wrote:
> 
>> When integrating web application with Spring one needs to use 
>> org.springframework.web.context.ContextLoaderListener. This listener 
>> registers spring ApplicationContext in web application's servlet context.
>>
>> In the application you fetch spring application context via:
>>
>> org.springframework.web.context.support.WebApplicationContextUtils
>>      .getWebApplicationContext(javax.servlet.ServletContext sc)
>>
>> The problem is: how can I get access to cocoon's bare servlet context 
>> (I know that breaks the abstraction but who cares)?
>>
>> Or how can I implement this functionality without direct acces to 
>> ServletContext (WebApplicationContextUtils source):
> 
> 
> // Forgetting exception handling for the moment ...
> 
> importClass(Packages.org.springframework.web.context.WebApplicationContext); 
> 
> 
> function appCtxTest() {
>     var appCtx = 
> cocoon.context.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);

> 
>     print(appCtx);
> }
> 
> 
>     Ugo
did it like this :

> 	public WebApplicationContext fetchContext() throws ContextException {
> 		HttpContext environmentContext = (HttpContext) context.get( Constants.CONTEXT_ENVIRONMENT_CONTEXT
);
> 		Object attr = environmentContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
> 
> 		if (attr == null) {
> 			throw new IllegalStateException( "Root application context not found in servlet context"
);
> 		}
> 		if (attr instanceof RuntimeException) {
> 			throw (RuntimeException) attr;
> 		}
> 		if (attr instanceof Error) {
> 			throw (Error) attr;
> 		}
> 		if (!(attr instanceof WebApplicationContext)) {
> 			throw new IllegalStateException("Root context attribute is not of type WebApplicationContext:
" + attr );
> 		}
> 		return (WebApplicationContext) attr;
> 	}
just if anyone ever needed this ... plain simple ...

-- 
Leszek Gawron                                      lgawron@mobilebox.pl


Mime
View raw message