001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)FileTransferManager.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.ui.common;
030:
031: import java.io.File;
032: import java.io.FileInputStream;
033: import java.io.FileOutputStream;
034: import java.util.logging.Logger;
035:
036: import javax.management.MBeanServerConnection;
037: import javax.management.ObjectName;
038:
039: /**
040: * Provides access to the ESB Repository for remote clients. The scope of
041: * this class is limited to operations which require the transfer of
042: * archive data to/from the ESB Central Administration Server. All other
043: * repository operations can be accessed through the appropriate ESB MBean.
044: *
045: * The default chunk size for archive transfer is 8K. This setting can be
046: * adjusted using <code>setChunkSize</code> if necessary.
047: */
048: public class FileTransferManager {
049: /** Name of the ArchiveDownload MBean */
050: private static final String DOWNLOAD_MBEAN_NAME =
051: //"com.sun.jbi.esb:ServiceType=ArchiveDownload";
052: "com.sun.jbi:Target=domain,ServiceName=FileTransferService,ServiceType=Download";
053:
054: /** Name of the ArchiveUpload MBean */
055: private static final String UPLOAD_MBEAN_NAME =
056: //"com.sun.jbi.esb:ServiceType=ArchiveUpload";
057: "com.sun.jbi:Target=domain,ServiceName=FileTransferService,ServiceType=Upload";
058:
059: /** Default chunk size for archive transfer. */
060: private static final int DEFAULT_CHUNK_SIZE = 8 * 1024;
061:
062: /** Upload MBean operations. */
063: private static final String UPLOAD_INITIATE = "initiateUpload";
064:
065: private static final String UPLOAD_TRANSFER = "uploadBytes";
066:
067: private static final String GET_ARCHIVE_FILE_PATH = "getArchiveFilePath";
068:
069: private static final String UPLOAD_TERMINATE = "terminateUpload";
070:
071: private static final String REMOVE_ARCHIVE = "removeArchive";
072:
073: /** Download MBean operations. */
074: private static final String DOWNLOAD_INITIATE = "initiateDownload";
075:
076: private static final String DOWNLOAD_TRANSFER = "downloadBytes";
077:
078: private static final String DOWNLOAD_TERMINATE = "terminateDownload";
079:
080: private MBeanServerConnection mServer;
081:
082: private ObjectName mDownloadMBean;
083:
084: private ObjectName mUploadMBean;
085:
086: private int mChunkSize = DEFAULT_CHUNK_SIZE;
087:
088: private Logger mLog = Logger
089: .getLogger("com.sun.jbi.management.repository");
090:
091: private Object mLastUploadId = null;
092:
093: public FileTransferManager(MBeanServerConnection server) {
094: mServer = server;
095:
096: try {
097: mDownloadMBean = new ObjectName(DOWNLOAD_MBEAN_NAME);
098: mUploadMBean = new ObjectName(UPLOAD_MBEAN_NAME);
099: } catch (javax.management.MalformedObjectNameException monEx) {
100: mLog.severe(monEx.getMessage());
101: }
102: }
103:
104: /** Uploads the specified archive to a temporary store in the ESB repository.
105: * @return CAS-local path to the archive
106: */
107: public String uploadArchive(File archive) throws Exception {
108: Object uploadId;
109: FileInputStream fis;
110:
111: // setup our stream before initiating the upload session
112: fis = new FileInputStream(archive);
113:
114: // initiate an upload session
115: uploadId = mServer.invoke(mUploadMBean, UPLOAD_INITIATE,
116: new Object[] { archive.getName() },
117: new String[] { "java.lang.String" });
118:
119: this .mLastUploadId = uploadId; // remember the last uploaded id as it can be used for remove archive later
120:
121: // get the upload url from the sesssion
122:
123: Object uploadFilePathObj = mServer.invoke(mUploadMBean,
124: GET_ARCHIVE_FILE_PATH, new Object[] { uploadId },
125: new String[] { "java.lang.Object" });
126:
127: // transfer the archive content to the server, chunk by chunk
128: byte[] chunk = new byte[mChunkSize];
129: int count = 0;
130: while ((count = fis.read(chunk)) != -1) {
131: mServer.invoke(mUploadMBean, UPLOAD_TRANSFER, new Object[] {
132: uploadId, trim(chunk, count) }, new String[] {
133: "java.lang.Object", "[B" });
134: }
135:
136: // transfer complete, terminate the session
137: mServer.invoke(mUploadMBean, UPLOAD_TERMINATE, new Object[] {
138: uploadId, Long.valueOf(0) }, new String[] {
139: "java.lang.Object", "java.lang.Long" });
140:
141: String uploadFilePath = null;
142:
143: if (uploadFilePathObj != null) {
144: uploadFilePath = uploadFilePathObj.toString();
145: }
146:
147: return uploadFilePath;
148: }
149:
150: public Object getLastUploadId() {
151: return this .mLastUploadId;
152: }
153:
154: /**
155: * this will remove any previously uploaded archive by using its uploaded id
156: */
157: public void removeUploadedArchive(Object uploadId) {
158: if (uploadId == null) {
159: return;
160: }
161: try {
162: mServer.invoke(mUploadMBean, REMOVE_ARCHIVE,
163: new Object[] { uploadId },
164: new String[] { "java.lang.Object" });
165: } catch (Exception ex) {
166: // ignore any exception as the removal of the archive file is not important
167: // and the server during restart anyway cleans it up.
168: //TODO log the exception if you want.
169: mLog.warning(ex.getMessage());
170: }
171: }
172:
173: public void downloadArchive(String archiveLocation, File target)
174: throws Exception {
175: Object downloadId;
176: FileOutputStream fos;
177:
178: // setup our stream before initiating the download session
179: fos = new FileOutputStream(target);
180:
181: // initiate a download session
182: downloadId = mServer.invoke(mDownloadMBean, DOWNLOAD_INITIATE,
183: new Object[] { archiveLocation },
184: new String[] { "java.lang.Object" });
185:
186: // transfer the archive content from the server, chunk by chunk
187: byte[] chunk;
188: do {
189: chunk = (byte[]) mServer.invoke(mDownloadMBean,
190: DOWNLOAD_TRANSFER, new Object[] { downloadId,
191: new Integer(mChunkSize) }, new String[] {
192: "java.lang.Object", "int" });
193:
194: fos.write(chunk);
195: fos.flush();
196: } while (chunk.length > 0);
197:
198: // transfer complete, terminate the session
199: mServer.invoke(mDownloadMBean, DOWNLOAD_TERMINATE,
200: new Object[] { downloadId },
201: new String[] { "java.lang.Object" });
202:
203: fos.close();
204: }
205:
206: /** Return the chunk size for archive transfers. */
207: public int getChunkSize() {
208: return mChunkSize;
209: }
210:
211: /** Sets the chuck size for archive transfers. */
212: public void setChunkSize(int size) {
213: mChunkSize = size;
214: }
215:
216: /** Returns a byte array of the specified size, using the source array
217: * as input. If the length of the source array is equal to the desired
218: * size, the original array is returned.
219: */
220: private byte[] trim(byte[] source, int size) {
221: byte[] trimmed;
222:
223: if (source.length == size) {
224: trimmed = source;
225: } else {
226: trimmed = new byte[size];
227: System.arraycopy(source, 0, trimmed, 0, size);
228: }
229:
230: return trimmed;
231: }
232: }
|