Source Code Cross Referenced for FileTransferManager.java in  » ESB » open-esb » com » sun » jbi » ui » common » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » ESB » open esb » com.sun.jbi.ui.common 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.