Search in sources :

Example 11 with ConfigurationException

use of org.osgi.service.cm.ConfigurationException in project openhab1-addons by openhab.

the class SqueezeServer method updated.

@Override
public synchronized void updated(Dictionary<String, ?> config) throws ConfigurationException {
    // disconnect first in case the config has changed for an existing
    // instance
    disconnect();
    host = null;
    cliPort = DEFAULT_CLI_PORT;
    webPort = DEFAULT_WEB_PORT;
    retries = DEFAULT_RETRIES;
    retryTimeout = DEFAULT_RETRY_TIMEOUT;
    ttsUrl = DEFAULT_TTS_URL;
    ttsMaxSentenceLength = DEFAULT_TTS_MAX_SENTENCE_LENGTH;
    playersById.clear();
    playersByMacAddress.clear();
    if (config == null || config.isEmpty()) {
        logger.warn("Empty or null configuration. Ignoring.");
        return;
    }
    Enumeration<String> keys = config.keys();
    while (keys.hasMoreElements()) {
        String key = keys.nextElement();
        // don't want to process here ...
        if ("service.pid".equals(key)) {
            continue;
        }
        Matcher serverMatcher = SERVER_CONFIG_PATTERN.matcher(key);
        Matcher playerMatcher = PLAYER_CONFIG_PATTERN.matcher(key);
        Matcher ttsUrlMatcher = TTS_URL_CONFIG_PATTERN.matcher(key);
        Matcher ttsMaxSentenceLengthMatcher = TTS_MAX_SENTENCE_LENGTH_CONFIG_PATTERN.matcher(key);
        String value = (String) config.get(key);
        if (serverMatcher.matches()) {
            String serverConfig = serverMatcher.group(2);
            if (serverConfig.equals("host") && StringUtils.isNotBlank(value)) {
                host = value;
            } else if (serverConfig.equals("cliport") && StringUtils.isNotBlank(value)) {
                cliPort = Integer.valueOf(value);
            } else if (serverConfig.equals("webport") && StringUtils.isNotBlank(value)) {
                webPort = Integer.valueOf(value);
            } else if (serverConfig.equals("retries") && StringUtils.isNotBlank(value)) {
                retries = Integer.valueOf(value);
            } else if (serverConfig.equals("retryTimeout") && StringUtils.isNotBlank(value)) {
                retryTimeout = Integer.valueOf(value);
            }
        } else if (playerMatcher.matches()) {
            String playerId = playerMatcher.group(1);
            String macAddress = value;
            SqueezePlayer player = new SqueezePlayer(this, playerId, macAddress);
            playersById.put(playerId.toLowerCase(), player);
            playersByMacAddress.put(macAddress.toLowerCase(), player);
        } else if (ttsUrlMatcher.matches() && StringUtils.isNotBlank(value)) {
            ttsUrl = value;
        } else if (ttsMaxSentenceLengthMatcher.matches() && StringUtils.isNotBlank(value)) {
            ttsMaxSentenceLength = Integer.valueOf(value);
        } else {
            logger.warn("Ignored unexpected or unsupported configuration: {}", key);
        }
    }
    if (StringUtils.isEmpty(host)) {
        throw new ConfigurationException("host", "No Squeeze Server host specified - this property is mandatory");
    }
    if (playersById.size() == 0) {
        throw new ConfigurationException("host", "No Squeezebox players specified - there must be at least one player");
    }
    // attempt to connect using our new config
    if (!connect()) {
        retryConnect();
    }
}
Also used : Matcher(java.util.regex.Matcher) ConfigurationException(org.osgi.service.cm.ConfigurationException)

Example 12 with ConfigurationException

use of org.osgi.service.cm.ConfigurationException in project openhab1-addons by openhab.

the class BticinoBinding method connectAllBticinoDevices.

private void connectAllBticinoDevices() throws ConfigurationException {
    for (String l_gw_if_id : m_bticino_devices_config.keySet()) {
        BticinoConfig l_current_device_config = m_bticino_devices_config.get(l_gw_if_id);
        // Create a gw service object
        BticinoDevice l_bticino_device = new BticinoDevice(l_current_device_config.id, this);
        l_bticino_device.setEventPublisher(eventPublisher);
        l_bticino_device.setHost(l_current_device_config.host);
        l_bticino_device.setPort(l_current_device_config.port);
        l_bticino_device.setRescanInterval(l_current_device_config.rescan_secs);
        try {
            l_bticino_device.initialize();
        } catch (InitializationException e) {
            throw new ConfigurationException(l_gw_if_id, "Could not open create BTicino interface with ID [" + l_gw_if_id + "], Exception : " + e.getMessage());
        } catch (Throwable e) {
            throw new ConfigurationException(l_gw_if_id, "Could not open create BTicino interface with ID [" + l_gw_if_id + "], Exception : " + e.getMessage());
        }
        m_bticino_devices.put(l_gw_if_id, l_bticino_device);
    }
}
Also used : ConfigurationException(org.osgi.service.cm.ConfigurationException)

Example 13 with ConfigurationException

use of org.osgi.service.cm.ConfigurationException in project openhab1-addons by openhab.

the class BticinoBinding method updated.

/**
     * {@inheritDoc}
     */
public void updated(Dictionary<String, ?> properties) throws ConfigurationException {
    if (!m_binding_initialized) {
        // remove all configs
        m_bticino_devices_config.clear();
        // remove all interfaces
        m_bticino_devices.clear();
        // We will read every configuration key, and encounter
        // hostname, port for the configured bticino gateways
        Enumeration<String> keys = properties.keys();
        while (keys.hasMoreElements()) {
            String key = keys.nextElement();
            if ("service.pid".equals(key)) {
                continue;
            }
            Matcher matcher = EXTRACT_BTICINO_GATEWAY_CONFIG_PATTERN.matcher(key);
            if (!matcher.matches()) {
                logger.debug("given bticino gateway-config-key '" + key + "' does not follow the expected pattern '<gateway_name>.<host|port>'");
                continue;
            }
            matcher.reset();
            matcher.find();
            // Get the interface id
            String l_gw_if_id = matcher.group(1);
            // Search the config, to update the values (row / row)
            BticinoConfig l_bticino_config = m_bticino_devices_config.get(l_gw_if_id);
            // Create a new config if it wasnt found now
            if (l_bticino_config == null) {
                l_bticino_config = new BticinoConfig();
                // set the id
                l_bticino_config.id = l_gw_if_id;
                // add (if_id, bticino config) entry
                m_bticino_devices_config.put(l_gw_if_id, l_bticino_config);
            }
            String configKey = matcher.group(2);
            String value = Objects.toString(properties.get(key), null);
            // parameter host
            if ("host".equals(configKey)) {
                l_bticino_config.host = value;
            } else // parameter port
            if ("port".equals(configKey)) {
                l_bticino_config.port = Integer.valueOf(value);
            } else if ("rescan_secs".equals(configKey)) {
                l_bticino_config.rescan_secs = Integer.valueOf(value);
            } else {
                throw new ConfigurationException(configKey, "the given configKey '" + configKey + "' with value '" + value + "' is unknown");
            }
        }
        // Now for all the bticino gateways configured in the configuration,
        // connect to the physical gateways
        connectAllBticinoDevices();
        // Now start all the bticino gateways
        startAllBticinoDevices();
        // Indicate that this binding has been initialized
        m_binding_initialized = true;
    }
}
Also used : Matcher(java.util.regex.Matcher) ConfigurationException(org.osgi.service.cm.ConfigurationException)

Example 14 with ConfigurationException

use of org.osgi.service.cm.ConfigurationException in project openhab1-addons by openhab.

the class AlarmDecoderBinding method updated.

@SuppressWarnings("rawtypes")
@Override
public void updated(Dictionary config) throws ConfigurationException {
    if (config == null) {
        throw new ConfigurationException("alarmdecoder:connect", "no config!");
    }
    logger.debug("config updated!");
    try {
        m_connectString = (String) config.get("connect");
        if (m_connectString == null) {
            throw new ConfigurationException("alarmdecoder:connect", "no connect config in openhab.cfg!");
        }
        String[] parts = m_connectString.split(":");
        if (parts.length < 2) {
            throw new ConfigurationException("alarmdecoder:connect", "missing :, check openhab.cfg!");
        }
        if (parts[0].equals("tcp")) {
            parseTcpConfig(parts);
        } else if (parts[0].startsWith("serial")) {
            parseSerialConfig(parts);
        } else {
            throw new ConfigurationException("alarmdecoder:connect", "invalid parameter " + parts[0]);
        }
        String reconn = (String) config.get("reconnect");
        if (reconn != null && reconn.trim().length() > 0) {
            refreshInterval = Long.parseLong(reconn);
        }
        String acceptCommands = (String) config.get("send_commands_and_compromise_security");
        if (acceptCommands != null && acceptCommands.equalsIgnoreCase("true")) {
            logger.info("accepting commands!");
            m_acceptCommands = true;
        }
        setProperlyConfigured(true);
    } catch (ConfigurationException e) {
        logger.error("configuration error: {} ", e.getMessage(), e);
        throw e;
    }
}
Also used : ConfigurationException(org.osgi.service.cm.ConfigurationException)

Example 15 with ConfigurationException

use of org.osgi.service.cm.ConfigurationException in project openhab1-addons by openhab.

the class AlarmDecoderBinding method parseSerialConfig.

/**
     * Parses and stores the serial configuration string of the binding configuration.
     * Expected form is serial@portspeed:devicename, where @portspeed is optional.
     *
     * @param parts config string, split on ':'
     * @throws ConfigurationException
     */
private void parseSerialConfig(String[] parts) throws ConfigurationException {
    if (parts.length != 2) {
        throw new ConfigurationException("alarmdecoder:connect", "serial device name cannot have :");
    }
    m_serialDeviceName = parts[1];
    // split again
    String[] p = parts[0].split("@");
    if (p.length == 2) {
        // an optional port speed is provided
        try {
            m_portSpeed = Integer.parseInt(p[1]);
        } catch (NumberFormatException e) {
            throw new ConfigurationException("alarmdecoder:connect", "serial port speed must be integer");
        }
    }
    logger.debug("serial port configuration: speed: {} device: {}", m_portSpeed, m_serialDeviceName);
}
Also used : ConfigurationException(org.osgi.service.cm.ConfigurationException)

Aggregations

ConfigurationException (org.osgi.service.cm.ConfigurationException)190 Activate (org.apache.felix.scr.annotations.Activate)31 ServiceReference (org.osgi.framework.ServiceReference)30 Matcher (java.util.regex.Matcher)28 Hashtable (java.util.Hashtable)26 Properties (java.util.Properties)22 IOException (java.io.IOException)21 Test (org.junit.Test)21 ManagedService (org.osgi.service.cm.ManagedService)20 FooService (org.apache.felix.ipojo.runtime.core.services.FooService)19 HashMap (java.util.HashMap)17 File (java.io.File)11 URISyntaxException (java.net.URISyntaxException)11 Collection (java.util.Collection)11 HashSet (java.util.HashSet)11 ServiceTracker (org.osgi.util.tracker.ServiceTracker)10 URI (java.net.URI)9 Dictionary (java.util.Dictionary)9 Map (java.util.Map)9 NotFoundException (org.opencastproject.util.NotFoundException)9