JPPF, java, parallel computing, distributed computing, grid computing, parallel, distributed, cluster, grid, cloud, open source, android, .net
JPPF, java, parallel computing, distributed computing, grid computing, parallel, distributed, cluster, grid, cloud, open source, android, .net
JPPF

The open source
grid computing
solution

 Home   About   Features   Download   Documentation   Forums 

The Location API

From JPPF 6.0 Documentation

Jump to: navigation, search

Contents

Main Page > Development guide > The Location API


1 Definition

This API allows developers to easily write data to, or read data from various sources: JVM heap, file system, URL or Maven central artifacts. It is based on the Location interface , which provides the following methods:

public interface Location<T> {
  // Copy the content at this location to another location
  <V> Location<V> copyTo(Location<V> location);
  // Obtain an input stream to read from this location
  InputStream getInputStream();
  // Obtain an output stream to write to this location
  OutputStream getOutputStream():
  // Get this location's path
  T getPath();
  // Get the size of the data this location points to
  long size();
  // Get the content at this location as an array of bytes
  byte[] toByteArray() throws Exception;
}

2 Built-in implementations

2.1 FileLocation

FileLocation represents a path in the file system.

Usage examples:

// create a location pointing to the source file:
Location<String> src = new FileLocation("/home/user/docs/some_file.txt");
// copy the source file to a new location and get the new location
Location<String> dest = src.copyTo(new FileLocation("/home/user/other_file.txt"));

2.2 URLLocation

URLLocation can be used to get data to and from a URL, including HTTP and FTP URLs

Example:

// create a location pointing to a file on an FTP server
Location<URL> src = new URLLocation("ftp://user:password@ftp.host.org/path/myFile.txt");
// download the file from the FTP server to the local file system
Location<String> dest = src.copyTo(new FileLocation("/home/user/destFile.txt"));

2.3 MavenCentralLocation

MavenCentralLocation is an extension of URLLocation which allows downloading Maven artifacts from the Maven central repository. Contrary to other Location implementations, this one does not permit uploading data. To enforce this, its getOutputStream() method will always throw an UnsupportedOperationException.

Examples:

// create a location that points to a Maven artifact with default "jar" packaging
Location<URL> jar = new MavenCentralLocation("org.jppf:jppf-client:5.2.9");
// copy the artifact to the file system
jar.copyTo(new FileLocation("lib/jppf-client-5.2.9.jar"));

// create a location that points to a Maven artifact with "war" packaging
Location<URL> war = new MavenCentralLocation("org.jppf:jppf-admin-web:6.0", "war");
// copy the artifact to the file system
jar.copyTo(new FileLocation("tomcat-7.0/webapps/JPPFWebAdmin.war"));

Note: instances of MavenCentralLocation only allow access to a single artifact and do not in any way handle Maven transitive dependencies.

2.4 MemoryLocation

MemoryLocation represents a block of data in memory that can be copied from or sent to another location.

Example:

MyClass object = ...;
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
     ObjectOutputStream oos = new ObjectOutputStream(baos)) {
  // serialize the object
  oos.writeObject(object);
  oos.flush();
  // store the serialized object in a memory location
  Location<byte[]> dataLocation = new MemoryLocation(baos.toByteArray());
  // copy the serialized object to a file
  dataLocation.copyTo(new FileLocation("/home/user/store/object.ser"));
} catch (Exception e) {
  e.printStackTrace();
}

3 Related reference

The location API is notably used to specify a classpath in a job SLA.


Main Page > Development guide > The Location API

JPPF Copyright © 2005-2018 JPPF.org Powered by MediaWiki