List of usage examples for java.lang Class getResourceAsStream
@CallerSensitive
public InputStream getResourceAsStream(String name)
From source file:org.jasig.ssp.TestUtils.java
/** * Load and parse a JSON file based on the testClass and file. * // ww w .ja v a2 s . c o m * <p> * JSON file must not contain any line breaks or new line characters. * * <p> * For example, if testClass is * <code>org.jasig.mygps.business.StudentIntakeFormManagerTest</code> and * file is <code>studentintakeform_empty.json</code>, this method will * attempt to load the file * <code>org/jasig/mygps/business/studentintakeform_empty.json</code>. * * @param testClass * Testing class, used for resource loading based on the * concatenated namespace and file name, via {link * Class#getResourceAsStream(String)}. Required. * @param file * the JSON file to load, relative to the namespace of the * calling testClass. Required. * @param TOClass * Required; serializes the JSON to this transfer object class. * If the JSON can not be serialized to this class, an exception * will be thrown. * @return Transfer object equivalent of the JSON file contents * @exception JsonParseException * if test file could not be parsed * @exception JsonMappingException * if test file could not be mapped * @exception IOException * if test file could not be loaded, or missing/empty file */ public static <T, TO, TS> TO loadJson(@NotNull final Class<TS> testClass, @NotNull final String file, @NotNull final Class<TO> TOClass) throws JsonParseException, JsonMappingException, IOException { if (!StringUtils.isNotBlank(file)) { throw new IOException("Missing file parameter."); } assertNotNull("Test suite class can not be null.", testClass); assertNotNull("Transfer object class can not be null.", TOClass); // Load file try { final InputStreamReader is = new InputStreamReader(testClass.getResourceAsStream(file)); final BufferedReader in = new BufferedReader(is); final String json = in.readLine(); LOGGER.debug("Test class {} loaded the following JSON: {}", testClass.getName() + " with file " + file, json); final TO obj = objectMapper.readValue(json, TOClass); if (in != null) { in.close(); } if (is != null) { is.close(); } assertNotNull("File data could not be parsed from " + file, obj); return obj; } catch (final NullPointerException exc) { // NOPMD NPE is in library throw new IOException("Could not load the specified file " + testClass.getPackage().getName().replace('.', '/') + "/" + file, exc); } catch (final UnrecognizedPropertyException exc) { throw new JsonMappingException( "JSON fields did not match transfer object class properties. See exception cause for erroneous field names.", exc); } }
From source file:de.alpharogroup.lang.ClassUtils.java
/** * This method call the getResourceAsStream from the ClassLoader. You can use this method to * read files from jar-files./*from ww w. jav a2s. c o m*/ * * @param clazz * the clazz * @param uri * The uri as String. * @return The InputStream from the uri. */ public static InputStream getResourceAsStream(final Class<?> clazz, final String uri) { InputStream is = clazz.getResourceAsStream(uri); if (null == is) { is = ClassUtils.getClassLoader().getResourceAsStream(uri); } return is; }
From source file:org.fuin.owndeb.commons.DebUtils.java
/** * Reads a resource and writes the replaced string into a file with the same * name.// w w w .j av a 2s . co m * * @param clasz * Class to use for reading teh resource. * @param resource * Full path to the resource. * @param outDir * Output directory to create a file inside. * @param vars * Variables to replace. */ public static void writeReplacedResource(final Class<?> clasz, final String resource, final File outDir, final Map<String, String> vars) { LOG.info("Write replaced resource '{}' to directory: {}", resource, outDir); final File outFile = new File(outDir, FilenameUtils.getName(resource)); try { final InputStream inStream = clasz.getResourceAsStream(resource); if (inStream == null) { throw new IllegalArgumentException("Unable to locate resource: " + resource); } try { final String inStr = IOUtils.toString(inStream); final String outStr = Utils4J.replaceVars(inStr, vars); LOG.debug("Write resource {}\n{}", outFile, outStr); FileUtils.writeStringToFile(outFile, outStr); } finally { inStream.close(); } } catch (IOException ex) { throw new RuntimeException( "Error replacing and writing resource file '" + resource + "' to: " + outFile, ex); } }
From source file:com.redhat.rhn.testing.TestUtils.java
/** * method to find a file relative to the calling class. primarily * useful when writing tests that need access to external data. * this lets you put the data relative to the test class file. * * @param path the path, relative to caller's location * @return URL a URL referencing the file * @throws ClassNotFoundException if the calling class can not be found * (i.e., should not happen)/*from w ww. j a v a2 s . c o m*/ * @throws IOException if the specified file in an archive (eg. jar) and * it cannot be copied to a temporary location */ public static URL findTestData(String path) throws ClassNotFoundException, IOException { Throwable t = new Throwable(); StackTraceElement[] ste = t.getStackTrace(); String className = ste[1].getClassName(); Class clazz = Class.forName(className); URL ret = clazz.getResource(path); if (ret.toString().contains("!")) { // file is from an archive String tempPath = "/tmp/" + filePrefix + ret.hashCode(); InputStream input = clazz.getResourceAsStream(path); OutputStream output = new FileOutputStream(tempPath); IOUtils.copy(input, output); return new File(tempPath).toURI().toURL(); } return ret; }
From source file:org.keycloak.testsuite.helper.adapter.SamlAdapterTestStrategy.java
public static RealmModel baseAdapterTestInitialization(KeycloakSession session, RealmManager manager, RealmModel adminRealm, Class<?> clazz) { RealmRepresentation representation = KeycloakServer .loadJson(clazz.getResourceAsStream("/keycloak-saml/testsaml.json"), RealmRepresentation.class); RealmModel demoRealm = manager.importRealm(representation); return demoRealm; }
From source file:de.alpharogroup.lang.ClassExtensions.java
/** * This method call the getResourceAsStream from the ClassLoader. You can use this method to * read files from jar-files./*from w w w . jav a2s. co m*/ * * @param clazz * the clazz * @param uri * The uri as String. * @return The InputStream from the uri. */ public static InputStream getResourceAsStream(final Class<?> clazz, final String uri) { InputStream is = clazz.getResourceAsStream(uri); if (null == is) { is = ClassExtensions.getClassLoader().getResourceAsStream(uri); } return is; }
From source file:GraphicsExample.java
static Image loadImage(Display display, Class clazz, String string) { InputStream stream = clazz.getResourceAsStream(string); if (stream == null) return null; Image image = null;/*from w ww. j ava2s . co m*/ try { image = new Image(display, stream); } catch (SWTException ex) { } finally { try { stream.close(); } catch (IOException ex) { } } return image; }
From source file:org.eclipse.sw360.datahandler.common.CommonUtils.java
public static Properties loadProperties(Class<?> clazz, String propertiesFilePath, boolean useSystemConfig) { Properties props = new Properties(); try (InputStream resourceAsStream = clazz.getResourceAsStream(propertiesFilePath)) { if (resourceAsStream == null) throw new IOException("cannot open " + propertiesFilePath); props.load(resourceAsStream);//from w w w . j ava2 s . c om } catch (IOException e) { getLogger(clazz).error("Error opening resources " + propertiesFilePath + ".", e); } if (useSystemConfig) { File systemPropertiesFile = new File(SYSTEM_CONFIGURATION_PATH, propertiesFilePath); if (systemPropertiesFile.exists()) { try (InputStream resourceAsStream = new FileInputStream(systemPropertiesFile.getPath())) { if (resourceAsStream == null) throw new IOException("cannot open " + systemPropertiesFile.getPath()); props.load(resourceAsStream); } catch (IOException e) { getLogger(clazz).error("Error opening resources " + systemPropertiesFile.getPath() + ".", e); } } } return props; }
From source file:com.tesora.dve.standalone.PETest.java
public static void populateSites(Class<?> testClass, Properties props, String prefix) throws PEException, SQLException { List<PersistentSite> allSites = getGlobalDAO().findAllPersistentSites(); for (StorageSite site : allSites) { String sqlRes = prefix + site.getName() + "-load.sql"; InputStream is = testClass.getResourceAsStream(sqlRes); if (is != null) { logger.info("Reading SQL statements from " + sqlRes); DBHelper dbh = new DBHelper(props).connect(); try { dbh.executeFromStream(is); dbh.disconnect();/* w w w . j a v a 2s . co m*/ is.close(); } catch (IOException e) { // ignore } finally { dbh.disconnect(); } } } }
From source file:org.wurstworks.tools.pinto.AbstractPintoBean.java
public static Properties getPropertiesForClass(final Class<?> parent) { final String bundle = "/" + parent.getName().replace(".", "/") + ".properties"; Properties properties = new Properties(); try {/*from www. j a va2 s . c o m*/ properties.load(parent.getResourceAsStream(bundle)); } catch (IOException e) { properties = null; } return properties; }