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 18 package tests.support.resource; 19 20 import java.io.File; 21 import java.io.FileNotFoundException; 22 import java.io.FileOutputStream; 23 import java.io.IOException; 24 import java.io.InputStream; 25 import java.io.OutputStream; 26 import java.net.MalformedURLException; 27 import java.net.URL; 28 import tests.support.Support_Configuration; 29 30 public class Support_Resources { 31 32 public static final String RESOURCE_PACKAGE = "/tests/resources/"; 33 34 public static final String RESOURCE_PACKAGE_NAME = "tests.resources"; 35 36 public static InputStream getStream(String name) { 37 String path = RESOURCE_PACKAGE + name; 38 InputStream result = Support_Resources.class.getResourceAsStream(path); 39 if (result == null) { 40 throw new IllegalArgumentException("No such resource: " + path); 41 } 42 return result; 43 } 44 45 public static String getURL(String name) { 46 String folder = null; 47 String fileName = name; 48 File resources = createTempFolder(); 49 int index = name.lastIndexOf("/"); 50 if (index != -1) { 51 folder = name.substring(0, index); 52 name = name.substring(index + 1); 53 } 54 copyFile(resources, folder, name); 55 URL url = null; 56 String resPath = resources.toString(); 57 if (resPath.charAt(0) == '/' || resPath.charAt(0) == '\\') { 58 resPath = resPath.substring(1); 59 } 60 try { 61 url = new URL("file:/" + resPath + "/" + fileName); 62 } catch (MalformedURLException e) { 63 // TODO Auto-generated catch block 64 e.printStackTrace(); 65 } 66 return url.toString(); 67 } 68 69 public static File createTempFolder() { 70 71 File folder = null; 72 try { 73 folder = File.createTempFile("hyts_resources", "", null); 74 folder.delete(); 75 folder.mkdirs(); 76 } catch (IOException e) { 77 // TODO Auto-generated catch block 78 e.printStackTrace(); 79 } 80 folder.deleteOnExit(); 81 return folder; 82 } 83 84 public static File copyFile(File root, String folder, String file) { 85 File f; 86 if (folder != null) { 87 f = new File(root.toString() + "/" + folder); 88 if (!f.exists()) { 89 f.mkdirs(); 90 f.deleteOnExit(); 91 } 92 } else { 93 f = root; 94 } 95 96 File dest = new File(f.toString() + "/" + file); 97 98 InputStream in = Support_Resources.getStream(folder == null ? file 99 : folder + "/" + file); 100 try { 101 copyLocalFileto(dest, in); 102 } catch (FileNotFoundException e) { 103 // TODO Auto-generated catch block 104 e.printStackTrace(); 105 } catch (IOException e) { 106 // TODO Auto-generated catch block 107 e.printStackTrace(); 108 } 109 110 return dest; 111 } 112 113 public static File createTempFile(String suffix) throws IOException { 114 return File.createTempFile("hyts_", suffix, null); 115 } 116 117 public static void copyLocalFileto(File dest, InputStream in) throws IOException { 118 if (!dest.exists()) { 119 FileOutputStream out = new FileOutputStream(dest); 120 copy(in, out); 121 out.close(); 122 dest.deleteOnExit(); 123 } 124 in.close(); 125 } 126 127 private static int copy(InputStream in, OutputStream out) throws IOException { 128 int total = 0; 129 byte[] buffer = new byte[8192]; 130 int c; 131 while ((c = in.read(buffer)) != -1) { 132 total += c; 133 out.write(buffer, 0, c); 134 } 135 return total; 136 } 137 138 public static File getExternalLocalFile(String url) throws IOException { 139 File resources = createTempFolder(); 140 InputStream in = new URL(url).openStream(); 141 File temp = new File(resources.toString() + "/local.tmp"); 142 copyLocalFileto(temp, in); 143 return temp; 144 } 145 146 public static String getResourceURL(String resource) { 147 return "http://" + Support_Configuration.TestResources + resource; 148 } 149 150 /** 151 * Util method to load resource files 152 * 153 * @param name - name of resource file 154 * @return - resource input stream 155 */ 156 public static InputStream getResourceStream(String name) { 157 InputStream is = Support_Resources.class.getResourceAsStream(name); 158 159 if (is == null) { 160 name = RESOURCE_PACKAGE + name; 161 is = Support_Resources.class.getResourceAsStream(name); 162 if (is == null) { 163 throw new RuntimeException("Failed to load resource: " + name); 164 } 165 } 166 167 return is; 168 } 169 170 public static File resourceToTempFile(String path) throws IOException { 171 File f = File.createTempFile("out", ".xml"); 172 f.deleteOnExit(); 173 FileOutputStream out = new FileOutputStream(f); 174 175 InputStream xml = Support_Resources.class.getResourceAsStream(path); 176 int b; 177 while ((b = xml.read()) != -1) { 178 out.write(b); 179 } 180 out.flush(); 181 out.close(); 182 xml.close(); 183 return f; 184 } 185 } 186