Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading properties file in a Java Jersey RESTful web app, to persist throughout the app?

I'm currently building a RESTful API using Jersey. So far, all has been going well, however, all of the configuration entries have been hard coded in. (i.e. Database Host, Database Username, etc...).

I'd like to be able to setup a config.properties file that exists in my WEB-INF folder to contain all of these configuration specs.

I'm concerned that if I do it the "classic" way of reading the file on the Classpath, I'm performing file I/O for every request. I want to be able to read once on startup (which I know involves a ServletListener in my web.xml file.

Here's what I have below:

web.xml:

<listener>
    <listener-class>com._1834Software.Config</listener-class>
</listener>

I'd like to do something like this (which I found here on StackOverflow) but I don't think it works necessarily with Jersey:

Config.java

public class Config implements ServletContextListener {
    private static final String ATTRIBUTE_NAME = "config";
    private Properties config = new Properties();

    @Override
    public void contextInitialized(ServletContextEvent event) {
        try {

            config.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("config.properties"));

        } catch (IOException err) {
            err.printStackTrace();
        }

        event.getServletContext().setAttribute(ATTRIBUTE_NAME, this);

    }

    @Override
    public void contextDestroyed(ServletContextEvent event) { /**/ }

    public static Config getInstance(ServletContext context) {
        return (Config) context.getAttribute(ATTRIBUTE_NAME);
    }

    public String getProperty(String key) {
        return config.getProperty(key);
    }

}

I try to call it like such:

Config config = Config.getInstance(getServletContext());
String property = config.getProperty("HEROKU_DATABASE_URL");

But I get the following error:

Error:(32, 40) java: cannot find symbol
symbol:   method getServletContext()
location: class com._1834Software.database.DatabaseHandler

And here is the file (DatabaseHandler.java where I'm trying to call it):

public class DatabaseHandler {
    public Connection connection = null;

    Config config = Config.getInstance(getServletContext());
    String property = config.getProperty("somekey");


    /* Database Parameters */
    private String DRIVER = "org.postgresql.Driver";
    private String host = "XXXXX";
    private String userName = "XXXXX";
    private String password = "XXXXX";

    public void connect() throws SQLException {
        try {

            Class.forName(DRIVER);

        } catch (ClassNotFoundException err) {

            err.printStackTrace();   

        }

        try {

            connection = DriverManager.getConnection(host, userName, password);

        } catch (SQLException err) {
            err.printStackTrace();
        }
    }

    public void disconnect() throws SQLException { connection.close(); }
}
like image 928
carbon_ghost Avatar asked Dec 25 '22 21:12

carbon_ghost


1 Answers

There are many ways to load properties files. To avoid introducing any new dependencies on your project, here are some code snippets that may help you. This is just one approach...

  1. Define your properties file. I put mine in src/main/resources/ as "config.properties"

    sample.property=i am a sample property
    
  2. In your jersey config file (assuming you are using class extending Application), you can load the properties file there and it will only be loaded once during the application initialization to avoid your concern of doing the File I/O over and over:

    import java.io.IOException;
    import java.io.InputStream;
    import java.util.HashSet;
    import java.util.Properties;
    import java.util.Set;
    
    import javax.ws.rs.ApplicationPath;
    import javax.ws.rs.core.Application;
    
    @ApplicationPath("sample")
    public class JerseyConfig extends Application {
    
    public static final String PROPERTIES_FILE = "config.properties";
    public static Properties properties = new Properties();
    
    private Properties readProperties() {
        InputStream inputStream = getClass().getClassLoader().getResourceAsStream(PROPERTIES_FILE);
        if (inputStream != null) {
            try {
                properties.load(inputStream);
            } catch (IOException e) {
                // TODO Add your custom fail-over code here
                e.printStackTrace();
            }
        }
        return properties;
    }
    
    @Override
    public Set<Class<?>> getClasses() {     
        // Read the properties file
        readProperties();
    
        // Set up your Jersey resources
        Set<Class<?>> rootResources = new HashSet<Class<?>>();
        rootResources.add(JerseySample.class);
        return rootResources;
    }
    
    }
    
  3. Then you can reference your properties in your endpoints like this:

    import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    import javax.ws.rs.Produces;
    import javax.ws.rs.core.MediaType;
    
    @Path("/")
    public class JerseySample {
    
        @GET
        @Path("hello")
        @Produces(MediaType.TEXT_PLAIN)
        public String get() {
            return "Property value is: " + JerseyConfig.properties.getProperty("sample.property");
        }
    
    }
    
like image 127
David Fleeman Avatar answered Dec 28 '22 06:12

David Fleeman