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: * @(#)ArchiveDownload.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.StringTranslator;
032: import com.sun.jbi.management.repository.Repository;
033: import com.sun.jbi.management.registry.Registry;
034: import com.sun.jbi.management.LocalStringKeys;
035: import com.sun.jbi.management.system.ManagementContext;
036:
037: import java.io.ByteArrayInputStream;
038: import java.io.File;
039: import java.io.InputStream;
040: import java.io.FileInputStream;
041: import java.rmi.server.UID;
042: import java.util.HashMap;
043: import java.util.Iterator;
044: import java.util.logging.Logger;
045:
046: /**
047: * Downloads archive bytes from the ESB repository.
048: */
049: public class ArchiveDownload implements ArchiveDownloadMBean {
050: private ManagementContext mContext;
051: private StringTranslator mStrings;
052: private Logger mLog;
053: private HashMap mSessions;
054: private Repository mRepository;
055: private String mInstallRoot;
056:
057: public ArchiveDownload(ManagementContext ctx) {
058: mContext = ctx;
059: mSessions = new HashMap();
060: mLog = ctx.getLogger();
061: mRepository = ctx.getRepository();
062: mStrings = ctx.getEnvironmentContext().getStringTranslator(
063: "com.sun.jbi.management");
064: mInstallRoot = new File(ctx.getEnvironmentContext()
065: .getJbiInstanceRoot()).getPath();
066: }
067:
068: /**
069: * Returns the archive id of a service assembly in the repository.
070: * @param saName name of the service assembly
071: * @return archive ID, or null if the service assembly does not exist
072: * in the repository.
073: */
074: public Object getServiceAssemblyArchiveId(String saName) {
075: return mRepository.findArchive(ArchiveType.SERVICE_ASSEMBLY,
076: saName);
077: }
078:
079: /**
080: * Returns the archive id of a component in the repository.
081: * @param componentName name of the component
082: * @return archive ID, or null if the component does not exist
083: * in the repository.
084: */
085: public Object getComponentArchiveId(String componentName) {
086: return mRepository.findArchive(ArchiveType.COMPONENT,
087: componentName);
088: }
089:
090: /**
091: * Returns the archive id of a shared library in the repository.
092: * @param sharedLibrayName name of the shared library
093: * @return archive ID, or null if the shared library does not exist
094: * in the repository.
095: */
096: public Object getSharedLibraryArchiveId(String sharedLibrayName) {
097: return mRepository.findArchive(ArchiveType.SHARED_LIBRARY,
098: sharedLibrayName);
099: }
100:
101: /** Initiates the download of the DAS registry. This is a special purpose
102: * method due to the need to synchronize copying the registry while others may be
103: * writing to it.
104: * @return download session id
105: */
106: public Object initiateRegistryDownload() throws java.io.IOException {
107: String id;
108:
109: //
110: // Save the snapshot result for the download.
111: //
112: try {
113: //mContext.getEnvironmentContext().isFrameworkReady(true);
114: mSessions.put(id = createUID(), ((Registry) mContext
115: .getEnvironmentContext().getRegistry()).snapshot());
116: return id;
117: } catch (Exception e) {
118: throw new java.io.IOException(e.toString());
119: }
120: }
121:
122: /** Initiates the download of an archive identified by 'archiveId'. In
123: * most situations, the archiveId will simply be the CAS-local path to
124: * the archive in the ESB repository. The path is checked to ensure that
125: * only files contains in the jbi install root can be copied. A relative path
126: * is prepended with the jbi install root. The returned Object is used as
127: * a session id for downloading the archive bytes with
128: * <code>downloadBytes()</code>.
129: * @return download session id
130: */
131: public Object initiateDownload(Object archiveId)
132: throws java.io.IOException {
133: String file = (String) archiveId;
134: String id;
135: File archive;
136: FileInputStream fis;
137:
138: archive = new File(file);
139: if (!archive.isAbsolute()) {
140: archive = new File(file = mInstallRoot + File.separator
141: + file);
142: }
143: if (file.startsWith(mInstallRoot)) {
144: id = createUID();
145:
146: if (!archive.exists()) {
147: throw new java.io.IOException(
148: mStrings
149: .getString(
150: LocalStringKeys.JBI_ADMIN_DOWNLOAD_ARCHIVE_DOES_NOT_EXIST,
151: archive.getPath()));
152: }
153:
154: fis = new FileInputStream(archive);
155: mSessions.put(id, fis);
156:
157: return id;
158: }
159: throw new java.io.IOException(
160: mStrings
161: .getString(
162: LocalStringKeys.JBI_ADMIN_DOWNLOAD_ARCHIVE_DOES_NOT_EXIST,
163: file));
164:
165: }
166:
167: /** Download <code>length</code> bytes from an existing download session.
168: * @return a byte array of the specified length. If the remaining bytes
169: * in the session <= length, the byte array is sized appropriately. If
170: * all bytes have been downloaded, the returned array length is 0.
171: */
172: public byte[] downloadBytes(Object id, int length)
173: throws java.io.IOException {
174: int count;
175: byte[] bytes;
176:
177: bytes = new byte[length];
178: count = getDownloadStream(id).read(bytes);
179:
180: // check to make sure array sizes match
181: if (count <= 0) {
182: bytes = new byte[0];
183: } else if (count != length) {
184: byte[] tmp = new byte[count];
185: System.arraycopy(bytes, 0, tmp, 0, count);
186: bytes = tmp;
187: }
188:
189: return bytes;
190: }
191:
192: /** Used to indicate that a download session is complete.
193: */
194: public void terminateDownload(Object id) throws java.io.IOException {
195: getDownloadStream(id).close();
196: mSessions.remove(id);
197: }
198:
199: public void terminateAllDownloads() {
200: Iterator ids;
201:
202: try {
203: ids = mSessions.keySet().iterator();
204: while (ids.hasNext()) {
205: terminateDownload(ids.next());
206: }
207: } catch (java.io.IOException ioEx) {
208: mLog.warning(ioEx.getMessage());
209: }
210: }
211:
212: /** Retrieve the input stream for a session. */
213: private InputStream getDownloadStream(Object id)
214: throws java.io.IOException {
215: InputStream is;
216:
217: is = (InputStream) mSessions.get(id);
218: if (is == null) {
219: throw new java.io.IOException(
220: mStrings
221: .getString(LocalStringKeys.JBI_ADMIN_DOWNLOAD_ID_NOT_FOUND));
222: }
223:
224: return is;
225: }
226:
227: /** Create a unique id for the download session. */
228: private String createUID() {
229: String uid;
230:
231: uid = new UID().toString();
232: if (uid.startsWith("-")) {
233: uid = uid.substring(1);
234: }
235:
236: return uid;
237: }
238: }
|