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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
16 * ============LICENSE_END==========================================================================
17 ******************************************************************************/
18 package org.onap.ccsdk.features.sdnr.wt.helpserver.data;
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;
29 * Extract subtree with resources from Opendaylight/Karaf/OSGi bundle into Karaf directory<br>
31 * Reference: Eclipsezone @see
32 * <a href="https://www.eclipsezone.com/eclipse/forums/t101557.html">https://www.eclipszone.com</a>
35 * Example for resource and directory path from karaf log. write resource: help/FAQ/0.4.0/README.md
36 * Create directories for: data/cache/com.highstreet.technologies.help/help/FAQ/0.4.0/README.md Open
37 * the file: data/cache/com.highstreet.technologies.help/help/FAQ/0.4.0/README.md Problem: Binary,
38 * JPG files => do not use buffer related functions
40 * Hint: Verify with file manager the content of the bundle.jar file to see the location of the
41 * resources. There is no need to mark them via the classpath.
44 public class ExtactBundleResource {
47 * Extract resources from Karaf/OSGi bundle into karaf directory structure.
49 * @param bundle Karaf/OSGi bundle with resources
50 * @param filePrefix prefix in karaf file system for destination e.g.
51 * "data/cache/com.highstreet.technologies."
52 * @param ressoureRoot root name of ressources, with leading "/". e.g. "/help"
53 * @throws IOException In case of problems.
55 public static void copyBundleResoucesRecursively(Bundle bundle, String filePrefix, String ressoureRoot)
57 copyResourceTreeRecurse(bundle, filePrefix, bundle.getEntryPaths(ressoureRoot));
61 * Delete a file or a directory and its children.
63 * @param file The directory to delete.
64 * @throws IOException Exception when problem occurs during deleting the directory.
66 public static void deleteRecursively(File file) throws IOException {
68 if (file.isDirectory()) {
69 for (File childFile : file.listFiles()) {
70 deleteRecursively(childFile);
75 throw new IOException("No file " + file.getName());
80 // ------------- Private functions
83 * Recurse function to steps through the resource element tree
85 * @param b Bundle index for bundle with resourcs
90 private static void copyResourceTreeRecurse(Bundle b, String filePrefix, Enumeration<String> resource)
92 while (resource.hasMoreElements()) {
93 String name = resource.nextElement();
94 Enumeration<String> list = b.getEntryPaths(name);
96 copyResourceTreeRecurse(b, filePrefix, list);
99 File targetFile = new File(filePrefix + name);
100 targetFile.getParentFile().mkdirs();
102 try (InputStream in = b.getEntry(name).openStream();
103 OutputStream outStream = new FileOutputStream(targetFile);) {
106 while ((theInt = in.read()) >= 0) {
107 outStream.write(theInt);