Search in sources :

Example 6 with ThreadContext

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;
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) ThreadContext(org.commonjava.cdi.util.weft.ThreadContext) IOException(java.io.IOException) SystemException(javax.transaction.SystemException) FileOutputStream(java.io.FileOutputStream) WeakReference(java.lang.ref.WeakReference) NotSupportedException(javax.transaction.NotSupportedException) File(java.io.File)

Example 7 with ThreadContext

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;
}
Also used : Transfer(org.commonjava.maven.galley.model.Transfer) ThreadContext(org.commonjava.cdi.util.weft.ThreadContext) IOException(java.io.IOException) Logger(org.slf4j.Logger)

Aggregations

ThreadContext (org.commonjava.cdi.util.weft.ThreadContext)7 IOException (java.io.IOException)3 Logger (org.slf4j.Logger)3 FileOutputStream (java.io.FileOutputStream)2 OutputStream (java.io.OutputStream)2 WeakReference (java.lang.ref.WeakReference)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 Transfer (org.commonjava.maven.galley.model.Transfer)2 File (java.io.File)1 ServletException (javax.servlet.ServletException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 NotSupportedException (javax.transaction.NotSupportedException)1 SystemException (javax.transaction.SystemException)1 StoreKey (org.commonjava.indy.model.core.StoreKey)1 TransferOperation (org.commonjava.maven.galley.model.TransferOperation)1