この章では、次のトピックについて説明します。
BasicService サービスの使い方ClipboardService サービスの使い方DownloadService サービスの使い方FileOpenService サービスの使い方FileSaveService サービスの使い方PrintService サービスの使い方PersistenceService サービスの使い方FileContents の使い方JNLPRandomAccessFile の使い方
SingleInstanceService サービスの使い方
ExtendedService サービスの使い方
JNLP API の目的は、標準の Java 2 SE API 経由では入手不可能な追加情報を、アプリケーションに対して提供することです。以下のサンプルコードは、サービス BasicService、ClipboardService、DownloadService、FileOpenService、FileSaveService、PrintService、および PersistenceService の使い方を示したものです。
JNLP API の 公開クラスとインタフェースは、jnlp.jar ファイル内に収められています。JNLP API を使用しているソースファイルをコンパイルする際には、この JAR ファイルをクラスパスに含める必要があります。たとえば、Windows 上では次のようにします。
javac -classpath .;jnlp.jar *.java
jnlp.jar ファイルは、JNLP 開発者向けパックに含まれています。
BasicService サービスの使い方javax.jnlp.BasicService サービスが提供する一連のメソッドを使えば、環境に対する問い合わせや対話を行えます。これらのメソッドは、AppletContext が Java アプレットに対して提供するメソッドに似ています。
showURL メソッドは JNLP API を使って、プラットフォーム上のデフォルトブラウザに対し、指定された URL を表示するよう指示します。このメソッドは、要求が成功した場合には true を返し、そうでない場合には false を返します。
import javax.jnlp.*;
...
// Method to show a URL
boolean showURL(URL url) {
try {
// Lookup the javax.jnlp.BasicService object
BasicService bs = (BasicService)ServiceManager.lookup("javax.jnlp.BasicService");
// Invoke the showDocument method
return bs.showDocument(url);
} catch(UnavailableServiceException ue) {
// Service is not supported
return false;
}
}
ClipboardService サービスの使い方javax.jnlp.ClipboardService サービスが提供するメソッドを使えば、システム全体の共有クリップボードにアクセスできます。このサービスは、制限されたランタイム内で動作中のアプリケーションからでも利用可能です。
Java Web Start は、クリップボードに格納されている内容 (機密情報が含まれる可能性もある) へのアクセスや、そうした内容の上書きが信頼できないアプリケーションに許可されると、セキュリティ上の問題が発生する可能性があることをユーザに警告します。
import javax.jnlp;
...
private ClipboardService cs;
try {
cs = (ClipboardService)ServiceManager.lookup
("javax.jnlp.ClipboardService");
} catch (UnavailableServiceException e) {
cs = null;
}
if (cs != null) {
// set the system clipboard contents to a string selection
StringSelection ss = new StringSelection("Java Web Start!");
cs.setContents(ss);
// get the contents of the system clipboard and print them
Transferable tr = cs.getContents();
if (tr.isDataFlavorSupported(DataFlavor.stringFlavor)) {
try {
String s = (String)tr.getTransferData(DataFlavor.stringFlavor);
System.out.println("Clipboard contents:" + s);
} catch (Exception e) {
e.printStackTrace();
}
}
}
DownloadService サービスの使い方javax.jnlp.DownloadService サービスを使えば、アプリケーションは自身のリソースをどのようにキャッシュするかを制御できます。
アプリケーションはこのサービスを使うことで、どのリソースがキャッシュされているかの確認、リソースの強制キャッシュ、およびキャッシュからのリソースの削除を行えます。
import javax.jnlp.*;
...
DownloadService ds;
try {
ds = (DownloadService)ServiceManager.lookup("javax.jnlp.DownloadService");
} catch (UnavailableServiceException e) {
ds = null;
}
if (ds != null) {
try {
// determine if a particular resource is cached
URL url =
new URL("http://java.sun.com/products/javawebstart/lib/draw.jar");
boolean cached = ds.isResourceCached(url, "1.0");
// remove the resource from the cache
if (cached) {
ds.removeResource(url, "1.0");
}
// reload the resource into the cache
DownloadServiceListener dsl = ds.getDefaultProgressWindow();
ds.loadResource(url, "1.0", dsl);
} catch (Exception e) {
e.printStackTrace( );
}
}
FileOpenService サービスの使い方javax.jnlp.FileOpenService サービスが提供するメソッドを使えば、ローカルディスクからファイルをインポートできます。このサービスは、制限されたランタイム内で動作中のアプリケーションからでも利用可能です。
このインタフェースの目的は、HTML 使用時に Web 開発者に対して提供されるのと同じ種類のディスクアクセス機能を、信頼できない可能性のある Web 配備アプリケーションに対して提供することです。 HTML フォームは、ファイル選択ダイアログによるファイルの組み込みをサポートしています。
import javax.jnlp.*;
...
FileOpenService fos;
try {
fos = (FileOpenService)ServiceManager.lookup("javax.jnlp.FileOpenService");
} catch (UnavailableServiceException e) {
fos = null;
}
if (fos != null) {
try {
// ask user to select a file through this service
FileContents fc = fos.openFileDialog(null, null);
// ask user to select multiple files through this service
FileContents[] fcs = fos.openMultiFileDialog(null, null);
} catch (Exception e) {
e.printStackTrace( );
}
}
FileSaveService サービスの使い方javax.jnlp.FileSaveService サービスが提供するメソッドを使えば、ファイルをローカルディスクにエクスポートできます。このサービスは、制限されたランタイム内で動作中のアプリケーションからでも利用可能です。
このインタフェースの目的は、Web ブラウザが表示中の内容に関して提供するのと同レベルのディスクアクセス機能を、信頼できない可能性のある Web 配備アプリケーションに対して提供することです。 ほとんどのブラウザは、別名保存ダイアログをユーザインタフェースの一部として提供しています。
import javax.jnlp.*;
...
FileSaveService fss;
FileOpenService fos;
try {
fos = (FileOpenService)ServiceManager.lookup("javax.jnlp.FileOpenService");
fss = (FileSaveService)ServiceManager.lookup
("javax.jnlp.FileSaveService");
} catch (UnavailableServiceException e) {
fss = null;
fos = null;
}
if (fss != null && fos != null) {
try {
// get a file with FileOpenService
FileContents fc = fos.openFileDialog(null, null);
// one way to save a file
FileContents newfc = fss.saveFileDialog(null, null,
fc.getInputStream(), "newFileName.txt");
// another way to save a file
FileContents newfc2 = fss.saveAsFileDialog(null, null, fc);
} catch (Exception e) {
e.printStackTrace( );
}
}
「FileContents の使い方」も参照してください。
PrintService サービスの使い方javax.jnlp.PrintService サービスが提供するメソッドを使えば、印刷機能にアクセスできます。このサービスは、制限されたランタイム内で動作中のアプリケーションからでも利用可能です。
アプリケーションはこのサービスを使うことで、印刷ジョブを発行できます。Java Web Start は、その要求をユーザに表示し、ユーザによって承認されると、その要求をプリンタのキューに追加します。
Java Web Start 5.0 では、直接 Java Printing API が使用できるようになりました。サンドボックスでアプリケーションが実行されている場合、この API を起動すると、PrintPermission の付与をユーザに確認するセキュリティダイアログが表示されます。JNLP Printing API を使用する必要はなくなりました。どの JNLP アプリケーションでも、Java Printing API にフルアクセスできます。
import javax.jnlp.*;
...
PrintService ps;
try {
ps = (PrintService)ServiceManager.lookup("javax.jnlp.PrintService");
} catch (UnavailableServiceException e) {
ps = null;
}
if (ps != null) {
try {
// get the default PageFormat
PageFormat pf = ps.getDefaultPage();
// ask the user to customize the PageFormat
PageFormat newPf = ps.showPageFormatDialog(pf);
// print the document with the PageFormat above
ps.print(new DocToPrint());
} catch (Exception e) {
e.printStackTrace( );
}
}
// Code to construct the Printable Document
class DocToPrint implements Printable {
public int print(Graphics g, PageFormat pageformat, int PageIndex){
// code to generate what you want to print
}
}
PersistenceService サービスの使い方 javax.jnlp.PersistenceService サービスが提供するメソッドを使えば、ローカルのクライアントシステム上にデータを格納できます。このサービスは、制限されたランタイム内で動作中のアプリケーションからでも利用可能です。
このサービスの目的は、cookie メカニズムが HTML ベースのアプリケーションに対して提供するのと類似の機能を提供することです。 cookie を使えば、ローカルのクライアントシステム上に少量のデータを格納できます。 それらのデータはブラウザによって安全に管理されます。つまり、特定のデータを取得できるのは、そのデータを格納したページと同じ URL からの HTML ページだけです。
import javax.jnlp.*;
...
PersistenceService ps;
BasicService bs;
try {
ps = (PersistenceService)ServiceManager.lookup("javax.jnlp.PersistenceService");
bs = (BasicService)ServiceManager.lookup("javax.jnlp.BasicService");
} catch (UnavailableServiceException e) {
ps = null;
bs = null;
}
if (ps != null && bs != null) {
try {
// find all the muffins for our URL
URL codebase = bs.getCodeBase();
String [] muffins = ps.getNames(url);
// get the attributes (tags) for each of these muffins.
// update the server's copy of the data if any muffins
// are dirty
int [] tags = new int[muffins.length];
URL [] muffinURLs = new URL[muffins.length];
for (int i = 0; i < muffins.length; i++) {
muffinURLs[i] = new URL(codebase.toString() + muffins[i]);
tags[i] = ps.getTag(muffinURLs[i]);
// update the server if anything is tagged DIRTY
if (tags[i] == PersistenceService.DIRTY) {
doUpdateServer(muffinURLs[i]);
}
}
// read in the contents of a muffin and then delete it
FileContents fc = ps.get(muffinURLs[0]);
long maxsize = fc.getMaxLength();
byte [] buf = new byte[fc.getLength()];
InputStream is = fc.getInputStream();
long pos = 0;
while((pos = is.read(buf, pos, buf.length - pos)) > 0) {
// just loop
}
is.close();
ps.delete(muffinURLs[0]);
// re-create the muffin and repopulate its data
ps.create(muffinURLs[0], maxsize);
fc = ps.get(muffinURLs[0]);
// don't append
OutputStream os = fc.getOutputStream(false);
os.write(buf);
os.close();
} catch (Exception e) {
e.printStackTrace( );
}
}
void doUpdateServer(URL url) {
// update the server's copy of the persistent data
// represented by the given URL
...
ps.setTag(url, PersistenceService.CACHED);
}
FileContents の使い方javax.jnlp.FileContents オブジェクトは、特定のファイルの名前と内容をカプセル化します。 このクラスのオブジェクトは、FileOpenService、FileSaveService、および PersistenceService によって使用されます。次のサンプルコードは、FileContents のインスタンスを使ってファイルに対する読み書きを行う方法を示したものです。
import javax.jnlp.*;
...
FileOpenService fos;
//Initialize fos (see Using a FileOpenService Service example)
...
if (fos != null) {
try {
// get a FileContents object to work with from the
// FileOpenService
FileContents fc = fos.openFileDialog(null, null);
// get the InputStream from the file and read a few bytes
byte [] buf = new byte[fc.getLength()];
InputStream is = fc.getInputStream();
int pos = 0;
while((pos = is.read(buf, pos, buf.length - pos)) > 0) {
// just loop
}
is.close();
// get the OutputStream and write the file back out
if (fc.canWrite()) {
// don't append
OutputStream os = fc.getOutputStream(false);
os.write(buf);
}
} catch (Exception e) {
e.printStackTrace( );
}
}
JNLPRandomAccessFile の使い方javax.jnlp.JNLPRandomAccessFile のインスタンスは、ランダムアクセスファイルに対し、読み取りと書き込みの両方をサポートします。 ランダムアクセスファイルの動作は、ファイルシステム内に格納された大規模なバイト配列の動作に似ています。 次のサンプルコードは、JNLPRandomAccessFile のインスタンスを使ってランダムアクセスファイルに書き込みを行う方法を示したものです。
import javax.jnlp.*;
...
FileOpenService fos;
//Initialize fos (see Using a FileOpenService Service example)
...
if (fos != null) {
try {
// ask the user to choose a file to open
FileContents fc = fos.openFileDialog(null, null);
// attempt to increase the maximum file length
long grantedLength = fc.getLength();
if (grantedLength + 1024 > fc.getMaxLength()) {
// attempt to increase the maximum file size defined by
// the client
grantedLength = fc.setMaxLength(grantedLength + 1024);
}
// if we were able to increase the maximum allowable file size,
// get a JNLPRandomAccessFile representation of the file, and
// write to it
if (fc.getMaxSize() > fc.getLength() && fc.canWrite()) {
JNLPRandomAccessFile raf = fc.getRandomAccessFile("rw");
raf.seek(raf.length() - 1);
raf.writeUTF("Java Web Start!");
raf.close();
}
} catch (Exception e) {
e.printStackTrace( );
}
}
SingleInstanceService サービスの使い方javax.jnlp.SingleInstanceService の提供する一連のメソッドを使用すれば、アプリケーションは自身を単独で登録することができます。また、アプリケーションの異なるインスタンスから渡された引数を処理するためのリスナーを登録できます。
import javax.jnlp.*;
...
SingleInstanceService sis;
...
try {
sis =
(SingleInstanceService)ServiceManager.lookup("javax.jnlp.SingleInstanceService");
} catch (UnavailableServiceException e) { sis=null; }
...
// Register the single instance listener at the start of your application
SISListener sisL = new SISListener();
sis.addSingleInstanceListener(sisL);
...
// Remember to remove the listener before your application exits
sis.removeSingleInstanceListener(sisL);
System.exit(0);
// Implement the SingleInstanceListener for your application
class SISListener implements SingleInstanceListener {
public void newActivation(String[] params) {
// your code to handle the new arguments here
...
}
}
ExtendedService サービスの使い方javax.jnlp.ExtendedService は、現在の JNLP API サポートを強化します。このサービスを使用すると、クライアントのファイルシステムで、アプリケーションにより特定のファイルが開かれます。
import javax.jnlp.*;
...
ExtendedService es;
...
try {
es =
(ExtendedService)ServiceManager.lookup("javax.jnlp.ExtendedService");
} catch (UnavailableServiceException e) { es=null; }
...
// Open a specific file in the local machine
File a = new File("c:\somefile.txt");
...
// Java Web Start will pop up a dialog asking the user to grant permission
// to read/write the file c:\somefile.txt
FileContents fc_a = es.openFile(a);
// You can now use the FileContents object to read/write the file
...
// Open a specific set of files in the local machine
File[2] fArray = new File[2];
fArray[0] = a;
fArray[1] = new File("c:\anotherFile.txt");
// Java Web Start will pop up a dialog asking the user to grant permission
// to read/write files in fArray
FileContents[] fc_Array = es.OpenFiles(fArray);
// You can now read/write the set of files in fc_Array using the
// FileContents objects
}
javaws の使い方の詳細は、「javaws コマンド行インタフェース」を参照してください。