001: /**
002: * $Id: UploadDownloadFileManager.java,v 1.6 2005/09/21 10:46:51 dg154973 Exp $
003: * Copyright 2004 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.portal.fabric.mbeans;
014:
015: import java.io.IOException;
016: import java.io.FileNotFoundException;
017:
018: import java.util.List;
019: import java.util.Hashtable;
020: import java.util.logging.Level;
021: import java.util.logging.Logger;
022:
023: import com.sun.portal.log.common.PortalLogger;
024: import com.sun.portal.admin.server.mbeans.PSResource;
025: import com.sun.portal.admin.common.context.PSConfigContext;
026: import com.sun.portal.admin.common.context.PortalDomainContext;
027: import com.sun.portal.admin.common.PSMBeanException;
028: import com.sun.portal.fabric.tasks.UploadInfo;
029: import com.sun.portal.fabric.tasks.DownloadInfo;
030:
031: /**
032: * This class implements UploadDownloadFileManagerMBean interface and implements
033: * methods to upload and download files.
034: */
035: public class UploadDownloadFileManager extends PSResource implements
036: UploadDownloadFileManagerMBean {
037:
038: private static final String STRING_UPLOAD = "upload";
039: private static final String STRING_DOWNLOAD = "download";
040: private static final String STRING_HYPHEN = "-";
041: private static final String MSG_PREFIX = "fileUploadDownload";
042: private String psPortalID;
043: private static Logger logger = PortalLogger
044: .getLogger(UploadDownloadFileManager.class);
045: private PSConfigContext cc = null;
046: private Hashtable mInfo = new Hashtable(); //hashtable used as it is synchronized.
047: private int fileCounter = 0;
048:
049: public UploadDownloadFileManager() {
050: ;
051: }
052:
053: public void init(PSConfigContext cc, PortalDomainContext pdc,
054: List path) {
055: super .init(cc, pdc, path);
056: this .cc = cc;
057: this .psPortalID = (String) path.get(1);
058: }
059:
060: /**
061: * This method prepares for file upload.
062: * Sets the appropriate values for this file in UploadInfo class object.
063: * @param fileName without path as in file.getName()
064: * @return String array with first element as fileID and second element as
065: * filename with complete path as generated on this file system.
066: */
067: public String[] initiateFileUpload(String fileName, Long fileSize)
068: throws PSMBeanException {
069:
070: String mObjectId = STRING_UPLOAD + STRING_HYPHEN + getCounter();
071: UploadInfo uploadInfo = null;
072: try {
073: uploadInfo = new UploadInfo(fileName, fileSize.longValue(),
074: cc.getPSDataDir());
075: } catch (IOException ioe) {
076: logger.log(Level.SEVERE, "PSFB_CSPFM0003", ioe);
077: Object tokens[] = { uploadInfo.getName() };
078: throw new PSMBeanException(MSG_PREFIX + "errorInUpload",
079: ioe.getMessage(), tokens);
080: }
081: mInfo.put(mObjectId, uploadInfo); //this is synchronized as hashtable used.
082: String[] nameIdArray = { mObjectId, uploadInfo.getName() };
083: return nameIdArray;
084: }
085:
086: /**
087: * This method is synchronized to return unique counter as it can be accessed
088: * by multiple requests.
089: * @return This method returns unique updated counter.
090: */
091: private synchronized int getCounter() {
092: fileCounter = fileCounter + 1;
093: return fileCounter;
094: }
095:
096: /**
097: * This method writes bytesToUpload to the file whose objectId is passed as uploadId.
098: * @param uploadId : Id of the file as generated in initiateFileUpload
099: *
100: */
101: public void uploadBytes(String uploadId, byte[] bytesToUpload)
102: throws PSMBeanException {
103:
104: UploadInfo uploadInfo = (UploadInfo) mInfo.get(uploadId);
105: try {
106: uploadInfo.writeBytes(bytesToUpload);
107: } catch (IOException ioe) {
108:
109: Object tokens[] = { uploadInfo.getName() };
110: logger.log(Level.SEVERE, "PSFB_CSPFM0004", tokens);
111: logger.log(Level.SEVERE, "PSFB_CSPFM0003", ioe);
112: throw new PSMBeanException(MSG_PREFIX + "errorInUpload",
113: ioe.getMessage(), tokens);
114: }
115: }
116:
117: /**
118: * This method prepares for file download.
119: * Sets the appropriate values for this file in DownloadInfo class
120: * object.
121: * @param fileName with full path on the filesystem.
122: * @return Object array with first element as fileID and second element as fileSize
123: */
124: public Object[] initiateFileDownload(String fileName)
125: throws PSMBeanException {
126:
127: String mObjectId = STRING_DOWNLOAD + STRING_HYPHEN
128: + getCounter();
129: DownloadInfo downloadInfo = null;
130: try {
131: downloadInfo = new DownloadInfo(fileName);
132: } catch (FileNotFoundException fne) {
133: logger.log(Level.SEVERE, "PSFB_CSPFM0003", fne);
134: Object tokens[] = { fileName };
135: throw new PSMBeanException(MSG_PREFIX
136: + "errorInDownloadFileNotFound", fne.getMessage(),
137: tokens);
138: }
139: mInfo.put(mObjectId, downloadInfo);
140: Object[] idSizeArray = { mObjectId,
141: new Long(downloadInfo.getSize()) };
142: return idSizeArray;
143: }
144:
145: /**
146: * This method reads downloadSize no. of bytes from the file whose object id
147: * is passed as downloadId
148: * @param downloadId : Id of the file as generated in initiateFileDownload
149: * downloadSize : No. of bytes to download
150: */
151: public byte[] downloadBytes(String downloadId, Integer downloadSize)
152: throws PSMBeanException {
153:
154: byte[] readBytes = null;
155: DownloadInfo downloadInfo = (DownloadInfo) mInfo
156: .get(downloadId);
157: try {
158: readBytes = downloadInfo.readBytes(downloadSize.intValue());
159: } catch (IOException ioe) {
160: logger.log(Level.SEVERE, "PSFB_CSPFM0003", ioe);
161: Object tokens[] = { downloadInfo.getName() };
162: logger.log(Level.SEVERE, "PSFB_CSPFM0004", tokens);
163: throw new PSMBeanException(MSG_PREFIX + "errorInDownload",
164: ioe.getMessage(), tokens);
165: }
166: return readBytes;
167: }
168:
169: /**
170: * This method deletes temporary file created while upload.
171: * Also deletes this id entry from hash table
172: * @param Id of the file as generated in initiateFileUpload or filedownloaded
173: */
174: public void cleanUp(String Id) {
175: Object info = mInfo.get(Id);
176: if (info instanceof UploadInfo) {
177: UploadInfo uploadInfo = (UploadInfo) info;
178: boolean ret = false;
179: ret = uploadInfo.deleteFile();
180: if (!ret) {
181: logger.log(Level.SEVERE, "PSFB_CSPFM0005", uploadInfo
182: .getName());
183: }
184: } else if (info instanceof DownloadInfo) {
185: DownloadInfo downloadInfo = (DownloadInfo) info;
186: downloadInfo.cleanUp();
187: }
188: mInfo.remove(Id);
189: }
190: }
|