Yoni Keren
Yoni Keren

Reputation: 1212

How do I run a web app code LOCALLY?

I've written a RESTful web api for my client (android app) to use. I need to implement another application, a local desktop application, which accesses Databases classes which are already implemented for the server side.

I don't know how to.

Specifically, this class for a server side:

public Database(String TableName, String KeyName) throws SQLException, NamingException {
  mTableName = TableName;
  mKeyName = KeyName;

  InitialContext ctx = new InitialContext();
  DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/nasdaq_companies");
}

Right now there is an exception thrown (NoInitialContextException) which makes perfect sense because I am missing context.xml or specifically within it this resource is missing

<Resource name="jdbc/nasdaq_companies" auth="Container"
          type="javax.sql.DataSource" driverClassName="com.mysql.jdbc.Driver"
          url="jdbc:mysql://localhost:3306/nasdaq_companies"
          username="*****" password="*****" maxActive="20" maxIdle="10" maxWait="-1"/>

Assuming that the server == localhost, How do I add a context resource to a local java application? (Or otherwise, how do i fix this problem?)

Upvotes: 0

Views: 103

Answers (1)

chehh984
chehh984

Reputation: 121

If you are talking about running something like a console app I solved it by setting up context manually by calling method in Main that looked something like this:

    public static void setupInitialContext() {
            try {
                NamingManager.setInitialContextFactoryBuilder(new InitialContextFactoryBuilder() {

                    @Override
                    public InitialContextFactory createInitialContextFactory(Hashtable<?, ?> environment) throws NamingException {
                        return new InitialContextFactory() {

                            @Override
                            public Context getInitialContext(Hashtable<?, ?> environment) throws NamingException {
                                return new InitialContext() {

                                    private Hashtable<String, DataSource> dataSources = new Hashtable<>();

                                    @Override
                                    public Object lookup(String name) throws NamingException {

                                        if (dataSources.isEmpty()) { //init datasources
                                            MysqlConnectionPoolDataSource ds = new MysqlConnectionPoolDataSource();
                                            ds.setURL("jdbc:mysql://localhost:3306/SomeDB");
                                            ds.setUser("dbuser");
                                            ds.setPassword("dbpass");
                                            dataSources.put("jdbc/SomeDataSourceName", ds); 
    //lets add another datasource (optional)
                                            MysqlConnectionPoolDataSource ds = new MysqlConnectionPoolDataSource();
                                            ds.setURL("jdbc:mysql://localhost:3306/OtherDB");
                                            ds.setUser("dbuser");
                                            ds.setPassword("dbpass");
                                            dataSources.put("jdbc/OtherDataSourceName", ds);    
//you can keep adding these as you need                                         
                                        }

                                        if (dataSources.containsKey(name)) {
                                            return dataSources.get(name);
                                        }

                                        return null; //or throw exception Java style :)
                                    }
                                };
                            }

                        };
                    }

                });
            }
            catch (NamingException ne) {

            }
        }

EDIT:

You use this method by calling it first in your Main and after that getting those datasources should work

public static void main( String[] args ) {

        setupInitialContext();

        Context initContext = new InitialContext();        
        DataSource dataSource = (DataSource) initContext.lookup("jdbc/SomeDB");

}

PS: i think i found this way on StackOverflow as well but honestly can't remember where :(

Upvotes: 1

Related Questions