ca3a4004348daa000ae967b819672b01d85d00df
[ccsdk/features.git] /
1 /*
2  * ============LICENSE_START========================================================================
3  * ONAP : ccsdk feature sdnr wt
4  * =================================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6  * =================================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software distributed under the License
13  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14  * or implied. See the License for the specific language governing permissions and limitations under
15  * the License.
16  * ============LICENSE_END==========================================================================
17  */
18 package org.onap.ccsdk.features.sdnr.wt.helpserver.data;
19
20 import java.io.File;
21 import java.io.FileOutputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.OutputStream;
25 import java.util.Enumeration;
26 import org.osgi.framework.Bundle;
27
28 /**
29  * Extract subtree with resources from Opendaylight/Karaf/OSGi bundle into Karaf directory<br>
30  *
31  * Reference: Eclipsezone @see
32  * <a href="https://www.eclipsezone.com/eclipse/forums/t101557.html">https://www.eclipszone.com</a> <br>
33  * <br>
34  * Example for resource and directory path from karaf log. write resource: help/FAQ/0.4.0/README.md Create directories
35  * for: data/cache/com.highstreet.technologies.help/help/FAQ/0.4.0/README.md Open the file:
36  * data/cache/com.highstreet.technologies.help/help/FAQ/0.4.0/README.md Problem: Binary, JPG files => do not use buffer
37  * related functions
38  *
39  * Hint: Verify with file manager the content of the bundle.jar file to see the location of the resources. There is no
40  * need to mark them via the classpath.
41  */
42
43 public class ExtactBundleResource {
44
45     /**
46      * Extract resources from Karaf/OSGi bundle into karaf directory structure.
47      *
48      * @param bundle Karaf/OSGi bundle with resources
49      * @param filePrefix prefix in karaf file system for destination e.g. "data/cache/com.highstreet.technologies."
50      * @param ressoureRoot root name of ressources, with leading "/". e.g. "/help"
51      * @throws IOException In case of problems.
52      */
53     public static void copyBundleResoucesRecursively(Bundle bundle, String filePrefix, String ressoureRoot)
54             throws IOException {
55         copyResourceTreeRecurse(bundle, filePrefix, bundle.getEntryPaths(ressoureRoot));
56     }
57
58     /**
59      * Delete a file or a directory and its children.
60      *
61      * @param file The directory to delete.
62      * @throws IOException Exception when problem occurs during deleting the directory.
63      */
64     public static void deleteRecursively(File file) throws IOException {
65
66         if (file.isDirectory()) {
67             for (File childFile : file.listFiles()) {
68                 deleteRecursively(childFile);
69             }
70         }
71         if (file.exists()) {
72             if (!file.delete()) {
73                 throw new IOException("No file " + file.getName());
74             }
75         }
76     }
77
78     // ------------- Private functions
79
80     /**
81      * Recurse function to steps through the resource element tree
82      *
83      * @param b Bundle index for bundle with resourcs
84      * @param filePrefix
85      * @param resource
86      * @throws IOException
87      */
88     private static void copyResourceTreeRecurse(Bundle b, String filePrefix, Enumeration<String> resource)
89             throws IOException {
90         while (resource.hasMoreElements()) {
91             String name = resource.nextElement();
92             Enumeration<String> list = b.getEntryPaths(name);
93             if (list != null) {
94                 copyResourceTreeRecurse(b, filePrefix, list);
95             } else {
96                 // Read
97                 File targetFile = new File(filePrefix + name);
98                 targetFile.getParentFile().mkdirs();
99
100                 try (InputStream in = b.getEntry(name).openStream();
101                         OutputStream outStream = new FileOutputStream(targetFile);) {
102
103                     int theInt;
104                     while ((theInt = in.read()) >= 0) {
105                         outStream.write(theInt);
106                     }
107                     in.close();
108                     outStream.flush();
109                     outStream.close();
110                 }
111             }
112         }
113     }
114 }