4

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(); }
}
3
  • I'm using Intellij, but what does that have to do with it? Apr 9, 2015 at 16:18
  • IDE gives compile errors when java: cannot find symbol. Apr 9, 2015 at 16:20
  • Notice that the method getServletContext() is not defined for class DatabaseHandler just as the error states. You cannot initialize the config object in the DatabaseHandler there because you do not have access to the servletContext. Perhaps add a new method to the DatabaseHandler class that takes the servletContext as a param and then initializes the config there in that new method. Apr 13, 2015 at 13:56

2 Answers 2

9

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");
        }
    
    }
    
1
  • 2
    Is it possible to create a multiple end points like JerseySample, JerseySample1 and JerseySample2 and adding them in config file like rootResources.add(JerseySample.class); rootResources.add(JerseySample1.class); rootResources.add(JerseySample2.class); Will it work? Mar 5, 2016 at 12:58
1

example:

private static String token;
static {
    token = getProperties().getProperty("token");
}

public static Properties getProperties() {
    Properties _properties = new Properties();

    try (InputStream inputStream = new FileInputStream("src/main/resources/app.properties")) {
        _properties.load(inputStream);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return _properties;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.