Skip to content

Commit

Permalink
- adding customized install method either in KBox.getResource and KBo…
Browse files Browse the repository at this point in the history
…x.install
  • Loading branch information
emarx committed Sep 2, 2016
1 parent fccca2e commit 4cb6b7b
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 7 deletions.
2 changes: 1 addition & 1 deletion kbox.core.test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<dependency>
<groupId>org.aksw.kbox</groupId>
<artifactId>kbox.core</artifactId>
<version>0.0.1-alpha1</version>
<version>0.0.1-alpha2</version>
</dependency>
</dependencies>
<repositories>
Expand Down
7 changes: 3 additions & 4 deletions kbox.core.test/src/test/java/org/aksw/kbox/KBoxTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public void testResourceFolderMethods() throws MalformedURLException, Exception
String currentResourceFolder = KBox.getResourceFolder();
KBox.setResourceFolder("c:" + File.separator);
assertEquals("c:" + File.separator, KBox.getResourceFolder());
KBox.setResourceFolder(KBox.KBOX_DIR);
KBox.setResourceFolder(currentResourceFolder);
}

Expand All @@ -36,13 +35,13 @@ public void testPublishMethods() throws MalformedURLException, Exception {
File test = KBox.getResource(new URL("http://tttt"));
assertEquals(null, test);
URL fileToInstall = new URL("http://downloads.dbpedia.org/3.8/en/contents-nt.txt");
File rboxedFile = KBox.getResource(fileToInstall);
File kboxedFile = KBox.getResource(fileToInstall);
KBox.install(new URL("http://test.org/context.txt"), fileToInstall);
String path = KBox.URLToPath(new URL("http://test.org/context.txt"));
File inDisk = new File(KBox.getResourceFolder() + File.separator + path);
assertEquals(true, inDisk.exists());
rboxedFile = KBox.getResource(new URL("http://test.org/context.txt"));
assertEquals(rboxedFile.getPath(), inDisk.getPath());
kboxedFile = KBox.getResource(new URL("http://test.org/context.txt"));
assertEquals(kboxedFile.getPath(), inDisk.getPath());
}

}
66 changes: 66 additions & 0 deletions kbox.core/src/main/java/org/aksw/kbox/KBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,32 @@ public static File getResource(URL url, boolean install) throws Exception {
return resource;
}

/**
* Get a local representation of the remote resource.
* If the representation of the resource already exists,
* return it, otherwise create a local representation and
* return it.
*
* @param url - the remote URL of the resource to be retrieved.
* @param install - specify if the resource should be installed (true) or not (false).
* @param method - the method that will be used to install the resource
* in case it is not installed and install install param is set true.
*
* @return a file pointing to a local materialization of the resource.
* @throws Exception if the resource can not be located or some error occurs during
* the local resource materialization.
*/
public static File getResource(URL url, Install method, boolean install) throws Exception {
File resource = new File(URLToAbsolutePath(url));
if(!resource.exists() && !install) {
return null;
} else if(resource.exists() && !install) {
return resource;
}
install(url, url, method);
return resource;
}

/**
* Get a local representation of the remote resource.
* If the representation of the resource already exists,
Expand Down Expand Up @@ -266,4 +292,44 @@ public static void install(URL url, InputStream inputStream) throws Exception {
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
}
}

/**
* Publish a given file in a given URL local directory.
* This function allows KBox to serve files to applications, acting as proxy to the publised file.
* The file that is published in a give URL u will be located when the client execute
* the function KBox.getResource(u).
*
* @param source - the URL of the file that is going to be published at the given URL.
* @param dest - the URL where the file is going to be published. *
* @param install - a customized method for installation.
*
* @throws Exception if the resource does not exist or can not be copied or some error
* occurs during the resource publication.
*/
public static void install(URL source, URL dest, Install install) throws Exception {
install.install(source, dest);
}

/**
* Install a given ZIP file from a given URL.
*
* @param source the source URL containing the ZIP file to be installed.
* @param dest the URL whereas the source will be installed.
* @throws Exception if an error occurs during the installation.
*/
public static void installFromZip(URL source, URL dest) throws Exception {
ZipInstall zipInstall = new ZipInstall();
install(source, dest, zipInstall);
}

/**
* Install a given ZIP file from a given URL.
*
* @param source the source URL containing the ZIP file to be installed.
* @throws Exception if an error occurs during the installation.
*/
public static void installFromZip(URL source) throws Exception {
ZipInstall zipInstall = new ZipInstall();
install(source, source, zipInstall);
}
}
2 changes: 1 addition & 1 deletion kbox.kibe.test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<dependency>
<groupId>org.aksw.kbox</groupId>
<artifactId>kbox.kibe</artifactId>
<version>0.0.1-alpha1</version>
<version>0.0.1-alpha2</version>
</dependency>
</dependencies>
<repositories>
Expand Down
2 changes: 1 addition & 1 deletion kbox.kibe/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<dependency>
<groupId>org.aksw.kbox</groupId>
<artifactId>kbox.core</artifactId>
<version>0.0.1-alpha1</version>
<version>0.0.1-alpha2</version>
</dependency>
<dependency>
<groupId>org.apache.jena</groupId>
Expand Down

0 comments on commit 4cb6b7b

Please sign in to comment.