[PORTAL-7] Rebase
[portal.git] / ecomp-portal-BE-common / src / main / java / jarutil / ExtractJar.java
1 /*-\r
2  * ================================================================================\r
3  * ECOMP Portal\r
4  * ================================================================================\r
5  * Copyright (C) 2017 AT&T Intellectual Property\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  * \r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * \r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ================================================================================\r
19  */\r
20 package jarutil;\r
21 \r
22 import java.io.File;\r
23 import java.io.FileInputStream;\r
24 import java.io.FileOutputStream;\r
25 import java.io.IOException;\r
26 import java.io.InputStream;\r
27 import java.io.OutputStream;\r
28 import java.io.Reader;\r
29 import java.io.Writer;\r
30 import java.net.URL;\r
31 import java.util.ArrayList;\r
32 import java.util.jar.JarEntry;\r
33 import java.util.jar.JarInputStream;\r
34 \r
35 public class ExtractJar {\r
36 \r
37         /**\r
38          * @param args\r
39          */\r
40         \r
41         public static int bufferSize = 8192;\r
42         public static String JARFILE = "raptor_upgrade.jar";\r
43          \r
44         public static void main(String[] args) throws Exception {\r
45                 if(args.length > 0 && args[0]!=null && args[0].length()>0)\r
46                         extractFilesFromJar(args[0]);\r
47                 else {\r
48                         System.out.println("Current Directory is taken as webapp path");\r
49                         String currentDir = new File(".").getAbsolutePath();\r
50                         extractFilesFromJar(currentDir);\r
51                 }\r
52         }\r
53         \r
54     public static void readJar(File jarFile) throws Exception{\r
55         JarInputStream in = new JarInputStream(new FileInputStream(jarFile));\r
56         ArrayList entries = new ArrayList( );\r
57         JarEntry je;\r
58         while((je=in.getNextJarEntry( ))!=null){\r
59                  if( je.isDirectory() == false ) {\r
60                          if(je.getName().startsWith("att/raptor/config/")) {\r
61                                  //while((je=in.getNextJarEntry( ))!=null) {\r
62                                          System.out.println(je.getName() + " " + je.getTime());\r
63                                  //}\r
64                                  \r
65                          }\r
66                  }\r
67         }\r
68         in.close( );\r
69     }\r
70     \r
71     public static void extractFilesFromJar(String directory) throws IOException{\r
72         //JarFile jar = new JarFile(jarFile);\r
73         Class clazz = ExtractJar.class;\r
74                 String classContainer = clazz.getProtectionDomain().getCodeSource().getLocation().toString();\r
75                 //System.out.println("classContainer ---------> " + classContainer);\r
76             URL jarUrl = clazz.getProtectionDomain().getCodeSource().getLocation();\r
77                 \r
78         JarInputStream entryStream = new JarInputStream(jarUrl.openStream()); \r
79         JarEntry entry;\r
80         while(true) {\r
81             entry = entryStream.getNextJarEntry();\r
82             if(entry == null)\r
83                 break;\r
84            if(entry.getName().indexOf("jarutil") < 0) {\r
85                System.out.println(entry.getName());\r
86                    File file=new File(directory,entry.getName());\r
87                if(entry.isDirectory()) {\r
88                    if (!file.exists())\r
89                            file.mkdirs();\r
90                } else {\r
91                    // make directory (some jars don't list dirs)\r
92                    File dir = new File(file.getParent());\r
93                    if (!dir.exists())\r
94                        dir.mkdirs();\r
95                     if(file.exists()) file.delete();  \r
96                    // Make file\r
97                    FileOutputStream fout = new FileOutputStream(file);\r
98                    copy(entryStream,fout);\r
99                    fout.close();\r
100 \r
101                    // touch the file.\r
102                    if (entry.getTime()>=0)\r
103                        file.setLastModified(entry.getTime());\r
104                }\r
105                \r
106            }\r
107           entryStream.closeEntry(); \r
108         }\r
109         entryStream.close();\r
110         System.out.println("************************************************");\r
111         System.out.println("*                                              *");\r
112         System.out.println("*                                              *");\r
113         System.out.println("*          RAPTOR SETUP COMPLETE.              *");\r
114         System.out.println("*                                              *");\r
115         System.out.println("*         Thank you for upgrading.             *");\r
116         System.out.println("*                                              *");\r
117         System.out.println("************************************************");\r
118     }\r
119     \r
120         \r
121     public static void copy(InputStream in,  OutputStream out, long byteCount)\r
122                                                                                                                 throws IOException {     \r
123         byte buffer[] = new byte[bufferSize];\r
124         int len=bufferSize;\r
125         if (byteCount>=0) {\r
126                 while (byteCount>0) {\r
127                         if (byteCount<bufferSize)\r
128                                 len=in.read(buffer,0,(int)byteCount);\r
129                         else\r
130                                 len=in.read(buffer,0,bufferSize);                   \r
131                         if (len==-1)\r
132                                 break;\r
133 \r
134                         byteCount -= len;\r
135                         out.write(buffer,0,len);\r
136                 }\r
137         } else {\r
138                 while (true) {\r
139                         len=in.read(buffer,0,bufferSize);\r
140                         if (len<0 )\r
141                                 break;\r
142                         out.write(buffer,0,len);\r
143                 }\r
144         }\r
145     }  \r
146 \r
147 /* ------------------------------------------------------------------- */\r
148 /** Copy Reader to Writer for byteCount bytes or until EOF or exception.\r
149 */\r
150     public static void copy(Reader in, Writer out, long byteCount)\r
151                                                                                 throws IOException {  \r
152         char buffer[] = new char[bufferSize];\r
153         int len=bufferSize;\r
154         if (byteCount>=0) {\r
155                 while (byteCount>0) {\r
156                         if (byteCount<bufferSize)\r
157                                 len=in.read(buffer,0,(int)byteCount);\r
158                         else\r
159                                 len=in.read(buffer,0,bufferSize);                   \r
160 \r
161                         if (len==-1)\r
162                                 break;\r
163                         byteCount -= len;\r
164                         out.write(buffer,0,len);\r
165                 }\r
166         } else {\r
167                 while (true) {\r
168                         len=in.read(buffer,0,bufferSize);\r
169                         if (len==-1)\r
170                                 break;\r
171                         out.write(buffer,0,len);\r
172                 }\r
173         }\r
174     }\r
175 \r
176     /* ------------------------------------------------------------------- */\r
177     /** Copy Stream in to Stream out until EOF or exception.\r
178      */\r
179     public static void copy(InputStream in, OutputStream out)\r
180          throws IOException\r
181     {\r
182         copy(in,out,-1);\r
183     }\r
184     \r
185     // Deletes all files and subdirectories under dir.\r
186     // Returns true if all deletions were successful.\r
187     // If a deletion fails, the method stops attempting to delete and returns false.\r
188     public static boolean deleteDir(File dir) {\r
189         if (dir.isDirectory()) {\r
190             String[] children = dir.list();\r
191             for (int i=0; i<children.length; i++) {\r
192                 boolean success = deleteDir(new File(dir, children[i]));\r
193                 if (!success) {\r
194                     return false;\r
195                 }\r
196             }\r
197         }\r
198     \r
199         // The directory is now empty so delete it\r
200         return dir.delete();\r
201     }\r
202     \r
203     \r
204 }\r