use of org.commonjava.cdi.util.weft.ThreadContext in project galley by Commonjava.
the class FastLocalCacheProvider method openOutputStream.
/**
* For file writing, will wrapping two output streams to caller - one for local cache file, another for nfs file -,
* and the caller can write to these two streams in the meantime. <br />
* For the local part, because it uses {@link org.commonjava.maven.galley.cache.partyline.PartyLineCacheProvider} as
* i/o provider, this supports the R/W on the same resource in the meantime. For details, please see
* {@link org.commonjava.maven.galley.cache.partyline.PartyLineCacheProvider}.
*
* @param resource - the resource will be read
* @return - the output stream for further writing
* @throws IOException
*/
@Override
public OutputStream openOutputStream(ConcreteResource resource) throws IOException {
OutputStream dualOut;
final String nodeIp = getCurrentNodeIp();
final String pathKey = getKeyForResource(resource);
final File nfsFile = getNFSDetachedFile(resource);
synchronized (getTransfer(resource)) {
try {
lockByISPN(resource);
nfsOwnerCache.put(pathKey, nodeIp);
logger.debug("Start to get output stream from local cache through partyline to do join stream");
final OutputStream localOut = plCacheProvider.openOutputStream(resource);
logger.debug("The output stream from local cache through partyline is got successfully");
if (!nfsFile.exists() && !nfsFile.isDirectory()) {
try {
if (!nfsFile.getParentFile().exists()) {
nfsFile.getParentFile().mkdirs();
}
nfsFile.createNewFile();
} catch (IOException e) {
logger.error("[galley] New nfs file created not properly.", e);
throw e;
}
}
final OutputStream nfsOutputStream = new FileOutputStream(nfsFile);
logger.debug("The output stream from NFS is got successfully");
// will wrap the cache manager in stream wrapper, and let it do tx commit in stream close to make sure
// the two streams writing's consistency.
dualOut = new DualOutputStreamsWrapper(localOut, nfsOutputStream, nfsOwnerCache, pathKey, resource);
if (nfsOwnerCache.getLockOwner(pathKey) != null) {
logger.trace("ISPN locker for key {} with resource {} is {}", pathKey, resource, nfsOwnerCache.getLockOwner(pathKey));
}
ThreadContext streamHolder = ThreadContext.getContext(true);
Set<WeakReference<OutputStream>> streams = (Set<WeakReference<OutputStream>>) streamHolder.get(FAST_LOCAL_STREAMS);
if (streams == null) {
streams = new HashSet<>(10);
}
streams.add(new WeakReference<>(dualOut));
streamHolder.put(FAST_LOCAL_STREAMS, streams);
} catch (NotSupportedException | SystemException e) {
logger.error("[galley] Transaction error for nfs cache during file writing.", e);
throw new IllegalStateException(String.format("[galley] Output stream for resource %s open failed.", resource.toString()), e);
}
logger.debug("The dual output stream wrapped and returned successfully");
return dualOut;
}
}
use of org.commonjava.cdi.util.weft.ThreadContext in project indy by Commonjava.
the class RelateContentGenerator method generateFileContent.
@Override
public Transfer generateFileContent(final ArtifactStore store, final String path, final EventMetadata eventMetadata) throws IndyWorkflowException {
Logger logger = LoggerFactory.getLogger(getClass());
if (!config.isEnabled()) {
logger.debug("Relate Add-on is not enabled.");
return null;
}
logger.info("Generate .rel for: {}/{}", store.getKey(), path);
if (!canProcess(path)) {
logger.debug("Not a .rel request");
return null;
}
logger.debug("Check for POM matching: {}", path);
String pomPath = path.substring(0, path.length() - REL_SUFFIX.length());
if (!pomPath.endsWith(POM_SUFFIX)) {
logger.debug("Not a POM {}", pomPath);
return null;
}
Transfer pomTransfer = fileManager.getTransfer(store, path);
if (!exists(pomTransfer)) {
ThreadContext threadContext = ThreadContext.getContext(true);
threadContext.put(REL_DIRECT_GENERATING, true);
pomTransfer = downloadManager.retrieve(store, pomPath, eventMetadata);
if (!exists(pomTransfer)) {
logger.debug("POM not exists for request {}", pomPath);
return null;
}
}
Transfer result = relateGenerationManager.generateRelationshipFile(pomTransfer, TransferOperation.DOWNLOAD);
if (result != null) {
Transfer meta = result.getSiblingMeta(HttpExchangeMetadata.FILE_EXTENSION);
try {
// delete *.rel.http-metadata.json, which is created unnecessarily during the unsuccessful get request
meta.delete();
// TODO: If mark .rel as generated in SpecialPathManager, should be able to avoid creating the meta file in the first place...
} catch (IOException e) {
logger.debug("Delete meta {} failed", meta, e);
}
}
// Finally, pass the Transfer back.
return result;
}
Aggregations