You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jena.apache.org by Zen 98052 <z9...@outlook.com> on 2016/06/09 19:01:29 UTC

proper way to pass app context

Hi,

I implement our own Sparql endpoint service, and I am trying to pass the session id (unique per each client request), so that it can be accessed from the code in OpExecutor implementation.

I store the session id in my graph object, the code looks something like:

MyGraph graph = createGraph(appContext);  // appContext contains session id and other application state

Model graphModel = ModelFactory.createModelForGraph(graph);


Then inside the class MyOpExecutor (extends Jena's OpExecutor class), I tried to access the session id thru this way:


protected QueryIterator execute(OpBGP opBGP, QueryIterator input) {\
    if (this.execCxt.getActiveGraph() instanceof MyGraph) {
        MyGraph g = (MyGraph)this.execCxt.getActiveGraph();

        // get the session id which can be passed to logging
        String sid = g.getAppContext().getSessionId();
        ...
    }
}



The problem I am having is the sid will be always the same sid (from the first client request), though I create a new MyGraph object for each request.

Do you know how to pass  the current graph, so that getActiveGraph() code above will get correct graph (for that session) instead of the graph from first client request?



Thanks,

Z


Re: proper way to pass app context

Posted by Andy Seaborne <an...@apache.org>.
On 15/06/16 23:19, Zen 98052 wrote:
> I don't think I can use UpdateFactory.create, because it expects a String arg (while my case is I want to pass an InputStream)

UpdateExecutionFactory.create (not UpdateFactory.create).

and

UpdateFactory.read takes a InputStream.

"read" for an indirected source (file, inputstream)
"create" for a string.

	Andy


Re: proper way to pass app context

Posted by Zen 98052 <z9...@outlook.com>.
I don't think I can use UpdateFactory.create, because it expects a String arg (while my case is I want to pass an InputStream)

________________________________
From: Andy Seaborne <an...@apache.org>
Sent: Wednesday, June 15, 2016 4:22:45 PM
To: users@jena.apache.org
Subject: Re: proper way to pass app context

On 15/06/16 13:35, Zen 98052 wrote:
> Hi Andy,
>
> I see the code parseExecute in UpdateAction, which I can copy and pass the context object.
>
> Before that, how do I get that per-request context at first place?
>
>
> For SELECT request, I am able to call QueryExecutionFactory.create to get the QueryExecution object, which has the per-request context object.
>
> I don't see there is UpdateExecution or something like that.

pass it when

UpdateExecutionFactory.
create(UpdateRequest updateRequest, Dataset dataset, Context context)

or manipulate the returned UpdateProcessor

        Andy

>
>
> Once I got the per-request context object, I want to pass it to StageBuilder.setGenerator(context, ...), and QC.setFactory(context, ...)
>
>
>
> Thanks,
>
> Z
>
> ________________________________
> From: Andy Seaborne <an...@apache.org>
> Sent: Tuesday, June 14, 2016 4:44:48 AM
> To: users@jena.apache.org
> Subject: Re: proper way to pass app context
>
> On 14/06/16 01:06, Zen 98052 wrote:
>> Hi Andy,
>>
>> How to set the custom op executor for UPDATE?
>> My code for update is:
>>
>>
>>           InputStream stream = new ByteArrayInputStream(postData.getBytes("UTF-8"));
>>           UpdateAction.parseExecute(null, graphDataset, stream, Syntax.syntaxARQ);
>>
>>
>>
>> Thanks,
>>
>> Z
>
> The class UpdateAction is a collection of convenience operations.  The
> full API is via UpdateExecutionFactory which includes
> passing in a context.
>
>      Andy
>
>


Re:

Posted by Zen 98052 <z9...@outlook.com>.
I don't think I can use UpdateFactory.create, because it expects a String arg (while my case is I want to pass an InputStream)

________________________________
From: Zen 98052
Sent: Wednesday, June 15, 2016 6:01:02 PM
To: users@jena.apache.org
Subject: Re: proper way to pass app context


Below is the Jena code that I can copy (I am doing this because UpdateAction API doesn't return the UpdateProcessor, or in this case UpdateProcessorStreaming, which what I need).

Unfortunately, UpdateFactory.setupParser is 'protected' instead of 'public'. Does it mean I have to subclass the UpdateFactory class?

Or can I simply call UpdateFactory.create? I am not sure if I need UpdateSink in my case (which is using input stream)



Thanks,

Z



    public static void parseExecute(UsingList usingList, DatasetGraph dataset, InputStream input, Binding inputBinding, String baseURI, Syntax syntax)
    {
        GraphStore graphStore = GraphStoreFactory.create(dataset);

        UpdateProcessorStreaming uProc = UpdateExecutionFactory.createStreaming(graphStore, inputBinding) ;
        if (uProc == null)
            throw new ARQException("No suitable update procesors are registered/able to execute your updates");

        uProc.startRequest();
        try
        {
            UpdateSink sink = new UsingUpdateSink(uProc.getUpdateSink(), usingList) ;
            try
            {
                UpdateParser parser = UpdateFactory.setupParser(sink.getPrologue(), baseURI, syntax) ;
                parser.parse(sink, input) ;
            }
            finally
            {
                sink.close() ;
            }
        }
        finally
        {
            uProc.finishRequest();
        }
    }


________________________________
From: Andy Seaborne <an...@apache.org>
Sent: Wednesday, June 15, 2016 4:22:45 PM
To: users@jena.apache.org
Subject: Re: proper way to pass app context

On 15/06/16 13:35, Zen 98052 wrote:
> Hi Andy,
>
> I see the code parseExecute in UpdateAction, which I can copy and pass the context object.
>
> Before that, how do I get that per-request context at first place?
>
>
> For SELECT request, I am able to call QueryExecutionFactory.create to get the QueryExecution object, which has the per-request context object.
>
> I don't see there is UpdateExecution or something like that.

pass it when

UpdateExecutionFactory.
create(UpdateRequest updateRequest, Dataset dataset, Context context)

or manipulate the returned UpdateProcessor

        Andy

>
>
> Once I got the per-request context object, I want to pass it to StageBuilder.setGenerator(context, ...), and QC.setFactory(context, ...)
>
>
>
> Thanks,
>
> Z
>
> ________________________________
> From: Andy Seaborne <an...@apache.org>
> Sent: Tuesday, June 14, 2016 4:44:48 AM
> To: users@jena.apache.org
> Subject: Re: proper way to pass app context
>
> On 14/06/16 01:06, Zen 98052 wrote:
>> Hi Andy,
>>
>> How to set the custom op executor for UPDATE?
>> My code for update is:
>>
>>
>>           InputStream stream = new ByteArrayInputStream(postData.getBytes("UTF-8"));
>>           UpdateAction.parseExecute(null, graphDataset, stream, Syntax.syntaxARQ);
>>
>>
>>
>> Thanks,
>>
>> Z
>
> The class UpdateAction is a collection of convenience operations.  The
> full API is via UpdateExecutionFactory which includes
> passing in a context.
>
>      Andy
>
>


Re: proper way to pass app context

Posted by Zen 98052 <z9...@outlook.com>.
Below is the Jena code that I can copy (I am doing this because UpdateAction API doesn't return the UpdateProcessor, or in this case UpdateProcessorStreaming, which what I need).

Unfortunately, UpdateFactory.setupParser is 'protected' instead of 'public'. Does it mean I have to subclass the UpdateFactory class?

Or can I simply call UpdateFactory.create? I am not sure if I need UpdateSink in my case (which is using input stream)



Thanks,

Z



    public static void parseExecute(UsingList usingList, DatasetGraph dataset, InputStream input, Binding inputBinding, String baseURI, Syntax syntax)
    {
        GraphStore graphStore = GraphStoreFactory.create(dataset);

        UpdateProcessorStreaming uProc = UpdateExecutionFactory.createStreaming(graphStore, inputBinding) ;
        if (uProc == null)
            throw new ARQException("No suitable update procesors are registered/able to execute your updates");

        uProc.startRequest();
        try
        {
            UpdateSink sink = new UsingUpdateSink(uProc.getUpdateSink(), usingList) ;
            try
            {
                UpdateParser parser = UpdateFactory.setupParser(sink.getPrologue(), baseURI, syntax) ;
                parser.parse(sink, input) ;
            }
            finally
            {
                sink.close() ;
            }
        }
        finally
        {
            uProc.finishRequest();
        }
    }


________________________________
From: Andy Seaborne <an...@apache.org>
Sent: Wednesday, June 15, 2016 4:22:45 PM
To: users@jena.apache.org
Subject: Re: proper way to pass app context

On 15/06/16 13:35, Zen 98052 wrote:
> Hi Andy,
>
> I see the code parseExecute in UpdateAction, which I can copy and pass the context object.
>
> Before that, how do I get that per-request context at first place?
>
>
> For SELECT request, I am able to call QueryExecutionFactory.create to get the QueryExecution object, which has the per-request context object.
>
> I don't see there is UpdateExecution or something like that.

pass it when

UpdateExecutionFactory.
create(UpdateRequest updateRequest, Dataset dataset, Context context)

or manipulate the returned UpdateProcessor

        Andy

>
>
> Once I got the per-request context object, I want to pass it to StageBuilder.setGenerator(context, ...), and QC.setFactory(context, ...)
>
>
>
> Thanks,
>
> Z
>
> ________________________________
> From: Andy Seaborne <an...@apache.org>
> Sent: Tuesday, June 14, 2016 4:44:48 AM
> To: users@jena.apache.org
> Subject: Re: proper way to pass app context
>
> On 14/06/16 01:06, Zen 98052 wrote:
>> Hi Andy,
>>
>> How to set the custom op executor for UPDATE?
>> My code for update is:
>>
>>
>>           InputStream stream = new ByteArrayInputStream(postData.getBytes("UTF-8"));
>>           UpdateAction.parseExecute(null, graphDataset, stream, Syntax.syntaxARQ);
>>
>>
>>
>> Thanks,
>>
>> Z
>
> The class UpdateAction is a collection of convenience operations.  The
> full API is via UpdateExecutionFactory which includes
> passing in a context.
>
>      Andy
>
>


Re: proper way to pass app context

Posted by Andy Seaborne <an...@apache.org>.
On 15/06/16 13:35, Zen 98052 wrote:
> Hi Andy,
>
> I see the code parseExecute in UpdateAction, which I can copy and pass the context object.
>
> Before that, how do I get that per-request context at first place?
>
>
> For SELECT request, I am able to call QueryExecutionFactory.create to get the QueryExecution object, which has the per-request context object.
>
> I don't see there is UpdateExecution or something like that.

pass it when

UpdateExecutionFactory.
create(UpdateRequest updateRequest, Dataset dataset, Context context)

or manipulate the returned UpdateProcessor

	Andy

>
>
> Once I got the per-request context object, I want to pass it to StageBuilder.setGenerator(context, ...), and QC.setFactory(context, ...)
>
>
>
> Thanks,
>
> Z
>
> ________________________________
> From: Andy Seaborne <an...@apache.org>
> Sent: Tuesday, June 14, 2016 4:44:48 AM
> To: users@jena.apache.org
> Subject: Re: proper way to pass app context
>
> On 14/06/16 01:06, Zen 98052 wrote:
>> Hi Andy,
>>
>> How to set the custom op executor for UPDATE?
>> My code for update is:
>>
>>
>>           InputStream stream = new ByteArrayInputStream(postData.getBytes("UTF-8"));
>>           UpdateAction.parseExecute(null, graphDataset, stream, Syntax.syntaxARQ);
>>
>>
>>
>> Thanks,
>>
>> Z
>
> The class UpdateAction is a collection of convenience operations.  The
> full API is via UpdateExecutionFactory which includes
> passing in a context.
>
>      Andy
>
>


Re: proper way to pass app context

Posted by Zen 98052 <z9...@outlook.com>.
Hi Andy,

I see the code parseExecute in UpdateAction, which I can copy and pass the context object.

Before that, how do I get that per-request context at first place?


For SELECT request, I am able to call QueryExecutionFactory.create to get the QueryExecution object, which has the per-request context object.

I don't see there is UpdateExecution or something like that.


Once I got the per-request context object, I want to pass it to StageBuilder.setGenerator(context, ...), and QC.setFactory(context, ...)



Thanks,

Z

________________________________
From: Andy Seaborne <an...@apache.org>
Sent: Tuesday, June 14, 2016 4:44:48 AM
To: users@jena.apache.org
Subject: Re: proper way to pass app context

On 14/06/16 01:06, Zen 98052 wrote:
> Hi Andy,
>
> How to set the custom op executor for UPDATE?
> My code for update is:
>
>
>          InputStream stream = new ByteArrayInputStream(postData.getBytes("UTF-8"));
>          UpdateAction.parseExecute(null, graphDataset, stream, Syntax.syntaxARQ);
>
>
>
> Thanks,
>
> Z

The class UpdateAction is a collection of convenience operations.  The
full API is via UpdateExecutionFactory which includes
passing in a context.

    Andy


Re: proper way to pass app context

Posted by Andy Seaborne <an...@apache.org>.
On 14/06/16 01:06, Zen 98052 wrote:
> Hi Andy,
>
> How to set the custom op executor for UPDATE?
> My code for update is:
>
>
>          InputStream stream = new ByteArrayInputStream(postData.getBytes("UTF-8"));
>          UpdateAction.parseExecute(null, graphDataset, stream, Syntax.syntaxARQ);
>
>
>
> Thanks,
>
> Z

The class UpdateAction is a collection of convenience operations.  The 
full API is via UpdateExecutionFactory which includes
passing in a context.

    Andy


Re: proper way to pass app context

Posted by Zen 98052 <z9...@outlook.com>.
Hi Andy,

How to set the custom op executor for UPDATE?
My code for update is:


        InputStream stream = new ByteArrayInputStream(postData.getBytes("UTF-8"));
        UpdateAction.parseExecute(null, graphDataset, stream, Syntax.syntaxARQ);



Thanks,

Z

________________________________
From: Andy Seaborne <an...@apache.org>
Sent: Friday, June 10, 2016 5:03:15 PM
To: users@jena.apache.org
Subject: Re: proper way to pass app context

On 10/06/16 18:52, Zen 98052 wrote:
> Hi Andy,
>
> I passed the graph object per my previous code, i.e. ctx.set(mySymbol, myGraph);
>
> When I retrieve it back in my own implementation of OpExecutor, i.e. MyGraph g = (MyGraph)this.execCxt.getContext().get(mySymbol);, it seems to me it always pick the first graph I assigned.
>
> I'd assume this.execCtx.getContext() is per request,

Yes - that's what it should be.

You can print it out and see for example the time stamp is changing.

> not global one, since I explicitly created QueryExecution and called its getContext().set(...), so it must be per request. This is bizarre.
>
>
> Thanks,
>
> Z
>
> ________________________________
> From: Andy Seaborne <an...@apache.org>
> Sent: Friday, June 10, 2016 12:02:01 PM
> To: users@jena.apache.org
> Subject: Re: proper way to pass app context
>
> On 10/06/16 16:39, Zen 98052 wrote:
>> Thanks Andy!
>>
>> Based on your suggestion, and looking at the code, it looks like I can do something like:
>>
>> // create a new symbol, then set it of current context that I want
>>
>> Symbol mySymbol = .. ;
>>
>> Context ctx = qe.getContext();
>>
>> ctx.set(mySymbol, myObjectToPass);
>>
>>
>> Is this correct?
>
> Yes.
>
>>
>>
>>
>> Thanks,
>>
>> Z
>>
>>
>> ________________________________
>> From: Andy Seaborne <an...@apache.org>
>> Sent: Friday, June 10, 2016 11:09:12 AM
>> To: users@jena.apache.org
>> Subject: Re: proper way to pass app context
>>
>> Hi there,
>>
>> The usual way to pass in information into the execution is to use the
>> Context.
>>
>> e.g. its where NOW() puts the timestampe that is returned on each call
>> inside a single query.
>>
>> There is a global context, a per dataset context and each query
>> execution has it's own Context (the accumulation from global and
>> datasets at the point the query starts).
>>
>>           Andy
>>
>>
>> On 10/06/16 14:16, Zen 98052 wrote:
>>> I figured it out, my bad. Somewhere I re-created a new QueryExecution, and passed that one (instead of the one below), hence it'll never work.
>>> ________________________________
>>> From: Zen 98052 <z9...@outlook.com>
>>> Sent: Thursday, June 9, 2016 8:13:41 PM
>>> To: users@jena.apache.org
>>> Subject: Re: proper way to pass app context
>>>
>>> I tried following:
>>>
>>>
>>> Model graphModel = ModelFactory.createModelForGraph(graph);
>>> Query query = QueryFactory.create(queryStr);
>>> QueryExecution qe = QueryExecutionFactory.create(query, graphModel);
>>>
>>>
>>> // create my custom stageGenerator and set it for per-query context
>>>
>>> ...
>>>
>>> StageBuilder.setGenerator(qe.getContext(), stageGenerator);
>>>
>>>
>>> // create my custom executor factory (this is where i pass the app context)
>>>
>>> ...
>>>
>>> QC.setFactory(qe.getContext(), opExecutorFactory);
>>>
>>>
>>> Somehow it doesn't use my custom stage generator or executor factory for per-query context, but it works for ARQ.getContext(), do you know what I missed here?
>>>
>>>
>>> Thanks,
>>> Z
>>>
>>>
>>> ________________________________
>>> From: Zen 98052 <z9...@outlook.com>
>>> Sent: Thursday, June 9, 2016 3:01:29 PM
>>> To: users@jena.apache.org
>>> Subject: proper way to pass app context
>>>
>>> Hi,
>>>
>>> I implement our own Sparql endpoint service, and I am trying to pass the session id (unique per each client request), so that it can be accessed from the code in OpExecutor implementation.
>>>
>>> I store the session id in my graph object, the code looks something like:
>>>
>>> MyGraph graph = createGraph(appContext);  // appContext contains session id and other application state
>>>
>>> Model graphModel = ModelFactory.createModelForGraph(graph);
>>>
>>>
>>> Then inside the class MyOpExecutor (extends Jena's OpExecutor class), I tried to access the session id thru this way:
>>>
>>>
>>> protected QueryIterator execute(OpBGP opBGP, QueryIterator input) {\
>>>        if (this.execCxt.getActiveGraph() instanceof MyGraph) {
>>>            MyGraph g = (MyGraph)this.execCxt.getActiveGraph();
>>>
>>>            // get the session id which can be passed to logging
>>>            String sid = g.getAppContext().getSessionId();
>>>            ...
>>>        }
>>> }
>>>
>>>
>>>
>>> The problem I am having is the sid will be always the same sid (from the first client request), though I create a new MyGraph object for each request.
>>>
>>> Do you know how to pass  the current graph, so that getActiveGraph() code above will get correct graph (for that session) instead of the graph from first client request?
>>>
>>>
>>>
>>> Thanks,
>>>
>>> Z
>>>
>>>
>>
>>
>
>


Re: proper way to pass app context

Posted by Andy Seaborne <an...@apache.org>.
On 10/06/16 18:52, Zen 98052 wrote:
> Hi Andy,
>
> I passed the graph object per my previous code, i.e. ctx.set(mySymbol, myGraph);
>
> When I retrieve it back in my own implementation of OpExecutor, i.e. MyGraph g = (MyGraph)this.execCxt.getContext().get(mySymbol);, it seems to me it always pick the first graph I assigned.
>
> I'd assume this.execCtx.getContext() is per request,

Yes - that's what it should be.

You can print it out and see for example the time stamp is changing.

> not global one, since I explicitly created QueryExecution and called its getContext().set(...), so it must be per request. This is bizarre.
>
>
> Thanks,
>
> Z
>
> ________________________________
> From: Andy Seaborne <an...@apache.org>
> Sent: Friday, June 10, 2016 12:02:01 PM
> To: users@jena.apache.org
> Subject: Re: proper way to pass app context
>
> On 10/06/16 16:39, Zen 98052 wrote:
>> Thanks Andy!
>>
>> Based on your suggestion, and looking at the code, it looks like I can do something like:
>>
>> // create a new symbol, then set it of current context that I want
>>
>> Symbol mySymbol = .. ;
>>
>> Context ctx = qe.getContext();
>>
>> ctx.set(mySymbol, myObjectToPass);
>>
>>
>> Is this correct?
>
> Yes.
>
>>
>>
>>
>> Thanks,
>>
>> Z
>>
>>
>> ________________________________
>> From: Andy Seaborne <an...@apache.org>
>> Sent: Friday, June 10, 2016 11:09:12 AM
>> To: users@jena.apache.org
>> Subject: Re: proper way to pass app context
>>
>> Hi there,
>>
>> The usual way to pass in information into the execution is to use the
>> Context.
>>
>> e.g. its where NOW() puts the timestampe that is returned on each call
>> inside a single query.
>>
>> There is a global context, a per dataset context and each query
>> execution has it's own Context (the accumulation from global and
>> datasets at the point the query starts).
>>
>>           Andy
>>
>>
>> On 10/06/16 14:16, Zen 98052 wrote:
>>> I figured it out, my bad. Somewhere I re-created a new QueryExecution, and passed that one (instead of the one below), hence it'll never work.
>>> ________________________________
>>> From: Zen 98052 <z9...@outlook.com>
>>> Sent: Thursday, June 9, 2016 8:13:41 PM
>>> To: users@jena.apache.org
>>> Subject: Re: proper way to pass app context
>>>
>>> I tried following:
>>>
>>>
>>> Model graphModel = ModelFactory.createModelForGraph(graph);
>>> Query query = QueryFactory.create(queryStr);
>>> QueryExecution qe = QueryExecutionFactory.create(query, graphModel);
>>>
>>>
>>> // create my custom stageGenerator and set it for per-query context
>>>
>>> ...
>>>
>>> StageBuilder.setGenerator(qe.getContext(), stageGenerator);
>>>
>>>
>>> // create my custom executor factory (this is where i pass the app context)
>>>
>>> ...
>>>
>>> QC.setFactory(qe.getContext(), opExecutorFactory);
>>>
>>>
>>> Somehow it doesn't use my custom stage generator or executor factory for per-query context, but it works for ARQ.getContext(), do you know what I missed here?
>>>
>>>
>>> Thanks,
>>> Z
>>>
>>>
>>> ________________________________
>>> From: Zen 98052 <z9...@outlook.com>
>>> Sent: Thursday, June 9, 2016 3:01:29 PM
>>> To: users@jena.apache.org
>>> Subject: proper way to pass app context
>>>
>>> Hi,
>>>
>>> I implement our own Sparql endpoint service, and I am trying to pass the session id (unique per each client request), so that it can be accessed from the code in OpExecutor implementation.
>>>
>>> I store the session id in my graph object, the code looks something like:
>>>
>>> MyGraph graph = createGraph(appContext);  // appContext contains session id and other application state
>>>
>>> Model graphModel = ModelFactory.createModelForGraph(graph);
>>>
>>>
>>> Then inside the class MyOpExecutor (extends Jena's OpExecutor class), I tried to access the session id thru this way:
>>>
>>>
>>> protected QueryIterator execute(OpBGP opBGP, QueryIterator input) {\
>>>        if (this.execCxt.getActiveGraph() instanceof MyGraph) {
>>>            MyGraph g = (MyGraph)this.execCxt.getActiveGraph();
>>>
>>>            // get the session id which can be passed to logging
>>>            String sid = g.getAppContext().getSessionId();
>>>            ...
>>>        }
>>> }
>>>
>>>
>>>
>>> The problem I am having is the sid will be always the same sid (from the first client request), though I create a new MyGraph object for each request.
>>>
>>> Do you know how to pass  the current graph, so that getActiveGraph() code above will get correct graph (for that session) instead of the graph from first client request?
>>>
>>>
>>>
>>> Thanks,
>>>
>>> Z
>>>
>>>
>>
>>
>
>


Re: proper way to pass app context

Posted by Zen 98052 <z9...@outlook.com>.
Hi Andy,

I passed the graph object per my previous code, i.e. ctx.set(mySymbol, myGraph);

When I retrieve it back in my own implementation of OpExecutor, i.e. MyGraph g = (MyGraph)this.execCxt.getContext().get(mySymbol);, it seems to me it always pick the first graph I assigned.

I'd assume this.execCtx.getContext() is per request, not global one, since I explicitly created QueryExecution and called its getContext().set(...), so it must be per request. This is bizarre.


Thanks,

Z

________________________________
From: Andy Seaborne <an...@apache.org>
Sent: Friday, June 10, 2016 12:02:01 PM
To: users@jena.apache.org
Subject: Re: proper way to pass app context

On 10/06/16 16:39, Zen 98052 wrote:
> Thanks Andy!
>
> Based on your suggestion, and looking at the code, it looks like I can do something like:
>
> // create a new symbol, then set it of current context that I want
>
> Symbol mySymbol = .. ;
>
> Context ctx = qe.getContext();
>
> ctx.set(mySymbol, myObjectToPass);
>
>
> Is this correct?

Yes.

>
>
>
> Thanks,
>
> Z
>
>
> ________________________________
> From: Andy Seaborne <an...@apache.org>
> Sent: Friday, June 10, 2016 11:09:12 AM
> To: users@jena.apache.org
> Subject: Re: proper way to pass app context
>
> Hi there,
>
> The usual way to pass in information into the execution is to use the
> Context.
>
> e.g. its where NOW() puts the timestampe that is returned on each call
> inside a single query.
>
> There is a global context, a per dataset context and each query
> execution has it's own Context (the accumulation from global and
> datasets at the point the query starts).
>
>          Andy
>
>
> On 10/06/16 14:16, Zen 98052 wrote:
>> I figured it out, my bad. Somewhere I re-created a new QueryExecution, and passed that one (instead of the one below), hence it'll never work.
>> ________________________________
>> From: Zen 98052 <z9...@outlook.com>
>> Sent: Thursday, June 9, 2016 8:13:41 PM
>> To: users@jena.apache.org
>> Subject: Re: proper way to pass app context
>>
>> I tried following:
>>
>>
>> Model graphModel = ModelFactory.createModelForGraph(graph);
>> Query query = QueryFactory.create(queryStr);
>> QueryExecution qe = QueryExecutionFactory.create(query, graphModel);
>>
>>
>> // create my custom stageGenerator and set it for per-query context
>>
>> ...
>>
>> StageBuilder.setGenerator(qe.getContext(), stageGenerator);
>>
>>
>> // create my custom executor factory (this is where i pass the app context)
>>
>> ...
>>
>> QC.setFactory(qe.getContext(), opExecutorFactory);
>>
>>
>> Somehow it doesn't use my custom stage generator or executor factory for per-query context, but it works for ARQ.getContext(), do you know what I missed here?
>>
>>
>> Thanks,
>> Z
>>
>>
>> ________________________________
>> From: Zen 98052 <z9...@outlook.com>
>> Sent: Thursday, June 9, 2016 3:01:29 PM
>> To: users@jena.apache.org
>> Subject: proper way to pass app context
>>
>> Hi,
>>
>> I implement our own Sparql endpoint service, and I am trying to pass the session id (unique per each client request), so that it can be accessed from the code in OpExecutor implementation.
>>
>> I store the session id in my graph object, the code looks something like:
>>
>> MyGraph graph = createGraph(appContext);  // appContext contains session id and other application state
>>
>> Model graphModel = ModelFactory.createModelForGraph(graph);
>>
>>
>> Then inside the class MyOpExecutor (extends Jena's OpExecutor class), I tried to access the session id thru this way:
>>
>>
>> protected QueryIterator execute(OpBGP opBGP, QueryIterator input) {\
>>       if (this.execCxt.getActiveGraph() instanceof MyGraph) {
>>           MyGraph g = (MyGraph)this.execCxt.getActiveGraph();
>>
>>           // get the session id which can be passed to logging
>>           String sid = g.getAppContext().getSessionId();
>>           ...
>>       }
>> }
>>
>>
>>
>> The problem I am having is the sid will be always the same sid (from the first client request), though I create a new MyGraph object for each request.
>>
>> Do you know how to pass  the current graph, so that getActiveGraph() code above will get correct graph (for that session) instead of the graph from first client request?
>>
>>
>>
>> Thanks,
>>
>> Z
>>
>>
>
>


Re: proper way to pass app context

Posted by Andy Seaborne <an...@apache.org>.
On 10/06/16 16:39, Zen 98052 wrote:
> Thanks Andy!
>
> Based on your suggestion, and looking at the code, it looks like I can do something like:
>
> // create a new symbol, then set it of current context that I want
>
> Symbol mySymbol = .. ;
>
> Context ctx = qe.getContext();
>
> ctx.set(mySymbol, myObjectToPass);
>
>
> Is this correct?

Yes.

>
>
>
> Thanks,
>
> Z
>
>
> ________________________________
> From: Andy Seaborne <an...@apache.org>
> Sent: Friday, June 10, 2016 11:09:12 AM
> To: users@jena.apache.org
> Subject: Re: proper way to pass app context
>
> Hi there,
>
> The usual way to pass in information into the execution is to use the
> Context.
>
> e.g. its where NOW() puts the timestampe that is returned on each call
> inside a single query.
>
> There is a global context, a per dataset context and each query
> execution has it's own Context (the accumulation from global and
> datasets at the point the query starts).
>
>          Andy
>
>
> On 10/06/16 14:16, Zen 98052 wrote:
>> I figured it out, my bad. Somewhere I re-created a new QueryExecution, and passed that one (instead of the one below), hence it'll never work.
>> ________________________________
>> From: Zen 98052 <z9...@outlook.com>
>> Sent: Thursday, June 9, 2016 8:13:41 PM
>> To: users@jena.apache.org
>> Subject: Re: proper way to pass app context
>>
>> I tried following:
>>
>>
>> Model graphModel = ModelFactory.createModelForGraph(graph);
>> Query query = QueryFactory.create(queryStr);
>> QueryExecution qe = QueryExecutionFactory.create(query, graphModel);
>>
>>
>> // create my custom stageGenerator and set it for per-query context
>>
>> ...
>>
>> StageBuilder.setGenerator(qe.getContext(), stageGenerator);
>>
>>
>> // create my custom executor factory (this is where i pass the app context)
>>
>> ...
>>
>> QC.setFactory(qe.getContext(), opExecutorFactory);
>>
>>
>> Somehow it doesn't use my custom stage generator or executor factory for per-query context, but it works for ARQ.getContext(), do you know what I missed here?
>>
>>
>> Thanks,
>> Z
>>
>>
>> ________________________________
>> From: Zen 98052 <z9...@outlook.com>
>> Sent: Thursday, June 9, 2016 3:01:29 PM
>> To: users@jena.apache.org
>> Subject: proper way to pass app context
>>
>> Hi,
>>
>> I implement our own Sparql endpoint service, and I am trying to pass the session id (unique per each client request), so that it can be accessed from the code in OpExecutor implementation.
>>
>> I store the session id in my graph object, the code looks something like:
>>
>> MyGraph graph = createGraph(appContext);  // appContext contains session id and other application state
>>
>> Model graphModel = ModelFactory.createModelForGraph(graph);
>>
>>
>> Then inside the class MyOpExecutor (extends Jena's OpExecutor class), I tried to access the session id thru this way:
>>
>>
>> protected QueryIterator execute(OpBGP opBGP, QueryIterator input) {\
>>       if (this.execCxt.getActiveGraph() instanceof MyGraph) {
>>           MyGraph g = (MyGraph)this.execCxt.getActiveGraph();
>>
>>           // get the session id which can be passed to logging
>>           String sid = g.getAppContext().getSessionId();
>>           ...
>>       }
>> }
>>
>>
>>
>> The problem I am having is the sid will be always the same sid (from the first client request), though I create a new MyGraph object for each request.
>>
>> Do you know how to pass  the current graph, so that getActiveGraph() code above will get correct graph (for that session) instead of the graph from first client request?
>>
>>
>>
>> Thanks,
>>
>> Z
>>
>>
>
>


Re: proper way to pass app context

Posted by Zen 98052 <z9...@outlook.com>.
Thanks Andy!

Based on your suggestion, and looking at the code, it looks like I can do something like:

// create a new symbol, then set it of current context that I want

Symbol mySymbol = .. ;

Context ctx = qe.getContext();

ctx.set(mySymbol, myObjectToPass);


Is this correct?



Thanks,

Z


________________________________
From: Andy Seaborne <an...@apache.org>
Sent: Friday, June 10, 2016 11:09:12 AM
To: users@jena.apache.org
Subject: Re: proper way to pass app context

Hi there,

The usual way to pass in information into the execution is to use the
Context.

e.g. its where NOW() puts the timestampe that is returned on each call
inside a single query.

There is a global context, a per dataset context and each query
execution has it's own Context (the accumulation from global and
datasets at the point the query starts).

        Andy


On 10/06/16 14:16, Zen 98052 wrote:
> I figured it out, my bad. Somewhere I re-created a new QueryExecution, and passed that one (instead of the one below), hence it'll never work.
> ________________________________
> From: Zen 98052 <z9...@outlook.com>
> Sent: Thursday, June 9, 2016 8:13:41 PM
> To: users@jena.apache.org
> Subject: Re: proper way to pass app context
>
> I tried following:
>
>
> Model graphModel = ModelFactory.createModelForGraph(graph);
> Query query = QueryFactory.create(queryStr);
> QueryExecution qe = QueryExecutionFactory.create(query, graphModel);
>
>
> // create my custom stageGenerator and set it for per-query context
>
> ...
>
> StageBuilder.setGenerator(qe.getContext(), stageGenerator);
>
>
> // create my custom executor factory (this is where i pass the app context)
>
> ...
>
> QC.setFactory(qe.getContext(), opExecutorFactory);
>
>
> Somehow it doesn't use my custom stage generator or executor factory for per-query context, but it works for ARQ.getContext(), do you know what I missed here?
>
>
> Thanks,
> Z
>
>
> ________________________________
> From: Zen 98052 <z9...@outlook.com>
> Sent: Thursday, June 9, 2016 3:01:29 PM
> To: users@jena.apache.org
> Subject: proper way to pass app context
>
> Hi,
>
> I implement our own Sparql endpoint service, and I am trying to pass the session id (unique per each client request), so that it can be accessed from the code in OpExecutor implementation.
>
> I store the session id in my graph object, the code looks something like:
>
> MyGraph graph = createGraph(appContext);  // appContext contains session id and other application state
>
> Model graphModel = ModelFactory.createModelForGraph(graph);
>
>
> Then inside the class MyOpExecutor (extends Jena's OpExecutor class), I tried to access the session id thru this way:
>
>
> protected QueryIterator execute(OpBGP opBGP, QueryIterator input) {\
>      if (this.execCxt.getActiveGraph() instanceof MyGraph) {
>          MyGraph g = (MyGraph)this.execCxt.getActiveGraph();
>
>          // get the session id which can be passed to logging
>          String sid = g.getAppContext().getSessionId();
>          ...
>      }
> }
>
>
>
> The problem I am having is the sid will be always the same sid (from the first client request), though I create a new MyGraph object for each request.
>
> Do you know how to pass  the current graph, so that getActiveGraph() code above will get correct graph (for that session) instead of the graph from first client request?
>
>
>
> Thanks,
>
> Z
>
>


Re: proper way to pass app context

Posted by Andy Seaborne <an...@apache.org>.
Hi there,

The usual way to pass in information into the execution is to use the 
Context.

e.g. its where NOW() puts the timestampe that is returned on each call 
inside a single query.

There is a global context, a per dataset context and each query 
execution has it's own Context (the accumulation from global and 
datasets at the point the query starts).

	Andy


On 10/06/16 14:16, Zen 98052 wrote:
> I figured it out, my bad. Somewhere I re-created a new QueryExecution, and passed that one (instead of the one below), hence it'll never work.
> ________________________________
> From: Zen 98052 <z9...@outlook.com>
> Sent: Thursday, June 9, 2016 8:13:41 PM
> To: users@jena.apache.org
> Subject: Re: proper way to pass app context
>
> I tried following:
>
>
> Model graphModel = ModelFactory.createModelForGraph(graph);
> Query query = QueryFactory.create(queryStr);
> QueryExecution qe = QueryExecutionFactory.create(query, graphModel);
>
>
> // create my custom stageGenerator and set it for per-query context
>
> ...
>
> StageBuilder.setGenerator(qe.getContext(), stageGenerator);
>
>
> // create my custom executor factory (this is where i pass the app context)
>
> ...
>
> QC.setFactory(qe.getContext(), opExecutorFactory);
>
>
> Somehow it doesn't use my custom stage generator or executor factory for per-query context, but it works for ARQ.getContext(), do you know what I missed here?
>
>
> Thanks,
> Z
>
>
> ________________________________
> From: Zen 98052 <z9...@outlook.com>
> Sent: Thursday, June 9, 2016 3:01:29 PM
> To: users@jena.apache.org
> Subject: proper way to pass app context
>
> Hi,
>
> I implement our own Sparql endpoint service, and I am trying to pass the session id (unique per each client request), so that it can be accessed from the code in OpExecutor implementation.
>
> I store the session id in my graph object, the code looks something like:
>
> MyGraph graph = createGraph(appContext);  // appContext contains session id and other application state
>
> Model graphModel = ModelFactory.createModelForGraph(graph);
>
>
> Then inside the class MyOpExecutor (extends Jena's OpExecutor class), I tried to access the session id thru this way:
>
>
> protected QueryIterator execute(OpBGP opBGP, QueryIterator input) {\
>      if (this.execCxt.getActiveGraph() instanceof MyGraph) {
>          MyGraph g = (MyGraph)this.execCxt.getActiveGraph();
>
>          // get the session id which can be passed to logging
>          String sid = g.getAppContext().getSessionId();
>          ...
>      }
> }
>
>
>
> The problem I am having is the sid will be always the same sid (from the first client request), though I create a new MyGraph object for each request.
>
> Do you know how to pass  the current graph, so that getActiveGraph() code above will get correct graph (for that session) instead of the graph from first client request?
>
>
>
> Thanks,
>
> Z
>
>


Re: proper way to pass app context

Posted by Zen 98052 <z9...@outlook.com>.
I figured it out, my bad. Somewhere I re-created a new QueryExecution, and passed that one (instead of the one below), hence it'll never work.
________________________________
From: Zen 98052 <z9...@outlook.com>
Sent: Thursday, June 9, 2016 8:13:41 PM
To: users@jena.apache.org
Subject: Re: proper way to pass app context

I tried following:


Model graphModel = ModelFactory.createModelForGraph(graph);
Query query = QueryFactory.create(queryStr);
QueryExecution qe = QueryExecutionFactory.create(query, graphModel);


// create my custom stageGenerator and set it for per-query context

...

StageBuilder.setGenerator(qe.getContext(), stageGenerator);


// create my custom executor factory (this is where i pass the app context)

...

QC.setFactory(qe.getContext(), opExecutorFactory);


Somehow it doesn't use my custom stage generator or executor factory for per-query context, but it works for ARQ.getContext(), do you know what I missed here?


Thanks,
Z


________________________________
From: Zen 98052 <z9...@outlook.com>
Sent: Thursday, June 9, 2016 3:01:29 PM
To: users@jena.apache.org
Subject: proper way to pass app context

Hi,

I implement our own Sparql endpoint service, and I am trying to pass the session id (unique per each client request), so that it can be accessed from the code in OpExecutor implementation.

I store the session id in my graph object, the code looks something like:

MyGraph graph = createGraph(appContext);  // appContext contains session id and other application state

Model graphModel = ModelFactory.createModelForGraph(graph);


Then inside the class MyOpExecutor (extends Jena's OpExecutor class), I tried to access the session id thru this way:


protected QueryIterator execute(OpBGP opBGP, QueryIterator input) {\
    if (this.execCxt.getActiveGraph() instanceof MyGraph) {
        MyGraph g = (MyGraph)this.execCxt.getActiveGraph();

        // get the session id which can be passed to logging
        String sid = g.getAppContext().getSessionId();
        ...
    }
}



The problem I am having is the sid will be always the same sid (from the first client request), though I create a new MyGraph object for each request.

Do you know how to pass  the current graph, so that getActiveGraph() code above will get correct graph (for that session) instead of the graph from first client request?



Thanks,

Z


Re: proper way to pass app context

Posted by Zen 98052 <z9...@outlook.com>.
I tried following:


Model graphModel = ModelFactory.createModelForGraph(graph);
Query query = QueryFactory.create(queryStr);
QueryExecution qe = QueryExecutionFactory.create(query, graphModel);


// create my custom stageGenerator and set it for per-query context

...

StageBuilder.setGenerator(qe.getContext(), stageGenerator);


// create my custom executor factory (this is where i pass the app context)

...

QC.setFactory(qe.getContext(), opExecutorFactory);


Somehow it doesn't use my custom stage generator or executor factory for per-query context, but it works for ARQ.getContext(), do you know what I missed here?


Thanks,
Z


________________________________
From: Zen 98052 <z9...@outlook.com>
Sent: Thursday, June 9, 2016 3:01:29 PM
To: users@jena.apache.org
Subject: proper way to pass app context

Hi,

I implement our own Sparql endpoint service, and I am trying to pass the session id (unique per each client request), so that it can be accessed from the code in OpExecutor implementation.

I store the session id in my graph object, the code looks something like:

MyGraph graph = createGraph(appContext);  // appContext contains session id and other application state

Model graphModel = ModelFactory.createModelForGraph(graph);


Then inside the class MyOpExecutor (extends Jena's OpExecutor class), I tried to access the session id thru this way:


protected QueryIterator execute(OpBGP opBGP, QueryIterator input) {\
    if (this.execCxt.getActiveGraph() instanceof MyGraph) {
        MyGraph g = (MyGraph)this.execCxt.getActiveGraph();

        // get the session id which can be passed to logging
        String sid = g.getAppContext().getSessionId();
        ...
    }
}



The problem I am having is the sid will be always the same sid (from the first client request), though I create a new MyGraph object for each request.

Do you know how to pass  the current graph, so that getActiveGraph() code above will get correct graph (for that session) instead of the graph from first client request?



Thanks,

Z