Search code examples
javatomcatfileinputstream

Tomcat doesn't create the "new Property()" instance


stackoverflow, please help me. I have a small web application (Servlet + jsp). Unit test passed correctly, but after deploy my Factory isn't able to create instances of my DAOs, because after action "Property property = new Property();" property = null. Why?(

public class DAOFactory <T>{

    private String daoType;
    private String propertyFilePath;
    private Properties property;
    private FileInputStream fis;
    private static Logger LOGGER;
    private String propertyKey;

    public DAOFactory(String propertyFilePath,String propertyKey) {
        this(propertyKey);
        this.propertyFilePath = propertyFilePath;
    }


    public DAOFactory(String propertyKey) {
        propertyFilePath = "src/main/resources/dao_factory.properties";
        LOGGER = LoggerFactory.getLogger(DAOFactory.class);
        this.propertyKey = propertyKey;
        try {
            property = new Properties();
            fis = new FileInputStream(propertyFilePath);
            property.load(fis);
        } catch (FileNotFoundException ex) {
            LOGGER.error("Property file " + propertyFilePath + " doesn't exist", ex);
        } catch (IOException ex) {
            LOGGER.error("Unable to download Property file: " + propertyFilePath, ex);
        }
        System.err.println("fis: " + fis);
        System.err.println("propertyKey: " + propertyKey);
        System.err.println("property: " + property);
        daoType = property.getProperty(propertyKey);
        System.err.println("daoType: " + daoType);
    }



    public T getInstance () throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException {
        Class c = Class.forName(daoType); 
        Method method = c.getDeclaredMethod("getInstance");
        return (T) method.invoke(null, null);
    }

}

When I try to use my DAOFactory, DAOFactory<BookDAO> daoFactory= new DAOFactory(""BookDAO"); // or even new DAOFactory("src/main/resources/dao_factory.properties","BookDAO");

I've got

IN TESTS

fis: java.io.FileInputStream@5025a98f
propertyKey: BookDAO
property: {BookDAO=com.softserve.siniaieva.bibliophile.dao.impl.BookDAOImitation, ReaderDAO=com.softserve.siniaieva.bibliophile.dao.impl.ReaderDAOImitation}
daoType: com.softserve.siniaieva.bibliophile.dao.impl.BookDAOImitation

WHEN TOMCAT CREATES DAOFactory
fis: null
propertyKey: BookDAO
property: null
daoType: null  

Should I add smth in web.xml for Tomcat to make it see FileInputStream ?


Solution

  • The issue is likely to do with this

    propertyFilePath = "src/main/resources/dao_factory.properties"; 
    

    The src directory won't be available after packaging, and things instead will be respectively moved to the bin directory. Give something like the following a go

    DAOFactory.class.getResourceAsStream("dao_factory.properties")
    

    You can read more about this here.