forked from community/device-mgt-plugins
parent
c7effff3ce
commit
64cdb33d67
Binary file not shown.
@ -1,470 +0,0 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/* graph */
|
||||
|
||||
.rickshaw_graph {
|
||||
position: relative;
|
||||
}
|
||||
|
||||
.rickshaw_graph svg {
|
||||
display: block;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
/* ticks */
|
||||
|
||||
.rickshaw_graph .x_tick {
|
||||
position: absolute;
|
||||
top: 0;
|
||||
bottom: 0;
|
||||
width: 0;
|
||||
border-left: 1px dotted rgba(0, 0, 0, 0.2);
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
.rickshaw_graph .x_tick .title {
|
||||
position: absolute;
|
||||
font-size: 12px;
|
||||
font-family: Arial, sans-serif;
|
||||
opacity: 0.5;
|
||||
white-space: nowrap;
|
||||
margin-left: 3px;
|
||||
bottom: -20px;
|
||||
height: auto;
|
||||
border-bottom: none;
|
||||
}
|
||||
|
||||
/* annotations */
|
||||
|
||||
.rickshaw_annotation_timeline {
|
||||
height: 1px;
|
||||
border-top: 1px solid #e0e0e0;
|
||||
margin-top: 10px;
|
||||
position: relative;
|
||||
}
|
||||
|
||||
.rickshaw_annotation_timeline .annotation {
|
||||
position: absolute;
|
||||
height: 6px;
|
||||
width: 6px;
|
||||
margin-left: -2px;
|
||||
top: -3px;
|
||||
border-radius: 5px;
|
||||
background-color: rgba(0, 0, 0, 0.25);
|
||||
}
|
||||
|
||||
.rickshaw_graph .annotation_line {
|
||||
position: absolute;
|
||||
top: 0;
|
||||
bottom: -6px;
|
||||
width: 0;
|
||||
border-left: 2px solid rgba(0, 0, 0, 0.3);
|
||||
display: none;
|
||||
}
|
||||
|
||||
.rickshaw_graph .annotation_line.active {
|
||||
display: block;
|
||||
}
|
||||
|
||||
.rickshaw_graph .annotation_range {
|
||||
background: rgba(0, 0, 0, 0.1);
|
||||
display: none;
|
||||
position: absolute;
|
||||
top: 0;
|
||||
bottom: -6px;
|
||||
}
|
||||
|
||||
.rickshaw_graph .annotation_range.active {
|
||||
display: block;
|
||||
}
|
||||
|
||||
.rickshaw_graph .annotation_range.active.offscreen {
|
||||
display: none;
|
||||
}
|
||||
|
||||
.rickshaw_annotation_timeline .annotation .content {
|
||||
background: white;
|
||||
color: black;
|
||||
opacity: 0.9;
|
||||
box-shadow: 0 0 2px rgba(0, 0, 0, 0.8);
|
||||
border-radius: 3px;
|
||||
position: relative;
|
||||
z-index: 20;
|
||||
font-size: 12px;
|
||||
padding: 6px 8px 8px;
|
||||
top: 18px;
|
||||
left: -11px;
|
||||
width: 160px;
|
||||
display: none;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.rickshaw_annotation_timeline .annotation .content:before {
|
||||
content: "\25b2";
|
||||
position: absolute;
|
||||
top: -11px;
|
||||
color: white;
|
||||
text-shadow: 0 -1px 1px rgba(0, 0, 0, 0.8);
|
||||
}
|
||||
|
||||
.rickshaw_annotation_timeline .annotation.active,
|
||||
.rickshaw_annotation_timeline .annotation:hover {
|
||||
background-color: rgba(0, 0, 0, 0.8);
|
||||
cursor: none;
|
||||
}
|
||||
|
||||
.rickshaw_annotation_timeline .annotation .content:hover {
|
||||
z-index: 50;
|
||||
}
|
||||
|
||||
.rickshaw_annotation_timeline .annotation.active .content {
|
||||
display: block;
|
||||
}
|
||||
|
||||
.rickshaw_annotation_timeline .annotation:hover .content {
|
||||
display: block;
|
||||
z-index: 50;
|
||||
}
|
||||
|
||||
.rickshaw_graph .y_axis,
|
||||
.rickshaw_graph .x_axis_d3 {
|
||||
fill: none;
|
||||
}
|
||||
|
||||
.rickshaw_graph .y_ticks .tick line,
|
||||
.rickshaw_graph .x_ticks_d3 .tick {
|
||||
stroke: rgba(0, 0, 0, 0.16);
|
||||
stroke-width: 2px;
|
||||
shape-rendering: crisp-edges;
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
.rickshaw_graph .y_grid .tick,
|
||||
.rickshaw_graph .x_grid_d3 .tick {
|
||||
z-index: -1;
|
||||
stroke: rgba(0, 0, 0, 0.20);
|
||||
stroke-width: 1px;
|
||||
stroke-dasharray: 1 1;
|
||||
}
|
||||
|
||||
.rickshaw_graph .y_grid .tick[data-y-value="0"] {
|
||||
stroke-dasharray: 1 0;
|
||||
}
|
||||
|
||||
.rickshaw_graph .y_grid path,
|
||||
.rickshaw_graph .x_grid_d3 path {
|
||||
fill: none;
|
||||
stroke: none;
|
||||
}
|
||||
|
||||
.rickshaw_graph .y_ticks path,
|
||||
.rickshaw_graph .x_ticks_d3 path {
|
||||
fill: none;
|
||||
stroke: #808080;
|
||||
}
|
||||
|
||||
.rickshaw_graph .y_ticks text,
|
||||
.rickshaw_graph .x_ticks_d3 text {
|
||||
opacity: 0.5;
|
||||
font-size: 12px;
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
.rickshaw_graph .x_tick.glow .title,
|
||||
.rickshaw_graph .y_ticks.glow text {
|
||||
fill: black;
|
||||
color: black;
|
||||
text-shadow: -1px 1px 0 rgba(255, 255, 255, 0.1),
|
||||
1px -1px 0 rgba(255, 255, 255, 0.1),
|
||||
1px 1px 0 rgba(255, 255, 255, 0.1),
|
||||
0 1px 0 rgba(255, 255, 255, 0.1),
|
||||
0 -1px 0 rgba(255, 255, 255, 0.1),
|
||||
1px 0 0 rgba(255, 255, 255, 0.1),
|
||||
-1px 0 0 rgba(255, 255, 255, 0.1),
|
||||
-1px -1px 0 rgba(255, 255, 255, 0.1);
|
||||
}
|
||||
|
||||
.rickshaw_graph .x_tick.inverse .title,
|
||||
.rickshaw_graph .y_ticks.inverse text {
|
||||
fill: white;
|
||||
color: white;
|
||||
text-shadow: -1px 1px 0 rgba(0, 0, 0, 0.8),
|
||||
1px -1px 0 rgba(0, 0, 0, 0.8),
|
||||
1px 1px 0 rgba(0, 0, 0, 0.8),
|
||||
0 1px 0 rgba(0, 0, 0, 0.8),
|
||||
0 -1px 0 rgba(0, 0, 0, 0.8),
|
||||
1px 0 0 rgba(0, 0, 0, 0.8),
|
||||
-1px 0 0 rgba(0, 0, 0, 0.8),
|
||||
-1px -1px 0 rgba(0, 0, 0, 0.8);
|
||||
}
|
||||
|
||||
.custom_rickshaw_graph {
|
||||
position: relative;
|
||||
left: 40px;
|
||||
}
|
||||
|
||||
.custom_y_axis {
|
||||
position: absolute;
|
||||
width: 40px;
|
||||
}
|
||||
|
||||
.custom_slider {
|
||||
left: 40px;
|
||||
}
|
||||
|
||||
.custom_x_axis {
|
||||
position: relative;
|
||||
left: 40px;
|
||||
height: 30px;
|
||||
width: 97%;
|
||||
top: 20px;
|
||||
text-align: right;
|
||||
}
|
||||
|
||||
.chartWrapper {
|
||||
padding-top: 50px;
|
||||
}
|
||||
|
||||
/*detail*/
|
||||
|
||||
.rickshaw_graph .detail {
|
||||
pointer-events: none;
|
||||
position: absolute;
|
||||
top: 0;
|
||||
z-index: 2;
|
||||
background: rgba(0, 0, 0, 0.1);
|
||||
bottom: 0;
|
||||
width: 1px;
|
||||
transition: opacity 0.25s linear;
|
||||
-moz-transition: opacity 0.25s linear;
|
||||
-o-transition: opacity 0.25s linear;
|
||||
-webkit-transition: opacity 0.25s linear;
|
||||
}
|
||||
|
||||
.rickshaw_graph .detail.inactive {
|
||||
opacity: 0;
|
||||
}
|
||||
|
||||
.rickshaw_graph .detail .item.active {
|
||||
opacity: 1;
|
||||
}
|
||||
|
||||
.rickshaw_graph .detail .x_label {
|
||||
font-family: Arial, sans-serif;
|
||||
border-radius: 3px;
|
||||
padding: 6px;
|
||||
opacity: 0.5;
|
||||
border: 1px solid #e0e0e0;
|
||||
font-size: 12px;
|
||||
position: absolute;
|
||||
background: white;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.rickshaw_graph .detail .x_label.left {
|
||||
left: 0;
|
||||
}
|
||||
|
||||
.rickshaw_graph .detail .x_label.right {
|
||||
right: 0;
|
||||
}
|
||||
|
||||
.rickshaw_graph .detail .item {
|
||||
position: absolute;
|
||||
z-index: 2;
|
||||
border-radius: 3px;
|
||||
padding: 0.25em;
|
||||
font-size: 12px;
|
||||
font-family: Arial, sans-serif;
|
||||
opacity: 0;
|
||||
background: rgba(0, 0, 0, 0.4);
|
||||
color: white;
|
||||
border: 1px solid rgba(0, 0, 0, 0.4);
|
||||
margin-left: 1em;
|
||||
margin-right: 1em;
|
||||
margin-top: -1em;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.rickshaw_graph .detail .item.left {
|
||||
left: 0;
|
||||
}
|
||||
|
||||
.rickshaw_graph .detail .item.right {
|
||||
right: 0;
|
||||
}
|
||||
|
||||
.rickshaw_graph .detail .item.active {
|
||||
opacity: 1;
|
||||
background: rgba(0, 0, 0, 0.8);
|
||||
}
|
||||
|
||||
.rickshaw_graph .detail .item:after {
|
||||
position: absolute;
|
||||
display: block;
|
||||
width: 0;
|
||||
height: 0;
|
||||
|
||||
content: "";
|
||||
|
||||
border: 5px solid transparent;
|
||||
}
|
||||
|
||||
.rickshaw_graph .detail .item.left:after {
|
||||
top: 1em;
|
||||
left: -5px;
|
||||
margin-top: -5px;
|
||||
border-right-color: rgba(0, 0, 0, 0.8);
|
||||
border-left-width: 0;
|
||||
}
|
||||
|
||||
.rickshaw_graph .detail .item.right:after {
|
||||
top: 1em;
|
||||
right: -5px;
|
||||
margin-top: -5px;
|
||||
border-left-color: rgba(0, 0, 0, 0.8);
|
||||
border-right-width: 0;
|
||||
}
|
||||
|
||||
.rickshaw_graph .detail .dot {
|
||||
width: 4px;
|
||||
height: 4px;
|
||||
margin-left: -3px;
|
||||
margin-top: -3.5px;
|
||||
border-radius: 5px;
|
||||
position: absolute;
|
||||
box-shadow: 0 0 2px rgba(0, 0, 0, 0.6);
|
||||
box-sizing: content-box;
|
||||
-moz-box-sizing: content-box;
|
||||
background: white;
|
||||
border-width: 2px;
|
||||
border-style: solid;
|
||||
display: none;
|
||||
background-clip: padding-box;
|
||||
}
|
||||
|
||||
.rickshaw_graph .detail .dot.active {
|
||||
display: block;
|
||||
}
|
||||
|
||||
/*legend*/
|
||||
.rickshaw_legend {
|
||||
font-family: Arial;
|
||||
font-size: 12px;
|
||||
color: white;
|
||||
background: #404040;
|
||||
display: inline-block;
|
||||
padding: 12px 5px;
|
||||
border-radius: 2px;
|
||||
position: relative;
|
||||
float: right;
|
||||
}
|
||||
|
||||
.rickshaw_legend:hover {
|
||||
z-index: 10;
|
||||
}
|
||||
|
||||
.rickshaw_legend .swatch {
|
||||
width: 10px;
|
||||
height: 10px;
|
||||
border: 1px solid rgba(0, 0, 0, 0.2);
|
||||
}
|
||||
|
||||
.rickshaw_legend .line {
|
||||
clear: both;
|
||||
line-height: 140%;
|
||||
padding-right: 15px;
|
||||
}
|
||||
|
||||
.rickshaw_legend .line .swatch {
|
||||
display: inline-block;
|
||||
margin-right: 3px;
|
||||
border-radius: 2px;
|
||||
}
|
||||
|
||||
.rickshaw_legend .label {
|
||||
margin: 0;
|
||||
white-space: nowrap;
|
||||
display: inline;
|
||||
font-size: inherit;
|
||||
background-color: transparent;
|
||||
color: inherit;
|
||||
font-weight: normal;
|
||||
line-height: normal;
|
||||
padding: 0;
|
||||
text-shadow: none;
|
||||
}
|
||||
|
||||
.rickshaw_legend .action:hover {
|
||||
opacity: 0.6;
|
||||
}
|
||||
|
||||
.rickshaw_legend .action {
|
||||
margin-right: 0.2em;
|
||||
opacity: 0.2;
|
||||
cursor: pointer;
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
.rickshaw_legend .line.disabled {
|
||||
opacity: 0.4;
|
||||
}
|
||||
|
||||
.rickshaw_legend ul {
|
||||
list-style-type: none;
|
||||
padding: 0;
|
||||
margin: 2px;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.rickshaw_legend li {
|
||||
padding: 0 0 0 2px;
|
||||
min-width: 80px;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.rickshaw_legend li:hover {
|
||||
background: rgba(255, 255, 255, 0.08);
|
||||
border-radius: 3px;
|
||||
}
|
||||
|
||||
.rickshaw_legend li:active {
|
||||
background: rgba(255, 255, 255, 0.2);
|
||||
border-radius: 3px;
|
||||
}
|
||||
|
||||
.legend {
|
||||
display: inline-block;
|
||||
position: relative;
|
||||
left: 8px;
|
||||
}
|
||||
|
||||
.legend_container {
|
||||
float: right;
|
||||
padding-right: 10px;
|
||||
width: 0;
|
||||
z-index: 1;
|
||||
position: relative;
|
||||
opacity: 0.7;
|
||||
}
|
||||
|
||||
.spaced {
|
||||
margin-top: 20px !important;
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@ -1,43 +0,0 @@
|
||||
/*
|
||||
* 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.exception;
|
||||
|
||||
public class IoTException extends Exception {
|
||||
|
||||
public IoTException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public IoTException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public IoTException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
public IoTException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
protected IoTException(String message, Throwable cause, boolean enableSuppression,
|
||||
boolean writableStackTrace) {
|
||||
super(message, cause, enableSuppression, writableStackTrace);
|
||||
}
|
||||
}
|
@ -1,45 +0,0 @@
|
||||
/*
|
||||
* 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.util;
|
||||
|
||||
import org.wso2.carbon.base.ServerConfiguration;
|
||||
import org.wso2.carbon.device.mgt.iot.exception.IoTException;
|
||||
import org.wso2.carbon.utils.NetworkUtils;
|
||||
|
||||
import java.net.SocketException;
|
||||
|
||||
public class IoTUtil {
|
||||
|
||||
public static final String HOST_NAME = "HostName";
|
||||
|
||||
public static String getHostName() throws IoTException {
|
||||
String hostName = ServerConfiguration.getInstance().getFirstProperty(HOST_NAME);
|
||||
|
||||
try {
|
||||
if (hostName == null) {
|
||||
hostName = NetworkUtils.getLocalHostname();
|
||||
}
|
||||
} catch (SocketException e) {
|
||||
throw new IoTException("Error while trying to read hostname.", e);
|
||||
}
|
||||
|
||||
return hostName;
|
||||
}
|
||||
|
||||
}
|
@ -1,319 +0,0 @@
|
||||
/*
|
||||
* 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.util;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
|
||||
import org.wso2.carbon.utils.CarbonUtils;
|
||||
import java.io.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.*;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
/**
|
||||
* Provides utility methods required by the iot device management bundle.
|
||||
*/
|
||||
public class IotDeviceManagementUtil {
|
||||
|
||||
private static final Log log = LogFactory.getLog(IotDeviceManagementUtil.class.getName());
|
||||
|
||||
public static ZipArchive getSketchArchive(String archivesPath, String templateSketchPath, Map contextParams)
|
||||
throws DeviceManagementException, IOException {
|
||||
|
||||
String sep = File.separator;
|
||||
String sketchPath = CarbonUtils.getCarbonHome() + sep + templateSketchPath;
|
||||
|
||||
FileUtils.deleteDirectory(new File(archivesPath));//clear directory
|
||||
FileUtils.deleteDirectory(new File(archivesPath + ".zip"));//clear zip
|
||||
if (!new File(archivesPath).mkdirs()) { //new dir
|
||||
String message = "Could not create directory at path: " + archivesPath;
|
||||
log.error(message);
|
||||
throw new DeviceManagementException(message);
|
||||
}
|
||||
|
||||
String zipFileName = "zipFile.zip";
|
||||
|
||||
try {
|
||||
Map<String, List<String>> properties = getProperties(sketchPath + sep + "sketch" + ".properties");
|
||||
List<String> templateFiles = properties.get("templates");
|
||||
|
||||
// zipFileName = properties.get("zipfilename").get(0);
|
||||
zipFileName = contextParams.get("DEVICE_NAME") + ".zip";
|
||||
|
||||
for (String templateFile : templateFiles) {
|
||||
parseTemplate(templateSketchPath + sep + templateFile, archivesPath + sep + templateFile,
|
||||
contextParams);
|
||||
}
|
||||
|
||||
templateFiles.add("sketch.properties"); // ommit copying the props file
|
||||
copyFolder(new File(sketchPath), new File(archivesPath), templateFiles);
|
||||
|
||||
} catch (IOException ex) {
|
||||
throw new DeviceManagementException(
|
||||
"Error occurred when trying to read property " + "file sketch.properties", ex);
|
||||
}
|
||||
|
||||
try {
|
||||
createZipArchive(archivesPath);
|
||||
} catch (IOException e) {
|
||||
String message = "Zip file for the specific device agent not found at path: " + archivesPath;
|
||||
log.error(message);
|
||||
log.error(e);
|
||||
throw new DeviceManagementException(message, e);
|
||||
}
|
||||
FileUtils.deleteDirectory(new File(archivesPath));//clear folder
|
||||
|
||||
/* now get the zip file */
|
||||
File zip = new File(archivesPath + ".zip");
|
||||
return new ZipArchive(zipFileName, zip);
|
||||
}
|
||||
|
||||
private static Map<String, List<String>> getProperties(String propertyFilePath) throws IOException {
|
||||
Properties prop = new Properties();
|
||||
InputStream input = null;
|
||||
|
||||
try {
|
||||
|
||||
input = new FileInputStream(propertyFilePath);
|
||||
|
||||
// load a properties file
|
||||
prop.load(input);
|
||||
Map<String, List<String>> properties = new HashMap<String, List<String>>();
|
||||
|
||||
String templates = prop.getProperty("templates");
|
||||
List<String> list = new ArrayList<String>(Arrays.asList(templates.split(",")));
|
||||
properties.put("templates", list);
|
||||
|
||||
final String filename = prop.getProperty("zipfilename");
|
||||
list = new ArrayList<String>() {{
|
||||
add(filename);
|
||||
}};
|
||||
properties.put("zipfilename", list);
|
||||
return properties;
|
||||
|
||||
} finally {
|
||||
if (input != null) {
|
||||
try {
|
||||
input.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void parseTemplate(String srcFile, String dstFile, Map contextParams) throws IOException {
|
||||
//TODO add velocity 1.7, currently commented
|
||||
//TODO conflicting when calling in CXF environment with the opensaml orbit
|
||||
|
||||
// /* create a context and add data */
|
||||
// VelocityContext context = new VelocityContext(contextParams);
|
||||
//
|
||||
// /* first, get and initialize an engine */
|
||||
// VelocityEngine ve = new VelocityEngine();
|
||||
// ve.setProperty( RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS,
|
||||
// "org.apache.velocity.runtime.log.Log4JLogChute" );
|
||||
// ve.setProperty("runtime.log.logsystem.log4j.logger", IotDeviceManagementUtil.class.getName());
|
||||
// ve.init();
|
||||
//
|
||||
// String sep = File.separator;
|
||||
// Template t = ve.getTemplate(srcFile);
|
||||
// FileWriter writer = null;
|
||||
// try {
|
||||
// writer = new FileWriter(dstFile);
|
||||
// t.merge(context, writer);
|
||||
// } finally {
|
||||
// if (writer != null) {
|
||||
// writer.flush();
|
||||
// writer.close();
|
||||
// }
|
||||
// }
|
||||
|
||||
//read from file
|
||||
FileInputStream inputStream = new FileInputStream(srcFile);
|
||||
String content = IOUtils.toString(inputStream, StandardCharsets.UTF_8.toString());
|
||||
Iterator iterator = contextParams.entrySet().iterator();
|
||||
while (iterator.hasNext()) {
|
||||
Map.Entry mapEntry = (Map.Entry) iterator.next();
|
||||
content = content.replaceAll("\\$\\{" + mapEntry.getKey() + "\\}", mapEntry.getValue().toString());
|
||||
}
|
||||
if (inputStream != null) {
|
||||
inputStream.close();
|
||||
}
|
||||
//write to file
|
||||
FileOutputStream outputStream = new FileOutputStream(dstFile);
|
||||
IOUtils.write(content, outputStream, StandardCharsets.UTF_8.toString());
|
||||
if (outputStream != null) {
|
||||
outputStream.close();
|
||||
}
|
||||
}
|
||||
|
||||
private static void copyFolder(File src, File dest, List<String> excludeFileNames) throws IOException {
|
||||
|
||||
if (src.isDirectory()) {
|
||||
|
||||
//if directory not exists, create it
|
||||
if (!dest.exists() && !dest.mkdirs()) {
|
||||
String message = "Could not create directory at path: " + dest;
|
||||
log.error(message);
|
||||
throw new IOException(message);
|
||||
}
|
||||
|
||||
//list all the directory contents
|
||||
String files[] = src.list();
|
||||
|
||||
if (files == null) {
|
||||
log.warn("There are no files insides the directory " + src.getAbsolutePath());
|
||||
return;
|
||||
}
|
||||
|
||||
for (String file : files) {
|
||||
//construct the src and dest file structure
|
||||
File srcFile = new File(src, file);
|
||||
File destFile = new File(dest, file);
|
||||
//recursive copy
|
||||
copyFolder(srcFile, destFile, excludeFileNames);
|
||||
}
|
||||
|
||||
} else {
|
||||
for (String fileName : excludeFileNames) {
|
||||
if (src.getName().equals(fileName)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
//if file, then copy it
|
||||
//Use bytes stream to support all file types
|
||||
InputStream in = null;
|
||||
OutputStream out = null;
|
||||
|
||||
try {
|
||||
in = new FileInputStream(src);
|
||||
out = new FileOutputStream(dest);
|
||||
|
||||
byte[] buffer = new byte[1024];
|
||||
|
||||
int length;
|
||||
//copy the file content in bytes
|
||||
while ((length = in.read(buffer)) > 0) {
|
||||
out.write(buffer, 0, length);
|
||||
}
|
||||
} finally {
|
||||
silentClose(in);
|
||||
silentClose(out);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void silentClose(InputStream is) {
|
||||
if (is == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
is.close();
|
||||
} catch (IOException e) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static void silentClose(OutputStream os) {
|
||||
if (os == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
os.close();
|
||||
} catch (IOException e) {
|
||||
// do nothing
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean createZipArchive(String srcFolder) throws IOException {
|
||||
BufferedInputStream origin = null;
|
||||
ZipOutputStream out = null;
|
||||
|
||||
try {
|
||||
final int BUFFER = 2048;
|
||||
|
||||
FileOutputStream dest = new FileOutputStream(new File(srcFolder + ".zip"));
|
||||
|
||||
out = new ZipOutputStream(new BufferedOutputStream(dest));
|
||||
byte data[] = new byte[BUFFER];
|
||||
|
||||
File subDir = new File(srcFolder);
|
||||
String subdirList[] = subDir.list();
|
||||
|
||||
if (subdirList == null) {
|
||||
log.warn("The sub directory " + subDir.getAbsolutePath() + " is empty");
|
||||
return false;
|
||||
}
|
||||
|
||||
for (String sd : subdirList) {
|
||||
// get a list of files from current directory
|
||||
File f = new File(srcFolder + "/" + sd);
|
||||
if (f.isDirectory()) {
|
||||
String files[] = f.list();
|
||||
|
||||
if (files == null) {
|
||||
log.warn("The current directory " + f.getAbsolutePath() + " is empty. Has no files");
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 0; i < files.length; i++) {
|
||||
FileInputStream fi = new FileInputStream(srcFolder + "/" + sd + "/" + files[i]);
|
||||
origin = new BufferedInputStream(fi, BUFFER);
|
||||
ZipEntry entry = new ZipEntry(sd + "/" + files[i]);
|
||||
out.putNextEntry(entry);
|
||||
int count;
|
||||
while ((count = origin.read(data, 0, BUFFER)) != -1) {
|
||||
out.write(data, 0, count);
|
||||
out.flush();
|
||||
}
|
||||
|
||||
}
|
||||
} else //it is just a file
|
||||
{
|
||||
FileInputStream fi = new FileInputStream(f);
|
||||
origin = new BufferedInputStream(fi, BUFFER);
|
||||
ZipEntry entry = new ZipEntry(sd);
|
||||
out.putNextEntry(entry);
|
||||
int count;
|
||||
while ((count = origin.read(data, 0, BUFFER)) != -1) {
|
||||
out.write(data, 0, count);
|
||||
out.flush();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
out.flush();
|
||||
} finally {
|
||||
silentClose(origin);
|
||||
silentClose(out);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,304 @@
|
||||
/*
|
||||
* 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.util;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.wso2.carbon.base.ServerConfiguration;
|
||||
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
|
||||
import org.wso2.carbon.utils.CarbonUtils;
|
||||
import org.wso2.carbon.utils.NetworkUtils;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.SocketException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
/**
|
||||
* Provides utility methods required by the device type plugins.
|
||||
*/
|
||||
public class Utils {
|
||||
|
||||
public static final String HOST_NAME = "HostName";
|
||||
private static final Log log = LogFactory.getLog(Utils.class);
|
||||
|
||||
public static String getHostName() {
|
||||
String hostName = ServerConfiguration.getInstance().getFirstProperty(HOST_NAME);
|
||||
try {
|
||||
if (hostName == null) {
|
||||
hostName = NetworkUtils.getLocalHostname();
|
||||
}
|
||||
} catch (SocketException e) {
|
||||
hostName = "localhost";
|
||||
}
|
||||
return hostName;
|
||||
}
|
||||
|
||||
public static ZipArchive getSketchArchive(String archivesPath, String templateSketchPath, Map contextParams,
|
||||
String zipFileName)
|
||||
throws DeviceManagementException, IOException {
|
||||
String sketchPath = CarbonUtils.getCarbonHome() + File.separator + templateSketchPath;
|
||||
FileUtils.deleteDirectory(new File(archivesPath));//clear directory
|
||||
FileUtils.deleteDirectory(new File(archivesPath + ".zip"));//clear zip
|
||||
if (!new File(archivesPath).mkdirs()) { //new dir
|
||||
String message = "Could not create directory at path: " + archivesPath;
|
||||
log.error(message);
|
||||
throw new DeviceManagementException(message);
|
||||
}
|
||||
zipFileName = zipFileName + ".zip";
|
||||
try {
|
||||
Map<String, List<String>> properties = getProperties(sketchPath + File.separator + "sketch" + ".properties");
|
||||
List<String> templateFiles = properties.get("templates");
|
||||
|
||||
for (String templateFile : templateFiles) {
|
||||
parseTemplate(templateSketchPath + File.separator + templateFile, archivesPath + File.separator + templateFile,
|
||||
contextParams);
|
||||
}
|
||||
|
||||
templateFiles.add("sketch.properties"); // ommit copying the props file
|
||||
copyFolder(new File(sketchPath), new File(archivesPath), templateFiles);
|
||||
|
||||
} catch (IOException ex) {
|
||||
throw new DeviceManagementException(
|
||||
"Error occurred when trying to read property " + "file sketch.properties", ex);
|
||||
}
|
||||
|
||||
try {
|
||||
createZipArchive(archivesPath);
|
||||
} catch (IOException e) {
|
||||
String message = "Zip file for the specific device agent not found at path: " + archivesPath;
|
||||
log.error(message);
|
||||
log.error(e);
|
||||
throw new DeviceManagementException(message, e);
|
||||
}
|
||||
FileUtils.deleteDirectory(new File(archivesPath));//clear folder
|
||||
|
||||
/* now get the zip file */
|
||||
File zip = new File(archivesPath + ".zip");
|
||||
return new ZipArchive(zipFileName, zip);
|
||||
}
|
||||
|
||||
private static Map<String, List<String>> getProperties(String propertyFilePath) throws IOException {
|
||||
Properties prop = new Properties();
|
||||
InputStream input = null;
|
||||
|
||||
try {
|
||||
|
||||
input = new FileInputStream(propertyFilePath);
|
||||
|
||||
// load a properties file
|
||||
prop.load(input);
|
||||
Map<String, List<String>> properties = new HashMap<String, List<String>>();
|
||||
|
||||
String templates = prop.getProperty("templates");
|
||||
List<String> list = new ArrayList<String>(Arrays.asList(templates.split(",")));
|
||||
properties.put("templates", list);
|
||||
|
||||
final String filename = prop.getProperty("zipfilename");
|
||||
list = new ArrayList<String>() {{
|
||||
add(filename);
|
||||
}};
|
||||
properties.put("zipfilename", list);
|
||||
return properties;
|
||||
|
||||
} finally {
|
||||
if (input != null) {
|
||||
try {
|
||||
input.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void parseTemplate(String srcFile, String dstFile, Map contextParams) throws IOException {
|
||||
//read from file
|
||||
FileInputStream inputStream = new FileInputStream(srcFile);
|
||||
String content = IOUtils.toString(inputStream, StandardCharsets.UTF_8.toString());
|
||||
Iterator iterator = contextParams.entrySet().iterator();
|
||||
while (iterator.hasNext()) {
|
||||
Map.Entry mapEntry = (Map.Entry) iterator.next();
|
||||
content = content.replaceAll("\\$\\{" + mapEntry.getKey() + "\\}", mapEntry.getValue().toString());
|
||||
}
|
||||
if (inputStream != null) {
|
||||
inputStream.close();
|
||||
}
|
||||
//write to file
|
||||
FileOutputStream outputStream = new FileOutputStream(dstFile);
|
||||
IOUtils.write(content, outputStream, StandardCharsets.UTF_8.toString());
|
||||
if (outputStream != null) {
|
||||
outputStream.close();
|
||||
}
|
||||
}
|
||||
|
||||
private static void copyFolder(File src, File dest, List<String> excludeFileNames) throws IOException {
|
||||
|
||||
if (src.isDirectory()) {
|
||||
//if directory not exists, create it
|
||||
if (!dest.exists() && !dest.mkdirs()) {
|
||||
String message = "Could not create directory at path: " + dest;
|
||||
log.error(message);
|
||||
throw new IOException(message);
|
||||
}
|
||||
//list all the directory contents
|
||||
String files[] = src.list();
|
||||
|
||||
if (files == null) {
|
||||
log.warn("There are no files insides the directory " + src.getAbsolutePath());
|
||||
return;
|
||||
}
|
||||
|
||||
for (String file : files) {
|
||||
//construct the src and dest file structure
|
||||
File srcFile = new File(src, file);
|
||||
File destFile = new File(dest, file);
|
||||
//recursive copy
|
||||
copyFolder(srcFile, destFile, excludeFileNames);
|
||||
}
|
||||
|
||||
} else {
|
||||
for (String fileName : excludeFileNames) {
|
||||
if (src.getName().equals(fileName)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
//if file, then copy it
|
||||
//Use bytes stream to support all file types
|
||||
InputStream in = null;
|
||||
OutputStream out = null;
|
||||
|
||||
try {
|
||||
in = new FileInputStream(src);
|
||||
out = new FileOutputStream(dest);
|
||||
|
||||
byte[] buffer = new byte[1024];
|
||||
|
||||
int length;
|
||||
//copy the file content in bytes
|
||||
while ((length = in.read(buffer)) > 0) {
|
||||
out.write(buffer, 0, length);
|
||||
}
|
||||
} finally {
|
||||
silentClose(in);
|
||||
silentClose(out);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void silentClose(InputStream is) {
|
||||
if (is == null) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
is.close();
|
||||
} catch (IOException e) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static void silentClose(OutputStream os) {
|
||||
if (os == null) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
os.close();
|
||||
} catch (IOException e) {
|
||||
// do nothing
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean createZipArchive(String srcFolder) throws IOException {
|
||||
BufferedInputStream origin = null;
|
||||
ZipOutputStream out = null;
|
||||
|
||||
try {
|
||||
final int BUFFER = 2048;
|
||||
FileOutputStream dest = new FileOutputStream(new File(srcFolder + ".zip"));
|
||||
out = new ZipOutputStream(new BufferedOutputStream(dest));
|
||||
byte data[] = new byte[BUFFER];
|
||||
File subDir = new File(srcFolder);
|
||||
String subdirList[] = subDir.list();
|
||||
if (subdirList == null) {
|
||||
log.warn("The sub directory " + subDir.getAbsolutePath() + " is empty");
|
||||
return false;
|
||||
}
|
||||
for (String sd : subdirList) {
|
||||
// get a list of files from current directory
|
||||
File f = new File(srcFolder + "/" + sd);
|
||||
if (f.isDirectory()) {
|
||||
String files[] = f.list();
|
||||
|
||||
if (files == null) {
|
||||
log.warn("The current directory " + f.getAbsolutePath() + " is empty. Has no files");
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 0; i < files.length; i++) {
|
||||
FileInputStream fi = new FileInputStream(srcFolder + "/" + sd + "/" + files[i]);
|
||||
origin = new BufferedInputStream(fi, BUFFER);
|
||||
ZipEntry entry = new ZipEntry(sd + "/" + files[i]);
|
||||
out.putNextEntry(entry);
|
||||
int count;
|
||||
while ((count = origin.read(data, 0, BUFFER)) != -1) {
|
||||
out.write(data, 0, count);
|
||||
out.flush();
|
||||
}
|
||||
|
||||
}
|
||||
} else //it is just a file
|
||||
{
|
||||
FileInputStream fi = new FileInputStream(f);
|
||||
origin = new BufferedInputStream(fi, BUFFER);
|
||||
ZipEntry entry = new ZipEntry(sd);
|
||||
out.putNextEntry(entry);
|
||||
int count;
|
||||
while ((count = origin.read(data, 0, BUFFER)) != -1) {
|
||||
out.write(data, 0, count);
|
||||
out.flush();
|
||||
}
|
||||
}
|
||||
}
|
||||
out.flush();
|
||||
} finally {
|
||||
silentClose(origin);
|
||||
silentClose(out);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue