public ContentReference(String contentIdentifier, byte[] hash, long timestamp) { this.contentIdentifier = contentIdentifier; if (hash == null || hash.length == 0) { this.hexHash = ""; } else { this.hexHash = HashUtil.bytesToHexString(hash); } }
@Override public void addContentReference(byte[] hash, Object reference) { String hashString = HashUtil.bytesToHexString(hash); synchronized (deploymentHashReferences) { Set<Object> references = deploymentHashReferences.get(hashString); if (references == null) { references = new HashSet<Object>(); deploymentHashReferences.put(hashString, references); } references.add(reference); } }
/** {@inheritDoc} */ @Override public File getDeploymentRoot(byte[] hash) { if (hash == null || hash.length == 0) { return deploymentRoot; } String hex = HashUtil.bytesToHexString(hash); File first = new File(deploymentRoot, hex.substring(0,2)); return new File(first, hex.substring(2)); }
private boolean lock(byte[] hash) throws InterruptedException { String hashHex = HashUtil.bytesToHexString(hash); synchronized(lockedContents) { if(!lockedContents.containsKey(hashHex)) { lockedContents.put(hashHex, new ReentrantLock()); } return lockedContents.get(hashHex).tryLock(lockTimeout, TimeUnit.MILLISECONDS); } }
/** {@inheritDoc} */ @Override public File getDeploymentRoot(byte[] deploymentHash) { if (deploymentHash == null || deploymentHash.length == 0) { return deploymentRoot; } String hex = HashUtil.bytesToHexString(deploymentHash); File first = new File(deploymentRoot, hex.substring(0,2)); return new File(first, hex.substring(2)); }
private void unlock(byte[] hash) { String hashHex = HashUtil.bytesToHexString(hash); synchronized (lockedContents) { if (lockedContents.containsKey(hashHex)) { ReentrantLock lock = lockedContents.get(hashHex); if (lock.isHeldByCurrentThread()) { lock.unlock(); if (!Files.exists(getDeploymentContentFile(hash))) { lockedContents.remove(hashHex); } } } } }
protected File getDeploymentHashDir(final byte[] deploymentHash, final boolean validate) { final String sha1 = HashUtil.bytesToHexString(deploymentHash); final String partA = sha1.substring(0,2); final String partB = sha1.substring(2); final File base = new File(getRepoRoot(), partA); if (validate) { validateDir(base); } final File hashDir = new File(base, partB); if (validate && !hashDir.exists() && !hashDir.mkdirs()) { throw DeploymentRepositoryMessages.MESSAGES.cannotCreateDirectory(hashDir.getAbsolutePath()); } return hashDir; }
protected Path getDeploymentHashDir(final byte[] deploymentHash, final boolean validate) { final String sha1 = HashUtil.bytesToHexString(deploymentHash); final String partA = sha1.substring(0, 2); final String partB = sha1.substring(2); final Path base = getRepoRoot().resolve(partA); if (validate) { validateDir(base); } final Path hashDir = base.resolve(partB); if (validate && !Files.exists(hashDir)) { try { Files.createDirectories(hashDir); } catch (IOException ioex) { throw DeploymentRepositoryLogger.ROOT_LOGGER.cannotCreateDirectory(ioex, hashDir.toAbsolutePath().toString()); } } return hashDir; }
@Override public void removeContent(byte[] hash, Object reference) { String hashString = HashUtil.bytesToHexString(hash); synchronized (deploymentHashReferences) { final Set<Object> references = deploymentHashReferences.get(hashString);
@Override public TypedInputStream readContent(byte[] deploymentHash, String path) throws ExplodedContentException { Path tmpDir = null; try { if(!lock(deploymentHash)) { throw DeploymentRepositoryLogger.ROOT_LOGGER.errorLockingDeployment(); } Path src = resolveSecurely(getDeploymentContentFile(deploymentHash), path); tmpDir = Files.createTempDirectory(tmpRoot.toPath(), HashUtil.bytesToHexString(deploymentHash)); Path file = PathUtil.readFile(src, tmpDir); Path tmp = Files.createTempFile(tmpRoot.toPath(), CONTENT, getFileExtension(src)); Files.copy(file, tmp, StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING); return new TemporaryFileInputStream(tmp); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); throw new RuntimeException(ex); } catch (IOException ex) { DeploymentRepositoryLogger.ROOT_LOGGER.warn(ex); throw DeploymentRepositoryLogger.ROOT_LOGGER.errorAccessingDeployment(ex); } finally { unlock(deploymentHash); deleteSilentlyRecursively(tmpDir); } }
@Override public List<ContentRepositoryElement> listContent(byte[] deploymentHash, String path, ContentFilter filter) throws ExplodedContentException { Path tmpDir = null; try { if (!lock(deploymentHash)) { throw DeploymentRepositoryLogger.ROOT_LOGGER.errorLockingDeployment(); } tmpDir = Files.createTempDirectory(tmpRoot.toPath(), HashUtil.bytesToHexString(deploymentHash)); final Path rootPath = resolveSecurely(getDeploymentContentFile(deploymentHash), path); List<ContentRepositoryElement> result = PathUtil.listFiles(rootPath, tmpDir, filter); return result; } catch (InterruptedException ex) { Thread.currentThread().interrupt(); throw new RuntimeException(ex); } catch (IOException ex) { DeploymentRepositoryLogger.ROOT_LOGGER.warn(ex); throw DeploymentRepositoryLogger.ROOT_LOGGER.errorAccessingDeployment(ex); } finally { unlock(deploymentHash); if(tmpDir != null) { deleteSilentlyRecursively(tmpDir); } } }