private File getIndex(final List<String> categories) throws IOException { final File dir = getPath(categories, true); final File index = new File(dir.getAbsolutePath() + File.separator + INDEX_NAME); synchronized (dir.getAbsolutePath()) { if (!index.exists()) { index.createNewFile(); } } return index; }
@Override public boolean deleteData(final List<String> categories) { final File dir = getPath(categories, false); if (dir != null && dir.exists()) { Misc.deleteDir(dir, 10, 5); if (!dir.exists()) { return true; } } return false; }
private File getFromIndex(final List<String> categories, final String key) throws IOException { final File index = getIndex(categories); synchronized (index.getAbsolutePath()) { final Properties properties = loadIndex(index); final String file = (String) properties.get(key); if (file == null) { return null; } return new File(getPath(categories, false) + File.separator + file); } }
private File removeFromIndex(final List<String> categories, final String key) throws IOException { final File index = getIndex(categories); synchronized (index.getAbsolutePath()) { final Properties properties = loadIndex(index); final String file = (String) properties.remove(key); storeIndex(index, properties); return new File(getPath(categories, false) + File.separator + file); } }
@Override public Set<String> getKeys(final List<String> categories, final String regex) { final File dir = getPath(categories, false); if (dir == null || !dir.exists()) { return Collections.emptySet(); } try { final File index = getIndex(categories); synchronized (index.getAbsolutePath()) { final Properties properties = loadIndex(index); final Enumeration<Object> keys = properties.keys(); final Set<String> result = new HashSet<String>(); while (keys.hasMoreElements()) { final String key = (String) keys.nextElement(); if (regex == null || key.matches(regex)) { result.add(key); } } return result; } } catch (final IOException e) { throw new BonitaRuntimeException(e); } }
@Override public <T> Map<String, T> getDataFromRegex(final Class<T> clazz, final List<String> categories, final String regex) { final File dir = getPath(categories, false); if (dir == null || !dir.exists()) { return Collections.emptyMap(); } try { final File index = getIndex(categories); synchronized (index.getAbsolutePath()) { final Properties properties = loadIndex(index); final Map<String, T> result = new HashMap<String, T>(); for (final Entry<Object, Object> entry : properties.entrySet()) { final String key = (String) entry.getKey(); final File f = new File(dir + File.separator + entry.getValue()); if (f.isFile() && (regex == null || key.matches(regex))) { result.put(key, getData(clazz, f)); } } return result; } } catch (final IOException e) { throw new BonitaRuntimeException(e); } }
@Override public <T> Map<String, T> getData(final Class<T> clazz, final List<String> categories, final Collection<String> keys) { if (keys == null) { return Collections.emptyMap(); } final File dir = getPath(categories, false); if (dir == null || !dir.exists()) { return Collections.emptyMap(); } try { final File index = getIndex(categories); synchronized (index.getAbsolutePath()) { final Properties properties = loadIndex(index); final Map<String, T> result = new HashMap<String, T>(); for (final Entry<Object, Object> entry : properties.entrySet()) { final String key = (String) entry.getKey(); if (keys.contains(key)) { final File f = new File(dir + File.separator + entry.getValue()); if (f.isFile()) { result.put(key, getData(clazz, f)); } } } return result; } } catch (final IOException e) { throw new BonitaRuntimeException(e); } }
@Override public void storeData(final List<String> categories, final String key, final Serializable value, final boolean overWrite) { try { File file = getFromIndex(categories, key); if (file != null && file.exists()) { if (!overWrite) { return; } else { file.delete(); } } final int lastDotIndex = key.lastIndexOf('.'); String extension = ""; if (lastDotIndex > 0) { extension = key.substring(lastDotIndex); } final String fileName = UUID.randomUUID().toString() + extension; file = new File(getPath(categories, true) + File.separator + fileName); file.createNewFile(); Misc.write(file, Misc.serialize(value)); addEntryToIndex(categories, key, fileName); } catch (final IOException e) { throw new BonitaRuntimeException(e); } catch (final ClassNotFoundException e) { throw new BonitaRuntimeException(e); } }