@Override @SuppressWarnings("unchecked") public Set<Class<? extends NotificationId>> getTypeClasses() { return Util.<Class<? extends NotificationId>> asSet(NotificationId.class); }
@Override public void deleteFile(File f) { String path = f.getPath(); // First remove the channel from the map FileChannel fc = streams.remove(path); // Then close the channel (handles null params) Util.close(fc); // Now that the channel is closed we can delete the file f.delete(); }
/** * Returns true if obj is a Fqn with the same elements. */ @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (!(obj instanceof Fqn)) { return false; } Fqn other = (Fqn) obj; if (elements.length != other.elements.length) return false; for (int i = elements.length - 1; i >= 0; i--) { if (!Util.safeEquals(elements[i], other.elements[i])) return false; } return true; }
/** * Returns a new comparator instance based on {@link #setComparatorClassName(String)}. */ public Comparator<?> createComparator() { return (Comparator<?>) Util.getInstance(comparatorClassName, getClassLoader()); }
@Override public void run() { try { cache.get(key); int i = count.getAndIncrement(); if (log.isDebugEnabled() && i % 100 == 0) log.debugf(">> Moved %s keys\n", i); } catch (Exception e) { log.keyMigrationFailed(Util.toStr(key), e); } } });
@Override public void stop() { for (FileChannel channel : streams.values()) { try { channel.force(true); } catch (IOException e) { log.errorFlushingToFileChannel(channel, e); } Util.close(channel); } streams.clear(); }
@Override public Set<Class<? extends Fqn>> getTypeClasses() { return Util.<Class<? extends Fqn>>asSet(Fqn.class); } }
/** * Returns true if this Fqn is equals or the child of parentFqn. Example usage: * <pre> * Fqn<String> f1 = Fqn.fromString("/a/b"); * Fqn<String> f2 = Fqn.fromString("/a/b/c"); * assertTrue(f1.isChildOrEquals(f2)); * assertTrue(f1.isChildOrEquals(f1)); * assertFalse(f2.isChildOrEquals(f1)); * </pre> * * @param parentFqn candidate parent to test against * @return true if this Fqn is equals or the child of parentFqn. */ public boolean isChildOrEquals(Fqn parentFqn) { Object[] parentEl = parentFqn.elements; if (parentEl.length > elements.length) { return false; } for (int i = parentEl.length - 1; i >= 0; i--) { if (!Util.safeEquals(parentEl[i], elements[i])) return false; } return true; }
@Override public void purge(File f) throws IOException { // Avoid a delete per-se because it hampers any fsync-like functionality // cos any cached file channel write won't change the file's exists // status. So, clear the file rather than delete it. FileChannel channel = streams.get(f.getPath()); if (channel == null) { channel = createChannel(f); String path = f.getPath(); FileChannel existingChannel = streams.putIfAbsent(path, channel); if (existingChannel != null) { Util.close(channel); channel = existingChannel; } } channel.truncate(0); // Apart from truncating, it's necessary to reset the position! channel.position(0); }
@Override @SuppressWarnings("unchecked") public Set<Class<? extends SubscriptionId>> getTypeClasses() { return Util.<Class<? extends SubscriptionId>> asSet(SubscriptionId.class); }
@Override public void run() { for (Map.Entry<String, FileChannel> entry : streams.entrySet()) { if (trace) log.tracef("Flushing channel in %s", entry.getKey()); FileChannel channel = entry.getValue(); try { channel.force(true); } catch (IOException e) { if (trace) log.tracef(e, "Error flushing output stream for %s", entry.getKey()); flushErrors.putIfAbsent(entry.getKey(), e); // If an error is encountered, close it. Next time it's used, // the exception will be propagated back to the user. Util.close(channel); } } } }, interval, interval, TimeUnit.MILLISECONDS);
@Override public void write(byte[] bytes, File f) throws IOException { if (bytes.length == 0) { // Short circuit for deleting files deleteFile(f); return; } String path = f.getPath(); FileChannel channel = streams.get(path); if (channel == null) { channel = createChannel(f); FileChannel existingChannel = streams.putIfAbsent(path, channel); if (existingChannel != null) { Util.close(channel); channel = existingChannel; } } else if (!f.exists()) { f.createNewFile(); FileChannel oldChannel = channel; channel = createChannel(f); boolean replaced = streams.replace(path, oldChannel, channel); if (replaced) { Util.close(oldChannel); } else { Util.close(channel); channel = streams.get(path); } } channel.write(ByteBuffer.wrap(bytes)); }
public Properties readEnvironmentProperties() throws CacheLoaderException { if (environmentPropertiesFile == null || environmentPropertiesFile.trim().length() == 0) return null; InputStream i = FileLookupFactory.newInstance().lookupFile(environmentPropertiesFile, getClassLoader()); if (i != null) { Properties p = new Properties(); try { p.load(i); } catch (IOException ioe) { throw new CacheLoaderException("Unable to read environment properties file " + environmentPropertiesFile, ioe); } finally { Util.close(i); } return p; } else { return null; } } }