forked from community/device-mgt-core
parent
664a85e805
commit
f511dc2bce
@ -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<Artifact> deviceTypePlugins = new ArrayList<Artifact>();
|
||||
private List<Artifact> deviceTypeUIs = new ArrayList<Artifact>();
|
||||
|
||||
@Override
|
||||
public void deployArtifacts(CarbonApplication carbonApplication, AxisConfiguration axisConfig)
|
||||
throws DeploymentException {
|
||||
List<Artifact.Dependency> 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<Artifact> 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<Artifact.Dependency> 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<Artifact> 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<CappFile> 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);
|
||||
}
|
||||
}
|
||||
}
|
6
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 → 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
6
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 → 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
@ -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<String, String> deviceTypeDeployedUIMap = new ConcurrentHashMap<String, String>();
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue