List of usage examples for java.lang ClassLoader getResource
public URL getResource(String name)
From source file:net.sf.nmedit.jtheme.store.DefaultStorageContext.java
public ImageResource getCachedImage(String source) { ImageResource ir = imageResourceMap.get(source); if (ir == null) { ClassLoader loader = classLoader != null ? classLoader : getClass().getClassLoader(); URL url = loader.getResource(source); ir = getCachedImage(url);/*w w w .j a va2s .c o m*/ } return ir; }
From source file:org.suren.autotest.web.framework.selenium.SeleniumEngine.java
/** * @param classLoader// ww w . j av a 2 s. c om * @return */ public boolean storePro(ClassLoader classLoader) { URL defaultResourceUrl = classLoader.getResource(ENGINE_CONFIG_FILE_NAME); try (OutputStream out = new FileOutputStream( new File(URLDecoder.decode(defaultResourceUrl.getFile(), "utf-8")))) { enginePro.store(out, ""); return true; } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return false; }
From source file:com.liferay.portal.configuration.ConfigurationImpl.java
protected String getFileName(ClassLoader classLoader, String name) { URL url = classLoader.getResource(name + ".properties"); // If the resource is located inside of a JAR, then EasyConf needs the // "jar:file:" prefix appended to the path. Use URL.toExternalForm() to // achieve that. When running under JBoss, the protocol returned is // "vfs", "vfsfile", or "vfszip". When running under OC4J, the protocol // returned is "code-source". When running under WebLogic, the protocol // returned is "zip". When running under WebSphere, the protocol // returned is "wsjar". String protocol = url.getProtocol(); if (protocol.equals("code-source") || protocol.equals("jar") || protocol.equals("vfs") || protocol.equals("vfsfile") || protocol.equals("vfszip") || protocol.equals("wsjar") || protocol.equals("zip")) { name = url.toExternalForm();/*from w w w.j a va2 s. c o m*/ } else { try { name = new URI(url.getPath()).getPath(); } catch (URISyntaxException urise) { name = url.getFile(); } } int pos = name.lastIndexOf(".properties"); if (pos != -1) { name = name.substring(0, pos); } return name; }
From source file:org.gtri.fhir.api.vistaex.resource.impl.VistaExResourceImpl.java
/** * Finds a file on the classpath/* ww w. j a va2 s .c o m*/ * @param fileName the name of the file to find * @return the file. */ private File getFileInClassPath(String fileName) { //how to find resource in Servlet //http://stackoverflow.com/questions/2161054/where-to-place-and-how-to-read-configuration-resource-files-in-servlet-based-app/2161583#2161583 ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); File propertiesFile = new File(classLoader.getResource(fileName).getFile()); return propertiesFile; }
From source file:org.picketbox.http.test.config.ProtectedResourceManagerUnitTestCase.java
@Override protected void establishUserApps() { ClassLoader tcl = Thread.currentThread().getContextClassLoader(); if (tcl == null) { tcl = getClass().getClassLoader(); }//from w w w. j a v a 2s. c o m final String WEBAPPDIR = "auth/webapp"; final String CONTEXTPATH = "/auth"; // for localhost:port/admin/index.html and whatever else is in the webapp directory final URL warUrl = tcl.getResource(WEBAPPDIR); final String warUrlString = warUrl.toExternalForm(); WebAppContext webapp = createWebApp(CONTEXTPATH, warUrlString); this.server.setHandler(webapp); Thread.currentThread().setContextClassLoader(webapp.getClassLoader()); System.setProperty(PicketBoxConstants.USERNAME, "Aladdin"); System.setProperty(PicketBoxConstants.CREDENTIAL, "Open Sesame"); FilterHolder filterHolder = new FilterHolder(DelegatingSecurityFilter.class); webapp.setInitParameter(PicketBoxConstants.AUTHENTICATION_KEY, PicketBoxConstants.HTTP_DIGEST); webapp.setInitParameter(PicketBoxConstants.HTTP_CONFIGURATION_PROVIDER, ProtectedResourcesConfigurationProvider.class.getName()); ServletHandler servletHandler = new ServletHandler(); servletHandler.addFilter(filterHolder, createFilterMapping("/*", filterHolder)); webapp.setServletHandler(servletHandler); }
From source file:gdt.data.grain.Support.java
/** * Get class resource as input stream. //w ww . j av a 2 s . com * @param handler the class * @param resource$ the resource name. * @return input stream. */ public static InputStream getClassResource(Class<?> handler, String resource$) { try { InputStream is = handler.getResourceAsStream(resource$); if (is != null) { //System.out.println("Support:getClassResource:resource stream="+is.toString()); return is; } else { // System.out.println("Support:getClassResource:cannot get embedded resource stream for handler="+handler.getName()); ClassLoader classLoader = handler.getClassLoader(); //if(classLoader!=null) // System.out.println("Support:getClassResource:class loader="+classLoader.toString()); is = classLoader.getResourceAsStream(resource$); //if(is!=null) // System.out.println("Support:getClassResource:resourse stream="+is.toString()); //else // System.out.println("Support:getClassResource:cannot get resource stream"); String handler$ = handler.getName(); //System.out.println("Support:getClassResource:class="+handler$); String handlerName$ = handler.getSimpleName(); //System.out.println("Support:getClassResource:class name="+handlerName$); String handlerPath$ = handler$.replace(".", "/"); //System.out.println("Support:getClassResource:class path="+handlerPath$); String resourcePath$ = "src/" + handlerPath$.replace(handlerName$, resource$); //System.out.println("Support:getClassResource:resource path="+resourcePath$); ClassLoader classloader = Thread.currentThread().getContextClassLoader(); URL resourceUrl = classloader.getResource(resourcePath$); if (resourceUrl != null) { //System.out.println("Support:getClassResource:resource URL="+resourceUrl.toString()); return resourceUrl.openStream(); } else { //System.out.println("Support:getClassResource:cannot get resource URL"); } } } catch (Exception e) { Logger.getLogger(gdt.data.grain.Support.class.getName()).severe(e.toString()); } return null; }
From source file:org.picketbox.test.config.ProtectedResourceManagerUnitTestCase.java
@Override protected void establishUserApps() { ClassLoader tcl = Thread.currentThread().getContextClassLoader(); if (tcl == null) { tcl = getClass().getClassLoader(); }/*from w ww . j a v a 2 s. c o m*/ final String WEBAPPDIR = "auth/webapp"; final String CONTEXTPATH = "/auth"; // for localhost:port/admin/index.html and whatever else is in the webapp directory final URL warUrl = tcl.getResource(WEBAPPDIR); final String warUrlString = warUrl.toExternalForm(); WebAppContext webapp = createWebApp(CONTEXTPATH, warUrlString); server.setHandler(webapp); /* * Context context = new WebAppContext(warUrlString, CONTEXTPATH); server.setHandler(context); */ // Thread.currentThread().setContextClassLoader(context.getClassLoader()); Thread.currentThread().setContextClassLoader(webapp.getClassLoader()); System.setProperty(PicketBoxConstants.USERNAME, "Aladdin"); System.setProperty(PicketBoxConstants.CREDENTIAL, "Open Sesame"); FilterHolder filterHolder = new FilterHolder(DelegatingSecurityFilter.class); /* * Map<String, String> contextParameters = new HashMap<String, String>(); * contextParameters.put(PicketBoxConstants.AUTHENTICATION_KEY, PicketBoxConstants.HTTP_DIGEST); * contextParameters.put(PicketBoxConstants.USER_ATTRIBUTE_NAME, "authenticatedUser"); * contextParameters.put(PicketBoxConstants.HTTP_CONFIGURATION_PROVIDER, * ProtectedResourcesConfigurationProvider.class.getName()); context.setInitParams(contextParameters); * * context.addFilter(filterHolder, "/*", 1); */ webapp.setInitParameter(PicketBoxConstants.AUTHENTICATION_KEY, PicketBoxConstants.HTTP_DIGEST); webapp.setInitParameter(PicketBoxConstants.HTTP_CONFIGURATION_PROVIDER, ProtectedResourcesConfigurationProvider.class.getName()); /* * context.setInitParams(contextParameters); * * context.addFilter(filterHolder, "/*", 1); */ ServletHandler servletHandler = new ServletHandler(); servletHandler.addFilter(filterHolder, createFilterMapping("/*", filterHolder)); webapp.setServletHandler(servletHandler); }
From source file:org.ajax4jsf.resource.ResourceBuilderImpl.java
/** * Create resurce to send from classpath relative to base class. * //from w w w. j a v a 2 s .co m * @param base * @param path * @return * @throws FacesException */ protected InternetResource createJarResource(Object base, String path) throws ResourceNotFoundException, FacesException { String key = buildKey(base, path); ClassLoader loader = Thread.currentThread().getContextClassLoader(); if (null != loader.getResource(key)) { JarResource res = new JarResource(key); setRenderer(res, path); res.setLastModified(new Date(getStartTime())); addResource(key, res); return res; } else { throw new ResourceNotFoundException(Messages.getMessage(Messages.NO_RESOURCE_EXISTS_ERROR, key)); } }
From source file:org.apache.pig.backend.hadoop.executionengine.MRExecutionEngine.java
@SuppressWarnings({ "deprecation", "resource" }) private void init(Properties properties) throws ExecException { // First set the ssh socket factory setSSHFactory();//from w ww.jav a 2 s . c o m String cluster = null; String nameNode = null; // We need to build a configuration object first in the manner described // below // and then get back a properties object to inspect the // JOB_TRACKER_LOCATION // and FILE_SYSTEM_LOCATION. The reason to do this is if we looked only // at // the existing properties object, we may not get the right settings. So // we want // to read the configurations in the order specified below and only then // look // for JOB_TRACKER_LOCATION and FILE_SYSTEM_LOCATION. // Hadoop by default specifies two resources, loaded in-order from the // classpath: // 1. hadoop-default.xml : Read-only defaults for hadoop. // 2. hadoop-site.xml: Site-specific configuration for a given hadoop // installation. // Now add the settings from "properties" object to override any // existing properties // All of the above is accomplished in the method call below JobConf jc = null; if (!this.pigContext.getExecType().isLocal()) { // Check existence of user provided configs String isHadoopConfigsOverriden = properties.getProperty("pig.use.overriden.hadoop.configs"); if (isHadoopConfigsOverriden != null && isHadoopConfigsOverriden.equals("true")) { jc = new JobConf(ConfigurationUtil.toConfiguration(properties)); } else { // Check existence of hadoop-site.xml or core-site.xml in // classpath // if user provided confs are not being used Configuration testConf = new Configuration(); ClassLoader cl = testConf.getClassLoader(); URL hadoop_site = cl.getResource(HADOOP_SITE); URL core_site = cl.getResource(CORE_SITE); if (hadoop_site == null && core_site == null) { throw new ExecException( "Cannot find hadoop configurations in classpath (neither hadoop-site.xml nor core-site.xml was found in the classpath)." + " If you plan to use local mode, please put -x local option in command line", 4010); } jc = new JobConf(); } jc.addResource("pig-cluster-hadoop-site.xml"); jc.addResource(YARN_SITE); // Trick to invoke static initializer of DistributedFileSystem to // add hdfs-default.xml // into configuration new DistributedFileSystem(); // the method below alters the properties object by overriding the // hadoop properties with the values from properties and recomputing // the properties recomputeProperties(jc, properties); } else { // If we are running in local mode we dont read the hadoop conf file if (properties.getProperty("mapreduce.framework.name") == null) { properties.setProperty("mapreduce.framework.name", "local"); } properties.setProperty(JOB_TRACKER_LOCATION, LOCAL); properties.setProperty(FILE_SYSTEM_LOCATION, "file:///"); properties.setProperty(ALTERNATIVE_FILE_SYSTEM_LOCATION, "file:///"); jc = new JobConf(false); jc.addResource("core-default.xml"); jc.addResource("mapred-default.xml"); jc.addResource("yarn-default.xml"); recomputeProperties(jc, properties); } cluster = jc.get(JOB_TRACKER_LOCATION); nameNode = jc.get(FILE_SYSTEM_LOCATION); if (nameNode == null) nameNode = (String) pigContext.getProperties().get(ALTERNATIVE_FILE_SYSTEM_LOCATION); if (cluster != null && cluster.length() > 0) { if (!cluster.contains(":") && !cluster.equalsIgnoreCase(LOCAL)) { cluster = cluster + ":50020"; } properties.setProperty(JOB_TRACKER_LOCATION, cluster); } if (nameNode != null && nameNode.length() > 0) { if (!nameNode.contains(":") && !nameNode.equalsIgnoreCase(LOCAL)) { nameNode = nameNode + ":8020"; } properties.setProperty(FILE_SYSTEM_LOCATION, nameNode); } log.info("Connecting to hadoop file system at: " + (nameNode == null ? LOCAL : nameNode)); // constructor sets DEFAULT_REPLICATION_FACTOR_KEY ds = new HDataStorage(properties); if (cluster != null && !cluster.equalsIgnoreCase(LOCAL)) { log.info("Connecting to map-reduce job tracker at: " + jc.get(JOB_TRACKER_LOCATION)); } // Set job-specific configuration knobs jobConf = jc; }