Skip to content
Snippets Groups Projects
Commit 70d644b9 authored by NTUMBA WA NTUMBA Patient's avatar NTUMBA WA NTUMBA Patient
Browse files

add method to support array byte of gidl file as an input

parent 0a904d24
No related branches found
No related tags found
No related merge requests found
...@@ -66,7 +66,7 @@ public class PathResolver { ...@@ -66,7 +66,7 @@ public class PathResolver {
URL res = className.getClassLoader().getResource(fileName); URL res = className.getClassLoader().getResource(fileName);
if(res == null){ if(res == null){
System.err.println("Null pointer className : "+className.getName()+" fileName "+fileName); System.err.println("Null pointer class name");
return ""; return "";
} }
...@@ -101,8 +101,17 @@ public class PathResolver { ...@@ -101,8 +101,17 @@ public class PathResolver {
} }
/**
* Returns a String path of a created temporary directory
* <p>
*
* @return String
*/
public static String createTempDir() public static String createTempDir()
{ {
String pathFolderToString = ""; String pathFolderToString = "";
Path pathFolder = FileSystems.getDefault().getPath(System.getProperty("user.dir")); Path pathFolder = FileSystems.getDefault().getPath(System.getProperty("user.dir"));
...@@ -122,15 +131,35 @@ public class PathResolver { ...@@ -122,15 +131,35 @@ public class PathResolver {
} }
/**
* Returns void and copy directory from source path to destination path
* <p>
*
* @param sourcePath directory string path
* @param destinationPath destination string path
* @return void
*/
public static void copyFolder(String sourcePath, String destinationPath){ public static void copyFolder(String sourcePath, String destinationPath){
File sourceFolder = new File(sourcePath); File sourceFolder = new File(sourcePath);
File destinationFolder = new File(destinationPath); File destinationFolder = new File(destinationPath);
doCopyJob(sourceFolder, destinationFolder); doCopyJob(sourceFolder, destinationFolder);
} }
/**
* Returns void and copy directory from source path to destination path
* it is a private method used in public method doCopyJob
* <p>
*
* @param sourcePath directory path as a file object
* @param destinationPath destination string path
* @return void
*/
private static void doCopyJob(File sourceFolder, File destinationFolder){ private static void doCopyJob(File sourceFolder, File destinationFolder){
...@@ -169,13 +198,20 @@ public class PathResolver { ...@@ -169,13 +198,20 @@ public class PathResolver {
} }
} }
/**
* Returns void and extract directory from a jar file and copy it to a destination path
* <p>
*
* @param urlSourcePath the jar location url
* @param destinationPath destination string path
* @return void
*/
public static void extractDirectoryFromJar( URL urlSourcePath, String destinationPath ) { public static void extractDirectoryFromJar( URL urlSourcePath, String destinationPath ) {
// make sure write directory exists
// make sure write directory exists
if(!new File(destinationPath).exists()){ if(!new File(destinationPath).exists()){
System.out.println("Not exist "+destinationPath);
new File(destinationPath).mkdirs(); new File(destinationPath).mkdirs();
} }
...@@ -190,6 +226,16 @@ public class PathResolver { ...@@ -190,6 +226,16 @@ public class PathResolver {
} }
/**
* Returns void and extract directory from a jar file and copy it to a destination path
* a private method called in the public method extractDirectoryFromJar
* <p>
*
* @param urlSourcePath the jar location url
* @param destinationPath destination string path
* @return void
*/
private static void extract( URL urlSourcePath, String destinationPath ) throws IOException{ private static void extract( URL urlSourcePath, String destinationPath ) throws IOException{
...@@ -197,13 +243,11 @@ public class PathResolver { ...@@ -197,13 +243,11 @@ public class PathResolver {
final URL dirURL = urlSourcePath; final URL dirURL = urlSourcePath;
String []sourceDirectoryToArray = sourceDirectory.split("/"); String []sourceDirectoryToArray = sourceDirectory.split("/");
final String path = sourceDirectoryToArray[sourceDirectoryToArray.length-1]; final String path = sourceDirectoryToArray[sourceDirectoryToArray.length-1];
System.out.println(" path ==> "+path);
System.out.println(" destinationPath "+destinationPath);
if( ( dirURL != null ) && dirURL.getProtocol().equals( "jar" ) ) { if( ( dirURL != null ) && dirURL.getProtocol().equals( "jar" ) ) {
final JarURLConnection jarConnection = (JarURLConnection) dirURL.openConnection(); final JarURLConnection jarConnection = (JarURLConnection) dirURL.openConnection();
System.out.println( "jarConnection is " + jarConnection );
final ZipFile jar = jarConnection.getJarFile(); final ZipFile jar = jarConnection.getJarFile();
final Enumeration< ? extends ZipEntry > entries = jar.entries(); // gives ALL entries in jar final Enumeration< ? extends ZipEntry > entries = jar.entries(); // gives ALL entries in jar
...@@ -225,7 +269,7 @@ public class PathResolver { ...@@ -225,7 +269,7 @@ public class PathResolver {
System.out.println( (bMade ? " creating " : " unable to create ") + name ); System.out.println( (bMade ? " creating " : " unable to create ") + name );
} }
else { else {
System.out.println( " writing " + name );
final InputStream is = jar.getInputStream( entry ); final InputStream is = jar.getInputStream( entry );
final OutputStream os = new BufferedOutputStream( new FileOutputStream( f ) ); final OutputStream os = new BufferedOutputStream( new FileOutputStream( f ) );
final byte buffer[] = new byte[4096]; final byte buffer[] = new byte[4096];
...@@ -247,4 +291,43 @@ public class PathResolver { ...@@ -247,4 +291,43 @@ public class PathResolver {
} }
/**
* Returns void and delete a given file or diectory in parameter
* <p>
* @param File object file to delete
* @return void
*/
public static void deleteTempDir(File file){
if(file.isDirectory()) {
//directory is empty, then delete it
if(file.list().length==0){
file.delete();
}
else {
//list all the directory contents
String files[] = file.list();
for (String fileName : files) {
//construct the file structure
File fileDelete = new File(file, fileName);
//recursive delete
deleteTempDir(fileDelete);
}
//check the directory again, if empty then delete it
if(file.list().length==0){
file.delete();
}
}
}
else {
//if file, then delete it
file.delete();
}
}
} }
...@@ -174,7 +174,7 @@ ...@@ -174,7 +174,7 @@
</configuration> </configuration>
</plugin> </plugin>
<!-- Plugin to install vsb-manager-with-dependencies jar in local repository --> <!-- Plugin to install vsb-manager-with-dependencies jar in local repository -->
<!-- <!--
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId> <artifactId>maven-install-plugin</artifactId>
......
...@@ -14,6 +14,9 @@ import java.net.MalformedURLException; ...@@ -14,6 +14,9 @@ import java.net.MalformedURLException;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
import java.net.URLClassLoader; import java.net.URLClassLoader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Iterator; import java.util.Iterator;
...@@ -83,30 +86,52 @@ public class VsbManager { ...@@ -83,30 +86,52 @@ public class VsbManager {
private boolean STARTING_FROM_JAR = false; private boolean STARTING_FROM_JAR = false;
public static void main(String[] args) { public VsbManager(){
String interfaceDescriptionPath = "/home/pntumba/inria_code/workspace/eclipse_neon/evolution-service-bus/bc-manager/target/classes/weather.gidl";
VsbManager vsbm = new VsbManager(); // Test if class is running from jar or from class file.
if(getClass().getClassLoader().getResource("config.json").toString().startsWith("jar")){
STARTING_FROM_JAR = true;
}
//VsbOutput warByteArray = vsbm.generateWar(BcManagerRestService.class.getClassLoader().getResource("weather.gidl").toExternalForm().substring(5), ProtocolType.SOAP); }
VsbOutput warByteArray = vsbm.generateWar(interfaceDescriptionPath, ProtocolType.SOAP);
public VsbOutput generateWar(String interfaceDescriptionPath, ProtocolType busProtocol){
return generate(interfaceDescriptionPath, busProtocol);
} }
public VsbManager(){ /******************************** Public method to call ***********************************************/
public VsbOutput generateWar(byte[] interfaceDescriptionByteArray, ProtocolType busProtocol){
String interfaceDescriptionPath = interfaceDescriptionBytesToFile(interfaceDescriptionByteArray);
return generate(interfaceDescriptionPath, busProtocol);
}
public boolean deleteGeratedFiles(){
// Test if class is running from jar or from class file. boolean deleteGerated = false;
if(getClass().getClassLoader().getResource("config.json").toString().startsWith("jar")){ File directory = new File(Constants.generatedCodePath);
// make sure directory exists
if(!directory.exists()){
System.out.println("Starting from jar"); System.out.println("Delete request failed: Directory does not exist.");
STARTING_FROM_JAR = true; }
else{
PathResolver.deleteTempDir(directory);
deleteGerated = true;
} }
return deleteGerated;
} }
/******************************************************************************************************/
/*************** Private method called by public method ************************************************/
private void setConstants(String interfaceDescriptionPath) { private void setConstants(String interfaceDescriptionPath) {
...@@ -167,7 +192,7 @@ public class VsbManager { ...@@ -167,7 +192,7 @@ public class VsbManager {
} }
public VsbOutput generateWar(String interfaceDescriptionPath, ProtocolType busProtocol) { private VsbOutput generate(String interfaceDescriptionPath, ProtocolType busProtocol) {
if(!isInterfaceDescriptionFile(interfaceDescriptionPath)){ if(!isInterfaceDescriptionFile(interfaceDescriptionPath)){
...@@ -221,16 +246,13 @@ public class VsbManager { ...@@ -221,16 +246,13 @@ public class VsbManager {
warGenerator.addDependencyFiles(gm_soap_pomxml); warGenerator.addDependencyFiles(gm_soap_pomxml);
warGenerator.addDependencyFiles(gm_coap_pomxml); warGenerator.addDependencyFiles(gm_coap_pomxml);
warGenerator.addDependencyFiles(gm_dpws_pomxml); warGenerator.addDependencyFiles(gm_dpws_pomxml);
return warGenerator.generate(busProtocol==ProtocolType.SOAP); return warGenerator.generate(busProtocol==ProtocolType.SOAP);
//deleteGeneratedFiles();
} }
public void generateBindingComponent(final String interfaceDescription, final ProtocolType busProtocol) { private void generateBindingComponent(final String interfaceDescription, final ProtocolType busProtocol) {
System.out.println(" interfaceDescription "+interfaceDescription);
copyInterfaceDescription(interfaceDescription); copyInterfaceDescription(interfaceDescription);
BcConfiguration bcConfiguration = null; BcConfiguration bcConfiguration = null;
...@@ -335,7 +357,7 @@ public class VsbManager { ...@@ -335,7 +357,7 @@ public class VsbManager {
} }
public void copyInterfaceDescription(String interfaceDescription) { private void copyInterfaceDescription(String interfaceDescription) {
try { try {
File input = new File(interfaceDescription); File input = new File(interfaceDescription);
File output = new File(Constants.webapp_src_bc + File.separator + "config" + File.separator + "serviceDescription.gxdl"); File output = new File(Constants.webapp_src_bc + File.separator + "config" + File.separator + "serviceDescription.gxdl");
...@@ -348,7 +370,8 @@ public class VsbManager { ...@@ -348,7 +370,8 @@ public class VsbManager {
sc.close(); sc.close();
printer.close(); printer.close();
} }
catch(FileNotFoundException e) { catch(FileNotFoundException e){
System.err.println("Interface file not found."); System.err.println("Interface file not found.");
} }
} }
...@@ -377,7 +400,7 @@ public class VsbManager { ...@@ -377,7 +400,7 @@ public class VsbManager {
throw new RuntimeException("Could not load " + file.getAbsolutePath() + " java file object"); throw new RuntimeException("Could not load " + file.getAbsolutePath() + " java file object");
} }
public static void generateBindingComponentClass(GmServiceRepresentation gmServiceRepresentation, ProtocolType busProtocol) { private static void generateBindingComponentClass(GmServiceRepresentation gmServiceRepresentation, ProtocolType busProtocol) {
String configTemplatePath = ""; String configTemplatePath = "";
JSONParser parser = new JSONParser(); JSONParser parser = new JSONParser();
...@@ -739,58 +762,16 @@ public class VsbManager { ...@@ -739,58 +762,16 @@ public class VsbManager {
} }
} }
public void deleteGeneratedFiles() {
File directory = new File(Constants.generatedCodePath + File.separator + Constants.target_namespace_path);
// make sure directory exists
if(!directory.exists()){
System.out.println("Delete reques failed: Directory does not exist.");
}
else {
delete(directory);
}
}
public static void delete(File file){
if(file.isDirectory()) {
//directory is empty, then delete it
if(file.list().length==0){
file.delete();
}
else {
//list all the directory contents
String files[] = file.list();
for (String fileName : files) {
System.out.println(fileName);
if(fileName.equals("BindingComponent.java")) continue;
//construct the file structure
File fileDelete = new File(file, fileName);
//recursive delete
delete(fileDelete);
}
//check the directory again, if empty then delete it private void copyBCClass(GmServiceRepresentation gmServiceRepresentation, ProtocolType busProtocol) {
if(file.list().length==0){
file.delete();
}
}
}
else {
//if file, then delete it
file.delete();
}
}
public void copyBCClass(GmServiceRepresentation gmServiceRepresentation, ProtocolType busProtocol) {
String namespace = Constants.target_namespace; String namespace = Constants.target_namespace;
namespace = namespace.replace(".", File.separator); namespace = namespace.replace(".", File.separator);
try{ try{
File input = new File(new File(BcManagerRestService.class.getClassLoader().getResource("example.json").toString()).getParent().substring(5) + File.separator + "BindingComponent.java"); File input = new File(new File(BcManagerRestService.class.getClassLoader().getResource("example.json").toString()).getParent().substring(5) + File.separator + "BindingComponent.java");
File output = new File(Constants.generatedCodePath + File.separator + namespace + File.separator + "BindingComponent.java"); File output = new File(Constants.generatedCodePath + File.separator + namespace + File.separator + "BindingComponent.java");
System.out.println(input.getAbsolutePath());
System.out.println(output.getAbsolutePath());
Scanner sc = new Scanner(input); Scanner sc = new Scanner(input);
PrintWriter printer = new PrintWriter(output); PrintWriter printer = new PrintWriter(output);
while(sc.hasNextLine()) { while(sc.hasNextLine()) {
...@@ -832,7 +813,30 @@ public class VsbManager { ...@@ -832,7 +813,30 @@ public class VsbManager {
break; break;
} }
return isGoodFile; return isGoodFile;
} }
private static String interfaceDescriptionBytesToFile(byte[] interfaceDescriptionByteArray){
File interfaceDescriptionFile = new File("interfaceDescription.gidl");
FileOutputStream fos;
try {
fos = new FileOutputStream(interfaceDescriptionFile);
fos.write(interfaceDescriptionByteArray);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return interfaceDescriptionFile.getAbsolutePath();
}
/******************************************************************************************************/
} }
\ No newline at end of file
...@@ -144,7 +144,7 @@ public class VsbManagerRestService { ...@@ -144,7 +144,7 @@ public class VsbManagerRestService {
} }
VsbManager vsbm = new VsbManager(); VsbManager vsbm = new VsbManager();
vsbm.generateWar(arguments[0], busProtocol); // vsbm.generateWar(arguments[0], busProtocol);
String returnMessage = ""; String returnMessage = "";
returnMessage = "Received!"; returnMessage = "Received!";
......
package eu.chorevolution.vsb.manager; package eu.chorevolution.vsb.manager;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import eu.chorevolution.vsb.gmdl.utils.enums.ProtocolType;
//import eu.chorevolution.vsb.bindingcomponent.generated.GeneratedFactory; //import eu.chorevolution.vsb.bindingcomponent.generated.GeneratedFactory;
public class VsbManagerTest{
public class VsbManagerTest {
public static void main(String[] args) {
VsbManager vsbm = new VsbManager(); ///home/siddhartha/Documents/gmdl/dts-google.json public static void main(String[] args) {
// GeneratedFactory.run();
} /* String interfaceDescriptionPath = "/home/pntumba/inria_code/workspace/eclipse_neon/evolution-service-bus/bc-manager/target/classes/weather.gidl";
generateWarFile(interfaceDescriptionPath);
generateWarBytes(interfaceDescriptionPath);*/
}
public static void generateWarFile(String interfaceDescriptionPath){
VsbManager vsbm = new VsbManager();
vsbm.generateWar(interfaceDescriptionPath, ProtocolType.SOAP);
vsbm.deleteGeratedFiles();
}
public static void generateWarBytes(String interfaceDescriptionPath){
byte[] interfaceDescriptionBytesArray = null;
Path path = Paths.get(interfaceDescriptionPath);
try {
interfaceDescriptionBytesArray = Files.readAllBytes(path);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
VsbManager vsbm = new VsbManager();
vsbm.generateWar(interfaceDescriptionBytesArray, ProtocolType.SOAP);
vsbm.deleteGeratedFiles();
}
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment