Source Code Cross Referenced for EarFileManager.java in  » J2EE » JOnAS-4.8.6 » org » objectweb » jonas_lib » deployment » work » 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 » J2EE » JOnAS 4.8.6 » org.objectweb.jonas_lib.deployment.work 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * JOnAS: Java(TM) Open Application Server
003:         * Copyright (C) 1999-2004 Bull S.A.
004:         * Contact: jonas-team@objectweb.org
005:         *
006:         * This library is free software; you can redistribute it and/or
007:         * modify it under the terms of the GNU Lesser General Public
008:         * License as published by the Free Software Foundation; either
009:         * version 2.1 of the License, or any later version.
010:         *
011:         * This library is distributed in the hope that it will be useful,
012:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
013:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014:         * Lesser General Public License for more details.
015:         *
016:         * You should have received a copy of the GNU Lesser General Public
017:         * License along with this library; if not, write to the Free Software
018:         * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
019:         * USA
020:         *
021:         * Initial developer(s): Florent BENOIT & Ludovic BERT
022:         * --------------------------------------------------------------------------
023:         * $Id: EarFileManager.java 5141 2004-07-16 13:57:50Z benoitf $
024:         * --------------------------------------------------------------------------
025:         */package org.objectweb.jonas_lib.deployment.work;
026:
027:        import java.io.File;
028:        import java.io.IOException;
029:        import java.io.InputStream;
030:        import java.net.URL;
031:        import java.util.Enumeration;
032:        import java.util.jar.JarEntry;
033:        import java.util.jar.JarFile;
034:
035:        /**
036:         * JOnAS Ear File manager. This class provides a way for managing the EAR files.
037:         * @author Florent Benoit
038:         * @author Ludovic Bert
039:         * @author Nicolas Van Caneghem <nicolas.vancaneghem@openpricer.com>Allow the
040:         *         deployment of an exploded ear
041:         */
042:
043:        public class EarFileManager extends FileManager {
044:
045:            /**
046:             * Constructor. Private as it is an utility class
047:             */
048:            private EarFileManager() {
049:                super ();
050:            }
051:
052:            /**
053:             * true If an unpacked directory has the same timestamp than the EAR file,
054:             * false otherwise.
055:             * @param urlFileName the url of the name of the EAR file (ends with the
056:             *        .ear extension).
057:             * @param urlDirName the url of the directory where the file must be
058:             *        unpacked.
059:             * @return true If an unpacked directory has the same timestamp than the EAR
060:             *         file, false otherwise.
061:             * @throws FileManagerException if the file doesn't exist
062:             */
063:            protected static boolean isUnpackedEar(URL urlFileName,
064:                    URL urlDirName) throws FileManagerException {
065:
066:                File urlFile = new File(urlFileName.getFile());
067:
068:                if (!urlFile.exists()) {
069:                    throw new FileManagerException("File " + urlFileName
070:                            + "doesn't exist");
071:                }
072:                String timeStampDir = fileToTimeStampDir(urlFileName);
073:
074:                File f = new File(urlDirName.getPath() + File.separator
075:                        + timeStampDir);
076:
077:                return f.exists();
078:            }
079:
080:            /**
081:             * Unpack the given EAR file to the specified directory.
082:             * @param urlFileName the url of the name of the EAR file to unpack.
083:             * @param urlDirName the url of the destination directory where is unpacked
084:             *        the EAR file.
085:             * @return the url of the unpacked directory
086:             * @throws FileManagerException if we can't unpack the file.
087:             */
088:            public static URL unpackEar(URL urlFileName, URL urlDirName)
089:                    throws FileManagerException {
090:                return unpackEar(urlFileName, urlDirName, true);
091:            }
092:
093:            /**
094:             * Unpack the given EAR file to the specified directory.
095:             * @param urlFileName the url of the name of the EAR file to unpack.
096:             * @param urlDirName the url of the destination directory where is unpacked
097:             *        the EAR file.
098:             * @param useTimeStamp use timestamping for unpacking the EAR or not
099:             * @return the url of the unpacked directory
100:             * @throws FileManagerException if we can't unpack the file.
101:             */
102:            public static URL unpackEar(URL urlFileName, URL urlDirName,
103:                    boolean useTimeStamp) throws FileManagerException {
104:
105:                // Check protocol
106:                if ((!urlFileName.getProtocol().equalsIgnoreCase("file"))
107:                        || (!urlDirName.getProtocol().equalsIgnoreCase("file"))) {
108:                    throw new FileManagerException(
109:                            "Only the file:/ URL can be used");
110:                }
111:
112:                // if ear is exploded
113:                if (new File(urlFileName.getFile()).isDirectory()) {
114:                    return urlFileName;
115:                }
116:
117:                // Get the dirName
118:                String timeStampDir = fileToTimeStampDir(urlFileName);
119:
120:                boolean unPacked = false;
121:                if (useTimeStamp) {
122:                    unPacked = isUnpackedEar(urlFileName, urlDirName);
123:                }
124:
125:                // EAR file and directory
126:                JarFile earFile = null;
127:                File parentDirectoryFile = null;
128:                URL parentDirectoryUrl = null;
129:                try {
130:                    earFile = new JarFile(urlFileName.getFile());
131:                    if (useTimeStamp) {
132:                        parentDirectoryFile = new File(urlDirName.getPath()
133:                                + File.separator + timeStampDir);
134:                    } else {
135:                        String stReturn = new File(urlFileName.getFile())
136:                                .getName();
137:                        String userName = System.getProperty("user.name",
138:                                "default");
139:                        //Remove extension .ear
140:                        int lastIndex = stReturn.lastIndexOf(".");
141:                        if (lastIndex == -1) {
142:                            throw new FileManagerException(
143:                                    "The specified file "
144:                                            + urlFileName.getFile()
145:                                            + " is not a file with the format XXX.ear.");
146:                        }
147:                        stReturn = stReturn.substring(0, lastIndex);
148:
149:                        parentDirectoryFile = new File(urlDirName.getPath()
150:                                + File.separator + userName + "_" + stReturn);
151:                    }
152:                    //Protocol file: no .toURL() method because we have a / at the end
153:                    // of the url otherwise
154:                    parentDirectoryUrl = new URL("file:"
155:                            + parentDirectoryFile.getPath());
156:                } catch (IOException e) {
157:                    throw new FileManagerException(
158:                            "Error while creating file for reading the ear file :"
159:                                    + urlFileName + ": " + e.getMessage());
160:                }
161:
162:                //Nothing to do if it's unpack
163:                if (unPacked) {
164:                    return parentDirectoryUrl;
165:                }
166:
167:                JarEntry earEntry = null;
168:                try {
169:                    try {
170:                        //get entries of the EAR file
171:                        for (Enumeration earEntries = earFile.entries(); earEntries
172:                                .hasMoreElements();) {
173:                            earEntry = (JarEntry) earEntries.nextElement();
174:
175:                            //File entry
176:                            File earEntryFile = new File(parentDirectoryFile,
177:                                    earEntry.getName());
178:
179:                            //Create directory
180:                            if (earEntry.isDirectory()) {
181:                                if (!earEntryFile.exists()) {
182:                                    //create parent directories (with mkdirs)
183:                                    if (!earEntryFile.mkdirs()) {
184:                                        String err = "Can not create directory "
185:                                                + earEntryFile
186:                                                + ", Check the write access.";
187:                                        throw new FileManagerException(err);
188:                                    }
189:                                }
190:                                continue;
191:                            }
192:
193:                            //If it's a file, we must extract the file
194:                            //Ensure that the directory exists.
195:                            earEntryFile.getParentFile().mkdirs();
196:
197:                            InputStream is = null;
198:                            try {
199:                                //get the input stream
200:                                is = earFile.getInputStream(earEntry);
201:
202:                                //Dump to the file
203:                                dump(is, earEntryFile);
204:                            } finally {
205:                                is.close();
206:                            }
207:                        }
208:                    } finally {
209:                        earFile.close();
210:                    }
211:                } catch (IOException e) {
212:                    throw new FileManagerException(
213:                            "Error while uncompressing entry " + earEntry
214:                                    + ": " + e.getMessage());
215:                }
216:                return parentDirectoryUrl;
217:            }
218:
219:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.