I've read Jerome's message from earlier this year about using Context
attributes to share application state among various Restlet components
(http://osdir.com/ml/java.restlet/2007-01/msg00121.html) but I can't
seem to get it to work. Here is the pieces of code -- first the main()
program:
public static void main(String[] args) {
// Create a new Restlet container and add a HTTP server connector
// to it
Component component = new Component();
component.getServers().add(Protocol.HTTP, 8182);
// Save away the URI prefix
Map<String, Object> restletAttributes =
component.getContext().getAttributes();
restletAttributes.put(MyApp.URI_KEY, arg[0]);
// Create a new application and attach the component
MyApp application = new MyApp(component.getContext());
component.getDefaultHost().attach("/base", application);
// Now, let's start the container!
try {
component.start();
} catch (Exception e) {
e.printStackTrace();
}
}
"MyApp.URI_KEY" is a string constant to be the key of the value I'm
trying to pass around. I'm setting up the Restlet to run either as a
Java Application or in a Servlet container, so I've put the Router
initialization pieces in MyApp.createRoot():
@Override
public Restlet createRoot() {
Router router = new Router();
Restlet getXml = new GetXml(getContext());
router.attach("/getXml/{NS}:{OBJECTID}", getXml);
Restlet getFullDisplay = new GetFullDisplay(getContext());
router.attach("/getFullDisplay/{NS}:{OBJECTID}", getFullDisplay);
Restlet trace = new Trace(getContext());
router.attach("/trace", trace);
return router;
}
For the sake of this question, I'm pretty sure you can ignore "NS" and
"OBJECTID". Now here is the start of getFullDisplay.handle():
public void handle(Request request, Response response) {
StringBuilder pid = new StringBuilder();
pid.append(request.getAttributes().get("NS"));
pid.append(":");
pid.append(request.getAttributes().get("OBJECTID"));
// Get the URI prefix
String baseUri = (String)
getContext().getAttributes().get(MyApp.URI_KEY);
At this point in the execution, baseUri is null. By all accounts,
though, it should be the value of arg[0] passed in from the command line
(or alternatively set as parameter in 'web.xml', but I haven't gotten
that far yet in the code). Interestingly, I do get the correct values
passed in for NS and OBJECTID.
Any pointers on what I might be missing?
Peter
--
Peter Murray http://www.pandc.org/peter/work/
Assistant Director, New Service Development tel:+1-614-728-3600;ext=338
OhioLINK: the Ohio Library and Information Network Columbus, Ohio
The Disruptive Library Technology Jester http://dltj.org/
Attrib-Noncomm-Share http://creativecommons.org/licenses/by-nc-sa/2.5/