List of usage examples for org.springframework.core.io Resource exists
boolean exists();
From source file:com.netflix.genie.web.tasks.node.DiskCleanupTaskUnitTests.java
/** * Test the constructor./*from ww w . jav a2 s .c o m*/ * * @throws IOException on error */ @Test public void willScheduleOnUnixWithoutSudo() throws IOException { final JobsProperties properties = new JobsProperties(); properties.getUsers().setRunAsUserEnabled(false); Assume.assumeTrue(SystemUtils.IS_OS_UNIX); final TaskScheduler scheduler = Mockito.mock(TaskScheduler.class); final Resource jobsDir = Mockito.mock(Resource.class); Mockito.when(jobsDir.exists()).thenReturn(true); Assert.assertNotNull(new DiskCleanupTask(new DiskCleanupProperties(), scheduler, jobsDir, Mockito.mock(JobSearchService.class), properties, Mockito.mock(Executor.class), Mockito.mock(Registry.class))); Mockito.verify(scheduler, Mockito.times(1)).schedule(Mockito.any(Runnable.class), Mockito.any(Trigger.class)); }
From source file:com.wavemaker.tools.project.LocalStudioFileSystem.java
public static Resource staticGetWaveMakerHome() { Resource ret = null; String env = System.getProperty(WMHOME_PROP_KEY, null); if (env != null && 0 != env.length()) { ret = new FileSystemResource(env); }/* ww w . j av a 2s . c o m*/ if (ret == null) { String pref = ConfigurationStore.getPreference(LocalStudioConfiguration.class, WMHOME_KEY, null); if (pref != null && 0 != pref.length()) { pref = pref.endsWith("/") ? pref : pref + "/"; ret = new FileSystemResource(pref); } } // we couldn't find a test value, a property, or a preference, so use // a default if (ret == null) { System.out.println("INFO: Using default WaveMaker Home folder"); ret = getDefaultWaveMakerHome(); } if (!ret.exists()) { try { ret.getFile().mkdir(); } catch (IOException ex) { throw new WMRuntimeException(ex); } } return ret; }
From source file:com.haulmont.cuba.desktop.sys.DesktopExternalUIComponentsSource.java
protected void _registerAppComponents() { String configName = AppContext.getProperty(DESKTOP_COMPONENTS_CONFIG_XML_PROP); StrTokenizer tokenizer = new StrTokenizer(configName); for (String location : tokenizer.getTokenArray()) { Resource resource = resources.getResource(location); if (resource.exists()) { InputStream stream = null; try { stream = resource.getInputStream(); _registerComponent(stream); } catch (ClassNotFoundException | IOException e) { throw new RuntimeException(e); } finally { IOUtils.closeQuietly(stream); }/*from www. j ava2 s.c o m*/ } else { log.warn("Resource {} not found, ignore it", location); } } }
From source file:org.alfresco.util.resource.HierarchicalResourceLoaderTest.java
private void checkResource(Resource resource, String check) throws Throwable { assertNotNull("Resource not found", resource); assertTrue("Resource doesn't exist", resource.exists()); InputStream is = resource.getInputStream(); StringBuilder builder = new StringBuilder(128); byte[] bytes = new byte[128]; InputStream tempIs = null;/*from ww w. ja va2 s . c om*/ try { tempIs = new BufferedInputStream(is, 128); int count = -2; while (count != -1) { // do we have something previously read? if (count > 0) { String toWrite = new String(bytes, 0, count, "UTF-8"); builder.append(toWrite); } // read the next set of bytes count = tempIs.read(bytes); } } catch (IOException e) { throw new AlfrescoRuntimeException("Unable to read stream", e); } finally { // close the input stream try { is.close(); } catch (Exception e) { } } // The string String fileValue = builder.toString(); assertEquals("Incorrect file retrieved: ", check, fileValue); }
From source file:com.haulmont.cuba.web.sys.WebExternalUIComponentsSource.java
protected void _registerAppComponents() { String configName = AppContext.getProperty(WEB_COMPONENTS_CONFIG_XML_PROP); StrTokenizer tokenizer = new StrTokenizer(configName); for (String location : tokenizer.getTokenArray()) { Resource resource = resources.getResource(location); if (resource.exists()) { InputStream stream = null; try { stream = resource.getInputStream(); _registerComponent(stream); } catch (ClassNotFoundException | IOException e) { throw new RuntimeException(e); } finally { IOUtils.closeQuietly(stream); }//from www .j av a 2 s. co m } else { log.warn("Resource {} not found, ignore it", location); } } }
From source file:com.wavemaker.commons.classloader.ThrowawayFileClassLoader.java
@Override protected Class<?> findClass(String name) throws ClassNotFoundException { if (this.classPath == null) { throw new ClassNotFoundException("invalid search root: " + this.classPath); } else if (name == null) { throw new ClassNotFoundException(MessageResource.NULL_CLASS.getMessage()); }// w ww . j a v a 2 s.com String classNamePath = name.replace('.', '/') + ".class"; byte[] fileBytes = null; try { InputStream is = null; JarFile jarFile = null; for (Resource entry : this.classPath) { if (entry.getFilename().toLowerCase().endsWith(".jar")) { jarFile = new JarFile(entry.getFile()); ZipEntry ze = jarFile.getEntry(classNamePath); if (ze != null) { is = jarFile.getInputStream(ze); break; } else { jarFile.close(); } } else { Resource classFile = entry.createRelative(classNamePath); if (classFile.exists()) { is = classFile.getInputStream(); break; } } } if (is != null) { try { fileBytes = IOUtils.toByteArray(is); is.close(); } finally { if (jarFile != null) { jarFile.close(); } } } } catch (IOException e) { throw new ClassNotFoundException(e.getMessage(), e); } if (name.contains(".")) { String packageName = name.substring(0, name.lastIndexOf('.')); if (getPackage(packageName) == null) { definePackage(packageName, "", "", "", "", "", "", null); } } Class<?> ret; if (fileBytes == null) { ret = ClassLoaderUtils.loadClass(name, this.parentClassLoader); } else { ret = defineClass(name, fileBytes, 0, fileBytes.length); } if (ret == null) { throw new ClassNotFoundException( "Couldn't find class " + name + " in expected classpath: " + this.classPath); } return ret; }
From source file:com.dianwoba.redcliff.blink.config.MybatisConfig.java
@PostConstruct public void checkConfigFileExists() { if (this.properties.isCheckConfigLocation() && StringUtils.hasText(this.properties.getConfigLocation())) { Resource resource = this.resourceLoader.getResource(this.properties.getConfigLocation()); Assert.state(resource.exists(), "Cannot find config location: " + resource + " (please add config file or check your Mybatis " + "configuration)"); }//from w ww . ja v a 2s . com }
From source file:com.github.mjeanroy.springmvc.view.mustache.configuration.MustacheTemplateLoaderFactoryBeanTest.java
@Test public void classpath_resource_loader_should_load_resource_from_classpath() throws Exception { Resource resource = classpathResourceLoader.getResource("/templates/foo.template.html"); assertThat(resource).isNotNull();/*www .j a v a2 s .c o m*/ assertThat(resource.exists()).isTrue(); assertThat(resource.getURI().toString()).startsWith("file:/") .endsWith("/target/test-classes/templates/foo.template.html"); }
From source file:com.wavemaker.common.util.ThrowawayFileClassLoader.java
@Override protected Class<?> findClass(String name) throws ClassNotFoundException { if (this.classPath == null) { throw new ClassNotFoundException("invalid search root: " + this.classPath); } else if (name == null) { throw new ClassNotFoundException(MessageResource.NULL_CLASS.getMessage()); }//from w w w. ja va2 s.com String classNamePath = name.replace('.', '/') + ".class"; byte[] fileBytes = null; try { InputStream is = null; JarFile jarFile = null; for (Resource entry : this.classPath) { if (entry.getFilename().toLowerCase().endsWith(".jar")) { jarFile = new JarFile(entry.getFile()); ZipEntry ze = jarFile.getEntry(classNamePath); if (ze != null) { is = jarFile.getInputStream(ze); break; } else { jarFile.close(); } } else { Resource classFile = entry.createRelative(classNamePath); if (classFile.exists()) { is = classFile.getInputStream(); break; } } } if (is != null) { try { fileBytes = IOUtils.toByteArray(is); is.close(); } finally { if (jarFile != null) { jarFile.close(); } } } } catch (IOException e) { throw new ClassNotFoundException(e.getMessage(), e); } if (name.contains(".")) { String packageName = name.substring(0, name.lastIndexOf('.')); if (getPackage(packageName) == null) { definePackage(packageName, "", "", "", "", "", "", null); } } Class<?> ret; if (fileBytes == null) { ret = ClassLoaderUtils.loadClass(name, this.parentClassLoader); } else { ret = defineClass(name, fileBytes, 0, fileBytes.length); } if (ret == null) { throw new ClassNotFoundException( "Couldn't find class " + name + " in expected classpath: " + this.classPath); } return ret; }
From source file:com.netflix.genie.web.tasks.node.DiskCleanupTaskTest.java
/** * Make sure we can run successfully when runAsUser is false for the system. * * @throws IOException on error/*from w ww . ja v a 2 s .c o m*/ * @throws GenieException on error */ @Test public void canRunWithoutSudo() throws IOException, GenieException { final JobsProperties jobsProperties = JobsProperties.getJobsPropertiesDefaults(); jobsProperties.getUsers().setRunAsUserEnabled(false); // Create some random junk file that should be ignored this.tmpJobDir.newFile(UUID.randomUUID().toString()); final DiskCleanupProperties properties = new DiskCleanupProperties(); final Instant threshold = TaskUtils.getMidnightUTC().minus(properties.getRetention(), ChronoUnit.DAYS); final String job1Id = UUID.randomUUID().toString(); final String job2Id = UUID.randomUUID().toString(); final String job3Id = UUID.randomUUID().toString(); final String job4Id = UUID.randomUUID().toString(); final String job5Id = UUID.randomUUID().toString(); final Job job1 = Mockito.mock(Job.class); Mockito.when(job1.getStatus()).thenReturn(JobStatus.INIT); final Job job2 = Mockito.mock(Job.class); Mockito.when(job2.getStatus()).thenReturn(JobStatus.RUNNING); final Job job3 = Mockito.mock(Job.class); Mockito.when(job3.getStatus()).thenReturn(JobStatus.SUCCEEDED); Mockito.when(job3.getFinished()).thenReturn(Optional.of(threshold.minus(1, ChronoUnit.MILLIS))); final Job job4 = Mockito.mock(Job.class); Mockito.when(job4.getStatus()).thenReturn(JobStatus.FAILED); Mockito.when(job4.getFinished()).thenReturn(Optional.of(threshold)); this.createJobDir(job1Id); this.createJobDir(job2Id); this.createJobDir(job3Id); this.createJobDir(job4Id); this.createJobDir(job5Id); final TaskScheduler scheduler = Mockito.mock(TaskScheduler.class); final Resource jobDir = Mockito.mock(Resource.class); Mockito.when(jobDir.exists()).thenReturn(true); Mockito.when(jobDir.getFile()).thenReturn(this.tmpJobDir.getRoot()); final JobSearchService jobSearchService = Mockito.mock(JobSearchService.class); Mockito.when(jobSearchService.getJob(job1Id)).thenReturn(job1); Mockito.when(jobSearchService.getJob(job2Id)).thenReturn(job2); Mockito.when(jobSearchService.getJob(job3Id)).thenReturn(job3); Mockito.when(jobSearchService.getJob(job4Id)).thenReturn(job4); Mockito.when(jobSearchService.getJob(job5Id)).thenThrow(new GenieServerException("blah")); final DiskCleanupTask task = new DiskCleanupTask(properties, scheduler, jobDir, jobSearchService, jobsProperties, Mockito.mock(Executor.class), new SimpleMeterRegistry()); task.run(); Assert.assertTrue(new File(jobDir.getFile(), job1Id).exists()); Assert.assertTrue(new File(jobDir.getFile(), job2Id).exists()); Assert.assertFalse(new File(jobDir.getFile(), job3Id).exists()); Assert.assertTrue(new File(jobDir.getFile(), job4Id).exists()); Assert.assertTrue(new File(jobDir.getFile(), job5Id).exists()); }