Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jetty-users] Help with datasource configuration in main class

Hi Troy,

I guess you've already seen the doco page here:

 Basically, anything you can do in xml in jetty you can do in code. The Resource definition you've got above will appear in the namespace visible across the jvm. That should be fine for your purposes. If you want to restrict it to only being visible to a particular Server or WebAppContext instance, then you need provide that object as the first argument to the constructor.  In order to do that, unless you're constructing the WebAppContext programmatically too, then you'll need to define your datasource back in the jetty-env.xml file. So you'll need to store the value of the dbUri from the code above somewhere it can be referenced by the jetty-env.xml. Some alternatives are: put it as an attribute on the Server instance and then retrieve it in the jetty-env.xml file using the "server" Ref;  or put it into a System property; or put it into some object that is visible both external to the webapp and internal to the webapp.


On 11 January 2013 11:53, Troy Sellers <troy.sellers@xxxxxxxxx> wrote:

I am trying to run embedded Jetty for Heroku deployment. Have configured a datasource using jetty-env.xml in the web app but I want to apply database url from environment string stored in my heroku app environment. 

I am trying this in my Main class .. but I am not sure if this is correct or how I apply this Resource object to the WebAppContext? 

//configure database properties

        URI dbUri = new URI(System.getenv("DATABASE_URL"));

        String username = dbUri.getUserInfo().split(":")[0];

        String password = dbUri.getUserInfo().split(":")[1];

        String dbUrl = "jdbc:postgresql://" + dbUri.getHost() + ':' + dbUri.getPort() + "/" + dbUri.getPath();

        "DBURI ["+dbUri+"]");"Username ["+username+"]");"Password ["+password+"]");"Host ["+dbUri.getHost()+"]");"Port ["+dbUri.getPort()+"]");"Path ["+dbUri.getPath()+"]");

        "DBRUL ["+dbUrl+"]");


        PGSimpleDataSource pgDS = new PGSimpleDataSource();







        Resource resource = new Resource("jdbc/obmDS", pgDS);

"A computer lets you make more mistakes faster than any other invention in human history, with the possible exceptions of handguns and tequila."
- Mitch Ratcliffe

jetty-users mailing list

Jan Bartel <janb@xxxxxxxxxxx> – Developer advice, services and support
from the Jetty & CometD experts.

Back to the top