Currently, I have a class which is read during startup. It reads the information fine.
@Startup @Scope(ScopeType.APPLICATION) @Stateful @Name("propertyInfo") public class PropertyInfo implements PropertyInfoLocal, Serializable { @Logger Log log; @PersistenceContext(type = EXTENDED) EntityManager em; @Out private String tmpDir; @Create public void initialize() { String fileSep = System .getProperty(SystemConstants.PROPERTY_FILE_SEPARATOR); String homeDir = System .getProperty(SystemConstants.PROPERTY_JBOSS_HOME_DIR); String dukeProperty = homeDir + fileSep + "deploy" + fileSep + "artwork.ear" + fileSep + "artwork.war" + fileSep + "WEB-INF" + fileSep + "configuration.properties"; log.info("configuration Properties [#0]", dukeProperty); Properties props = new Properties(); try { FileInputStream fis = new FileInputStream(dukeProperty); props.load(fis); String tmpDir = props.getProperty("tmp.dir"); System.out.println(" "); System.out.println("tmpDir --->> " + tmpDir); System.out.println(" "); } catch (FileNotFoundException ex) { log.error("The 'configuration.properties' file is missing."); } catch (IOException ex) { log.error("Unable to read the 'configuration.properties' file."); } } ... }
The problem is when I try to read this information from within another class. I have tried both:
PropertyInfo targetDir = (PropertyInfo) Contexts.getApplicationContext().get("propertyInfo");
and
PropertyInfo pi = (PropertyInfo) Component.getInstance(PropertyInfo.class);
These give me a classCastException.
Could someone tell me what I am doing wrong? Is there a better way of handling application settings?
Try to output what the class really is that you are trying to cast.
Try using a POJO, you are probably trying to cast an interface to a bean, vice verse or something...