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: * @(#)ArchiveUpload.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.management.repository;
030:
031: import com.sun.jbi.management.system.ManagementContext;
032:
033: import com.sun.jbi.StringTranslator;
034: import com.sun.jbi.management.LocalStringKeys;
035: import com.sun.jbi.management.util.FileHelper;
036:
037: import java.io.File;
038: import java.io.OutputStream;
039: import java.io.FileOutputStream;
040: import java.net.URI;
041: import java.util.HashMap;
042: import java.util.Iterator;
043: import java.util.logging.Logger;
044: import java.rmi.server.UID;
045:
046: /**
047: * Uploads archive bytes to a temporary store in the ESB repository.
048: */
049: public class ArchiveUpload implements ArchiveUploadMBean {
050: private StringTranslator mStrings;
051: private Logger mLog;
052: private File mUploadDir;
053: private HashMap mSessions;
054: private HashMap<String, File> mFiles;
055:
056: public ArchiveUpload(ManagementContext ctx) {
057: mSessions = new HashMap();
058: mFiles = new HashMap();
059: mLog = ctx.getLogger();
060: mUploadDir = ctx.getRepository().getTempStore();
061: mStrings = ctx.getEnvironmentContext().getStringTranslator(
062: "com.sun.jbi.management");
063: }
064:
065: /** Initiates an upload session. The Object returned from this method
066: * is used as an identifier for uploading bytes with the
067: * <code>uploadBytes()</code> method.
068: * @return a unique id used for uploading bytes
069: */
070: public Object initiateUpload(String archiveName)
071: throws java.io.IOException {
072: String id;
073: File dir;
074: File file;
075: FileOutputStream fos;
076:
077: id = createUID();
078: dir = new File(mUploadDir, id);
079: dir.mkdir();
080: file = new File(dir, archiveName);
081: fos = new FileOutputStream(file);
082:
083: mSessions.put(id, fos);
084: mFiles.put(id, file);
085: return id;
086: }
087:
088: /** Uploads a chunk of bytes to an existing temporary archive file.
089: * @param id the upload identifier
090: * @param bytes the content to upload
091: */
092: public void uploadBytes(Object id, byte[] bytes)
093: throws java.io.IOException {
094: OutputStream out;
095:
096: out = getUploadStream(id);
097: out.write(bytes);
098: out.flush();
099: }
100:
101: /** Used to indicate that the upload session with the specified id is
102: * complete.
103: */
104: public void terminateUpload(Object id, Long timestamp)
105: throws java.io.IOException {
106: File file = mFiles.get(id);
107:
108: getUploadStream(id).close();
109: mSessions.remove(id);
110: mFiles.remove(id);
111: if (file != null && timestamp != null
112: && timestamp.longValue() != 0) {
113: file.setLastModified(timestamp.longValue());
114: }
115: }
116:
117: public void terminateAllUploads() {
118: Iterator ids;
119:
120: try {
121: ids = mSessions.keySet().iterator();
122: while (ids.hasNext()) {
123: terminateUpload(ids.next(), null);
124: }
125: } catch (java.io.IOException ioEx) {
126: mLog.warning(ioEx.getMessage());
127: }
128: }
129:
130: /** Returns the location of the uploaded archive.
131: * @param id the upload identifier
132: * @return the location of the archive as a String URL, or null
133: * if the specified archive id does not exist in the repository.
134: */
135: public String getArchiveURL(Object id) {
136: String arUrl = null;
137: File dir = new File(mUploadDir, (String) id);
138:
139: if (dir.exists()) {
140: File[] files = dir.listFiles();
141:
142: if (files.length > 0) {
143: File archive = files[0];
144: URI archiveURI = archive.toURI();
145: try {
146: arUrl = archiveURI.toURL().toString();
147: } catch (java.net.MalformedURLException muex) {
148: mLog.warning(muex.getMessage());
149: }
150: }
151: }
152:
153: return arUrl;
154: }
155:
156: /** Returns the absolute path to the location of the uploaded archive.
157: * @param id the upload identifier
158: * @return the absolute path to the location of the archive as a String , or null
159: * if the specified archive id does not exist in the repository.
160: */
161: public String getArchiveFilePath(Object id) {
162: String archivePath = null;
163: File dir = new File(mUploadDir, (String) id);
164:
165: if (dir.exists()) {
166: File[] files = dir.listFiles();
167:
168: if (files.length > 0) {
169: File archive = files[0];
170: if (archive != null) {
171: archivePath = archive.getAbsolutePath();
172: }
173: }
174: }
175:
176: return archivePath;
177: }
178:
179: /** Delete the uploaded archive.
180: *
181: * @return true if the archive is deleted succcessfully, false otherwise
182: */
183: public boolean removeArchive(Object id) {
184: File dir = new File(mUploadDir, (String) id);
185: boolean removed = false;
186:
187: removed = FileHelper.cleanDirectory(dir);
188: if (removed) {
189: dir.delete();
190: }
191: return removed;
192: }
193:
194: /** Retrieve the output stream for a session. */
195: private OutputStream getUploadStream(Object id)
196: throws java.io.IOException {
197: FileOutputStream fos;
198:
199: fos = (FileOutputStream) mSessions.get(id);
200: if (fos == null) {
201: throw new java.io.IOException(
202: mStrings
203: .getString(LocalStringKeys.JBI_ADMIN_UPLOAD_ID_NOT_FOUND));
204: }
205:
206: return fos;
207: }
208:
209: /** Create a unique id for the upload session. */
210: private String createUID() {
211: String uid;
212:
213: uid = new UID().toString();
214: uid = uid.replaceAll("-", "");
215: uid = uid.replaceAll(":", "");
216:
217: return uid;
218: }
219: }
|