You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by hitesh_jain <hi...@gmail.com> on 2009/01/14 07:58:58 UTC
RE: problems getting the BackgroundEnvironment working within a
thread
Hi,
I am using cocoon 2.1.11 and facing the similar problem of "The cocoon
protocol can not be used outside an environment" while resolving with
resolver.resolveURI("cocoon://test.xml").The code of resolveURI() is inside
a new thread. I tried to create the BackgroundEnvironment on the similar
lines as below inside the run() method of thread code. I have created a
class called Transformer extending
org.apache.cocoon.transformation.AbstractTransformer. With help of this
class i am able to get context by
context = ObjectModelHelper.getContext(objectModel);
and Logger by logger = transformer.getLogger();
So I am able to create BackgroundEnvironment(logger,context). But the
problem is i am no getting the manager(ComponentManager) instance to use
resolver = (SourceResolver) manager.lookup(SourceResolver.ROLE);
Can you help me here, in getting this manager instance?
Thanks & Regards,
Hitesh
Ard Schrijvers wrote:
>
>
>>
>> Hi Ard,
>>
>> Thanks for your answer. I hope you didn't go throw to much effort to
>> give me this answer because got is running a few hours back -it was a
>> real hard one to solve-. I didn't had the time to reports it back to
>> this mailing list. I will study you solution to see if I can
>> improve my
>> own solution.
>
> I did copy-paste of something I got so don't worry :-)
>
>>
>> Thanks,
>>
>> Marcel
>>
>>
>>
>> -----Oorspronkelijk bericht-----
>> Van: Ard Schrijvers [mailto:a.schrijvers@hippo.nl]
>> Verzonden: woensdag 28 december 2005 13:25
>> Aan: users@cocoon.apache.org
>> Onderwerp: RE: problems getting the BackgroundEnvironment
>> working within
>> a thread
>>
>> Hi Marcel,
>>
>> first of all, I notice a lot of people lately having more or less the
>> same problem, including myself. I think it is way to hard in cocoon to
>> call pipelines which were initiated from some background things.
>>
>> Anyway, think you should do something like:
>>
>> Make your class Contextualizable (which you probably already did)
>>
>>
>> private org.apache.cocoon.environment.Context environmentContext;
>>
>> public void contextualize(Context ctx) throws ContextException
>> {
>> this.environmentContext =
>> (org.apache.cocoon.environment.Context)
>> ctx.get(Constants.CONTEXT_ENVIRONMENT_CONTEXT);
>>
>> }
>>
>> You will need
>>
>> import org.apache.avalon.framework.context.Context;
>> import org.apache.avalon.framework.context.ContextException;
>> import org.apache.avalon.framework.context.Contextualizable;
>>
>> and
>>
>> import org.apache.cocoon.Constants;
>>
>>
>> Now, when you want the SourceResolver, do someting like:
>>
>> BackgroundEnvironment env = null;
>> Processor processor = null;
>> Object key = null;
>>
>> try
>> {
>> env = new BackgroundEnvironment(getLogger(),
>> environmentContext);
>>
>> try {
>> processor = (Processor)
>> manager.lookup(Processor.ROLE);
>> } catch (ServiceException e) {
>> throw new CascadingRuntimeException("Couldn't look
>> up the Processor",e);
>> }
>>
>> env.startingProcessing();
>>
>> key = CocoonComponentManager.startProcessing(env);
>> CocoonComponentManager.enterEnvironment(env, new
>> WrapperComponentManager(manager), processor);
>>
>> SourceResolver resolver = null;
>> try
>> {
>>
>> resolver = (SourceResolver)
>> manager.lookup(SourceResolver.ROLE);
>>
>> Source inputSource =
>> resolver.resolveURI("cocoon://foo");
>> InputStream sourceStream =
>> inputSource.getInputStream();
>> try
>> {
>> // example what you could do with the
>> inputStream
>> DefaultHandler doHandler = new
>> DefaultHandler();
>> SourceUtil.toSAX(inputSource, doHandler);
>>
>> }
>> catch (ProcessingException e)
>> {
>> e.printStackTrace();
>> }
>> finally
>> {
>> sourceStream.close();
>> }
>> }
>> catch (ServiceException e2)
>> {
>> // TODO Auto-generated catch block
>> e2.printStackTrace();
>> }
>> catch (MalformedURLException e1)
>> {
>> e1.printStackTrace();
>> }
>> catch (IOException e1)
>> {
>> e1.printStackTrace();
>> }
>> catch (SAXException e)
>> {
>> e.printStackTrace();
>> }
>> finally
>> {
>> if(resolver!=null)
>> manager.release(resolver);
>> }
>>
>>
>> }
>> catch (MalformedURLException e)
>> {
>>
>> e.printStackTrace();
>> }
>> finally
>> {
>> CocoonComponentManager.leaveEnvironment();
>> CocoonComponentManager.endProcessing(env, key);
>> if (processor != null) {
>> manager.release(processor);
>> }
>> }
>>
>> Hope you can extract the parts you need,
>>
>> AS
>>
>>
>>
>> -----Original Message-----
>> From: Marcel Rouwenhorst [mailto:marcel.rouwenhorst@mobillion.nl]
>> Posted At: zaterdag 24 december 2005 11:11
>> Posted To: Cocoon User List
>> Conversation: problems getting the BackgroundEnvironment
>> working within
>> a thread
>> Subject: problems getting the BackgroundEnvironment working within a
>> thread
>>
>>
>> Hi,
>>
>> I'm currently working on a mail daemon that sends mail from an oracle
>> queue. (I don't want to use the cron block because it should be a
>> continues thread in de background). I created a new daemon thread with
>> the RunnableManager. This still works fine but when I want to call a
>> pipeline with the SourceResolver from within the run method I get an
>> exception that the cocoon protocol can't be used.
>>
>> I understood from some other mail in this mailing list that I should
>> make a 'fake' environment with the BackgroundEnvironment. I'm
>> trying to
>> create a new instance of this class but that fails because I need a
>> context. If I implement Contextualizable I get a Context - an Avalon
>> context - but this one is incompatible with context - a Cocoon
>> environment context - that the BackgroundEnvironment needs.
>>
>> What am I doing wrong here? Any help is appreciated!
>>
>> Thanks,
>> Marcel
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
>> For additional commands, e-mail: users-help@cocoon.apache.org
>>
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
>> For additional commands, e-mail: users-help@cocoon.apache.org
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
> For additional commands, e-mail: users-help@cocoon.apache.org
>
>
>
--
View this message in context: http://www.nabble.com/problems-getting-the-BackgroundEnvironment-working-within-a-thread-tp2083955p21450927.html
Sent from the Cocoon - Users mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org