Skip to content

Commit

Permalink
Merge pull request #102 from Maninda/migratesl
Browse files Browse the repository at this point in the history
Migrate to Ballerina Swan Lake
  • Loading branch information
Maninda authored Jun 24, 2020
2 parents a0b6085 + 9774f31 commit b04774e
Show file tree
Hide file tree
Showing 19 changed files with 178 additions and 95 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
# limitations under the License.

script:
- wget https://dist.ballerina.io/downloads/1.2.4/ballerina-linux-installer-x64-1.2.4.deb
- sudo dpkg -i ballerina-linux-installer-x64-1.2.4.deb
- wget http://dist-dev.ballerina.io/downloads/swan-lake-preview1/ballerina-linux-installer-x64-swan-lake-preview1.deb
- sudo dpkg -i ballerina-linux-installer-x64-swan-lake-preview1.deb
- sudo apt-get install -f
- export JAVA_OPTS="-DBALLERINA_DEV_COMPILE_BALLERINA_ORG=true"
- mvn clean install -DskipTests=true
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ The following sections provide you details on how to use the FTP connector.

| | Version |
|:---------------------------:|:---------------------------:|
| Ballerina Language | 1.2.x |
| Ballerina Language | Swan Lake Preview1 |

## Feature Overview

Expand Down
4 changes: 2 additions & 2 deletions ftp-compiler-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
<parent>
<groupId>org.wso2.ei</groupId>
<artifactId>module-ftp</artifactId>
<version>0.5.1</version>
<version>0.9.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>ftp-compiler-plugin</artifactId>
<version>0.5.1</version>
<version>0.9.0</version>
<packaging>jar</packaging>
<name>FTP Module - Compiler Validation Utility</name>
<url>https://ballerina.io/</url>
Expand Down
4 changes: 2 additions & 2 deletions ftp-utils/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
<parent>
<groupId>org.wso2.ei</groupId>
<artifactId>module-ftp</artifactId>
<version>0.5.1</version>
<version>0.9.0</version>
<relativePath>../pom.xml</relativePath>
</parent>

<modelVersion>4.0.0</modelVersion>
<version>0.5.1</version>
<version>0.9.0</version>
<artifactId>ftp-utils</artifactId>
<packaging>jar</packaging>
<name>FTP Module - Java utility library</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.wso2.ei.b7a.ftp.core.client;

import org.ballerinalang.jvm.BRuntime;
import org.ballerinalang.jvm.StringUtils;
import org.ballerinalang.jvm.values.MapValue;
import org.ballerinalang.jvm.values.ObjectValue;
import org.ballerinalang.stdlib.io.channels.base.Channel;
Expand Down Expand Up @@ -57,7 +58,8 @@ private FTPClient() {
public static Object initClientEndpoint(ObjectValue clientEndpoint, MapValue<Object, Object> config)
throws BallerinaFTPException {

String protocol = config.getStringValue(FTPConstants.ENDPOINT_CONFIG_PROTOCOL);
String protocol = (config.getStringValue(StringUtils.fromString(FTPConstants.ENDPOINT_CONFIG_PROTOCOL)))
.getValue();
if (FTPUtil.notValidProtocol(protocol)) {
throw new BallerinaFTPException("Only FTP, SFTP and FTPS protocols are supported by FTP client.");
}
Expand All @@ -68,19 +70,23 @@ public static Object initClientEndpoint(ObjectValue clientEndpoint, MapValue<Obj
clientEndpoint.addNativeData(FTPConstants.ENDPOINT_CONFIG_PASS_KEY,
authMap.get(FTPConstants.ENDPOINT_CONFIG_PASS_KEY));
clientEndpoint.addNativeData(FTPConstants.ENDPOINT_CONFIG_HOST,
config.getStringValue(FTPConstants.ENDPOINT_CONFIG_HOST));
(config.getStringValue(StringUtils.fromString(FTPConstants.ENDPOINT_CONFIG_HOST))).getValue());
clientEndpoint.addNativeData(FTPConstants.ENDPOINT_CONFIG_PORT,
FTPUtil.extractPortValue(config.getIntValue(FTPConstants.ENDPOINT_CONFIG_PORT)));
FTPUtil.extractPortValue(config.getIntValue(StringUtils.fromString(
FTPConstants.ENDPOINT_CONFIG_PORT))));
clientEndpoint.addNativeData(FTPConstants.ENDPOINT_CONFIG_PROTOCOL, protocol);
Map<String, String> ftpConfig = new HashMap<>(5);
MapValue secureSocket = config.getMapValue(FTPConstants.ENDPOINT_CONFIG_SECURE_SOCKET);
MapValue secureSocket = config.getMapValue(StringUtils.fromString(FTPConstants.ENDPOINT_CONFIG_SECURE_SOCKET));
if (secureSocket != null) {
final MapValue privateKey = secureSocket.getMapValue(FTPConstants.ENDPOINT_CONFIG_PRIVATE_KEY);
final MapValue privateKey = secureSocket.getMapValue(StringUtils.fromString(
FTPConstants.ENDPOINT_CONFIG_PRIVATE_KEY));
if (privateKey != null) {
final String privateKeyPath = privateKey.getStringValue(FTPConstants.ENDPOINT_CONFIG_PATH);
final String privateKeyPath = (privateKey.getStringValue(StringUtils.fromString(
FTPConstants.ENDPOINT_CONFIG_PATH))).getValue();
if (privateKeyPath != null && !privateKeyPath.isEmpty()) {
ftpConfig.put(Constants.IDENTITY, privateKeyPath);
final String privateKeyPassword = privateKey.getStringValue(FTPConstants.ENDPOINT_CONFIG_PASS_KEY);
final String privateKeyPassword = (privateKey.getStringValue(StringUtils.fromString(
FTPConstants.ENDPOINT_CONFIG_PASS_KEY))).getValue();
if (privateKeyPassword != null && !privateKeyPassword.isEmpty()) {
ftpConfig.put(Constants.IDENTITY_PASS_PHRASE, privateKeyPassword);
}
Expand Down Expand Up @@ -119,20 +125,23 @@ public static Object append(ObjectValue clientConnector, MapValue<Object, Object
throws BallerinaFTPException {

try {
String url = FTPUtil.createUrl(clientConnector,
inputContent.getStringValue(FTPConstants.INPUT_CONTENT_FILE_PATH_KEY));
String url = FTPUtil.createUrl(clientConnector, (inputContent.getStringValue(StringUtils.fromString(
FTPConstants.INPUT_CONTENT_FILE_PATH_KEY))).getValue());
Map<String, String> propertyMap = new HashMap<>(
(Map<String, String>) clientConnector.getNativeData(FTPConstants.PROPERTY_MAP));
propertyMap.put(FTPConstants.PROPERTY_URI, url);

boolean isFile = inputContent.getBooleanValue(FTPConstants.INPUT_CONTENT_IS_FILE_KEY);
boolean isFile = inputContent.getBooleanValue(StringUtils.fromString(
FTPConstants.INPUT_CONTENT_IS_FILE_KEY));
RemoteFileSystemMessage message;
if (isFile) {
ObjectValue fileContent = inputContent.getObjectValue(FTPConstants.INPUT_CONTENT_FILE_CONTENT_KEY);
ObjectValue fileContent = inputContent.getObjectValue(StringUtils.fromString(
FTPConstants.INPUT_CONTENT_FILE_CONTENT_KEY));
Channel byteChannel = (Channel) fileContent.getNativeData(IOConstants.BYTE_CHANNEL_NAME);
message = new RemoteFileSystemMessage(byteChannel.getInputStream());
} else {
String textContent = inputContent.getStringValue(FTPConstants.INPUT_CONTENT_TEXT_CONTENT_KEY);
String textContent = (inputContent.getStringValue(StringUtils.fromString(
FTPConstants.INPUT_CONTENT_TEXT_CONTENT_KEY))).getValue();
InputStream stream = new ByteArrayInputStream(textContent.getBytes());
message = new RemoteFileSystemMessage(stream);
}
Expand All @@ -157,28 +166,29 @@ public static Object put(ObjectValue clientConnector, MapValue<Object, Object> i

Map<String, String> propertyMap = new HashMap<>(
(Map<String, String>) clientConnector.getNativeData(FTPConstants.PROPERTY_MAP));
boolean isFile = inputContent.getBooleanValue(FTPConstants.INPUT_CONTENT_IS_FILE_KEY);
boolean compressInput = inputContent.getBooleanValue(FTPConstants.INPUT_CONTENT_COMPRESS_INPUT_KEY);
boolean isFile = inputContent.getBooleanValue(StringUtils.fromString(FTPConstants.INPUT_CONTENT_IS_FILE_KEY));
boolean compressInput = inputContent.getBooleanValue(StringUtils.fromString(
FTPConstants.INPUT_CONTENT_COMPRESS_INPUT_KEY));
InputStream stream = FTPClientHelper.getUploadStream(inputContent, isFile);
RemoteFileSystemMessage message;
ByteArrayInputStream compressedStream = null;

try {
if (stream != null) {
if (compressInput) {
compressedStream = FTPUtil.compress(stream,
inputContent.getStringValue(FTPConstants.INPUT_CONTENT_FILE_PATH_KEY));
compressedStream = FTPUtil.compress(stream, (inputContent.getStringValue(StringUtils.fromString(
FTPConstants.INPUT_CONTENT_FILE_PATH_KEY))).getValue());
if (compressedStream != null) {
message = FTPClientHelper.getCompressedMessage(clientConnector,
inputContent.getStringValue(FTPConstants.INPUT_CONTENT_FILE_PATH_KEY),
message = FTPClientHelper.getCompressedMessage(clientConnector, (inputContent.getStringValue(
StringUtils.fromString(FTPConstants.INPUT_CONTENT_FILE_PATH_KEY))).getValue(),
propertyMap, compressedStream);
} else {
throw new BallerinaFTPException("Error in compressing file");
}
} else {
message = FTPClientHelper.getUncompressedMessage(clientConnector,
inputContent.getStringValue(FTPConstants.INPUT_CONTENT_FILE_PATH_KEY),
propertyMap, stream);
message = FTPClientHelper.getUncompressedMessage(clientConnector, (inputContent.getStringValue(
StringUtils.fromString(FTPConstants.INPUT_CONTENT_FILE_PATH_KEY))).getValue(), propertyMap,
stream);
}
} else {
throw new BallerinaFTPException("Error in reading file");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@

import org.apache.commons.vfs2.FileSystemException;
import org.ballerinalang.jvm.BallerinaValues;
import org.ballerinalang.jvm.StringUtils;
import org.ballerinalang.jvm.types.BArrayType;
import org.ballerinalang.jvm.types.BPackage;
import org.ballerinalang.jvm.values.MapValue;
import org.ballerinalang.jvm.values.ObjectValue;
import org.ballerinalang.jvm.values.api.BArray;
import org.ballerinalang.jvm.values.api.BString;
import org.ballerinalang.jvm.values.api.BValueCreator;
import org.ballerinalang.stdlib.io.channels.base.Channel;
import org.ballerinalang.stdlib.io.utils.IOConstants;
Expand Down Expand Up @@ -55,9 +57,6 @@
class FTPClientHelper {

private static final String READABLE_BYTE_CHANNEL = "ReadableByteChannel";
private static final String PACKAGE_BALLERINA = "ballerina";
private static final String PACKAGE_IO = "io";

private static final Logger log = LoggerFactory.getLogger(FTPClientHelper.class);

private FTPClientHelper() {
Expand All @@ -79,7 +78,8 @@ static boolean executeGetAction(RemoteFileSystemBaseMessage remoteFileSystemBase
Channel channel = new FTPChannel(byteChannel);

ObjectValue channelStruct = BallerinaValues.createObjectValue(
new BPackage(PACKAGE_BALLERINA, PACKAGE_IO), READABLE_BYTE_CHANNEL);
new BPackage(FTPConstants.IO_ORG_NAME, FTPConstants.IO_MODULE_NAME, FTPConstants.IO_MODULE_VERSION),
READABLE_BYTE_CHANNEL);
channelStruct.addNativeData(IOConstants.BYTE_CHANNEL_NAME, channel);
future.complete(channelStruct);
}
Expand Down Expand Up @@ -133,9 +133,9 @@ static boolean executeListAction(RemoteFileSystemBaseMessage remoteFileSystemBas
log.error("Error while evaluating the pathDecoded value.", e);
}

final MapValue<String, Object> ballerinaFileInfo = BallerinaValues.createRecordValue(
new BPackage(FTPConstants.FTP_ORG_NAME, FTPConstants.FTP_MODULE_NAME),
FTPConstants.FTP_FILE_INFO, fileInfoParams);
final MapValue<BString, Object> ballerinaFileInfo = BallerinaValues.createRecordValue(
new BPackage(FTPConstants.FTP_ORG_NAME, FTPConstants.FTP_MODULE_NAME,
FTPConstants.FTP_MODULE_VERSION), FTPConstants.FTP_FILE_INFO, fileInfoParams);
arrayValue.add(i++, ballerinaFileInfo);
}
future.complete(arrayValue);
Expand All @@ -157,15 +157,17 @@ static InputStream getUploadStream(MapValue<Object, Object> inputContent, boolea

if (isFile) {
try {
ObjectValue fileContent = inputContent.getObjectValue(FTPConstants.INPUT_CONTENT_FILE_CONTENT_KEY);
ObjectValue fileContent = inputContent.getObjectValue(StringUtils.fromString(
FTPConstants.INPUT_CONTENT_FILE_CONTENT_KEY));
Channel byteChannel = (Channel) fileContent.getNativeData(IOConstants.BYTE_CHANNEL_NAME);
return byteChannel.getInputStream();
} catch (IOException e) {
log.error("Error in reading input from file");
return null;
}
} else {
String textContent = inputContent.getStringValue(FTPConstants.INPUT_CONTENT_TEXT_CONTENT_KEY);
String textContent = (inputContent.getStringValue(StringUtils.fromString(
FTPConstants.INPUT_CONTENT_TEXT_CONTENT_KEY))).getValue();
return new ByteArrayInputStream(textContent.getBytes());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.ballerinalang.jvm.values.MapValue;
import org.ballerinalang.jvm.values.ObjectValue;
import org.ballerinalang.jvm.values.api.BArray;
import org.ballerinalang.jvm.values.api.BString;
import org.ballerinalang.jvm.values.api.BValueCreator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -60,17 +61,18 @@ public boolean onMessage(RemoteFileSystemBaseMessage remoteFileSystemBaseMessage

if (remoteFileSystemBaseMessage instanceof RemoteFileSystemEvent) {
RemoteFileSystemEvent event = (RemoteFileSystemEvent) remoteFileSystemBaseMessage;
MapValue<String, Object> parameters = getSignatureParameters(event);
MapValue<BString, Object> parameters = getSignatureParameters(event);
runtime.invokeMethodSync(service, service.getType().getAttachedFunctions()[0].getName(),
parameters, true);
}
return true;
}

private MapValue<String, Object> getSignatureParameters(RemoteFileSystemEvent fileSystemEvent) {
private MapValue<BString, Object> getSignatureParameters(RemoteFileSystemEvent fileSystemEvent) {

MapValue<String, Object> watchEventStruct = BallerinaValues.createRecordValue(
new BPackage(FTPConstants.FTP_ORG_NAME, FTPConstants.FTP_MODULE_NAME), FTPConstants.FTP_SERVER_EVENT);
MapValue<BString, Object> watchEventStruct = BallerinaValues.createRecordValue(
new BPackage(FTPConstants.FTP_ORG_NAME, FTPConstants.FTP_MODULE_NAME, FTPConstants.FTP_MODULE_VERSION),
FTPConstants.FTP_SERVER_EVENT);
List<FileInfo> addedFileList = fileSystemEvent.getAddedFiles();
List<String> deletedFileList = fileSystemEvent.getDeletedFiles();

Expand All @@ -84,9 +86,9 @@ private MapValue<String, Object> getSignatureParameters(RemoteFileSystemEvent fi
fileInfoParams.put("size", info.getFileSize());
fileInfoParams.put("lastModifiedTimestamp", info.getLastModifiedTime());

final MapValue<String, Object> fileInfo = BallerinaValues.createRecordValue(
new BPackage(FTPConstants.FTP_ORG_NAME, FTPConstants.FTP_MODULE_NAME), FTPConstants.FTP_FILE_INFO,
fileInfoParams);
final MapValue<BString, Object> fileInfo = BallerinaValues.createRecordValue(
new BPackage(FTPConstants.FTP_ORG_NAME, FTPConstants.FTP_MODULE_NAME,
FTPConstants.FTP_MODULE_VERSION), FTPConstants.FTP_FILE_INFO, fileInfoParams);
addedFiles.add(i, fileInfo);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.wso2.ei.b7a.ftp.core.server;

import org.ballerinalang.jvm.BRuntime;
import org.ballerinalang.jvm.StringUtils;
import org.ballerinalang.jvm.values.MapValue;
import org.ballerinalang.jvm.values.ObjectValue;
import org.wso2.ei.b7a.ftp.core.util.BallerinaFTPException;
Expand Down Expand Up @@ -69,17 +70,21 @@ private static Map<String, String> getServerConnectorParamMap(MapValue serviceEn

Map<String, String> params = new HashMap<>(12);

MapValue secureSocket = serviceEndpointConfig.getMapValue(FTPConstants.ENDPOINT_CONFIG_SECURE_SOCKET);
MapValue secureSocket = serviceEndpointConfig.getMapValue(StringUtils.fromString(
FTPConstants.ENDPOINT_CONFIG_SECURE_SOCKET));
String url = FTPUtil.createUrl(serviceEndpointConfig);
params.put(Constants.URI, url);
addStringProperty(serviceEndpointConfig, params);
if (secureSocket != null) {
final MapValue privateKey = secureSocket.getMapValue(FTPConstants.ENDPOINT_CONFIG_PRIVATE_KEY);
final MapValue privateKey = secureSocket.getMapValue(StringUtils.fromString(
FTPConstants.ENDPOINT_CONFIG_PRIVATE_KEY));
if (privateKey != null) {
final String privateKeyPath = privateKey.getStringValue(FTPConstants.ENDPOINT_CONFIG_PATH);
final String privateKeyPath = (privateKey.getStringValue(StringUtils.fromString(
FTPConstants.ENDPOINT_CONFIG_PATH))).getValue();
if (privateKeyPath != null && !privateKeyPath.isEmpty()) {
params.put(Constants.IDENTITY, privateKeyPath);
final String privateKeyPassword = privateKey.getStringValue(FTPConstants.ENDPOINT_CONFIG_PASS_KEY);
final String privateKeyPassword = (privateKey.getStringValue(StringUtils.fromString(
FTPConstants.ENDPOINT_CONFIG_PASS_KEY))).getValue();
if (privateKeyPassword != null && !privateKeyPassword.isEmpty()) {
params.put(Constants.IDENTITY_PASS_PHRASE, privateKeyPassword);
}
Expand All @@ -94,7 +99,8 @@ private static Map<String, String> getServerConnectorParamMap(MapValue serviceEn

private static void addStringProperty(MapValue config, Map<String, String> params) {

final String value = config.getStringValue(FTPConstants.ENDPOINT_CONFIG_FILE_PATTERN);
final String value = (config.getStringValue(StringUtils.fromString(FTPConstants.ENDPOINT_CONFIG_FILE_PATTERN)))
.getValue();
if (value != null && !value.isEmpty()) {
params.put(Constants.FILE_NAME_PATTERN, value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ private FTPConstants() {
public static final String PROPERTY_MAP = "map";
public static final String FTP_ORG_NAME = "ballerina";
public static final String FTP_MODULE_NAME = "ftp";
public static final String FTP_MODULE_VERSION = "0.9.0";
public static final String FTP_LISTENER = "Listener";
public static final String FTP_SERVER_EVENT = "WatchEvent";
public static final String FTP_FILE_INFO = "FileInfo";
Expand All @@ -60,5 +61,8 @@ private FTPConstants() {
public static final String INPUT_CONTENT_FILE_CONTENT_KEY = "fileContent";
public static final String INPUT_CONTENT_TEXT_CONTENT_KEY = "textContent";
public static final String INPUT_CONTENT_COMPRESS_INPUT_KEY = "compressInput";
public static final String IO_ORG_NAME = "ballerina";
public static final String IO_MODULE_NAME = "io";
public static final String IO_MODULE_VERSION = "0.5.0";

}
Loading

0 comments on commit b04774e

Please sign in to comment.