>From the Connection, I can search Jetty Server for the right servlet
context to find state set by the servlet (see code below). But this seems
a bit fragile and would appreciate it if anybody knows a cleaner solution.
Thanks,
Josh
Set from servlet:
*protected* *void* doGet(HttpServletRequest request, HttpServletResponse
response) *throws* ServletException, IOException {
ServletContext s = *this*.getServletContext();
s.setAttribute("foo", "foo value");
...
}
Set from Connection:
@Override
*public* Connection newConnection(Connector connector, EndPoint ep) {
ServerConnector sc = (ServerConnector)connector;
System.*out*.println(findAttribute(sc.getServer(), "foo"));
....
}
*private* Object findAttribute(Server server, String name) {
Deque<Handler> handlers = *new* ArrayDeque<Handler>();
addHandlers(handlers, server.getHandlers());
*while* (!handlers.isEmpty()) {
Handler h = handlers.pop();
*if* (h *instanceof* HandlerContainer) {
addHandlers(handlers, ((HandlerContainer)h).getChildHandlers());
}
*if* (!(h *instanceof* WebAppContext)) {
*continue*;
}
WebAppContext ctx = (WebAppContext)h;
Object o = ctx.getServletContext().getAttribute(name);
*if* (o != *null*) {
*return* o;
}
}
*return* *null*;
}
*private* *void* addHandlers(Deque<Handler> deque, Handler[] handlers) {
*for* (Handler h : handlers) {
deque.add(h);
}
}
On Mon, Jan 20, 2020 at 7:52 AM Josh Spiegel <[email protected]> wrote:
> I have the ConnectionFactory and the Connection implementations.
> I have the Servlet (deployed in a war file)
>
> The ConnectionFactory/Connection are deployed using a Jetty module/xml
> with start.jar.
> The servlet is deployed by dropping the war file in webapps.
>
> I have some dynamic state (mainly database connection pools) that I would
> like to share between the ConnectionFactory and the Servlet.
>
> I can set the state object as a context attribute from the servlet.
> However, then it isn't clear to me how I would retrieve it from either the
> ConnectionFactory or Connection.
>
> Thanks,
> Josh
>
>
>
>
>
>
>
> On Mon, Jan 20, 2020 at 1:29 AM Simone Bordet <[email protected]> wrote:
>
>> Hi,
>>
>> On Mon, Jan 20, 2020 at 12:36 AM Josh Spiegel <[email protected]>
>> wrote:
>> >
>> > Hi,
>> >
>> > What is the cleanest way for a servlet (e.g. in a war file under
>> webapps) to communicate state with a custom ConnectionFactory running in
>> the same server?
>>
>> A custom ConnectionFactory typically creates Connections that parse
>> bytes that have been read from the network.
>> If that's the case, then your servlet should open a socket and write
>> the custom protocol bytes so that they can be read and parsed by the
>> custom Connection - basically you have to write a client for your
>> custom protocol.
>>
>> > Is there is some way to set Server attributes from the servlet or
>> something like that?
>>
>> You need to detail what you want to do here, and consider the
>> lifetimes of the objects you want to deal with.
>> You do not want that a second request to the servlet overwrites the
>> attribute set by the first request.
>>
>> > A static variable would probably work for me but I am hoping for
>> something that is more decoupled and scoped at the Server level.
>>
>> I doubt a static will work - would not be overwritten for every request?
>>
>> Can you detail the problem first, rather than the solution?
>>
>> --
>> Simone Bordet
>> ----
>> http://cometd.org
>> http://webtide.com
>> Developer advice, training, services and support
>> from the Jetty & CometD experts.
>> _______________________________________________
>> jetty-users mailing list
>> [email protected]
>> To change your delivery options, retrieve your password, or unsubscribe
>> from this list, visit
>> https://www.eclipse.org/mailman/listinfo/jetty-users
>>
>
_______________________________________________
jetty-users mailing list
[email protected]
To change your delivery options, retrieve your password, or unsubscribe from
this list, visit
https://www.eclipse.org/mailman/listinfo/jetty-users