use of org.exist.storage.lock.ManagedLock in project exist by eXist-db.
the class MemoryContentsImpl method read.
@Override
public int read(byte[] dst, long position, int off, int len) {
try (ManagedLock lock = readLock()) {
if (position >= size) {
return -1;
}
int toRead = (int) min(min(size - position, len), Integer.MAX_VALUE);
int currentBlock = (int) (position / BLOCK_SIZE);
int startIndexInBlock = (int) (position - (currentBlock * (long) BLOCK_SIZE));
int read = 0;
while (read < toRead) {
int lengthInBlock = min(BLOCK_SIZE - startIndexInBlock, toRead - read);
byte[] block = getBlock(currentBlock);
System.arraycopy(block, startIndexInBlock, dst, off + read, lengthInBlock);
read += lengthInBlock;
startIndexInBlock = 0;
currentBlock += 1;
}
return read;
}
}
use of org.exist.storage.lock.ManagedLock in project exist by eXist-db.
the class SecurityManagerImpl method processParameter.
@Override
public void processParameter(final DBBroker broker, final DocumentImpl document) throws ConfigurationException {
XmldbURI uri = document.getCollection().getURI();
final boolean isRemoved = uri.endsWith(SecurityManager.REMOVED_COLLECTION_URI);
if (isRemoved) {
uri = uri.removeLastSegment();
}
final boolean isAccount = uri.endsWith(SecurityManager.ACCOUNTS_COLLECTION_URI);
final boolean isGroup = uri.endsWith(SecurityManager.GROUPS_COLLECTION_URI);
if (isAccount || isGroup) {
uri = uri.removeLastSegment();
final String realmId = uri.lastSegment().toString();
final AbstractRealm realm = (AbstractRealm) findRealmForRealmId(realmId);
final Configuration conf = Configurator.parse(broker.getBrokerPool(), document);
Integer id = -1;
if (isRemoved) {
id = conf.getPropertyInteger("id");
}
final String name = conf.getProperty("name");
if (isAccount) {
if (isRemoved && id > 2 && !hasUser(id)) {
final AccountImpl account = new AccountImpl(realm, conf);
account.removed = true;
registerAccount(account);
} else if (name != null) {
if (realm.hasAccount(name)) {
final Integer oldId = saving.get(document.getURI());
final Integer newId = conf.getPropertyInteger("id");
if (!newId.equals(oldId)) {
final Account current = realm.getAccount(name);
try (final ManagedLock<ReadWriteLock> lock = ManagedLock.acquire(accountLocks.getLock(current), LockMode.WRITE_LOCK)) {
usersById.write(principalDb -> {
principalDb.remove(oldId);
principalDb.put(newId, current);
});
}
}
} else {
final Account account = new AccountImpl(realm, conf);
if (account.getGroups().length == 0) {
try {
account.setPrimaryGroup(realm.getGroup(SecurityManager.UNKNOWN_GROUP));
LOG.warn("Account '{}' has no groups, but every account must have at least 1 group. Assigned group: " + SecurityManager.UNKNOWN_GROUP, account.getName());
} catch (final PermissionDeniedException e) {
throw new ConfigurationException("Account has no group, unable to default to " + SecurityManager.UNKNOWN_GROUP + ": " + e.getMessage(), e);
}
}
registerAccount(account);
realm.registerAccount(account);
}
} else {
// this can't be! log any way
LOG.error("Account '{}' already exists in realm: '{}', but received notification that a new one was created.", name, realmId);
}
} else if (isGroup) {
if (isRemoved && id > 2 && !hasGroup(id)) {
final GroupImpl group = new GroupImpl(realm, conf);
group.removed = true;
registerGroup(group);
} else if (name != null && !realm.hasGroup(name)) {
final GroupImpl group = new GroupImpl(realm, conf);
registerGroup(group);
realm.registerGroup(group);
} else {
// this can't be! log any way
LOG.error("Group '{}' already exists in realm: '{}', but received notification that a new one was created.", name, realmId);
}
}
saving.remove(document.getURI());
}
}
use of org.exist.storage.lock.ManagedLock in project exist by eXist-db.
the class MemoryContentsImpl method transferTo.
@Override
public long transferTo(OutputStream target, long position) throws IOException {
try (ManagedLock lock = readLock()) {
long transferred = 0L;
long toTransfer = size - position;
int currentBlock = (int) (position / BLOCK_SIZE);
int startIndexInBlock = (int) (position - (currentBlock * (long) BLOCK_SIZE));
while (transferred < toTransfer) {
int lengthInBlock = (int) min(BLOCK_SIZE - startIndexInBlock, toTransfer - transferred);
byte[] block = getBlock(currentBlock);
target.write(block, startIndexInBlock, lengthInBlock);
transferred += lengthInBlock;
startIndexInBlock = 0;
currentBlock += 1;
}
return transferred;
}
}
use of org.exist.storage.lock.ManagedLock in project exist by eXist-db.
the class MemoryContentsImpl method write.
@Override
public int write(byte[] src, long position, int off, int len) {
try (ManagedLock lock = writeLock()) {
ensureCapacity(position + len);
int toWrite = min(len, Integer.MAX_VALUE);
int currentBlock = (int) (position / BLOCK_SIZE);
int startIndexInBlock = (int) (position - (currentBlock * (long) BLOCK_SIZE));
int written = 0;
while (written < toWrite) {
int lengthInBlock = min(BLOCK_SIZE - startIndexInBlock, toWrite - written);
byte[] block = getBlock(currentBlock);
System.arraycopy(src, off + written, block, startIndexInBlock, lengthInBlock);
written += lengthInBlock;
startIndexInBlock = 0;
currentBlock += 1;
}
// REVIEW, possibility to fill with random data
size = max(size, position + written);
return written;
}
}
Aggregations