On Mar 24, 2009, at 3:12 PM, Paul Sandoz wrote:
>
> On Mar 24, 2009, at 2:48 PM, Ian Clarke wrote:
>
>> On Tue, Mar 24, 2009 at 8:44 AM, Paul Sandoz <Paul.Sandoz_at_sun.com>
>> wrote:
>> See here for some more details on injection:
>>
>> http://wikis.sun.com/display/Jersey/Overview+of+JAX-RS+1.0+Features#OverviewofJAX-RS1.0Features-RulesofInjection
>>
>> Thanks Paul, ok - I think I understand how to retrieve the
>> ServletContext (and then retrieve objects from this using
>> ServletContext.getAttribute() ), however how do I put objects into
>> it?
>>
>> Here is how I'm setting up the server:
>>
>> final Map<String, String> initParams = new HashMap<String,
>> String>();
>>
>>
>> initParams
>> .put("com.sun.jersey.config.property.resourceConfigClass",
>> "com.sun.jersey.api.core.PackagesResourceConfig");
>> initParams.put("com.sun.jersey.config.property.packages",
>> "com.uprizer.sensearray.io.net.resources");
>>
>> log.info("Starting Sensearray Server...");
>> return GrizzlyWebContainerFactory.create(baseUri,
>> initParams);
>>
>> Given this, how do I access the ServletContext so that I can add
>> the attributes I need to it?
>>
>
> To do that independently of Jersey you can create a servlet context
> listener.
>
> For Grizzly you would need to use more of the Grizzly APIs rather
> than the helper methods provided by Jersey:
>
> https://grizzly.dev.java.net/nonav/apidocs/com/sun/grizzly/http/embed/GrizzlyWebServer.html
> https://grizzly.dev.java.net/nonav/apidocs/com/sun/grizzly/http/servlet/ServletAdapter.html
>
> GrizzlyWebServer ws = new GrizzlyWebServer(port);
> ServletAdapter sa = new ServletAdapter();
> sa.setServletInstance(ServletConfig.class.newInstance());
^
|
That should be
ServletContainer.
Paul.
> sa.addServletListener("foo.ContextLoaderListener");
> sa.addInitParameter( PackagesResourceConfig.PROPERTY_PACKAGES,
> "com.sun.jersey.spring.jerseymanaged" );
> sa.setServletPath(servletPath);
> ws.addGrizzlyAdapter(sa);
> ws.start();
>
>
> That said i think we can improve things with Jersey to get a pre-
> configured ServerAdapter you can modify.
>
>
> Alternatively you can override the Jersey ServletContainer:
>
> public class MyServlet extends ServletContainer {
>
> protected void configure(final ServletConfig sc,
> ResourceConfig rc, WebApplication wa) {
> super(sc, rc, wa);
> // ...
> }
>
> And then do:
>
> return GrizzlyWebContainerFactory.create(baseUri, MyServlet.class,
> initParams);
>
> Paul.