diff --git a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/pom.xml b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/pom.xml index eaebda7a..cf34e955 100644 --- a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/pom.xml +++ b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/pom.xml @@ -70,7 +70,14 @@ org.wso2.carbon.device.mgt.common, org.wso2.carbon.device.mgt.iot.*, org.wso2.carbon.device.mgt.extensions.feature.mgt.*, - org.wso2.carbon.utils.* + org.wso2.carbon.utils.*, + org.wso2.carbon.event.output.adapter.core, + org.wso2.carbon.event.output.adapter.core.exception, + org.wso2.carbon.base, + org.wso2.carbon.core.util, + org.wso2.carbon.context, + org.wso2.carbon.core, + org.apache.commons.codec.binary !${project-base-package}.plugin.internal, @@ -82,6 +89,10 @@ + + commons-codec.wso2 + commons-codec + org.eclipse.osgi org.eclipse.osgi @@ -99,16 +110,24 @@ org.wso2.carbon.device.mgt.common - org.wso2.carbon - org.wso2.carbon.ndatasource.core + org.wso2.carbon.devicemgt + org.wso2.carbon.device.mgt.core - org.wso2.carbon.devicemgt-plugins - org.wso2.carbon.device.mgt.iot + org.wso2.carbon + org.wso2.carbon.ndatasource.core org.wso2.carbon.devicemgt org.wso2.carbon.device.mgt.extensions + + org.wso2.carbon + org.wso2.carbon.utils + + + org.wso2.carbon.analytics-common + org.wso2.carbon.event.output.adapter.core + \ No newline at end of file diff --git a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/constants/DeviceTypeConstants.java b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/constants/DeviceTypeConstants.java index b1680be8..7107829a 100644 --- a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/constants/DeviceTypeConstants.java +++ b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/constants/DeviceTypeConstants.java @@ -18,19 +18,49 @@ package ${groupId}.${rootArtifactId}.plugin.constants; +import org.wso2.carbon.utils.CarbonUtils; +import java.io.File; + public class DeviceTypeConstants { public final static String DEVICE_TYPE = "${deviceType}"; public final static String DEVICE_PLUGIN_DEVICE_NAME = "DEVICE_NAME"; public final static String DEVICE_PLUGIN_DEVICE_ID = "${deviceType}_DEVICE_ID"; - public final static String SENSOR_READING = "sensorValue"; - public static final String DATA_SOURCE_NAME = "jdbc/${deviceType}DM_DB"; - public final static String DEVICE_PLUGIN_PROPERTY_ACCESS_TOKEN = "accessToken"; - public final static String DEVICE_PLUGIN_PROPERTY_REFRESH_TOKEN = "refreshToken"; - public final static String DEVICE_TYPE_PROVIDER_DOMAIN = "carbon.super"; public final static String STATE_ON = "ON"; public final static String STATE_OFF = "OFF"; - public final static String SENSOR_CONTEXT = "SENSOR"; - public final static String TEMPERATURE_EVENT_TABLE = "DEVICE_TEMPERATURE_SUMMARY"; - public final static String TEMPERATURE_STREAM_DEFINITION = "org.wso2.iot.devices.temperature"; - public final static String TEMPERATURE_STREAM_DEFINITION_VERSION = "1.0.0"; + + //sensor events summerized table name + public static final String TEMPERATURE_EVENT_TABLE = "ORG_WSO2_IOT_DEVICES_${nameOfTheSensor}"; + public static final String DATA_SOURCE_NAME = "jdbc/${deviceType}DM_DB"; + public final static String DEVICE_TYPE_PROVIDER_DOMAIN = "carbon.super"; + + //mqtt tranport related constants + public static final String MQTT_ADAPTER_NAME = "${nameOfTheSensor}_mqtt"; + public static final String MQTT_ADAPTER_TYPE = "oauth-mqtt"; + public static final String ADAPTER_TOPIC_PROPERTY = "topic"; + public static final String MQTT_PORT = "\\$\\{mqtt.broker.port\\}"; + public static final String MQTT_BROKER_HOST = "\\$\\{mqtt.broker.host\\}"; + public static final String CARBON_CONFIG_PORT_OFFSET = "Ports.Offset"; + public static final String DEFAULT_CARBON_LOCAL_IP_PROPERTY = "carbon.local.ip"; + public static final int CARBON_DEFAULT_PORT_OFFSET = 0; + public static final int DEFAULT_MQTT_PORT = 1883; + public static final String SUBSCRIBED_TOPIC = "carbon.super/${deviceType}/+/publisher"; + public static final String CONTENT_TRANSFORMATION = "contentTransformer"; + public static final String CONTENT_VALIDATION = "contentValidator"; + public static final String RESOURCE = "resource"; + + public static final String USERNAME_PROPERTY_KEY = "username"; + public static final String DCR_PROPERTY_KEY = "dcrUrl"; + public static final String BROKER_URL_PROPERTY_KEY = "url"; + public static final String SCOPES_PROPERTY_KEY = "scopes"; + public static final String QOS_PROPERTY_KEY = "qos"; + public static final String CLIENT_ID_PROPERTY_KEY = "qos"; + public static final String CLEAR_SESSION_PROPERTY_KEY = "clearSession"; + public static final String TOPIC = "topic"; + + public final static String SENSOR_STREAM_DEFINITION = "org.wso2.iot.devices.${nameOfTheSensor}"; + public final static String SENSOR_STREAM_DEFINITION_VERSION = "1.0.0"; + + public static final String MQTT_CONFIG_LOCATION = CarbonUtils.getEtcCarbonConfigDirPath() + File.separator + + "mqtt.properties"; } + diff --git a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/impl/DeviceTypeManagerService.java b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/impl/DeviceTypeManagerService.java index eca6621c..1038f636 100644 --- a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/impl/DeviceTypeManagerService.java +++ b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/impl/DeviceTypeManagerService.java @@ -32,71 +32,37 @@ import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; import java.util.List; -public class DeviceTypeManagerService implements DeviceManagementService{ - private DeviceManager deviceManager; +public class DeviceTypeManagerService implements DeviceManagementService { + private DeviceManager deviceManager; + + @Override + public String getType() { + return DeviceTypeConstants.DEVICE_TYPE; + } + + @Override + public void init() throws DeviceManagementException { + this.deviceManager = new DeviceTypeManager(); + } + + @Override + public DeviceManager getDeviceManager() { + return deviceManager; + } + + @Override + public ApplicationManager getApplicationManager() { + return null; + } + + @Override + public ProvisioningConfig getProvisioningConfig() { + return new ProvisioningConfig(DeviceTypeConstants.DEVICE_TYPE_PROVIDER_DOMAIN, false); + } + + @Override + public PushNotificationConfig getPushNotificationConfig() { + return null; + } - @Override - public String getType() { - return DeviceTypeConstants.DEVICE_TYPE; - } - - @Override - public void init() throws DeviceManagementException { - this.deviceManager = new DeviceTypeManager(); - } - - @Override - public DeviceManager getDeviceManager() { - return deviceManager; - } - - @Override - public ApplicationManager getApplicationManager() { - return null; - } - - @Override - public ProvisioningConfig getProvisioningConfig() { - return new ProvisioningConfig(DeviceTypeConstants.DEVICE_TYPE_PROVIDER_DOMAIN, true); - } - - @Override - public PushNotificationConfig getPushNotificationConfig() { - return null; - } - - /*@Override - public Application[] getApplications(String domain, int pageNumber, int size) - throws ApplicationManagementException { - return new Application[0]; - }*/ - - /*@Override - public void updateApplicationStatus(DeviceIdentifier deviceId, Application application, - String status) throws ApplicationManagementException { - - }*/ - - /*@Override - public String getApplicationStatus(DeviceIdentifier deviceId, Application application) - throws ApplicationManagementException { - return null; - } -*/ - /*@Override - public void installApplicationForDevices(Operation operation, List deviceIdentifiers) - throws ApplicationManagementException { - } - - @Override - public void installApplicationForUsers(Operation operation, List userNameList) - throws ApplicationManagementException { - - } - - @Override - public void installApplicationForUserRoles(Operation operation, List userRoleList) - throws ApplicationManagementException { - - }*/ } diff --git a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/impl/dao/DeviceTypeDAO.java b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/impl/dao/DeviceTypeDAO.java index a189b5ad..2092d615 100644 --- a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/impl/dao/DeviceTypeDAO.java +++ b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/impl/dao/DeviceTypeDAO.java @@ -20,11 +20,11 @@ package ${groupId}.${rootArtifactId}.plugin.impl.dao; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; + import ${groupId}.${rootArtifactId}.plugin.constants.DeviceTypeConstants; import ${groupId}.${rootArtifactId}.plugin.impl.dao.impl.DeviceTypeDAOImpl; import ${groupId}.${rootArtifactId}.plugin.exception.DeviceMgtPluginException; - import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; @@ -32,7 +32,7 @@ import javax.sql.DataSource; import java.sql.Connection; import java.sql.SQLException; -public class DeviceTypeDAO{ +public class DeviceTypeDAO { private static final Log log = LogFactory.getLog(DeviceTypeDAO.class); static DataSource dataSource; // package local variable @@ -42,17 +42,13 @@ public class DeviceTypeDAO{ initDeviceTypeDAO(); } - public DeviceTypeDAOImpl getDeviceTypeDAO() { - return new DeviceTypeDAOImpl(); - } - - public static void initDeviceTypeDAO(){ + public static void initDeviceTypeDAO() { try { Context ctx = new InitialContext(); dataSource = (DataSource) ctx.lookup(DeviceTypeConstants.DATA_SOURCE_NAME); } catch (NamingException e) { log.error("Error while looking up the data source: " + - DeviceTypeConstants.DATA_SOURCE_NAME); + DeviceTypeConstants.DATA_SOURCE_NAME); } } @@ -98,14 +94,14 @@ public class DeviceTypeDAO{ public static void closeConnection() throws DeviceMgtPluginException { - Connection con = currentConnection.get(); - if(con != null){ - try { - con.close(); - } catch (SQLException e) { - log.error("Error occurred while close the connection"); - } - } + Connection con = currentConnection.get(); + if (con != null) { + try { + con.close(); + } catch (SQLException e) { + log.error("Error occurred while close the connection"); + } + } currentConnection.remove(); } @@ -126,4 +122,8 @@ public class DeviceTypeDAO{ closeConnection(); } } + + public DeviceTypeDAOImpl getDeviceTypeDAO() { + return new DeviceTypeDAOImpl(); + } } \ No newline at end of file diff --git a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/impl/dao/impl/DeviceTypeDAOImpl.java b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/impl/dao/impl/DeviceTypeDAOImpl.java index 64b0faba..db5e4031 100644 --- a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/impl/dao/impl/DeviceTypeDAOImpl.java +++ b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/impl/dao/impl/DeviceTypeDAOImpl.java @@ -20,10 +20,12 @@ package ${groupId}.${rootArtifactId}.plugin.impl.dao.impl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; + import ${groupId}.${rootArtifactId}.plugin.constants.DeviceTypeConstants; import ${groupId}.${rootArtifactId}.plugin.exception.DeviceMgtPluginException; import ${groupId}.${rootArtifactId}.plugin.impl.dao.DeviceTypeDAO; import ${groupId}.${rootArtifactId}.plugin.impl.util.DeviceTypeUtils; + import org.wso2.carbon.device.mgt.common.Device; import java.sql.Connection; diff --git a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/impl/util/DeviceTypeStartupListener.java b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/impl/util/DeviceTypeStartupListener.java new file mode 100644 index 00000000..be6c243c --- /dev/null +++ b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/impl/util/DeviceTypeStartupListener.java @@ -0,0 +1,43 @@ +/* + * 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 ${groupId}.${rootArtifactId}.plugin.impl.util; + +import ${groupId}.${rootArtifactId}.plugin.impl.util.DeviceTypeUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.core.ServerStartupObserver; + +import java.io.IOException; + +public class DeviceTypeStartupListener implements ServerStartupObserver { + private static final Log log = LogFactory.getLog(${groupId}.${rootArtifactId}.plugin.impl.util.DeviceTypeStartupListener.class); + + @Override + public void completingServerStartup() { + } + + @Override + public void completedServerStartup() { + try { + DeviceTypeUtils.setupMqttOutputAdapter(); + } catch (IOException e) { + log.error("Failed to initialize the ${deviceType} output adapter", e); + } + } +} diff --git a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/impl/util/DeviceTypeUtils.java b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/impl/util/DeviceTypeUtils.java index c9a4eabf..eb0252c9 100644 --- a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/impl/util/DeviceTypeUtils.java +++ b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/impl/util/DeviceTypeUtils.java @@ -18,51 +18,40 @@ package ${groupId}.${rootArtifactId}.plugin.impl.util; -import javax.naming.Context; -import javax.naming.InitialContext; -import javax.naming.NamingException; -import javax.sql.DataSource; - - - import ${groupId}.${rootArtifactId}.plugin.constants.DeviceTypeConstants; import ${groupId}.${rootArtifactId}.plugin.exception.DeviceMgtPluginException; +import ${groupId}.${rootArtifactId}.plugin.internal.DeviceTypeManagementDataHolder; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.wso2.carbon.device.mgt.common.Device; +import org.wso2.carbon.base.ServerConfiguration; +import org.wso2.carbon.context.PrivilegedCarbonContext; +import org.wso2.carbon.core.util.Utils; +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 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 ${rootArtifactId} plugin. + * Contains utility methods used by ${deviceType} plugin. */ public class DeviceTypeUtils { - private static Log log = LogFactory.getLog(DeviceTypeUtils.class); - - public static String getDeviceProperty(List 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; - } + private static Log log = LogFactory.getLog(${groupId}.${rootArtifactId}.plugin.impl.util.DeviceTypeUtils.class); public static void cleanupResources(Connection conn, PreparedStatement stmt, ResultSet rs) { if (rs != null) { @@ -111,4 +100,91 @@ public class DeviceTypeUtils { } } + public static String replaceMqttProperty(String urlWithPlaceholders) { + String MQTT_BROKER_HOST = null; + String MQTT_PORT = null; + if(!DeviceTypeConstants.MQTT_BROKER_HOST.startsWith("$")){ + MQTT_BROKER_HOST = "\\$".concat(DeviceTypeConstants.MQTT_BROKER_HOST); + } + if(!DeviceTypeConstants.MQTT_PORT.startsWith("$")){ + MQTT_PORT = "\\$".concat(DeviceTypeConstants.MQTT_PORT); + } + urlWithPlaceholders = Utils.replaceSystemProperty(urlWithPlaceholders); + urlWithPlaceholders = urlWithPlaceholders.replaceAll(MQTT_PORT, "" + + (DeviceTypeConstants.DEFAULT_MQTT_PORT + getPortOffset())); + urlWithPlaceholders = urlWithPlaceholders.replaceAll(MQTT_BROKER_HOST, + System.getProperty(DeviceTypeConstants.DEFAULT_CARBON_LOCAL_IP_PROPERTY, "localhost")); + return urlWithPlaceholders; + } + + private static int getPortOffset() { + ServerConfiguration carbonConfig = ServerConfiguration.getInstance(); + String portOffset = System.getProperty("portOffset", carbonConfig.getFirstProperty( + DeviceTypeConstants.CARBON_CONFIG_PORT_OFFSET)); + try { + if ((portOffset != null)) { + return Integer.parseInt(portOffset.trim()); + } else { + return DeviceTypeConstants.CARBON_DEFAULT_PORT_OFFSET; + } + } catch (NumberFormatException e) { + return DeviceTypeConstants.CARBON_DEFAULT_PORT_OFFSET; + } + } + + public static void setupMqttOutputAdapter() throws IOException { + OutputEventAdapterConfiguration outputEventAdapterConfiguration = + createMqttOutputEventAdapterConfiguration(DeviceTypeConstants.MQTT_ADAPTER_NAME, + DeviceTypeConstants.MQTT_ADAPTER_TYPE, MessageType.TEXT); + try { + PrivilegedCarbonContext.startTenantFlow(); + PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain( + DeviceTypeConstants.DEVICE_TYPE_PROVIDER_DOMAIN, true); + DeviceTypeManagementDataHolder.getInstance().getOutputEventAdapterService() + .create(outputEventAdapterConfiguration); + } catch (OutputEventAdapterException e) { + log.error("Unable to create Output Event Adapter : " + DeviceTypeConstants.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(DeviceTypeConstants.MQTT_CONFIG_LOCATION); + if (configFile.exists()) { + Map mqttAdapterProperties = new HashMap<>(); + InputStream propertyStream = configFile.toURI().toURL().openStream(); + Properties properties = new Properties(); + properties.load(propertyStream); + mqttAdapterProperties.put(DeviceTypeConstants.USERNAME_PROPERTY_KEY, properties.getProperty( + DeviceTypeConstants.USERNAME_PROPERTY_KEY)); + mqttAdapterProperties.put(DeviceTypeConstants.DCR_PROPERTY_KEY, Utils.replaceSystemProperty( + properties.getProperty(DeviceTypeConstants.DCR_PROPERTY_KEY))); + mqttAdapterProperties.put(DeviceTypeConstants.BROKER_URL_PROPERTY_KEY, replaceMqttProperty( + properties.getProperty(DeviceTypeConstants.BROKER_URL_PROPERTY_KEY))); + mqttAdapterProperties.put(DeviceTypeConstants.SCOPES_PROPERTY_KEY, properties.getProperty( + DeviceTypeConstants.SCOPES_PROPERTY_KEY)); + mqttAdapterProperties.put(DeviceTypeConstants.CLEAR_SESSION_PROPERTY_KEY, properties.getProperty( + DeviceTypeConstants.CLEAR_SESSION_PROPERTY_KEY)); + mqttAdapterProperties.put(DeviceTypeConstants.QOS_PROPERTY_KEY, properties.getProperty( + DeviceTypeConstants.QOS_PROPERTY_KEY)); + mqttAdapterProperties.put(DeviceTypeConstants.CLIENT_ID_PROPERTY_KEY, ""); + mqttAdapterProperties.put(DeviceTypeConstants.RESOURCE, "output-event"); + outputEventAdapterConfiguration.setStaticProperties(mqttAdapterProperties); + } + return outputEventAdapterConfiguration; + } } diff --git a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/internal/DeviceTypeManagementDataHolder.java b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/internal/DeviceTypeManagementDataHolder.java new file mode 100644 index 00000000..4caab566 --- /dev/null +++ b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/internal/DeviceTypeManagementDataHolder.java @@ -0,0 +1,47 @@ +/* + * 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 ${groupId}.${rootArtifactId}.plugin.internal; + +import org.wso2.carbon.event.output.adapter.core.OutputEventAdapterService; + +/** + * DataHolder class of fgfgt plugins component. + */ +public class DeviceTypeManagementDataHolder { + + private static DeviceTypeManagementDataHolder thisInstance = new DeviceTypeManagementDataHolder(); + private OutputEventAdapterService outputEventAdapterService; + + private DeviceTypeManagementDataHolder() { + } + + public static DeviceTypeManagementDataHolder getInstance() { + return thisInstance; + } + + public OutputEventAdapterService getOutputEventAdapterService() { + return outputEventAdapterService; + } + + public void setOutputEventAdapterService( + OutputEventAdapterService outputEventAdapterService) { + this.outputEventAdapterService = outputEventAdapterService; + } +} + diff --git a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/internal/ServiceComponent.java b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/internal/ServiceComponent.java index a4f12aff..d675a0ed 100644 --- a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/internal/ServiceComponent.java +++ b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/internal/ServiceComponent.java @@ -18,34 +18,47 @@ package ${groupId}.${rootArtifactId}.plugin.internal; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; +import ${groupId}.${rootArtifactId}.plugin.impl.util.DeviceTypeStartupListener; import ${groupId}.${rootArtifactId}.plugin.exception.DeviceMgtPluginException; import ${groupId}.${rootArtifactId}.plugin.impl.util.DeviceTypeUtils; import ${groupId}.${rootArtifactId}.plugin.impl.DeviceTypeManagerService; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceRegistration; import org.osgi.service.component.ComponentContext; +import org.wso2.carbon.core.ServerStartupObserver; import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; +import org.wso2.carbon.event.output.adapter.core.OutputEventAdapterService; /** * @scr.component name="${groupId}.${rootArtifactId}.plugin.internal.ServiceComponent" * immediate="true" + * @scr.reference name="event.output.adapter.service" + * interface="org.wso2.carbon.event.output.adapter.core.OutputEventAdapterService" + * cardinality="1..1" + * policy="dynamic" + * bind="setOutputEventAdapterService" + * unbind="unsetOutputEventAdapterService" */ public class ServiceComponent { - private ServiceRegistration serviceRegistration; private static final Log log = LogFactory.getLog(ServiceComponent.class); + private ServiceRegistration serviceRegistration; protected void activate(ComponentContext ctx) { if (log.isDebugEnabled()) { - log.debug("Activating ${rootArtifactId} Management Service Component"); + log.debug("Activating b Management Service Component"); } try { + DeviceTypeManagerService deviceTypeManagerService = new DeviceTypeManagerService(); BundleContext bundleContext = ctx.getBundleContext(); serviceRegistration = - bundleContext.registerService(DeviceManagementService.class.getName(), new - DeviceTypeManagerService(), null); + bundleContext.registerService(DeviceManagementService.class.getName(), + deviceTypeManagerService, null); + bundleContext.registerService(ServerStartupObserver.class.getName(), new DeviceTypeStartupListener(), + null); String setupOption = System.getProperty("setup"); if (setupOption != null) { if (log.isDebugEnabled()) { @@ -59,7 +72,7 @@ public class ServiceComponent { } } if (log.isDebugEnabled()) { - log.debug("${rootArtifactId} Management Service Component has been successfully activated"); + log.debug("b Management Service Component has been successfully activated"); } } catch (Throwable e) { log.error("Error occurred while activating Current Sensor Management Service Component", e); @@ -68,7 +81,7 @@ public class ServiceComponent { protected void deactivate(ComponentContext ctx) { if (log.isDebugEnabled()) { - log.debug("De-activating ${rootArtifactId} Management Service Component"); + log.debug("De-activating b Management Service Component"); } try { if (serviceRegistration != null) { @@ -81,4 +94,20 @@ public class ServiceComponent { log.error("Error occurred while de-activating Iot Device Management bundle", e); } } + + /** + * Initialize the Output EventAdapter Service dependency + * + * @param outputEventAdapterService Output EventAdapter Service reference + */ + protected void setOutputEventAdapterService(OutputEventAdapterService outputEventAdapterService) { + DeviceTypeManagementDataHolder.getInstance().setOutputEventAdapterService(outputEventAdapterService); + } + + /** + * De-reference the Output EventAdapter Service dependency. + */ + protected void unsetOutputEventAdapterService(OutputEventAdapterService outputEventAdapterService) { + DeviceTypeManagementDataHolder.getInstance().setOutputEventAdapterService(null); + } } diff --git a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/mqtt/MqttConfig.java b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/mqtt/MqttConfig.java new file mode 100644 index 00000000..f5dbdb09 --- /dev/null +++ b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/mqtt/MqttConfig.java @@ -0,0 +1,60 @@ +/* + * 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 ${groupId}.${rootArtifactId}.plugin.mqtt; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import ${groupId}.${rootArtifactId}.plugin.constants.DeviceTypeConstants; +import ${groupId}.${rootArtifactId}.plugin.impl.util.DeviceTypeUtils; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.util.Properties; + +public class MqttConfig { + + private static String brokerEndpoint; + + private static MqttConfig mqttConfig = new MqttConfig(); + private static final Log log = LogFactory.getLog(MqttConfig.class); + + private MqttConfig() { + File configFile = new File(DeviceTypeConstants.MQTT_CONFIG_LOCATION); + if (configFile.exists()) { + try { + InputStream propertyStream = configFile.toURI().toURL().openStream(); + Properties properties = new Properties(); + properties.load(propertyStream); + brokerEndpoint = DeviceTypeUtils.replaceMqttProperty( + properties.getProperty(DeviceTypeConstants.BROKER_URL_PROPERTY_KEY)); + } catch (IOException e) { + log.error("Failed to read the mqtt.properties file" + e); + } + } + } + + public static MqttConfig getInstance() { + return mqttConfig; + } + + public String getBrokerEndpoint() { + return brokerEndpoint; + } +}