static final constants in local bean interface

Discussions

EJB design: static final constants in local bean interface

  1. static final constants in local bean interface (2 messages)

    I've got a question about constants in a local interface of a bean that come from evaluation an expression. If I evaluate the expression ConfigurationManager.getInstance().getProperty("welcomeSMSTriggerEnabled"); during debugging then I get the string ENABLED. If I evaluate the constant variable using the call SMSMsgHandlerLocal.WELCOME_SMS_TRIGGER_ENABLED on the local interface, then I get the null value.

    Can anyone help me with this "problem"? Does it also not work in a normal Java environment without J2EE?

    /**
     * @author Mark Noten <mark dot noten dot ext at siemens dot com>
     * (c) 2005 SIEMENS AG
     */
    package com.siemens.mobile.imc.core.imes;

    import ...

    public interface SMSMsgHandlerLocal extends EJBLocalObject {

        public static final String WELCOME_SMS_TRIGGER_ENABLED = ConfigurationManager
                .getInstance().getProperty("welcomeSMSTriggerEnabled");

        public int createWelcomeMessage(String msisdn, String password)
                throws ProtocolSenderException;
    }

    Best regards,

    Mark
  2. Are you sure ConfigurationManager.getInstance() returns something that's ready to go the very first time it might be called when SMSMsgHandlerLocal has its statics initialized?

    Have you checked for console output? If an exception is thrown while initializing statics, you'll probably see a stack trace in the console.
  3. Here's the code of the ConfigurationManager class. When the getInstance method is called, it will return an unique instance or create a new. No exceptions are thrown. I just don't get it.

    public class ConfigurationManager {

        private static ConfigurationManager instance = null;

        private static Properties imcProperties = null;

        // TODO Get the config files URL from a system environment variable
        private static final String CONFIG_FILES_URL = "config/MAGELLAN/configfiles.properties";

        private static Logger logger = Logger.getLogger(ConfigurationManager.class);

        private ConfigurationManager() {
            init();
        }

        public static ConfigurationManager getInstance() {
            if (instance == null) {
                instance = new ConfigurationManager();
            }
            return instance;
        }

        public String getProperty(String key) {
            if (!imcProperties.containsKey(key)) {
                return null;
            } else {
                return imcProperties.getProperty(key).trim();
            }
        }

        private static void init() {
            imcProperties = new Properties();
            InputStream inputStream = ConfigurationManager.class.getClassLoader()
                    .getResourceAsStream(ConfigurationManager.CONFIG_FILES_URL);
            Properties configFilesProperties = new Properties();
            try {
                configFilesProperties.load(inputStream);
                Enumeration configFilesKeys = configFilesProperties.keys();
                while (configFilesKeys.hasMoreElements()) {
                    String propertiesFileURLKey = (String) configFilesKeys
                            .nextElement();
                    String propertiesFileURL = configFilesProperties
                            .getProperty(propertiesFileURLKey);
                    readPropertiesFromFile(propertiesFileURL);
                }
            } catch (IOException exc) {
                logger
                        .error("Could not load properties file from imc/config/IMC210/configfiles.properties");
                exc.printStackTrace();
            }
        }

        private static void readPropertiesFromFile(String fileURL) {
            InputStream inputStream = ConfigurationManager.class.getClassLoader()
                    .getResourceAsStream(fileURL);
            Properties properties = new Properties();
            try {
                properties.load(inputStream);
                imcProperties.putAll(properties);
            } catch (IOException exc) {
                logger
                        .error("Could not load properties file from " + fileURL
                                + ".");
                exc.printStackTrace();
            }
        }

    }