diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/pom.xml b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/pom.xml index df0314173c..f521ec1437 100644 --- a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/pom.xml +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/pom.xml @@ -66,6 +66,10 @@ org.wso2.carbon.devicemgt org.wso2.carbon.device.mgt.extensions + + org.wso2.carbon + org.wso2.carbon.application.deployer + @@ -108,9 +112,13 @@ org.wso2.carbon.registry.core, org.wso2.carbon.registry.core.*, org.wso2.carbon.utils.*, - javax.xml.namespace + javax.xml.namespace, + org.apache.commons.io, + org.wso2.carbon.application.deployer.*, + org.apache.axis2.engine DeviceTypeDeployer + Device Type Capp Deployer diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/DeviceTypeCAppDeployer.java b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/DeviceTypeCAppDeployer.java new file mode 100644 index 0000000000..e442174e9b --- /dev/null +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/DeviceTypeCAppDeployer.java @@ -0,0 +1,178 @@ +/* + * 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.extensions.device.type.deployer; + +import org.apache.axis2.deployment.Deployer; +import org.apache.axis2.deployment.DeploymentException; +import org.apache.axis2.deployment.repository.util.DeploymentFileData; +import org.apache.axis2.engine.AxisConfiguration; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.application.deployer.AppDeployerConstants; +import org.wso2.carbon.application.deployer.AppDeployerUtils; +import org.wso2.carbon.application.deployer.CarbonApplication; +import org.wso2.carbon.application.deployer.config.Artifact; +import org.wso2.carbon.application.deployer.config.CappFile; +import org.wso2.carbon.application.deployer.handler.AppDeploymentHandler; +import org.wso2.carbon.device.mgt.extensions.device.type.deployer.util.DeviceTypePluginConstants; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +/** + * This is the device deployer that will read and deploy the device type files from + * "deployment/server/carbonapps" + * directory. + */ +public class DeviceTypeCAppDeployer implements AppDeploymentHandler { + + + private static Log log = LogFactory.getLog(DeviceTypePluginDeployer.class); + private List deviceTypePlugins = new ArrayList(); + private List deviceTypeUIs = new ArrayList(); + + @Override + public void deployArtifacts(CarbonApplication carbonApplication, AxisConfiguration axisConfig) + throws DeploymentException { + List artifacts = + carbonApplication.getAppConfig().getApplicationArtifact().getDependencies(); + + for (Artifact.Dependency dep : artifacts) { + Artifact artifact = dep.getArtifact(); + if (!validateArtifact(artifact)) { + continue; + } + addArtifact(artifact); + } + + try { + deployTypeSpecifiedArtifacts(deviceTypeUIs, axisConfig, null, + DeviceTypePluginConstants.CDMF_UI_TYPE_DIR); + deployTypeSpecifiedArtifacts(deviceTypePlugins, axisConfig, + DeviceTypePluginConstants.CDMF_PLUGIN_TYPE_EXTENSION, + DeviceTypePluginConstants.CDMF_PLUGIN_TYPE_DIR); + + } catch (Exception e) { + throw new DeploymentException(e.getMessage(), e); + } finally { + deviceTypePlugins.clear(); + deviceTypeUIs.clear(); + } + + } + + private void deployTypeSpecifiedArtifacts(List artifacts, AxisConfiguration axisConfig, + String fileType, String directory) throws DeploymentException { + for (Artifact artifact : artifacts) { + Deployer deployer = AppDeployerUtils.getArtifactDeployer(axisConfig, directory, fileType); + if (deployer != null) { + deploy(deployer, artifact); + } + } + } + + @Override + public void undeployArtifacts(CarbonApplication carbonApplication, AxisConfiguration axisConfig) + throws DeploymentException { + List artifacts = + carbonApplication.getAppConfig().getApplicationArtifact().getDependencies(); + + deviceTypePlugins.clear(); + deviceTypeUIs.clear(); + + for (Artifact.Dependency dep : artifacts) { + Artifact artifact = dep.getArtifact(); + if (!validateArtifact(artifact)) { + continue; + } + addArtifact(artifact); + } + + try { + undeployTypeSpecifiedArtifacts(deviceTypeUIs, axisConfig, null, + DeviceTypePluginConstants.CDMF_UI_TYPE_DIR); + undeployTypeSpecifiedArtifacts(deviceTypePlugins, axisConfig, + DeviceTypePluginConstants.CDMF_PLUGIN_TYPE_EXTENSION, + DeviceTypePluginConstants.CDMF_PLUGIN_TYPE_DIR); + } finally { + deviceTypePlugins.clear(); + deviceTypeUIs.clear(); + } + + } + + private void undeployTypeSpecifiedArtifacts(List artifacts, AxisConfiguration axisConfig, String fileType + , String directory) throws DeploymentException { + for (Artifact artifact : artifacts) { + Deployer deployer = AppDeployerUtils.getArtifactDeployer(axisConfig, directory, fileType); + if (deployer != null && + AppDeployerConstants.DEPLOYMENT_STATUS_DEPLOYED.equals(artifact.getDeploymentStatus())) { + undeploy(deployer, artifact); + } + } + } + + private boolean validateArtifact(Artifact artifact) { + if (artifact == null) { + return false; + } + List files = artifact.getFiles(); + if (files.size() != 1) { + log.error("Synapse artifact types must have a single file to " + + "be deployed. But " + files.size() + " files found."); + return false; + } + return true; + } + + private void addArtifact(Artifact artifact) { + if (DeviceTypePluginConstants.CDMF_PLUGIN_TYPE.equals(artifact.getType())) { + deviceTypePlugins.add(artifact); + } else if (DeviceTypePluginConstants.CDMF_UI_TYPE.equals(artifact.getType())) { + deviceTypeUIs.add(artifact); + } + } + + void deploy(Deployer deployer, Artifact artifact) throws DeploymentException { + String fileName = artifact.getFiles().get(0).getName(); + String artifactPath = artifact.getExtractedPath() + File.separator + fileName; + try { + deployer.deploy(new DeploymentFileData(new File(artifactPath))); + artifact.setDeploymentStatus(AppDeployerConstants.DEPLOYMENT_STATUS_DEPLOYED); + } catch (Exception e) { + artifact.setDeploymentStatus(AppDeployerConstants.DEPLOYMENT_STATUS_FAILED); + log.error("Deployment is failed due to " + e.getMessage(), e); + throw new DeploymentException(e.getMessage(), e); + } + } + + private void undeploy(Deployer deployer, Artifact artifact) throws DeploymentException { + String fileName = artifact.getFiles().get(0).getName(); + String artifactPath = artifact.getExtractedPath() + File.separator + fileName; + try { + deployer.undeploy(new DeploymentFileData(new File(artifactPath), deployer).getAbsolutePath()); + artifact.setDeploymentStatus(AppDeployerConstants.DEPLOYMENT_STATUS_PENDING); + } catch (Exception e) { + artifact.setDeploymentStatus(AppDeployerConstants.DEPLOYMENT_STATUS_FAILED); + log.error("Undeployment is failed due to " + e.getMessage(), e); + throw new DeploymentException(e.getMessage(), e); + } + } +} diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/DeviceTypeDeployer.java b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/DeviceTypePluginDeployer.java similarity index 97% rename from components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/DeviceTypeDeployer.java rename to components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/DeviceTypePluginDeployer.java index ff74abb0a3..5dfa895f39 100644 --- a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/DeviceTypeDeployer.java +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/DeviceTypePluginDeployer.java @@ -44,12 +44,12 @@ import java.util.Map; import java.util.concurrent.ConcurrentHashMap; /** - * This is the device deployer that will read and deploy the device type files from "deployment/server/devicetypes" + * This is the device deployer that will read and deploy the device type plugin files from "deployment/server/devicetypes" * directory. */ -public class DeviceTypeDeployer extends AbstractDeployer { +public class DeviceTypePluginDeployer extends AbstractDeployer { - private static Log log = LogFactory.getLog(DeviceTypeDeployer.class); + private static Log log = LogFactory.getLog(DeviceTypePluginDeployer.class); private ConfigurationContext configurationContext; protected Map deviceTypeServiceRegistrations = new ConcurrentHashMap(); protected Map deviceTypeConfigurationDataMap = new ConcurrentHashMap(); diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/DeviceTypeUIDeployer.java b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/DeviceTypeUIDeployer.java new file mode 100644 index 0000000000..5ee352751e --- /dev/null +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/DeviceTypeUIDeployer.java @@ -0,0 +1,123 @@ +/* + * 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.extensions.device.type.deployer; + +import org.apache.axis2.context.ConfigurationContext; +import org.apache.axis2.deployment.AbstractDeployer; +import org.apache.axis2.deployment.DeploymentException; +import org.apache.axis2.deployment.repository.util.DeploymentFileData; +import org.apache.commons.io.FileUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.context.PrivilegedCarbonContext; +import org.wso2.carbon.utils.CarbonUtils; + +import java.io.File; +import java.io.IOException; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +/** + * This is the device deployer that will read and deploy the device type ui files from + * "deployment/server/devicetypes-ui" + * directory. + */ +public class DeviceTypeUIDeployer extends AbstractDeployer { + + private static Log log = LogFactory.getLog(DeviceTypeUIDeployer.class); + protected Map deviceTypeDeployedUIMap = new ConcurrentHashMap(); + private static final String DEVICEMGT_JAGGERY_APP_PATH = CarbonUtils.getCarbonRepository() + File.separator + + "jaggeryapps" + File.separator + "devicemgt" + File.separator + "app" + File.separator + "units" + + File.separator; + + private static final String UNIT_PREFIX = "cdmf.unit.device.type"; + + @Override + public void init(ConfigurationContext configurationContext) { + } + + @Override + public void setDirectory(String s) { + + } + + @Override + public void setExtension(String s) { + + } + + @Override + public void deploy(DeploymentFileData deploymentFileData) throws DeploymentException { + if (!deploymentFileData.getFile().isDirectory()) { + return; + } + String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(true); + if (tenantDomain != null && !tenantDomain.isEmpty()) { + File jaggeryAppPath = new File( + DEVICEMGT_JAGGERY_APP_PATH + tenantDomain + "." + deploymentFileData.getName()); + try { + if (!jaggeryAppPath.exists()) { + FileUtils.forceMkdir(jaggeryAppPath); + FileUtils.copyDirectory(deploymentFileData.getFile(), jaggeryAppPath); + File[] listOfFiles = jaggeryAppPath.listFiles(); + + for (int i = 0; i < listOfFiles.length; i++) { + if (listOfFiles[i].isFile()) { + String content = FileUtils.readFileToString(listOfFiles[i]); + FileUtils.writeStringToFile(listOfFiles[i], content.replaceAll(UNIT_PREFIX + , tenantDomain + "." + UNIT_PREFIX)); + } + } + } else { + log.debug("units already exists " + deploymentFileData.getName()); + } + this.deviceTypeDeployedUIMap.put(deploymentFileData.getAbsolutePath(), + jaggeryAppPath.getAbsolutePath()); + } catch (IOException e) { + if (jaggeryAppPath.exists()) { + try { + FileUtils.deleteDirectory(jaggeryAppPath); + } catch (IOException e1) { + log.error("Failed to delete directory " + jaggeryAppPath.getAbsolutePath()); + } + } + log.error("Cannot deploy deviceType ui : " + deploymentFileData.getName(), e); + throw new DeploymentException( + "Device type ui file " + deploymentFileData.getName() + " is not deployed ", e); + } + + } else { + log.error("Cannot deploy deviceType ui: " + deploymentFileData.getName()); + } + + + } + + @Override + public void undeploy(String filePath) throws DeploymentException { + try { + String jaggeryUnitPath = this.deviceTypeDeployedUIMap.remove(filePath); + FileUtils.deleteDirectory(new File(jaggeryUnitPath)); + log.info("Device Type units un deployed successfully."); + } catch (IOException e) { + throw new DeploymentException("Failed to remove the units: " + filePath); + } + } + +} diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/util/DeviceTypePluginConstants.java b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/util/DeviceTypePluginConstants.java index 6058f10fc2..290fe93b7b 100644 --- a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/util/DeviceTypePluginConstants.java +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/util/DeviceTypePluginConstants.java @@ -25,4 +25,11 @@ public class DeviceTypePluginConstants { public static final String MEDIA_TYPE_XML = "application/xml"; public static final String CHARSET_UTF8 = "UTF8"; public static final String LANGUAGE_CODE_ENGLISH_US = "en_US"; + + public static final String CDMF_UI_TYPE = "devicetype/ui"; + public static final String CDMF_UI_TYPE_DIR = "devicetypes-ui"; + + public static final String CDMF_PLUGIN_TYPE = "devicetype/plugin"; + public static final String CDMF_PLUGIN_TYPE_DIR = "devicetypes"; + public static final String CDMF_PLUGIN_TYPE_EXTENSION = "xml"; } diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/resources/META-INF/component.xml b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/resources/META-INF/component.xml index c1c5f52048..71045cbf72 100644 --- a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/resources/META-INF/component.xml +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/resources/META-INF/component.xml @@ -17,7 +17,11 @@ devicetypes xml - org.wso2.carbon.device.mgt.extensions.device.type.deployer.DeviceTypeDeployer + org.wso2.carbon.device.mgt.extensions.device.type.deployer.DeviceTypePluginDeployer + + devicetypes-ui + org.wso2.carbon.device.mgt.extensions.device.type.deployer.DeviceTypeUIDeployer +