Deprecated as of 4.3.13, in favor of selective use of ClassPathResource.ClassPathResource(String, ClassLoader) vs ClassPathResource.ClassPathResource(String, Class)
@Override public void setAsText(String text) throws IllegalArgumentException { if (StringUtils.hasText(text)) { String uri = text.trim(); if (this.classLoader != null && uri.startsWith(ResourceUtils.CLASSPATH_URL_PREFIX)) { ClassPathResource resource = new ClassPathResource( uri.substring(ResourceUtils.CLASSPATH_URL_PREFIX.length()), this.classLoader); try { setValue(resource.getURI()); } catch (IOException ex) { throw new IllegalArgumentException("Could not retrieve URI for " + resource + ": " + ex.getMessage()); } } else { try { setValue(createURI(uri)); } catch (URISyntaxException ex) { throw new IllegalArgumentException("Invalid URI syntax: " + ex); } } } else { setValue(null); } }
@Override public void setAsText(String text) throws IllegalArgumentException { if (StringUtils.hasText(text)) { String uri = text.trim(); if (this.classLoader != null && uri.startsWith(ResourceUtils.CLASSPATH_URL_PREFIX)) { ClassPathResource resource = new ClassPathResource( uri.substring(ResourceUtils.CLASSPATH_URL_PREFIX.length()), this.classLoader); try { setValue(resource.getURI()); } catch (IOException ex) { throw new IllegalArgumentException("Could not retrieve URI for " + resource + ": " + ex.getMessage()); } } else { try { setValue(createURI(uri)); } catch (URISyntaxException ex) { throw new IllegalArgumentException("Invalid URI syntax: " + ex); } } } else { setValue(null); } }
@Test public void readWithExternalReference() throws IOException { String body = "<!DOCTYPE MyBean SYSTEM \"http://192.168.28.42/1.jsp\" [" + " <!ELEMENT root ANY >\n" + " <!ENTITY ext SYSTEM \"" + new ClassPathResource("external.txt", getClass()).getURI() + "\" >]><MyBean><string>&ext;</string></MyBean>"; MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8")); inputMessage.getHeaders().setContentType(new MediaType("application", "xml")); this.thrown.expect(HttpMessageNotReadableException.class); this.converter.read(MyBean.class, inputMessage); }
@Test public void testConfigurationParsedCorrectlyWithKafkaClient() throws Exception { ConfigFile configFile = new ConfigFile(new ClassPathResource("jaas-sample-kafka-only.conf").getURI()); final AppConfigurationEntry[] kafkaConfigurationArray = configFile .getAppConfigurationEntry(KafkaJaasLoginModuleInitializer.KAFKA_CLIENT_CONTEXT_NAME); javax.security.auth.login.Configuration configuration = javax.security.auth.login.Configuration .getConfiguration(); final AppConfigurationEntry[] kafkaConfiguration = configuration .getAppConfigurationEntry(KafkaJaasLoginModuleInitializer.KAFKA_CLIENT_CONTEXT_NAME); assertThat(kafkaConfiguration).hasSize(1); assertThat(kafkaConfiguration[0].getOptions()).isEqualTo(kafkaConfigurationArray[0].getOptions()); JaasContext context = JaasContext.loadClientContext(Collections.emptyMap()); List<AppConfigurationEntry> appConfigurationEntries = context.configurationEntries(); assertThat(appConfigurationEntries).hasSize(1); assertThat(appConfigurationEntries.get(0).getOptions()).isEqualTo(kafkaConfigurationArray[0].getOptions()); }
/** * Find a Resource on the Classpath and return it's URI (as a String). * * Intended for calling code which accepts URI, such as the javax.xml APIs. Better than File-based API, which should * never be used. Better than InputStream, because an InputStream needs to be closed by somebody - and that's * usually forgotten. * * If calling code can deal directly with a org.springframework.core.io.Resource, that's always preferable over a * raw URI; simply use new ClassPathResource(path) in that case. * * @param path Path on the classpath, e.g. "org/mifos/something.xml" * @return a Stringified Classpath URI */ public static String getClassPathResourceAsURIString(String path) throws IOException { return new ClassPathResource(path).getURI().toString(); }
protected static String getPathByLog4j() { ClassPathResource resource = new ClassPathResource("/log4j.properties"); if (resource.exists()) { String path; try { path = resource.getURI().toString(); } catch (IOException e) { throw new RuntimeException(e.getMessage(), e); } System.err.println("pathpath:" + path); return path; } else { return null; } } }
@Override public void startService() throws JasDBException { if(consoleEnabled) { LOG.info("Starting Web console"); server = new Server(portNr); WebAppContext app = new WebAppContext(); app.setContextPath("/"); try { app.setWar(new ClassPathResource("webapp").getURI().toString()); server.setHandler(app); LOG.info("Starting Console service on port: {}", portNr); server.start(); } catch (Exception e) { throw new JasDBException("Unable to start Console UI service", e); } } }
private static ServletContextHandler getServletContextHandler(WebApplicationContext context) throws IOException { ServletContextHandler contextHandler = new ServletContextHandler(); contextHandler.setErrorHandler(null); contextHandler.setContextPath(CONTEXT_PATH); contextHandler.addServlet(new ServletHolder(new DispatcherServlet(context)), MAPPING_URL); contextHandler.addEventListener(new ContextLoaderListener(context)); contextHandler.setResourceBase(new ClassPathResource("webapp").getURI().toString()); return contextHandler; }
private static ServletContextHandler getServletContextHandler() throws IOException { ServletContextHandler contextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS); // SESSIONS requerido para JSP contextHandler.setErrorHandler(null); contextHandler.setResourceBase(new ClassPathResource(WEBAPP_DIRECTORY).getURI().toString()); contextHandler.setContextPath(CONTEXT_PATH); // JSP contextHandler.setClassLoader(Thread.currentThread().getContextClassLoader()); // Necesario para cargar JspServlet contextHandler.addServlet(JspServlet.class, "*.jsp"); // Spring WebApplicationContext webAppContext = getWebApplicationContext(); DispatcherServlet dispatcherServlet = new DispatcherServlet(webAppContext); ServletHolder springServletHolder = new ServletHolder("mvc-dispatcher", dispatcherServlet); contextHandler.addServlet(springServletHolder, MAPPING_URL); contextHandler.addEventListener(new ContextLoaderListener(webAppContext)); return contextHandler; }
@Override public void setAsText(String text) throws IllegalArgumentException { if (StringUtils.hasText(text)) { String uri = text.trim(); if (this.classLoader != null && uri.startsWith(ResourceUtils.CLASSPATH_URL_PREFIX)) { ClassPathResource resource = new ClassPathResource( uri.substring(ResourceUtils.CLASSPATH_URL_PREFIX.length()), this.classLoader); try { setValue(resource.getURI()); } catch (IOException ex) { throw new IllegalArgumentException("Could not retrieve URI for " + resource + ": " + ex.getMessage()); } } else { try { setValue(createURI(uri)); } catch (URISyntaxException ex) { throw new IllegalArgumentException("Invalid URI syntax: " + ex); } } } else { setValue(null); } }
@Override public void customize(Server server) { Handler[] childHandlersByClass = server.getChildHandlersByClass(WebAppContext.class); final WebAppContext webAppContext = (WebAppContext) childHandlersByClass[0]; try { ClassPathResource classPathResource = new ClassPathResource(this.jettyProperties.getClassPathResource()); webAppContext.setBaseResource(new ResourceCollection(classPathResource.getURI().toString())); AccessController.doPrivileged(new PrivilegedAction<Void>() { @Override public Void run() { webAppContext.setClassLoader(new URLClassLoader(new URL[0], this.getClass().getClassLoader())); return null; } }); LOGGER.info("Setting Jetty classLoader to " + this.jettyProperties.getClassPathResource() + " directory"); } catch (IOException exception) { LOGGER.error("Unable to configure Jetty classLoader to " + this.jettyProperties.getClassPathResource() + " directory " + exception.getMessage()); throw new RuntimeException(exception); } } }
@Override public void customize(Server server) { Handler[] childHandlersByClass = server.getChildHandlersByClass(WebAppContext.class); final WebAppContext webAppContext = (WebAppContext) childHandlersByClass[0]; try { ClassPathResource classPathResource = new ClassPathResource(this.jettyProperties.getClassPathResource()); webAppContext.setBaseResource(new ResourceCollection(classPathResource.getURI().toString())); AccessController.doPrivileged(new PrivilegedAction<Void>() { @Override public Void run() { webAppContext.setClassLoader(new URLClassLoader(new URL[0], this.getClass().getClassLoader())); return null; } }); log.info("Setting Jetty classLoader to {} directory", this.jettyProperties.getClassPathResource()); } catch (IOException exception) { log.error("Unable to configure Jetty classLoader to {} directory {}", this.jettyProperties.getClassPathResource(), exception.getMessage()); throw new RuntimeException(exception); } } }
@Override public void customize(Server server) { Handler[] childHandlersByClass = server.getChildHandlersByClass(WebAppContext.class); final WebAppContext webAppContext = (WebAppContext) childHandlersByClass[0]; try { ClassPathResource classPathResource = new ClassPathResource(this.jettyProperties.getClassPathResource()); webAppContext.setBaseResource(new ResourceCollection(classPathResource.getURI().toString())); AccessController.doPrivileged(new PrivilegedAction<Void>() { @Override public Void run() { webAppContext.setClassLoader(new URLClassLoader(new URL[0], this.getClass().getClassLoader())); return null; } }); log.info("Setting Jetty classLoader to {} directory", this.jettyProperties.getClassPathResource()); } catch (IOException exception) { log.error("Unable to configure Jetty classLoader to {} directory {}", this.jettyProperties.getClassPathResource(), exception.getMessage()); throw new RuntimeException(exception); } } }
@BeforeClass public static void setup() throws IOException { LOGGER_CONTEXT.setConfigLocation(new ClassPathResource("log4j2-amqp-appender.xml").getURI()); LOGGER_CONTEXT.reconfigure(); }