use of com.google.common.io.ByteSource in project rhino by PLOS.
the class ArticleCrudServiceImpl method repack.
@Override
public Archive repack(ArticleIngestionIdentifier ingestionId) {
ArticleIngestion ingestion = readIngestion(ingestionId);
List<ArticleFile> files = hibernateTemplate.execute(session -> {
Query query = session.createQuery("FROM ArticleFile WHERE ingestion = :ingestion");
query.setParameter("ingestion", ingestion);
return (List<ArticleFile>) query.list();
});
Map<String, ByteSource> archiveMap = files.stream().collect(Collectors.toMap(ArticleFile::getIngestedFileName, (ArticleFile file) -> new ByteSource() {
@Override
public InputStream openStream() throws IOException {
return contentRepoService.getRepoObject(file.getCrepoVersion());
}
}));
return Archive.pack(extractFilenameStub(ingestionId.getDoiName()) + ".zip", archiveMap);
}
use of com.google.common.io.ByteSource in project gerrit by GerritCodeReview.
the class AddSshKey method apply.
public Response<SshKeyInfo> apply(IdentifiedUser user, Input input) throws BadRequestException, IOException, ConfigInvalidException {
if (input == null) {
input = new Input();
}
if (input.raw == null) {
throw new BadRequestException("SSH public key missing");
}
final RawInput rawKey = input.raw;
String sshPublicKey = new ByteSource() {
@Override
public InputStream openStream() throws IOException {
return rawKey.getInputStream();
}
}.asCharSource(UTF_8).read();
try {
AccountSshKey sshKey = authorizedKeys.addKey(user.getAccountId(), sshPublicKey);
try {
addKeyFactory.create(user, sshKey).send();
} catch (EmailException e) {
log.error("Cannot send SSH key added message to " + user.getAccount().getPreferredEmail(), e);
}
sshKeyCache.evict(user.getUserName());
return Response.<SshKeyInfo>created(GetSshKeys.newSshKeyInfo(sshKey));
} catch (InvalidSshKeyException e) {
throw new BadRequestException(e.getMessage());
}
}
use of com.google.common.io.ByteSource in project ddf by codice.
the class TestTemporaryFileBackedOutputStream method testWriteByteArrayOffLen.
@Test
public void testWriteByteArrayOffLen() throws IOException {
temporaryFileBackedOutputStream.write(TEST_BYTE_ARRAY, 0, 1);
ByteSource byteSource = temporaryFileBackedOutputStream.asByteSource();
assertThat(byteSource.read(), is(TEST_BYTE_ARRAY));
}
use of com.google.common.io.ByteSource in project ddf by codice.
the class TestTemporaryFileBackedOutputStream method testWriteByteArray.
@Test
public void testWriteByteArray() throws IOException {
temporaryFileBackedOutputStream.write(TEST_BYTE_ARRAY);
ByteSource byteSource = temporaryFileBackedOutputStream.asByteSource();
assertThat(byteSource.read(), is(TEST_BYTE_ARRAY));
}
use of com.google.common.io.ByteSource in project GeoGig by boundlessgeo.
the class EnvironmentBuilder method get.
/**
* @return
* @see com.google.inject.Provider#get()
*/
@Override
public synchronized Environment get() {
final Optional<URL> repoUrl = new ResolveGeogigDir(platform).call();
if (!repoUrl.isPresent() && absolutePath == null) {
throw new IllegalStateException("Can't find geogig repository home");
}
final File storeDirectory;
if (absolutePath != null) {
storeDirectory = absolutePath;
} else {
File currDir;
try {
currDir = new File(repoUrl.get().toURI());
} catch (URISyntaxException e) {
throw Throwables.propagate(e);
}
File dir = currDir;
for (String subdir : path) {
dir = new File(dir, subdir);
}
storeDirectory = dir;
}
if (!storeDirectory.exists() && !storeDirectory.mkdirs()) {
throw new IllegalStateException("Unable to create Environment directory: '" + storeDirectory.getAbsolutePath() + "'");
}
EnvironmentConfig envCfg;
if (this.forceConfig == null) {
File conf = new File(storeDirectory, "je.properties");
if (!conf.exists()) {
String resource = stagingDatabase ? "je.properties.staging" : "je.properties.objectdb";
ByteSource from = Resources.asByteSource((getClass().getResource(resource)));
try {
from.copyTo(Files.asByteSink(conf));
} catch (IOException e) {
Throwables.propagate(e);
}
}
// use the default settings
envCfg = new EnvironmentConfig();
envCfg.setAllowCreate(true);
envCfg.setCacheMode(CacheMode.MAKE_COLD);
envCfg.setLockTimeout(5, TimeUnit.SECONDS);
envCfg.setDurability(Durability.COMMIT_SYNC);
// envCfg.setReadOnly(readOnly);
} else {
envCfg = this.forceConfig;
}
// // envCfg.setSharedCache(true);
// //
// final boolean transactional = false;
// envCfg.setTransactional(transactional);
// envCfg.setCachePercent(75);// Use up to 50% of the heap size for the shared db cache
// envCfg.setConfigParam(EnvironmentConfig.LOG_FILE_MAX, String.valueOf(256 * 1024 * 1024));
// // check <http://www.oracle.com/technetwork/database/berkeleydb/je-faq-096044.html#35>
// envCfg.setConfigParam("je.evictor.lruOnly", "false");
// envCfg.setConfigParam("je.evictor.nodesPerScan", "100");
//
// envCfg.setConfigParam(EnvironmentConfig.CLEANER_MIN_UTILIZATION, "25");
// envCfg.setConfigParam(EnvironmentConfig.CHECKPOINTER_HIGH_PRIORITY, "true");
//
// envCfg.setConfigParam(EnvironmentConfig.CLEANER_THREADS, "4");
// // TODO: check whether we can set is locking to false
// envCfg.setConfigParam(EnvironmentConfig.ENV_IS_LOCKING, String.valueOf(transactional));
//
// envCfg.setConfigParam(EnvironmentConfig.ENV_RUN_CHECKPOINTER,
// String.valueOf(!transactional));
// envCfg.setConfigParam(EnvironmentConfig.ENV_RUN_CLEANER, String.valueOf(!transactional));
//
// // envCfg.setConfigParam(EnvironmentConfig.ENV_RUN_EVICTOR, "false");
Environment env;
try {
env = new Environment(storeDirectory, envCfg);
} catch (RuntimeException lockedEx) {
// lockedEx.printStackTrace();
if (readOnly) {
// this happens when trying to open the env in read only mode when its already open
// in read/write mode inside the same process. So we re-open it read-write but the
// database itself will be open read-only by JEObjectDatabase.
envCfg.setReadOnly(true);
env = new Environment(storeDirectory, envCfg);
} else {
throw lockedEx;
}
}
return env;
}
Aggregations