You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cocoon.apache.org by Thorsten Scherler <sc...@gmail.com> on 2012/09/18 23:57:29 UTC

BlockContextURLConnection is not context safe

Hi all,

I am working on COCOON3-105 whenever I find a sec, but I am thinking 
about the underlying problem for a while now.

To resume, the blockcontext:/ is being registered as an extension to 
java.net.URLConnection. In case of a not-known protocol we will create 
the StreamHandler (who is responsible to open the urlConnection) once - 
for the whole container!

This happens in the BlockContextURLStreamHandlerFactory in the method 
createURLStreamHandler(String protocol). Like said: only once (!) we 
call this method even if we have two different servlets using different 
blockcontext. This finally leads to that the BlockContextURLConnection 
only knows the blockcontext of the first requested blockcontext and 
ignores any others.

Some possible solution I see:
- move the resolving of the blockcontext into the 
BlockContextURLConnection. I tried that in the patch I attached to the 
issue but the problem is that the second request has no ServletContext 
associated to it so we cannot get the blocks. -> since we cannot resolve 
here the blockcontext this solution does not work
- use a global blockcontext map. A reference to a global blockcontext 
map will be kept and this map needs to be merged when a new app is 
deployed (if needed). However that leads to many open issue like 
security, version, etc.

Further how do you undeploy the protocol since undeploying the 
underlying servlet would need to undeploy only a part of the 
blockcontext protocol.

I am not sure how to overcome above obstacles and would love any 
thoughts from the community.

salu2

-- 
Thorsten Scherler <scherler.at.gmail.com>
codeBusters S.L. - web based systems
<consulting, training and solutions>

http://www.codebusters.es/


Re: BlockContextURLConnection is not context safe

Posted by Francesco Chicchiriccò <il...@apache.org>.
On 19/09/2012 18:30, Reinhard Pötz wrote:
> On 09/19/2012 03:17 PM, Francesco Chicchiriccò wrote:
>> Anyway, what if we try to use cocoon-jnet [2] for handling
>> blockcontext:// ? AFAIK it is only used for servlet:// ATM.
>
> JNet was my first thought too ...

I am actually having some troubles in understanding how JNet works.

I've made some modifications to cocoon-block-deployment trunk, basically:
 * removed [1] so that BlockContextURLStreamHandlerFactory does not get
automatically installed
 * modified BlockContextURLStreamHandlerFactory [2] so that blockContext
map is not got through Spring but provided via constructor
 * modified BlockDeploymentServletContextListener [3] to use JNet in the
following way, in contextInitialized(), after getting the blockContext map:

 DynamicURLStreamHandlerFactory.push(new
BlockContextURLStreamHandlerFactory(this.blockContexts));

For my understanding of JNet, this should mean that, since
DynamicURLStreamHandlerFactory is already installed for webapps by
org.apache.cocoon.jnet.URLHandlerFactoryCollector (for servlet://),
there is a ThreadLocal list of URLStreamHandlerFactory containing the
BlockContextURLStreamHandlerFactory instantiated by
BlockDeploymentServletContextListener as above.

Hence - always for my understanding - every thread should have its own
list of URLStreamHandlerFactor; as a result, two separate C3 webapps in
the same container should resolve blockcontext:// URLs according to two
different and separate blockContext maps.

...but this has proven to be failing :-)

Any thought?

[1]
https://svn.apache.org/repos/asf/cocoon/subprojects/cocoon-block-deployment/trunk/src/main/resources/META-INF/cocoon/spring/cocoon-blockdeployment-protocol.xml
[2]
https://svn.apache.org/repos/asf/cocoon/subprojects/cocoon-block-deployment/trunk/src/main/java/org/apache/cocoon/blockdeployment/BlockContextURLStreamHandlerFactory.java
[3]
https://svn.apache.org/repos/asf/cocoon/subprojects/cocoon-block-deployment/trunk/src/main/java/org/apache/cocoon/blockdeployment/BlockDeploymentServletContextListener.java

-- 
Francesco Chicchiriccò

ASF Member, Apache Cocoon PMC and Apache Syncope PPMC Member
http://people.apache.org/~ilgrosso/


Re: BlockContextURLConnection is not context safe

Posted by Reinhard Pötz <re...@apache.org>.
On 09/19/2012 03:17 PM, Francesco Chicchiriccò wrote:
> Anyway, what if we try to use cocoon-jnet [2] for handling
> blockcontext:// ? AFAIK it is only used for servlet:// ATM.

JNet was my first thought too ...

-- 
Reinhard Pötz         Founder & Managing Director, Indoqa and Deepsearch
                         http://www.indoqa.com/people/reinhard-poetz.html

Member of the Apache Software Foundation
Apache Cocoon Committer, PMC member                  reinhard@apache.org
________________________________________________________________________

       Furthermore, I think Oracle has to honor the JSPA agreement.
     http://s.apache.org/JCPIsDead       http://s.apache.org/tck-trap

Re: BlockContextURLConnection is not context safe

Posted by Francesco Chicchiriccò <il...@apache.org>.
On 19/09/2012 09:14, Francesco Chicchiriccò wrote:
> [...]
> Indeed, I think this is the route to follow, i.e. move the 
> blockContexts Map in a place bound to app scope, not to JVM scope 
> (like as BlockContextURLStreamHandlerFactory).
>
> What if you get to the servletContext not via
>
> ServletContext servletContext = 
> CallStackHelper.getCurrentServletContext();
>
> but instead, using an approach like as [1]:
>
>         WebApplicationContext webApplicationContext = 
> (WebApplicationContext) AppContext.getApplicationContext();
>         ServletContext servletContext = 
> webApplicationContext.getServletContext();
>         this.blockContexts = (Map<String, String>) servletContext
> .getAttribute(BlockDeploymentServletContextListener.BLOCK_CONTEXT_MAP);
>
> i.e. closer to the current implementation inside 
> BlockContextURLStreamHandlerFactory?
>
> As soon as I have enough spare time for this, I'll try to play anyway 
> with this issue.

FYI,
this approach is not working either, because since 
BlockContextURLStreamHandlerFactory is executed once per JVM, the 
AppContext is stuck with that execution either. No way.

Anyway, what if we try to use cocoon-jnet [2] for handling 
blockcontext:// ? AFAIK it is only used for servlet:// ATM.

Regards.

> [1] 
> http://blog.jdevelop.eu/2008/07/06/access-the-spring-applicationcontext-from-everywhere-in-your-application/
[2] http://cocoon.apache.org/subprojects/jnet/

-- 
Francesco Chicchiriccò

ASF Member, Apache Cocoon PMC and Apache Syncope PPMC Member
http://people.apache.org/~ilgrosso/


Re: BlockContextURLConnection is not context safe

Posted by Francesco Chicchiriccò <il...@apache.org>.
On 18/09/2012 23:57, Thorsten Scherler wrote:
> Hi all,
>
> I am working on COCOON3-105 whenever I find a sec, but I am thinking 
> about the underlying problem for a while now.

Hi Thorsten,
I don't know how much useful can be my contribution in this field, 
anyway here are my 2 cents.

> To resume, the blockcontext:/ is being registered as an extension to 
> java.net.URLConnection. In case of a not-known protocol we will create 
> the StreamHandler (who is responsible to open the urlConnection) once 
> - for the whole container!

Ok, this is clear: purpose of URLStreamHandlerFactory is to register the 
handler for all URLs across the JVM, hence the "whole container" as you say.

> This happens in the BlockContextURLStreamHandlerFactory in the method 
> createURLStreamHandler(String protocol). Like said: only once (!) we 
> call this method even if we have two different servlets using 
> different blockcontext. This finally leads to that the 
> BlockContextURLConnection only knows the blockcontext of the first 
> requested blockcontext and ignores any others.
>
> Some possible solution I see:
> - move the resolving of the blockcontext into the 
> BlockContextURLConnection. I tried that in the patch I attached to the 
> issue but the problem is that the second request has no ServletContext 
> associated to it so we cannot get the blocks. -> since we cannot 
> resolve here the blockcontext this solution does not work

Indeed, I think this is the route to follow, i.e. move the blockContexts 
Map in a place bound to app scope, not to JVM scope (like as 
BlockContextURLStreamHandlerFactory).

What if you get to the servletContext not via

ServletContext servletContext = CallStackHelper.getCurrentServletContext();

but instead, using an approach like as [1]:

         WebApplicationContext webApplicationContext = 
(WebApplicationContext) AppContext.getApplicationContext();
         ServletContext servletContext = 
webApplicationContext.getServletContext();
         this.blockContexts = (Map<String, String>) servletContext
.getAttribute(BlockDeploymentServletContextListener.BLOCK_CONTEXT_MAP);

i.e. closer to the current implementation inside 
BlockContextURLStreamHandlerFactory?

As soon as I have enough spare time for this, I'll try to play anyway 
with this issue.

> - use a global blockcontext map. A reference to a global blockcontext 
> map will be kept and this map needs to be merged when a new app is 
> deployed (if needed). However that leads to many open issue like 
> security, version, etc.

I don't see this as viable option anyway.

> Further how do you undeploy the protocol since undeploying the 
> underlying servlet would need to undeploy only a part of the 
> blockcontext protocol.
>
> I am not sure how to overcome above obstacles and would love any 
> thoughts from the community.

Regards.

[1] 
http://blog.jdevelop.eu/2008/07/06/access-the-spring-applicationcontext-from-everywhere-in-your-application/

-- 
Francesco Chicchiriccò

ASF Member, Apache Cocoon PMC and Apache Syncope PPMC Member
http://people.apache.org/~ilgrosso/