1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18package tests.support.resource;
19
20import java.io.File;
21import java.io.FileNotFoundException;
22import java.io.FileOutputStream;
23import java.io.IOException;
24import java.io.InputStream;
25import java.net.MalformedURLException;
26import java.net.URISyntaxException;
27import java.net.URL;
28
29import tests.support.Support_Configuration;
30
31public class Support_Resources {
32
33    public static final String RESOURCE_PACKAGE = "/tests/resources/";
34
35    public static final String RESOURCE_PACKAGE_NAME = "tests.resources";
36
37    public static InputStream getStream(String name) {
38        // System.err.println("getResourceAsStream(" + RESOURCE_PACKAGE + name + ")");
39        return Support_Resources.class.getResourceAsStream(RESOURCE_PACKAGE + name);
40    }
41
42    public static String getURL(String name) {
43        String folder = null;
44        String fileName = name;
45        File resources = createTempFolder();
46        int index = name.lastIndexOf("/");
47        if (index != -1) {
48            folder = name.substring(0, index);
49            name = name.substring(index + 1);
50        }
51        copyFile(resources, folder, name);
52        URL url = null;
53        String resPath = resources.toString();
54        if (resPath.charAt(0) == '/' || resPath.charAt(0) == '\\') {
55            resPath = resPath.substring(1);
56        }
57        try {
58            url = new URL("file:/" + resPath + "/" + fileName);
59        } catch (MalformedURLException e) {
60            throw new RuntimeException(e);
61        }
62        return url.toString();
63    }
64
65    public static File createTempFolder() {
66        File folder = null;
67        try {
68            folder = File.createTempFile("hyts_resources", "", null);
69            folder.delete();
70            folder.mkdirs();
71        } catch (IOException e) {
72            throw new RuntimeException(e);
73        }
74        folder.deleteOnExit();
75        return folder;
76    }
77
78    public static void copyFile(File root, String folder, String file) {
79        File f;
80        if (folder != null) {
81            f = new File(root.toString() + "/" + folder);
82            if (!f.exists()) {
83                f.mkdirs();
84                f.deleteOnExit();
85            }
86        } else {
87            f = root;
88        }
89
90        String src = folder == null ? file : folder + "/" + file;
91        InputStream in = Support_Resources.getStream(src);
92        try {
93            File dst = new File(f.toString() + "/" + file);
94            copyLocalFileTo(dst, in);
95        } catch (Exception e) {
96            throw new RuntimeException("copyFile failed: root=" + root + " folder=" + folder + " file=" + file + " (src=" + src + ")", e);
97        }
98    }
99
100    public static File createTempFile(String suffix) throws IOException {
101        return File.createTempFile("hyts_", suffix, null);
102    }
103
104    public static void copyLocalFileTo(File dest, InputStream in) throws IOException {
105        if (!dest.exists()) {
106            FileOutputStream out = new FileOutputStream(dest);
107            int result;
108            byte[] buf = new byte[4096];
109            while ((result = in.read(buf)) != -1) {
110                out.write(buf, 0, result);
111            }
112            in.close();
113            out.close();
114            dest.deleteOnExit();
115        }
116    }
117
118    public static File getExternalLocalFile(String url) throws IOException, MalformedURLException {
119        File resources = createTempFolder();
120        InputStream in = new URL(url).openStream();
121        File temp = new File(resources.toString() + "/local.tmp");
122        copyLocalFileTo(temp, in);
123        return temp;
124    }
125
126    public static String getResourceURL(String resource) {
127        return "http://" + Support_Configuration.TestResources + resource;
128    }
129
130    /**
131     * Util method to load resource files
132     *
133     * @param name - name of resource file
134     * @return - resource input stream
135     */
136    public static InputStream getResourceStream(String name) {
137        InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream(name);
138        if (is == null) {
139            throw new RuntimeException("Failed to load resource: " + name);
140        }
141        return is;
142    }
143
144    /**
145     * Util method to get absolute path to resource file
146     *
147     * @param name - name of resource file
148     * @return - path to resource
149     */
150    public static String getAbsoluteResourcePath(String name) {
151        URL url = ClassLoader.getSystemClassLoader().getResource(name);
152        if (url == null) {
153            throw new RuntimeException("Failed to load resource: " + name);
154        }
155
156        try {
157            return new File(url.toURI()).getAbsolutePath();
158        } catch (URISyntaxException e) {
159            throw new RuntimeException("Failed to load resource: " + name);
160        }
161    }
162}
163