skip to main content
Programmers documentation > Context and Deployment Descriptor > The context services > URL access service
 
URL access service
The URL access service is defined by the interface IlpURLAccessService.
It turns relative URLs into absolute URLs or path names and can be used to deploy applications without hard coding path names or URLs.
More specifically, it can be used to access configuration files, data source files, and resource files.
IlpURLAccessService provides the method getFileLocation which returns the URL of a file in the path, or null if the file is not found.
This method takes as its parameter a relative URL or path name as follows:
 
public java.net.URL getFileLocation (String filename);
Optionally, this service also provides the method getFileLocation with two optional parameters which specify whether the file shall be searched for in the CLASSPATH (directories and jars) or not, as follows:
 
public java.net.URL getFileLocation (String filename,
                                     boolean searchClassPathFirst,
                                     boolean searchClassPathLast)
How to initialize the URL access service through the deployment descriptor file
The URL access service can be initialized in the deployment descriptor file using the tag <urlAccess>.
 
[<urlAccess [verbose="bool"] [documentBase="xxx"]>
   [<relativePath>xxx</relativePath>]*
   [<absolutePath>xxx</absolutePath>]*
</urlAccess>]
For:
*documentBase : set the base URL.
*<relativePath> : add a path relative to the document base.
*<absolutePath> : add an absolute path.
How to customize the URL access service through the deployment descriptor
The following example illustrates the customization of the URL access service through the deployment descriptor:
 
<urlAccess>
    <!-- Add relative path to sample root directory -->
    <relativePath>../..</relativePath>
</urlAccess>
How to use the URL access service in an application
The following code extract shows you how you can use the URL access service in your application:
 
IlpContext context = IltSystem.GetDefaultContext();
IlpNetwork networkComponent = new IlpNetwork(context);
 
IlpURLAccessService urlService = context.getURLAccessService();
URL url = urlService.getFileLocation("europe.ivl");
if (url != null) {
  networkComponent.addBackgroundURL(url);
} else {
  System.err.println("Background file could not be found.");
}
By default, the URL access service is implemented by the class IlpDefaultURLAccessService. This default implementation uses one base URL that is usually defined according to the deployment model:
*Applications can also use setDocumentBase to register their base URL but, by default, they use the current directory. Please be aware that, in the case of a web application, the current directory might not be the root directory of the web application. Instead, it may be the directory of the JVM booted from by the web server. So, it is necessary to add relative paths to properly access the files in the root web application directory.
You can specify additional paths to the locations to be searched for a given filename. These additional paths can be:
*Paths relative to the document base. You set them using the method addRelative.
*Absolute paths. You set them using the method addAbsolute.
*JAR files defined by a URL relative to the document base. You set them using the method addRelative with a JAR filename as parameter.
*JAR files defined by an absolute URL. You set them using the method addAbsolute with a JAR filename as parameter.
How to use the URL access service through the API
The following example illustrates the use of the URL access service through the API:
 
IlpDefaultURLAccessService accessService = new IltDefaultURLAccessService();
accessService.setDocumentBase(new URL("file:C:/myfiles/"));
accessService.addRelative("myname/");
acesssService.addAbsolute(new URL("file://nfs/shared/resources/"));
accessService.addRelative("my.jar");
accessService.addAbsolute(new URL("file://nfs/shared/resources.jar"));
A call to getFileLocation("myresource") will return one of the following URLs:
 
file:C:/myfiles/myname/myresource
file://nfs/shared/resources/myresource
jar:file:C:/myfiles/my.jar!/myresource
jar:file://nfs/shared/resources.jar!/myresource
For details about the file URL syntax, refer to http://www.w3.org/Addressing/rfc1738.txt.
For details about the jar URL syntax, refer to http://java.sun.com/javase/7/docs/api/java/net/JarURLConnection.html.

Copyright © 2018, Rogue Wave Software, Inc. All Rights Reserved.