は、ここでファイルにあなたのコンテンツを取得するために、比較的単純なアプローチです。
セッションBeanでは
は、のようなものですビジネスメソッドを実装します。
public Object getContent(String fileName, <Other Args>) {
// Get content
// write to a byte array
byte[] content = ......;
return new ByteArrayFile(content, fileName);
}
(DBから)データを取得し、一緒に(そしてロードされたバイト配列にそれをシリアル化する必要があるメソッドの実装オプションのファイル名)をByteArrayFileのインスタンスに挿入して返します。
ByteArrayFile をは(提供された名前または一時ファイルを使用して)ローカルファイルにバイト配列を変換する方法をreadResolvedとjava.ioとして呼び出し元に返さ実装シリアライズオブジェクトであります.File。
ここ ByteArrayFile のラフな実装です:
public class ByteArrayFile implements Serializable {
protected final byte[] content;
protected final String fileName;
/**
* Creates a new ByteArrayFile
* @param content The file content
*/
public ByteArrayFile(byte[] content) {
this(content, null);
}
/**
* Creates a new ByteArrayFile
* @param content The file content
* @param fileName The file name to deserialize as
*/
public ByteArrayFile(byte[] content, String fileName) {
super();
this.content = content;
this.fileName = fileName;
}
/**
* Returns this object as a resolved file when the object is deserialized.
* @return
* @throws ObjectStreamException
*/
protected Object readResolve() throws ObjectStreamException {
FileOutputStream fos = null;
try {
File f = fileName==null ? File.createTempFile(getClass().getSimpleName(), ".file") : new File(fileName);
if(!f.canWrite()) {
throw new Exception("Unable to write to designated file [" + fileName + "]", new Throwable());
}
fos = new FileOutputStream(f);
fos.write(content);
fos.close();
fos = null;
return f;
} catch (Exception e) {
throw new RuntimeException("Failed to readResolve", e);
} finally {
try { if(fos!=null) fos.close(); } catch (Exception e) {}
}
}
}
これは、 ByteArrayFile を作成し、それをシリアル化して、としてそれを読み戻す表示するために(すなわちないリモートEJBコール)簡単な例でありますファイル:
public static void main(String[] args) {
ByteArrayFile baf = new ByteArrayFile("Hello World".getBytes());
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(baf);
oos.flush();
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bais);
File file = (File)ois.readObject();
System.out.println("File:" + file.getAbsolutePath() + " Size:" + file.length());
} catch (Exception e) {
e.printStackTrace(System.err);
}
}
出力された:
ファイル:/tmp/ByteArrayFile2790187442947455193.fileサイズ:真実で11
、セッションBeanは、絶対に直接ファイルを書き込むことができます。厳密なEJBの制限はJBossによって強制されず、気にしないかもしれない移植性の保証を提供するために存在します。ただし、上記のアプローチの利点は、リモートクライアントが遠隔から呼び出しを呼び出すことができますが、ローカルでファイルを取得できることです。
ニコラス - あなたの非常に徹底的な説明に感謝します。 – Ilane
これは、Runtime.getRuntime()。execを使用して必要なコマンドを実行する別の既存のステートレスセッションBeanを呼び出すことによって、ファイルを使用してコマンドを呼び出すセッションBeanです(これはおそらく許可されません)。機能は既に存在します。レガシーソフトウェア - それが大好き!)ビジネスメソッドは "sendContent()"となります。このファイルは、2つのEJBに対してのみ認識されます。オブジェクトをリモートクライアントに渡すのではなく、オブジェクトをどのようにして非直列化しますか。 2番目のセッションBeanのメソッドが引数として呼び出されたときでしょうか? –
Ilane