@Override public String name() { return delegate.name(); }
@Override public String getFilename() { return resource.name(); }
public String parentDirectoryName() { return parentDirectory.name(); } }
/** * Some config directories in GeoServer are used to store workspace specific configurations, * identify them so that we don't log complaints about their existence * * @param f */ private boolean isConfigDirectory(Resource dir) { String name = dir.name(); boolean result = "styles".equals(name) || "layergroups".equals(name); return result; }
/** * Test if the file or directory behind the resource is hidden. For file system based resources, * the platform-dependent hidden property is used. For other resource implementations, filenames * starting with a "." are considered hidden, irrespective of the platform. * * @see File#isHidden() * @param resource Resource indicated * @return true If resource is hidden */ public static boolean isHidden(Resource resource) { if (resource instanceof SerializableResourceWrapper) { resource = ((SerializableResourceWrapper) resource).delegate; } if (resource instanceof FileSystemResourceStore.FileSystemResource || resource instanceof Files.ResourceAdaptor) { // this is a file based resource, just check the file return find(resource).isHidden(); } else { // not a file system based resource, no point in caching // we only support linux style hidden file. return resource.name().startsWith("."); } }
private void copyResToDir(Resource r, Resource newDir) throws IOException { Resource newR = newDir.get(r.name()); try (InputStream in = r.in(); OutputStream out = newR.out()) { IOUtils.copy(in, out); } } }
public LockFile(Resource file) throws IOException { lockFileTarget = file; if (!Resources.exists(file)) { throw new IOException("Cannot lock a not existing file: " + file.path()); } lockFile = file.parent().get(lockFileTarget.name() + ".lock"); Runtime.getRuntime() .addShutdownHook( new Thread( new Runnable() { // remove on shutdown @Override public void run() { lockFile.delete(); } })); }
SortedSet<String> listFiles(Resource dir) { SortedSet<String> result = new TreeSet<String>(); List<Resource> dirs = dir.list(); for (Resource d : dirs) { if (d.getType() == Type.DIRECTORY && d.get(CONFIG_FILENAME).getType() == Type.RESOURCE) { result.add(d.name()); } } return result; }
private void renameStyle(StyleInfo s, String newName) throws IOException { // rename style definition file Resource style = dd.style(s); StyleHandler format = Styles.handler(s.getFormat()); Resource target = uniqueResource(style, newName, format.getFileExtension()); renameRes(style, target.name()); s.setFilename(target.name()); // rename generated sld if appropriate if (!SLDHandler.FORMAT.equals(format.getFormat())) { Resource sld = style.parent().get(FilenameUtils.getBaseName(style.name()) + ".sld"); if (sld.getType() == Type.RESOURCE) { LOGGER.fine("Renaming style resource " + s.getName() + " to " + newName); Resource generated = uniqueResource(sld, newName, "sld"); renameRes(sld, generated.name()); } } }
private void moveResToDir(Resource r, Resource newDir) { rl.move(r.path(), newDir.get(r.name()).path()); }
private void moveResToDir(Resource r, Resource newDir) { try { rl.move(r.path(), newDir.get(r.name()).path()); } catch (Exception e) { throw new CatalogException(e); } }
@Theory public void theoryNameIsEndOfPath(String path) throws Exception { Resource res = getResource(path); List<String> elements = Paths.names(path); String lastElement = elements.get(elements.size() - 1); String result = res.name(); assertThat(result, equalTo(lastElement)); }
@Theory public void theoryHaveName(String path) throws Exception { Resource res = getResource(path); String result = res.name(); assertThat(result, notNullValue()); }
private void removeStyle(StyleInfo s) throws IOException { Resource sld = dd.style(s); if (Resources.exists(sld)) { Resource sldBackup = dd.get(sld.path() + ".bak"); int i = 1; while (Resources.exists(sldBackup)) { sldBackup = dd.get(sld.path() + ".bak." + i++); } LOGGER.fine("Removing the SLD as well but making backup " + sldBackup.name()); sld.renameTo(sldBackup); } }
void loadStyles(Resource styles, Catalog catalog, XStreamPersister xp) throws IOException { Filter<Resource> styleFilter = r -> XML_FILTER.accept(r) && !Resources.exists(styles.get(r.name() + ".xml")); try (AsynchResourceIterator<byte[]> it = new AsynchResourceIterator<>(styles, styleFilter, r -> r.getContents())) { while (it.hasNext()) { try { StyleInfo s = depersist(xp, it.next(), StyleInfo.class); catalog.add(s); if (LOGGER.isLoggable(Level.INFO)) { LOGGER.info("Loaded style '" + s.getName() + "'"); } } catch (Exception e) { LOGGER.log(Level.WARNING, "Failed to load style", e); } } } }
@Test public void resourcesTest() throws IOException { Resource source = getResource(); Resource directory = getDirectory(); Resources.copy(source.file(), directory); Resource target = directory.get(source.name()); assertTrue(Resources.exists(target)); assertEquals(target.name(), source.name()); } }
/** * Write the contents of a resource into another resource. Also supports directories * (recursively). * * @param data resource to read * @param destination resource to write to * @throws IOException If data could not be copied to destination */ public static void copy(Resource data, Resource destination) throws IOException { if (data.getType() == Type.DIRECTORY) { for (Resource child : data.list()) { copy(child, destination.get(child.name())); } } else { try (InputStream in = data.in()) { copy(in, destination); } } }
@Theory public void theoryDirectoriesHaveFileWithSameNamedChildren(String path) throws Exception { Resource res = getResource(path); assumeThat(res, is(directory())); File dir = res.dir(); Collection<Resource> resChildren = res.list(); String[] fileChildrenNames = dir.list(); String[] resChildrenNames = new String[resChildren.size()]; int i = 0; for (Resource child : resChildren) { resChildrenNames[i] = child.name(); i++; } assertThat(fileChildrenNames, arrayContainingInAnyOrder(resChildrenNames)); }
@Test public void testReloadDefaultStyles() throws Exception { // clear up all "point" styles final Resource styles = getDataDirectory().getStyles(); styles.list() .stream() .filter(r -> r.getType() == Resource.Type.RESOURCE && r.name().contains("point")) .forEach(r -> r.delete()); // reload getGeoServer().reload(); // check the default point style has been re-created final StyleInfo point = getCatalog().getStyleByName("point"); assertNotNull(point); }