Search in sources :

Example 1 with ThreadContext

use of org.commonjava.cdi.util.weft.ThreadContext in project indy by Commonjava.

the class ContentIndexActions method clearMergedPath.

public void clearMergedPath(ArtifactStore originatingStore, Set<Group> groups, String path) {
    Logger logger = LoggerFactory.getLogger(getClass());
    logger.debug("Clearing merged path: {} from indexes of: {} (triggered by: {})", path, groups, originatingStore);
    StoreKey key = originatingStore.getKey();
    ThreadContext context = ThreadContext.getContext(true);
    context.put(ORIGIN_KEY, key);
    try {
        // the only time a group will have local storage of the path is when it has been merged
        // ...in which case we should try to delete it.
        indexManager.clearIndexedPathFrom(path, groups, null);
    } finally {
        context.remove(ORIGIN_KEY);
    }
}
Also used : ThreadContext(org.commonjava.cdi.util.weft.ThreadContext) Logger(org.slf4j.Logger) StoreKey(org.commonjava.indy.model.core.StoreKey)

Example 2 with ThreadContext

use of org.commonjava.cdi.util.weft.ThreadContext in project indy by Commonjava.

the class RelatePomStorageListener method onPomStorage.

public void onPomStorage(@Observes final FileStorageEvent event) {
    final Logger logger = LoggerFactory.getLogger(getClass());
    if (!config.isEnabled()) {
        logger.debug("Relate Add-on is not enabled.");
        return;
    }
    logger.debug("FILE STORAGE: {}", event);
    final TransferOperation op = event.getType();
    if (op != TransferOperation.UPLOAD && op != TransferOperation.DOWNLOAD) {
        logger.debug("Not a download/upload transfer operation. No .rel generation.");
        return;
    }
    ThreadContext threadContext = ThreadContext.getContext(false);
    if (threadContext != null) {
        Object obj = threadContext.get(REL_DIRECT_GENERATING);
        if (obj != null) {
            logger.debug("Direct .rel generation in progress. Ignore POM storage event. ");
            return;
        }
    }
    final Transfer transfer = event.getTransfer();
    executor.execute(() -> {
        relateGenerationManager.generateRelationshipFile(transfer, op);
    });
}
Also used : ThreadContext(org.commonjava.cdi.util.weft.ThreadContext) Transfer(org.commonjava.maven.galley.model.Transfer) Logger(org.slf4j.Logger) TransferOperation(org.commonjava.maven.galley.model.TransferOperation)

Example 3 with ThreadContext

use of org.commonjava.cdi.util.weft.ThreadContext in project indy by Commonjava.

the class ResourceManagementFilter method doFilter.

@Override
public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException {
    String name = Thread.currentThread().getName();
    String clientAddr = request.getRemoteAddr();
    final HttpServletRequest hsr = (HttpServletRequest) request;
    String tn = hsr.getMethod() + " " + hsr.getPathInfo() + " (" + System.currentTimeMillis() + "." + System.nanoTime() + ")";
    String qs = hsr.getQueryString();
    try {
        ThreadContext.clearContext();
        ThreadContext threadContext = ThreadContext.getContext(true);
        threadContext.put(ORIGINAL_THREAD_NAME, name);
        threadContext.put(HTTP_REQUEST, hsr);
        threadContext.put(METHOD_PATH_TIME, tn);
        putRequestIDs(hsr, threadContext);
        logger.info("START request: {} (from: {})", tn, clientAddr);
        Thread.currentThread().setName(tn);
        restLogger.info("START {}{} (from: {})", hsr.getRequestURL(), qs == null ? "" : "?" + qs, clientAddr);
        chain.doFilter(request, response);
    } finally {
        logger.debug("Cleaning up resources for thread: {}", Thread.currentThread().getName());
        try {
            cacheProvider.cleanupCurrentThread();
        } catch (Exception e) {
            logger.error("Failed to cleanup resources", e);
        }
        restLogger.info("END {}{} (from: {})", hsr.getRequestURL(), qs == null ? "" : "?" + qs, clientAddr);
        Thread.currentThread().setName(name);
        ThreadContext.clearContext();
        logger.info("END request: {} (from: {})", tn, clientAddr);
        MDC.clear();
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ThreadContext(org.commonjava.cdi.util.weft.ThreadContext) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 4 with ThreadContext

use of org.commonjava.cdi.util.weft.ThreadContext in project galley by Commonjava.

the class FastLocalCacheProvider method lockByISPN.

private void lockByISPN(final ConcreteResource resource) throws SystemException, NotSupportedException, IOException {
    //FIXME: This whole method is not thread-safe, especially for the lock state of the path, so the caller needs to take care
    // We need to think about the way of the ISPN lock and wait. If directly
    // use the nfsOwnerCache.lock but not consider if the lock has been acquired by another
    // thread, the ISPN lock will fail with a RuntimeException. So we need to let the
    // thread wait for the ISPN lock until it's released by the thread holds it. It's
    // like "tryLock" and "wait" of a thread lock.
    final String path = getKeyForResource(resource);
    // thread, will not wait.
    if (!isCurrentThread()) {
        waitForISPNLock(resource, nfsOwnerCache.isLocked(path));
    }
    if (!nfsOwnerCache.isLocked(path)) {
        nfsOwnerCache.beginTransaction();
        nfsOwnerCache.lock(path);
        // This thread holder is used to add some "re-entrant" like function for the ISPN transaction lock. The ISPN lock is
        // used for ISPN transaction but not for thread level with re-entrant, that means if we want to own this lock in one
        // transaction for more than twice in single thread, it will block this thread. So we need this thread holder to by-pass
        // the ISPN lock waiting when thread not changed.
        final ThreadContext threadHolder = ThreadContext.getContext(true);
        threadHolder.put(CURRENT_THREAD_ID, Thread.currentThread().getId());
    }
}
Also used : ThreadContext(org.commonjava.cdi.util.weft.ThreadContext)

Example 5 with ThreadContext

use of org.commonjava.cdi.util.weft.ThreadContext in project galley by Commonjava.

the class FastLocalCacheProvider method cleanupCurrentThread.

@Override
public void cleanupCurrentThread() {
    plCacheProvider.cleanupCurrentThread();
    ThreadContext streamHolder = ThreadContext.getContext(false);
    if (streamHolder != null) {
        final String threadId = String.valueOf(Thread.currentThread().getId());
        final Set<WeakReference<OutputStream>> streams = (Set<WeakReference<OutputStream>>) streamHolder.get(FAST_LOCAL_STREAMS);
        if (streams != null && !streams.isEmpty()) {
            Iterator<WeakReference<OutputStream>> iter = streams.iterator();
            while (iter.hasNext()) {
                WeakReference<OutputStream> streamRef = iter.next();
                IOUtils.closeQuietly(streamRef.get());
                iter.remove();
            }
        }
        streamHolder.remove(threadId);
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) WeakReference(java.lang.ref.WeakReference) ThreadContext(org.commonjava.cdi.util.weft.ThreadContext)

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