Java Code Examples for org.slf4j.MDC#MDCCloseable

The following examples show how to use org.slf4j.MDC#MDCCloseable . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WeblichtChainServiceImpl.java    From inception with Apache License 2.0 6 votes vote down vote up
@Override
@Transactional
public void deleteChain(WeblichtChain aChain) throws IOException
{
    entityManager.remove(
            entityManager.contains(aChain) ? aChain : entityManager.merge(aChain));
    
    File gaz = getChainFile(aChain);
    if (gaz.exists()) {
        gaz.delete();
    }
    
    try (MDC.MDCCloseable closable = MDC.putCloseable(Logging.KEY_PROJECT_ID,
            String.valueOf(aChain.getRecommender().getProject().getId()))) {
        log.info("Removed chain [{}] from recommender [{}]({}) in project [{}]({})",
                aChain.getName(), aChain.getRecommender().getName(),
                aChain.getRecommender().getId(),
                aChain.getRecommender().getProject().getName(),
                aChain.getRecommender().getProject().getId());
    }
}
 
Example 2
Source File: AnnotationSchemaServiceImpl.java    From webanno with Apache License 2.0 6 votes vote down vote up
@Override
@Transactional
public void createLayer(AnnotationLayer aLayer)
{
    if (isNull(aLayer.getId())) {
        entityManager.persist(aLayer);
    }
    else {
        entityManager.merge(aLayer);
    }
    
    try (MDC.MDCCloseable closable = MDC.putCloseable(Logging.KEY_PROJECT_ID,
            String.valueOf(aLayer.getProject().getId()))) {
        Project project = aLayer.getProject();
        log.info("Created layer [{}]({}) in project [{}]({})", aLayer.getName(),
                aLayer.getId(), project.getName(), project.getId());
    }
}
 
Example 3
Source File: AnnotationSchemaServiceImpl.java    From webanno with Apache License 2.0 6 votes vote down vote up
@Override
@Transactional
public void createTagSet(TagSet aTagSet)
{
    if (isNull(aTagSet.getId())) {
        entityManager.persist(aTagSet);
    }
    else {
        entityManager.merge(aTagSet);
    }
    
    try (MDC.MDCCloseable closable = MDC.putCloseable(Logging.KEY_PROJECT_ID,
            String.valueOf(aTagSet.getProject().getId()))) {
        Project project = aTagSet.getProject();
        log.info("Created tagset [{}]({}) in project [{}]({})", aTagSet.getName(),
                aTagSet.getId(), project.getName(), project.getId());
    }
}
 
Example 4
Source File: AnnotationSchemaServiceImpl.java    From webanno with Apache License 2.0 6 votes vote down vote up
@Override
public void upgradeCas(CAS aCas, SourceDocument aSourceDocument, String aUser)
    throws UIMAException, IOException
{
    upgradeCas(aCas, aSourceDocument.getProject());
    
    try (MDC.MDCCloseable closable = MDC.putCloseable(
            Logging.KEY_PROJECT_ID,
            String.valueOf(aSourceDocument.getProject().getId()))) {
        Project project = aSourceDocument.getProject();
        log.info(
                "Upgraded CAS of user [{}] for "
                        + "document [{}]({}) in project [{}]({})",
                aUser, aSourceDocument.getName(), aSourceDocument.getId(), project.getName(),
                project.getId());
    }
}
 
Example 5
Source File: ConstraintsServiceImpl.java    From webanno with Apache License 2.0 6 votes vote down vote up
/**
 * Provides exporting constraints as a file.
 */
@Override
public File exportConstraintAsFile(ConstraintSet aSet)
{
    String constraintRulesPath = repositoryProperties.getPath().getAbsolutePath() + "/"
            + PROJECT_FOLDER + "/" + aSet.getProject().getId() + "/"
            + ConstraintsService.CONSTRAINTS + "/";
    String filename = aSet.getId() + ".txt";
    File constraintsFile = new File(constraintRulesPath, filename);
    if (constraintsFile.exists()) {
        try (MDC.MDCCloseable closable = MDC.putCloseable(Logging.KEY_PROJECT_ID,
                String.valueOf(aSet.getProject().getId()))) {
            log.info("Exported constraints set [{}] from project [{}]({})",
                    aSet.getName(), aSet.getProject().getName(), aSet.getProject().getId());
        }
        return constraintsFile;
    }
    else {
        try (MDC.MDCCloseable closable = MDC.putCloseable(Logging.KEY_PROJECT_ID,
                String.valueOf(aSet.getProject().getId()))) {
            log.info("Unable to read constraints set file [{}] in project [{}]({})",
                    filename, aSet.getProject().getName(), aSet.getProject().getId());
        }
        return null;
    }
}
 
Example 6
Source File: ConstraintsServiceImpl.java    From webanno with Apache License 2.0 6 votes vote down vote up
@Override
public void writeConstraintSet(ConstraintSet aSet, InputStream aContent)
    throws IOException
{
    String constraintRulesPath = repositoryProperties.getPath().getAbsolutePath() + "/"
            + PROJECT_FOLDER + "/" + aSet.getProject().getId() + "/"
            + ConstraintsService.CONSTRAINTS + "/";
    String filename = aSet.getId() + ".txt";
    FileUtils.forceMkdir(new File(constraintRulesPath));
    FileUtils.copyInputStreamToFile(aContent, new File(constraintRulesPath, filename));

    
    try (MDC.MDCCloseable closable = MDC.putCloseable(Logging.KEY_PROJECT_ID,
            String.valueOf(aSet.getProject().getId()))) {
        log.info("Saved constraints set [{}] in project [{}]({})",
                aSet.getName(), aSet.getProject().getName(), aSet.getProject().getId());
    }
}
 
Example 7
Source File: ConstraintsServiceImpl.java    From webanno with Apache License 2.0 6 votes vote down vote up
@Override
public String readConstrainSet(ConstraintSet aSet)
    throws IOException
{
    String constraintRulesPath = repositoryProperties.getPath().getAbsolutePath() + "/"
            + PROJECT_FOLDER + "/" + aSet.getProject().getId() + "/"
            + ConstraintsService.CONSTRAINTS + "/";
    String filename = aSet.getId() + ".txt";
    
    String data;
    try (BOMInputStream is = new BOMInputStream(
            new FileInputStream(new File(constraintRulesPath, filename)))) {
        data = IOUtils.toString(is, "UTF-8");
    }

    try (MDC.MDCCloseable closable = MDC.putCloseable(Logging.KEY_PROJECT_ID,
            String.valueOf(aSet.getProject().getId()))) {
        log.info("Read constraints set [{}] in project [{}]({})",
                aSet.getName(), aSet.getProject().getName(), aSet.getProject().getId());
    }
    
    return data;
}
 
Example 8
Source File: CurationDocumentServiceImpl.java    From webanno with Apache License 2.0 6 votes vote down vote up
@Override
public void removeCurationDocumentContent(SourceDocument aSourceDocument, String aUsername)
    throws IOException
{
    if (new File(casStorageService.getAnnotationFolder(aSourceDocument),
            WebAnnoConst.CURATION_USER + ".ser").exists()) {
        FileUtils.forceDelete(new File(casStorageService.getAnnotationFolder(aSourceDocument),
                WebAnnoConst.CURATION_USER + ".ser"));

        try (MDC.MDCCloseable closable = MDC.putCloseable(Logging.KEY_PROJECT_ID,
                String.valueOf(aSourceDocument.getProject().getId()))) {
            Project project = aSourceDocument.getProject();
            log.info("Removed curation of source document [{}]({}) from project [{}]({})",
                    aSourceDocument.getName(), aSourceDocument.getId(), project.getName(),
                    project.getId());
        }
    }
}
 
Example 9
Source File: DocumentServiceImpl.java    From webanno with Apache License 2.0 6 votes vote down vote up
@Override
@Transactional
public void createAnnotationDocument(AnnotationDocument aAnnotationDocument)
{
    Validate.notNull(aAnnotationDocument, "Annotation document must be specified");
    
    if (isNull(aAnnotationDocument.getId())) {
        entityManager.persist(aAnnotationDocument);
        
        try (MDC.MDCCloseable closable = MDC.putCloseable(Logging.KEY_PROJECT_ID,
                String.valueOf(aAnnotationDocument.getProject().getId()))) {
            log.info(
                    "Created annotation document [{}] for user [{}] for source document "
                    + "[{}]({}) in project [{}]({})",
                    aAnnotationDocument.getId(), aAnnotationDocument.getUser(), 
                    aAnnotationDocument.getDocument().getName(),
                    aAnnotationDocument.getDocument().getId(),
                    aAnnotationDocument.getProject().getName(),
                    aAnnotationDocument.getProject().getId());
        }
    }
    else {
        entityManager.merge(aAnnotationDocument);
    }
}
 
Example 10
Source File: GazeteerServiceImpl.java    From inception with Apache License 2.0 6 votes vote down vote up
@Override
@Transactional
public void deleteGazeteers(Gazeteer aGazeteer) throws IOException
{
    entityManager.remove(
            entityManager.contains(aGazeteer) ? aGazeteer : entityManager.merge(aGazeteer));
    
    File gaz = getGazeteerFile(aGazeteer);
    if (gaz.exists()) {
        gaz.delete();
    }
    
    try (MDC.MDCCloseable closable = MDC.putCloseable(Logging.KEY_PROJECT_ID,
            String.valueOf(aGazeteer.getRecommender().getProject().getId()))) {
        log.info("Removed gazeteer [{}] from recommender [{}]({}) in project [{}]({})",
                aGazeteer.getName(), aGazeteer.getRecommender().getName(),
                aGazeteer.getRecommender().getId(),
                aGazeteer.getRecommender().getProject().getName(),
                aGazeteer.getRecommender().getProject().getId());
    }
}
 
Example 11
Source File: ProjectServiceImpl.java    From webanno with Apache License 2.0 5 votes vote down vote up
@Override
@Transactional
public void removeProject(Project aProject)
    throws IOException
{
    long start = System.currentTimeMillis();
    
    // remove metadata from DB
    Project project = aProject;
    if (!entityManager.contains(project)) {
        project = entityManager.merge(project);
    }
    
    applicationEventPublisher.publishEvent(new BeforeProjectRemovedEvent(this, aProject));

    for (ProjectPermission permissions : getProjectPermissions(aProject)) {
        entityManager.remove(permissions);
    }
            
    entityManager.remove(project);
    
    // remove the project directory from the file system
    String path = repositoryProperties.getPath().getAbsolutePath() + "/" + PROJECT_FOLDER + "/"
            + aProject.getId();
    try {
        FastIOUtils.delete(new File(path));
    }
    catch (FileNotFoundException e) {
        try (MDC.MDCCloseable closable = MDC.putCloseable(Logging.KEY_PROJECT_ID,
                String.valueOf(aProject.getId()))) {
            log.info("Project directory to be deleted was not found: [{}]. Ignoring.", path);
        }
    }

    try (MDC.MDCCloseable closable = MDC.putCloseable(Logging.KEY_PROJECT_ID,
            String.valueOf(aProject.getId()))) {
        log.info("Removed project [{}]({}) ({})", aProject.getName(), aProject.getId(),
                formatDurationWords(System.currentTimeMillis() - start, true, true));
    }
}
 
Example 12
Source File: DocumentServiceImpl.java    From webanno with Apache License 2.0 5 votes vote down vote up
@Override
@Transactional
public void removeSourceDocument(SourceDocument aDocument)
    throws IOException
{
    Validate.notNull(aDocument, "Source document must be specified");
    
    // BeforeDocumentRemovedEvent is triggered first, since methods that rely 
    // on it might need to have access to the associated annotation documents 
    applicationEventPublisher.publishEvent(new BeforeDocumentRemovedEvent(this, aDocument));
    
    for (AnnotationDocument annotationDocument : listAllAnnotationDocuments(aDocument)) {
        removeAnnotationDocument(annotationDocument);
    }
    
    entityManager.remove(
            entityManager.contains(aDocument) ? aDocument : entityManager.merge(aDocument));

    String path = repositoryProperties.getPath().getAbsolutePath() + "/" + PROJECT_FOLDER + "/"
            + aDocument.getProject().getId() + "/" + DOCUMENT_FOLDER + "/" + aDocument.getId();
    
    // remove from file both source and related annotation file
    if (new File(path).exists()) {
        FileUtils.forceDelete(new File(path));
    }
    
    try (MDC.MDCCloseable closable = MDC.putCloseable(Logging.KEY_PROJECT_ID,
            String.valueOf(aDocument.getProject().getId()))) {
        Project project = aDocument.getProject();
        log.info("Removed source document [{}]({}) from project [{}]({})", aDocument.getName(),
                aDocument.getId(), project.getName(), project.getId());
    }
}
 
Example 13
Source File: ProjectServiceImpl.java    From webanno with Apache License 2.0 5 votes vote down vote up
@Override
@Transactional
public void removeProjectPermission(ProjectPermission aPermission)
{
    entityManager.remove(aPermission);
    
    try (MDC.MDCCloseable closable = MDC.putCloseable(Logging.KEY_PROJECT_ID,
            String.valueOf(aPermission.getProject().getId()))) {
        log.info("Removed permission [{}] for user [{}] on project [{}]({})",
                aPermission.getLevel(), aPermission.getUser(),
                aPermission.getProject().getName(), aPermission.getProject().getId());
    }
}
 
Example 14
Source File: AnnotationSchemaServiceImpl.java    From webanno with Apache License 2.0 5 votes vote down vote up
@Override
public void upgradeCas(CAS aCas, SourceDocument aSourceDocument, String aUser,
        CasUpgradeMode aMode)
    throws UIMAException, IOException
{
    switch (aMode) {
    case NO_CAS_UPGRADE:
        return;
    case AUTO_CAS_UPGRADE: {
        boolean upgraded = upgradeCasIfRequired(aCas, aSourceDocument);
        if (!upgraded) {
            try (MDC.MDCCloseable closable = MDC.putCloseable(Logging.KEY_PROJECT_ID,
                    String.valueOf(aSourceDocument.getProject().getId()))) {
                log.debug(
                        "CAS of user [{}] for document [{}]({}) in project [{}]({}) is already "
                                + "compatible with project type system - skipping upgrade",
                        aUser, aSourceDocument.getName(), aSourceDocument.getId(),
                        aSourceDocument.getProject().getName(),
                        aSourceDocument.getProject().getId());
            }
        }
        return;
    }
    case FORCE_CAS_UPGRADE:
        upgradeCas(aCas, aSourceDocument, aUser);
        return;
    }
}
 
Example 15
Source File: ConstraintsServiceImpl.java    From webanno with Apache License 2.0 5 votes vote down vote up
@Override
@Transactional
public void removeConstraintSet(ConstraintSet aSet)
{
    entityManager.remove(entityManager.merge(aSet));
    
    try (MDC.MDCCloseable closable = MDC.putCloseable(Logging.KEY_PROJECT_ID,
            String.valueOf(aSet.getProject().getId()))) {
        log.info("Removed constraints set [{}] in project [{}]({})",
                aSet.getName(), aSet.getProject().getName(), aSet.getProject().getId());
    }
}
 
Example 16
Source File: MDCUtil.java    From styx with Apache License 2.0 5 votes vote down vote up
public static MDC.MDCCloseable safePutCloseable(String key, String value) {
  try {
    return MDC.putCloseable(key, value);
  } catch (Exception e) {
    log.error("Failed to put MDC", e);
    // Returning null here is ok as try-with-resources ignores null resources.
    return null;
  }
}
 
Example 17
Source File: OpenIdConnectServlet.java    From vipps-developers with MIT License 5 votes vote down vote up
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    String[] pathParts;
    if(req.getPathInfo() != null){
        pathParts = req.getPathInfo().split("/");
    }else{
        pathParts = req.getServletPath().split("/");
    }


    if (pathParts.length < 2) {
        resp.sendError(404);
        return;
    }

    try (MDC.MDCCloseable ignored = MDC.putCloseable("provider", req.getServletPath())) {
        String action = pathParts[1];
        if (action.equals("authenticate")) {
            authenticate(req, resp);
        } else if (action.equals("callback")) {
            oauth2callback(req, resp);
        } else if (action.equals("token")) {
            getToken(req, resp);
        } else if (action.equals("session")) {
            setupSession(req, resp);
        }else if (action.equals("logout")) {
            logoutSession(req, resp);
        } else {
            logger.warn("Unknown request {}", req.getServletPath() + req.getPathInfo() + "?" + req.getQueryString());
            resp.sendError(404);
        }
    }
}
 
Example 18
Source File: GazeteerServiceImpl.java    From inception with Apache License 2.0 5 votes vote down vote up
@Override
@Transactional
public void createOrUpdateGazeteer(Gazeteer aGazeteer)
{
    if (aGazeteer.getId() == null) {
        entityManager.persist(aGazeteer);
        
        try (MDC.MDCCloseable closable = MDC.putCloseable(Logging.KEY_PROJECT_ID,
                String.valueOf(aGazeteer.getRecommender().getProject().getId()))) {
            log.info("Created gazeteer [{}] for recommender [{}]({}) in project [{}]({})",
                    aGazeteer.getName(), aGazeteer.getRecommender().getName(),
                    aGazeteer.getRecommender().getId(),
                    aGazeteer.getRecommender().getProject().getName(),
                    aGazeteer.getRecommender().getProject().getId());
        }
    }
    else {
        entityManager.merge(aGazeteer);
        
        try (MDC.MDCCloseable closable = MDC.putCloseable(Logging.KEY_PROJECT_ID,
                String.valueOf(aGazeteer.getRecommender().getProject().getId()))) {
            log.info("Updated gazeteer [{}] for recommender [{}]({}) in project [{}]({})",
                    aGazeteer.getName(), aGazeteer.getRecommender().getName(),
                    aGazeteer.getRecommender().getId(),
                    aGazeteer.getRecommender().getProject().getName(),
                    aGazeteer.getRecommender().getProject().getId());
        }
    }
}
 
Example 19
Source File: DocumentServiceImpl.java    From webanno with Apache License 2.0 4 votes vote down vote up
@EventListener
@Transactional
public void beforeProjectRemove(BeforeProjectRemovedEvent aEvent)
    throws IOException
{
    Project project = aEvent.getProject();
    
    Validate.notNull(project, "Project must be specified");

    // Since the project is being deleted anyway, we don't bother sending around
    // BeforeDocumentRemovedEvent anymore. If we did, we would likely trigger a
    // a lot of CPU usage and DB bashing (e.g. for re-calculating the project state
    // (ProjectServiceImpl.recalculateProjectState).
    // List<SourceDocument> sourceDocuments = listSourceDocuments(project);
    // for (SourceDocument doc : sourceDocuments) {
    // applicationEventPublisher.publishEvent(new BeforeDocumentRemovedEvent(this, doc));
    // }

    // There should be a cascade-on-delete for annotation documents when the respective
    // source document is deleted, but that is not there at the moment...
    String deleteAnnotationDocumentsQuery = String.join("\n",
            "DELETE FROM AnnotationDocument",
            "WHERE project = :project");
    entityManager.createQuery(deleteAnnotationDocumentsQuery)
            .setParameter("project", project)
            .executeUpdate();

    // Delete all the source documents for the given project
    String deleteSourceDocumentsQuery = String.join("\n",
            "DELETE FROM SourceDocument",
            "WHERE project = :project");
    entityManager.createQuery(deleteSourceDocumentsQuery)
            .setParameter("project", project)
            .executeUpdate();
    
    // Delete all the source documents files for the given project
    File docFolder = new File(repositoryProperties.getPath().getAbsolutePath() + "/"
            + PROJECT_FOLDER + "/" + project.getId() + "/" + DOCUMENT_FOLDER + "/");
    if (docFolder.exists()) {
        FastIOUtils.delete(docFolder);
    }
    
    try (MDC.MDCCloseable closable = MDC.putCloseable(Logging.KEY_PROJECT_ID,
            String.valueOf(project.getId()))) {
        log.info("Removed all documents from project [{}]({}) being deleted", project.getName(),
                project.getId());
    }
}
 
Example 20
Source File: ImportExportServiceImpl.java    From webanno with Apache License 2.0 4 votes vote down vote up
@Override
@Transactional
public File exportAnnotationDocument(SourceDocument aDocument, String aUser,
        FormatSupport aFormat, String aFileName, Mode aMode, boolean aStripExtension,
        Map<Pair<Project, String>, Object> aBulkOperationContext)
    throws UIMAException, IOException, ClassNotFoundException
{
    Map<Pair<Project, String>, Object> bulkOperationContext = aBulkOperationContext;
    if (bulkOperationContext == null) {
        bulkOperationContext = new HashMap<>();
    }
    
    String username;
    // for Correction, it will export the corrected document (of the logged in user)
    // (CORRECTION_USER.ser is the automated result displayed for the user to correct it, not
    // the final result) for automation, it will export either the corrected document
    // (Annotated) or the automated document
    if (aMode.equals(ANNOTATION) || aMode.equals(AUTOMATION) || aMode.equals(CORRECTION)) {
        username = aUser;
    }
    // The merge result will be exported
    else {
        username = CURATION_USER;
    }

    // Read file
    File exportFile;
    try (CasStorageSession session = CasStorageSession.openNested()) {
        CAS cas = casStorageService.readCas(aDocument, username);
        exportFile = exportCasToFile(cas, aDocument, aFileName, aFormat, aStripExtension,
                aBulkOperationContext);
    }

    Project project = aDocument.getProject();
    try (MDC.MDCCloseable closable = MDC.putCloseable(KEY_PROJECT_ID,
            String.valueOf(project.getId()))) {
        log.info("Exported annotations [{}]({}) for user [{}] from project [{}]({}) "
                + "using format [{}]", aDocument.getName(), aDocument.getId(), aUser, 
                project.getName(), project.getId(), aFormat.getId());
    }

    return exportFile;
}