@Override public boolean exists() { return Resources.exists(resource); }
public static Resource createRandom(String prefix, String suffix, Resource dir) throws IOException { // Use only the file name from the supplied prefix prefix = (new File(prefix)).getName(); Resource res; do { UUID uuid = UUID.randomUUID(); String name = prefix + uuid + suffix; res = dir.get(name); } while (exists(res)); return res; }
/** * return true if a write lock is hold by another file watcher * * @throws IOException */ public boolean hasForeignWriteLock() throws IOException { return Resources.exists(lockFile) && lockFile.lastmodified() != lockFileLastModified; }
/** * return true if a write lock is hold by this file watcher * * @throws IOException */ public boolean hasWriteLock() throws IOException { return Resources.exists(lockFile) && lockFile.lastmodified() == lockFileLastModified; }
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(); } })); }
/** remove the lockfile */ public void writeUnLock() { if (Resources.exists(lockFile)) { if (lockFile.lastmodified() == lockFileLastModified) { lockFileLastModified = 0; lockFile.delete(); } else { LOGGER.warning("Tried to unlock foreign lock: " + lockFile.path()); } } else { LOGGER.warning("Tried to unlock not exisiting lock: " + lockFile.path()); } }
@Override public WorkspaceContents apply(Resource rd) throws IOException { Resource wr = rd.get("workspace.xml"); Resource nr = rd.get("namespace.xml"); if (Resources.exists(wr) && Resources.exists(nr)) { byte[] contents = wr.getContents(); byte[] nrContents = nr.getContents(); return new WorkspaceContents(rd, contents, nrContents); } else { LOGGER.warning("Ignoring workspace directory " + rd.path()); return null; } } }
@Override public LayerContents apply(Resource rd) throws IOException { Resource r = rd.get(resourceFileName); Resource lr = rd.get("layer.xml"); if (Resources.exists(r) && Resources.exists(lr)) { byte[] contents = r.getContents(); byte[] lrContents = lr.getContents(); return new LayerContents(rd, contents, lrContents); } else { LOGGER.warning("Ignoring " + resourceType + " directory " + rd.path()); return null; } } }
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); } }
Resource resource = resourceLoader.fromURL(url); File file; if (Resources.exists(resource)) {
/** * Try to get a lock * * @throws IOException */ public void writeLock() throws IOException { if (hasWriteLock()) return; // already locked if (Resources.exists(lockFile)) { LOGGER.warning("Cannot obtain lock: " + lockFile.path()); Properties props = new Properties(); try (InputStream in = lockFile.in()) { props.load(in); } throw new IOException(Util.convertPropsToString(props, "Already locked")); } else { // success writeLockFileContent(lockFile); lockFileLastModified = lockFile.lastmodified(); LOGGER.info("Successful lock: " + lockFile.path()); } }
/** * Helper method to create all create/modify events caused by a rename/move operation. (delete * events must be created separately.) This method should be called just before the action takes * place in order to analyze the effects properly. * * <p>Rename/move causes children to be moved as well. * * @param src Resource being renamed * @param dest Target resource after renatmed * @return List of notification events covering all the modified Resources */ public static List<Event> createRenameEvents(Resource src, Resource dest) { List<Event> events = new ArrayList<Event>(); events.add( new ResourceNotification.Event( dest.path(), Resources.exists(dest) ? Kind.ENTRY_MODIFY : Kind.ENTRY_CREATE)); for (Resource child : Resources.listRecursively(src)) { Resource newChild = dest.get(child.path().substring(src.path().length() + 1)); events.add( new ResourceNotification.Event( newChild.path(), Resources.exists(newChild) ? Kind.ENTRY_MODIFY : Kind.ENTRY_CREATE)); } return events; } }
boolean checkStoresOnStartup(XStreamPersister xp) { Resource f = resourceLoader.get("global.xml"); if (Resources.exists(f)) { try { GeoServerInfo global = depersist(xp, f, GeoServerInfo.class); final ResourceErrorHandling resourceErrorHandling = global.getResourceErrorHandling(); return resourceErrorHandling != null && !ResourceErrorHandling.SKIP_MISCONFIGURED_LAYERS.equals( resourceErrorHandling); } catch (IOException e) { LOGGER.log( Level.INFO, "Failed to determine the capabilities resource error handling", e); } } return true; }
+ (s.isEnabled() ? "enabled" : "disabled")); } catch (Throwable t) { if (Resources.exists(directory)) { LOGGER.log( Level.SEVERE,
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); } } } }
while (parent != null && !Resources.exists(parent)) { events.add(new ResourceNotification.Event(parent.path(), kind)); parent = parent.parent();
/** Copies a well known style out to the data directory and adds a catalog entry for it. */ void initializeStyle(Catalog catalog, String styleName, String sld) throws IOException { // copy the file out to the data directory if necessary Resource styleResource = resourceLoader.get(Paths.path("styles", sld)); if (!Resources.exists(styleResource)) { try (InputStream in = GeoServerLoader.class.getResourceAsStream(sld); OutputStream out = styleResource.out()) { IOUtils.copy(in, out); } } // create a style for it StyleInfo s = catalog.getFactory().createStyle(); s.setName(styleName); s.setFilename(sld); catalog.add(s); }
public final T load(GeoServer gs, Resource directory) throws Exception { // look for file matching classname Resource file; if (Resources.exists(file = directory.get(getFilename()))) { // xstream it in try (BufferedInputStream in = new BufferedInputStream(file.in())) { XStreamPersister xp = xpf.createXMLPersister(); initXStreamPersister(xp, gs); return initialize(xp.load(in, getServiceClass())); } } else { // create an 'empty' object ServiceInfo service = createServiceFromScratch(gs); return initialize((T) service); } }
protected void readCatalog(Catalog catalog, XStreamPersister xp) throws Exception { // we are going to synch up the catalogs and need to preserve listeners, // but these two fellas are attached to the new catalog as well catalog.removeListeners(ResourcePool.CacheClearingListener.class); catalog.removeListeners(GeoServerConfigPersister.class); catalog.removeListeners(GeoServerResourcePersister.class); List<CatalogListener> listeners = new ArrayList<CatalogListener>(catalog.getListeners()); // look for catalog.xml, if it exists assume we are dealing with // an old data directory Resource f = resourceLoader.get("catalog.xml"); if (!Resources.exists(f)) { // assume 2.x style data directory CatalogImpl catalog2 = (CatalogImpl) readCatalog(xp); // make to remove the old resource pool catalog listener ((CatalogImpl) catalog).sync(catalog2); } else { // import old style catalog, register the persister now so that we start // with a new version of the catalog CatalogImpl catalog2 = (CatalogImpl) readLegacyCatalog(f, xp); ((CatalogImpl) catalog).sync(catalog2); } // attach back the old listeners for (CatalogListener listener : listeners) { catalog.addListener(listener); } }
@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()); } }