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