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