List of usage examples for java.lang ClassLoader getSystemResource
public static URL getSystemResource(String name)
From source file:de.unisb.cs.st.javalanche.rhino.RhinoTestRunnable.java
public static String getCommand(String[] args) { StringBuilder sb = new StringBuilder(); sb.append("java -cp "); String main = "org/mozilla/javascript/tools/shell/Main.class"; URL resource = ClassLoader.getSystemResource(main); if (resource != null) { String location = resource.toString().substring(0, resource.toString().length() - main.length()); logger.info(location);// w w w .j a va2s . c om File f; try { f = new File(new URI(location)); sb.append(f.toString()); } catch (URISyntaxException e) { e.printStackTrace(); } } // sb // .append(":/scratch/schuler/subjects/ibugs_rhino-0.1/jars/xmlbeans-2.2.0/lib/jsr173_1.0_api.jar:/scratch/schuler/subjects/ibugs_rhino-0.1/jars/xmlbeans-2.2.0/lib/xbean.jar"); sb.append(":" + System.getProperty("java.class.path")); sb.append(" org.mozilla.javascript.tools.shell.Main "); for (String arg : args) { sb.append(arg + " "); } String message = sb.toString(); return message; }
From source file:org.jahia.utils.i18n.JahiaTemplatesRBLoader.java
public URL getResource(String name) { if (loader != null) { return loader.getResource(name); }//from w ww . j a va 2 s.c o m return ClassLoader.getSystemResource(name); }
From source file:org.apache.james.mailbox.elasticsearch.json.MailboxMessageToElasticSearchJsonTest.java
@Test public void htmlEmailShouldBeWellConvertedToJson() throws IOException { MessageToElasticSearchJson messageToElasticSearchJson = new MessageToElasticSearchJson( new DefaultTextExtractor(), ZoneId.of("Europe/Paris")); MailboxMessage htmlMail = new SimpleMailboxMessage(date, SIZE, BODY_START_OCTET, new SharedByteArrayInputStream( IOUtils.toByteArray(ClassLoader.getSystemResourceAsStream("eml/htmlMail.eml"))), new FlagsBuilder().add(Flags.Flag.DELETED, Flags.Flag.SEEN).add("social", "pocket-money").build(), propertyBuilder, MAILBOX_ID); htmlMail.setModSeq(MOD_SEQ);// ww w.jav a 2 s . co m htmlMail.setUid(UID); assertThatJson(messageToElasticSearchJson.convertToJson(htmlMail, ImmutableList.of(new MockMailboxSession("username").getUser()))).when(IGNORING_ARRAY_ORDER) .isEqualTo(IOUtils.toString(ClassLoader.getSystemResource("eml/htmlMail.json"))); }
From source file:org.java.plugin.ObjectFactory.java
private static ExtendedProperties loadProperties(final ClassLoader cl) { Log log = LogFactory.getLog(ObjectFactory.class); File file = new File(System.getProperty("java.home") //$NON-NLS-1$ + File.separator + "lib" + File.separator //$NON-NLS-1$ + "jpf.properties"); //$NON-NLS-1$ URL url = null;/*from ww w.j a v a 2 s .c o m*/ if (file.canRead()) { try { url = IoUtil.file2url(file); } catch (MalformedURLException mue) { log.error("failed converting file " + file //$NON-NLS-1$ + " to URL", mue); //$NON-NLS-1$ } } if (url == null) { if (cl != null) { url = cl.getResource("jpf.properties"); //$NON-NLS-1$ if (url == null) { url = ClassLoader.getSystemResource("jpf.properties"); //$NON-NLS-1$ } } else { url = ClassLoader.getSystemResource("jpf.properties"); //$NON-NLS-1$ } if (url == null) { log.debug("no jpf.properties file found in ${java.home}/lib (" //$NON-NLS-1$ + file + ") nor in CLASSPATH, using standard properties"); //$NON-NLS-1$ url = StandardObjectFactory.class.getResource("jpf.properties"); //$NON-NLS-1$ } } try { InputStream strm = IoUtil.getResourceInputStream(url); try { ExtendedProperties props = new ExtendedProperties(); props.load(strm); log.debug("loaded jpf.properties from " + url); //$NON-NLS-1$ return props; } finally { try { strm.close(); } catch (IOException ioe) { // ignore } } } catch (Exception e) { log.error("failed loading jpf.properties from CLASSPATH", //$NON-NLS-1$ e); } return null; }
From source file:org.opennms.sms.monitor.MobileMsgSequenceMonitorTest.java
private String getXmlBuffer(String fileName) throws IOException { File xmlFile = new File(ClassLoader.getSystemResource(fileName).getFile()); assertTrue("xml file is readable", xmlFile.canRead()); return FileUtils.readFileToString(xmlFile); }
From source file:com.dianping.resource.io.util.ResourceUtils.java
/** * Resolve the given resource location to a {@code java.io.File}, * i.e. to a file in the file system.//from w w w .j av a 2 s . c o m * <p>Does not check whether the file actually exists; simply returns * the File that the given location would correspond to. * @param resourceLocation the resource location to resolve: either a * "classpath:" pseudo URL, a "file:" URL, or a plain file path * @return a corresponding File object * @throws java.io.FileNotFoundException if the resource cannot be resolved to * a file in the file system */ public static File getFile(String resourceLocation) throws FileNotFoundException { Assert.notNull(resourceLocation, "Resource location must not be null"); if (resourceLocation.startsWith(CLASSPATH_URL_PREFIX)) { String path = resourceLocation.substring(CLASSPATH_URL_PREFIX.length()); String description = "class path resource [" + path + "]"; ClassLoader cl = ClassUtils.getDefaultClassLoader(); URL url = (cl != null ? cl.getResource(path) : ClassLoader.getSystemResource(path)); if (url == null) { throw new FileNotFoundException(description + " cannot be resolved to absolute file path " + "because it does not reside in the file system"); } return getFile(url, description); } try { // try URL return getFile(new URL(resourceLocation)); } catch (MalformedURLException ex) { // no URL -> treat as file path return new File(resourceLocation); } }
From source file:org.agiso.core.lang.util.ClassUtils.java
public static String locate(String name, ClassLoader cl) /*throws ClassNotFoundException*/ { final URL location; final String classLocation = name.replace('.', '/') + ".class"; if (cl == null) { location = ClassLoader.getSystemResource(classLocation); } else {//from w w w . jav a 2 s . c o m location = cl.getResource(classLocation); } if (location != null) { Pattern p = Pattern.compile("^.*:(.*)!.*$"); Matcher m = p.matcher(location.toString()); if (m.find()) { return m.group(1); } else { if (logger.isLoggable(Level.WARNING)) { logger.warning( "Cannot parse location of '" + location + "'. " + "Probably not loaded from a Jar"); } // throw new ClassNotFoundException("Cannot parse location of '" // + location + "'. Probably not loaded from a Jar"); return null; } } else { if (logger.isLoggable(Level.WARNING)) { logger.warning("Cannot find class '" + name + " using the " + cl); } // throw new ClassNotFoundException("Cannot find class '" // + name + " using the " + cl); return null; } }
From source file:org.jboss.pressgang.ccms.contentspec.client.utils.ClientUtilitiesTest.java
@Before public void setUp() throws IOException { bindStdOut();/* w w w .j a v a 2s.c om*/ doCallRealMethod().when(command).printErrorAndShutdown(anyInt(), anyString(), anyBoolean()); // Return the test directory as the root directory rootTestDirectory = FileUtils.toFile(ClassLoader.getSystemResource("")); when(cspConfig.getRootOutputDirectory()).thenReturn(rootTestDirectory.getAbsolutePath() + File.separator); // Make the book title directory bookDir = new File(rootTestDirectory, BOOK_TITLE); bookDir.mkdir(); // Make a empty file in that directory emptyFile = new File(bookDir, EMPTY_FILE_NAME); emptyFile.createNewFile(); // Set up the server settings mock TestUtil.setUpServerSettings(serverSettings, serverEntities); }
From source file:org.apache.rya.jena.jenasesame.JenaReasoningWithRulesTest.java
private static InfModel createInfModel(final RepositoryConnection queryConnection, final String rulesRelativeFileName) throws FileNotFoundException { final Dataset dataset = JenaSesame.createDataset(queryConnection); final Model model = dataset.getDefaultModel(); log.info(model.getNsPrefixMap());/*from ww w .ja v a 2 s . co m*/ final URL rulesUrl = ClassLoader.getSystemResource(rulesRelativeFileName); final File rulesFile = ResourceUtils.getFile(rulesUrl); final String rulesFileName = rulesFile.getAbsolutePath(); final Reasoner reasoner = new GenericRuleReasoner(Rule.rulesFromURL(rulesFileName)); final InfModel infModel = ModelFactory.createInfModel(reasoner, model); return infModel; }
From source file:org.jboss.test.cluster.httpsessionreplication.HttpSessionReplicationUnitTestCase.java
/** * Reads in the properties file// ww w . j a v a2s . co m */ public void getPropertiesFile() { prop = new Properties(); try { java.net.URL url = ClassLoader.getSystemResource("cluster/cluster-test.properties"); prop.load(url.openStream()); } catch (Exception e) { fail("Need a properties file under src/resources/cluster:" + e.getMessage()); } }