parent
44fa7aa486
commit
d53942eeaa
@ -1,109 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.wso2.carbon.device.mgt.iot.androidsense.plugin.impl.dao.util;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.wso2.carbon.device.mgt.common.Device;
|
||||
import org.wso2.carbon.device.mgt.iot.androidsense.plugin.constants.AndroidSenseConstants;
|
||||
import org.wso2.carbon.device.mgt.iot.androidsense.plugin.exception.AndroidSenseDeviceMgtPluginException;
|
||||
|
||||
import javax.naming.Context;
|
||||
import javax.naming.InitialContext;
|
||||
import javax.naming.NamingException;
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Contains utility methods used by plugin.
|
||||
*/
|
||||
public class AndroidSenseUtils {
|
||||
|
||||
private static Log log = LogFactory.getLog(AndroidSenseUtils.class);
|
||||
|
||||
public static String getDeviceProperty(List<Device.Property> deviceProperties, String propertyKey) {
|
||||
String deviceProperty = "";
|
||||
for(Device.Property property :deviceProperties){
|
||||
if(propertyKey.equals(property.getName())){
|
||||
deviceProperty = property.getValue();
|
||||
}
|
||||
}
|
||||
return deviceProperty;
|
||||
}
|
||||
|
||||
public static Device.Property getProperty(String property, String value) {
|
||||
if (property != null) {
|
||||
Device.Property prop = new Device.Property();
|
||||
prop.setName(property);
|
||||
prop.setValue(value);
|
||||
return prop;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void cleanupResources(Connection conn, PreparedStatement stmt, ResultSet rs) {
|
||||
if (rs != null) {
|
||||
try {
|
||||
rs.close();
|
||||
} catch (SQLException e) {
|
||||
log.warn("Error occurred while closing result set", e);
|
||||
}
|
||||
}
|
||||
if (stmt != null) {
|
||||
try {
|
||||
stmt.close();
|
||||
} catch (SQLException e) {
|
||||
log.warn("Error occurred while closing prepared statement", e);
|
||||
}
|
||||
}
|
||||
if (conn != null) {
|
||||
try {
|
||||
conn.close();
|
||||
} catch (SQLException e) {
|
||||
log.warn("Error occurred while closing database connection", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void cleanupResources(PreparedStatement stmt, ResultSet rs) {
|
||||
cleanupResources(null, stmt, rs);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the device management schema.
|
||||
*/
|
||||
public static void setupDeviceManagementSchema() throws AndroidSenseDeviceMgtPluginException {
|
||||
try {
|
||||
Context ctx = new InitialContext();
|
||||
DataSource dataSource = (DataSource) ctx.lookup(AndroidSenseConstants.DATA_SOURCE_NAME);
|
||||
DeviceSchemaInitializer initializer =
|
||||
new DeviceSchemaInitializer(dataSource);
|
||||
log.info("Initializing device management repository database schema");
|
||||
initializer.createRegistryDatabase();
|
||||
|
||||
} catch (NamingException e) {
|
||||
log.error("Error while looking up the data source: " + AndroidSenseConstants.DATA_SOURCE_NAME);
|
||||
} catch (Exception e) {
|
||||
throw new AndroidSenseDeviceMgtPluginException("Error occurred while initializing Iot Device " +
|
||||
"Management database schema", e);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.wso2.carbon.device.mgt.iot.androidsense.plugin.impl.util;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.wso2.carbon.core.ServerStartupObserver;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Startup listener to create an output adapter after server starts up.
|
||||
*/
|
||||
public class AndroidSenseStartupListener implements ServerStartupObserver {
|
||||
private static final Log log = LogFactory.getLog(AndroidSenseStartupListener.class);
|
||||
|
||||
@Override
|
||||
public void completingServerStartup() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void completedServerStartup() {
|
||||
try {
|
||||
AndroidSenseUtils.setupMqttOutputAdapter();
|
||||
} catch (IOException e) {
|
||||
log.error("Failed to intilaize the virtual firealarm output adapter", e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,186 @@
|
||||
/*
|
||||
* Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.wso2.carbon.device.mgt.iot.androidsense.plugin.impl.util;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.wso2.carbon.base.ServerConfiguration;
|
||||
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
||||
import org.wso2.carbon.core.util.Utils;
|
||||
import org.wso2.carbon.device.mgt.common.Device;
|
||||
import org.wso2.carbon.device.mgt.iot.androidsense.plugin.constants.AndroidSenseConstants;
|
||||
import org.wso2.carbon.device.mgt.iot.androidsense.plugin.exception.AndroidSenseDeviceMgtPluginException;
|
||||
import org.wso2.carbon.device.mgt.iot.androidsense.plugin.internal.AndroidSenseManagementDataHolder;
|
||||
import org.wso2.carbon.event.output.adapter.core.MessageType;
|
||||
import org.wso2.carbon.event.output.adapter.core.OutputEventAdapterConfiguration;
|
||||
import org.wso2.carbon.event.output.adapter.core.exception.OutputEventAdapterException;
|
||||
import org.wso2.carbon.utils.CarbonUtils;
|
||||
|
||||
import javax.naming.Context;
|
||||
import javax.naming.InitialContext;
|
||||
import javax.naming.NamingException;
|
||||
import javax.sql.DataSource;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* Contains utility methods used by plugin.
|
||||
*/
|
||||
public class AndroidSenseUtils {
|
||||
|
||||
private static Log log = LogFactory.getLog(AndroidSenseUtils.class);
|
||||
|
||||
private static final String VIRTUAL_FIREALARM_CONFIG_LOCATION =
|
||||
CarbonUtils.getCarbonHome() + File.separator + "repository" + File.separator + "conf" +
|
||||
File.separator + "iot" + File.separator + "mqtt.properties";
|
||||
|
||||
public static void cleanupResources(Connection conn, PreparedStatement stmt, ResultSet rs) {
|
||||
if (rs != null) {
|
||||
try {
|
||||
rs.close();
|
||||
} catch (SQLException e) {
|
||||
log.warn("Error occurred while closing result set", e);
|
||||
}
|
||||
}
|
||||
if (stmt != null) {
|
||||
try {
|
||||
stmt.close();
|
||||
} catch (SQLException e) {
|
||||
log.warn("Error occurred while closing prepared statement", e);
|
||||
}
|
||||
}
|
||||
if (conn != null) {
|
||||
try {
|
||||
conn.close();
|
||||
} catch (SQLException e) {
|
||||
log.warn("Error occurred while closing database connection", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void cleanupResources(PreparedStatement stmt, ResultSet rs) {
|
||||
cleanupResources(null, stmt, rs);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the device management schema.
|
||||
*/
|
||||
public static void setupDeviceManagementSchema() throws AndroidSenseDeviceMgtPluginException {
|
||||
try {
|
||||
Context ctx = new InitialContext();
|
||||
DataSource dataSource = (DataSource) ctx.lookup(AndroidSenseConstants.DATA_SOURCE_NAME);
|
||||
DeviceSchemaInitializer initializer =
|
||||
new DeviceSchemaInitializer(dataSource);
|
||||
log.info("Initializing device management repository database schema");
|
||||
initializer.createRegistryDatabase();
|
||||
|
||||
} catch (NamingException e) {
|
||||
log.error("Error while looking up the data source: " + AndroidSenseConstants.DATA_SOURCE_NAME);
|
||||
} catch (Exception e) {
|
||||
throw new AndroidSenseDeviceMgtPluginException("Error occurred while initializing Iot Device " +
|
||||
"Management database schema", e);
|
||||
}
|
||||
}
|
||||
|
||||
public static void setupMqttOutputAdapter() throws IOException {
|
||||
OutputEventAdapterConfiguration outputEventAdapterConfiguration =
|
||||
createMqttOutputEventAdapterConfiguration(AndroidSenseConstants.MQTT_ADAPTER_NAME,
|
||||
AndroidSenseConstants.MQTT_ADAPTER_TYPE, MessageType.TEXT);
|
||||
try {
|
||||
PrivilegedCarbonContext.startTenantFlow();
|
||||
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(
|
||||
AndroidSenseConstants.DEVICE_TYPE_PROVIDER_DOMAIN, true);
|
||||
AndroidSenseManagementDataHolder.getInstance().getOutputEventAdapterService()
|
||||
.create(outputEventAdapterConfiguration);
|
||||
} catch (OutputEventAdapterException e) {
|
||||
log.error("Unable to create Output Event Adapter : " + AndroidSenseConstants.MQTT_ADAPTER_NAME, e);
|
||||
} finally {
|
||||
PrivilegedCarbonContext.endTenantFlow();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create Output Event Adapter Configuration for given configuration.
|
||||
*
|
||||
* @param name Output Event Adapter name
|
||||
* @param type Output Event Adapter type
|
||||
* @param msgFormat Output Event Adapter message format
|
||||
* @return OutputEventAdapterConfiguration instance for given configuration
|
||||
*/
|
||||
private static OutputEventAdapterConfiguration createMqttOutputEventAdapterConfiguration(String name, String type,
|
||||
String msgFormat) throws IOException {
|
||||
OutputEventAdapterConfiguration outputEventAdapterConfiguration = new OutputEventAdapterConfiguration();
|
||||
outputEventAdapterConfiguration.setName(name);
|
||||
outputEventAdapterConfiguration.setType(type);
|
||||
outputEventAdapterConfiguration.setMessageFormat(msgFormat);
|
||||
File configFile = new File(VIRTUAL_FIREALARM_CONFIG_LOCATION);
|
||||
if (configFile.exists()) {
|
||||
Map<String, String> mqttAdapterProperties = new HashMap<>();
|
||||
InputStream propertyStream = configFile.toURI().toURL().openStream();
|
||||
Properties properties = new Properties();
|
||||
properties.load(propertyStream);
|
||||
mqttAdapterProperties.put(AndroidSenseConstants.USERNAME_PROPERTY_KEY, properties.getProperty(
|
||||
AndroidSenseConstants.USERNAME_PROPERTY_KEY));
|
||||
mqttAdapterProperties.put(AndroidSenseConstants.DCR_PROPERTY_KEY, Utils.replaceSystemProperty(
|
||||
properties.getProperty(AndroidSenseConstants.DCR_PROPERTY_KEY)));
|
||||
mqttAdapterProperties.put(AndroidSenseConstants.BROKER_URL_PROPERTY_KEY, replaceMqttProperty(
|
||||
properties.getProperty(AndroidSenseConstants.BROKER_URL_PROPERTY_KEY)));
|
||||
mqttAdapterProperties.put(AndroidSenseConstants.SCOPES_PROPERTY_KEY, properties.getProperty(
|
||||
AndroidSenseConstants.SCOPES_PROPERTY_KEY));
|
||||
mqttAdapterProperties.put(AndroidSenseConstants.CLEAR_SESSION_PROPERTY_KEY, properties.getProperty(
|
||||
AndroidSenseConstants.CLEAR_SESSION_PROPERTY_KEY));
|
||||
mqttAdapterProperties.put(AndroidSenseConstants.QOS_PROPERTY_KEY, properties.getProperty(
|
||||
AndroidSenseConstants.QOS_PROPERTY_KEY));
|
||||
mqttAdapterProperties.put(AndroidSenseConstants.CLIENT_ID_PROPERTY_KEY, "");
|
||||
outputEventAdapterConfiguration.setStaticProperties(mqttAdapterProperties);
|
||||
}
|
||||
return outputEventAdapterConfiguration;
|
||||
}
|
||||
|
||||
private static String replaceMqttProperty(String urlWithPlaceholders) {
|
||||
urlWithPlaceholders = Utils.replaceSystemProperty(urlWithPlaceholders);
|
||||
urlWithPlaceholders = urlWithPlaceholders.replaceAll(AndroidSenseConstants.MQTT_PORT, "" +
|
||||
(AndroidSenseConstants.DEFAULT_MQTT_PORT + getPortOffset()));
|
||||
urlWithPlaceholders = urlWithPlaceholders.replaceAll(AndroidSenseConstants.MQTT_BROKER_HOST,
|
||||
System.getProperty(AndroidSenseConstants.DEFAULT_CARBON_SERVER_HOST_PROPERTY, "localhost"));
|
||||
return urlWithPlaceholders;
|
||||
}
|
||||
|
||||
private static int getPortOffset() {
|
||||
ServerConfiguration carbonConfig = ServerConfiguration.getInstance();
|
||||
String portOffset = System.getProperty("portOffset", carbonConfig.getFirstProperty(
|
||||
AndroidSenseConstants.CARBON_CONFIG_PORT_OFFSET));
|
||||
try {
|
||||
if ((portOffset != null)) {
|
||||
return Integer.parseInt(portOffset.trim());
|
||||
} else {
|
||||
return AndroidSenseConstants.CARBON_DEFAULT_PORT_OFFSET;
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
return AndroidSenseConstants.CARBON_DEFAULT_PORT_OFFSET;
|
||||
}
|
||||
}
|
||||
}
|
2
components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/plugin/impl/dao/util/DeviceSchemaInitializer.java → components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/plugin/impl/util/DeviceSchemaInitializer.java
2
components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/plugin/impl/dao/util/DeviceSchemaInitializer.java → components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/plugin/impl/util/DeviceSchemaInitializer.java
@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* you may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.wso2.carbon.device.mgt.iot.androidsense.plugin.internal;
|
||||
|
||||
import org.wso2.carbon.event.output.adapter.core.OutputEventAdapterService;
|
||||
|
||||
/**
|
||||
* DataHolder class of plugins component.
|
||||
*/
|
||||
public class AndroidSenseManagementDataHolder {
|
||||
|
||||
private OutputEventAdapterService outputEventAdapterService;
|
||||
|
||||
private static AndroidSenseManagementDataHolder thisInstance = new AndroidSenseManagementDataHolder();
|
||||
|
||||
private AndroidSenseManagementDataHolder() {
|
||||
}
|
||||
|
||||
public static AndroidSenseManagementDataHolder getInstance() {
|
||||
return thisInstance;
|
||||
}
|
||||
|
||||
public OutputEventAdapterService getOutputEventAdapterService() {
|
||||
return outputEventAdapterService;
|
||||
}
|
||||
|
||||
public void setOutputEventAdapterService(
|
||||
OutputEventAdapterService outputEventAdapterService) {
|
||||
this.outputEventAdapterService = outputEventAdapterService;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue