| Context and Deployment Descriptor > The Context Services > URL Access Service |
URL Access Service |
INDEX
PREVIOUS
NEXT
|
The URL access service is defined by the interface IlpURLAccessService.
It turns relative URLs into absolute URLs or pathnames and can be used to deploy applications and applets without hard coding pathnames 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 pathname 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)
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.
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>
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:
setDocumentBase to register their base URL.
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:
addRelative.
addAbsolute.
addRelative with a JAR filename as parameter.
addAbsolute with a JAR filename as parameter.
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/6/docs/api/java/net/JarURLConnection.html.
| Copyright © 1987-2007 ILOG S.A. All rights reserved. Documentation homepage. All rights reserved. Legal terms. | PREVIOUS NEXT |