Search in sources :

Example 46 with MCRException

use of org.mycore.common.MCRException in project mycore by MyCoRe-Org.

the class MCRLayoutService method doLayout.

public void doLayout(HttpServletRequest req, HttpServletResponse res, MCRContent source) throws IOException, TransformerException, SAXException {
    if (res.isCommitted()) {
        LOGGER.warn("Response already committed: {}:{}", res.getStatus(), res.getContentType());
        return;
    }
    String docType = source.getDocType();
    try {
        MCRParameterCollector parameter = new MCRParameterCollector(req);
        MCRContentTransformer transformer = getContentTransformer(docType, parameter);
        String filename = getFileName(req, parameter);
        transform(res, transformer, source, parameter, filename);
    } catch (IOException | TransformerException | SAXException ex) {
        throw ex;
    } catch (MCRException ex) {
        // generating an error page when there is an error in the stylesheet
        if (!"mcr_error".equals(docType)) {
            throw ex;
        }
        String msg = "Error while generating error page!";
        LOGGER.warn(msg, ex);
        res.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg);
    } catch (Exception e) {
        throw new MCRException(e);
    }
}
Also used : MCRParameterCollector(org.mycore.common.xsl.MCRParameterCollector) MCRException(org.mycore.common.MCRException) MCRContentTransformer(org.mycore.common.content.transformer.MCRContentTransformer) IOException(java.io.IOException) TransformerException(javax.xml.transform.TransformerException) TransformerException(javax.xml.transform.TransformerException) IOException(java.io.IOException) MCRException(org.mycore.common.MCRException) SAXException(org.xml.sax.SAXException) SAXException(org.xml.sax.SAXException)

Example 47 with MCRException

use of org.mycore.common.MCRException in project mycore by MyCoRe-Org.

the class MCREventManager method handleEvent.

/**
 * This method is called by the component that created the event and acts as
 * a multiplexer that invokes all registered event handlers doHandleEvent
 * methods. If something goes wrong and an exception is caught, the
 * undoHandleEvent methods of all event handlers that are at a position
 * BEFORE the failed one, will be called in reversed order. The parameter
 * direction controls the order in which the event handlers are called.
 *
 * @see MCREventHandler#doHandleEvent
 * @see MCREventHandlerBase
 *
 * @param evt
 *            the event that happened
 * @param direction
 *            the order in which the event handlers are called
 */
public void handleEvent(MCREvent evt, boolean direction) {
    List<MCREventHandler> list = handlers.get(evt.getObjectType());
    if (list == null) {
        return;
    }
    int first = direction ? 0 : list.size() - 1;
    int last = direction ? list.size() - 1 : 0;
    int step = direction ? 1 : -1;
    int undoPos = first;
    Exception handleEventExceptionCaught = null;
    for (int i = first; i != last + step; i += step) {
        MCREventHandler eh = list.get(i);
        logger.debug("EventManager {} {} calling handler {}", evt.getObjectType(), evt.getEventType(), eh.getClass().getName());
        try {
            eh.doHandleEvent(evt);
        } catch (Exception ex) {
            handleEventExceptionCaught = ex;
            logger.error("Exception caught while calling event handler", ex);
            logger.error("Trying rollback by calling undo method of event handlers");
            undoPos = i;
            break;
        }
    }
    // Rollback by calling undo of successfull handlers
    for (int i = undoPos - step; i != first - step; i -= step) {
        MCREventHandler eh = list.get(i);
        logger.debug("EventManager {} {} calling undo of handler {}", evt.getObjectType(), evt.getEventType(), eh.getClass().getName());
        try {
            eh.undoHandleEvent(evt);
        } catch (Exception ex) {
            logger.error("Exception caught while calling undo of event handler", ex);
        }
    }
    if (handleEventExceptionCaught != null) {
        String msg = "Exception caught in EventHandler, rollback by calling undo of successfull handlers done.";
        throw new MCRException(msg, handleEventExceptionCaught);
    }
}
Also used : MCRException(org.mycore.common.MCRException) MCRException(org.mycore.common.MCRException) MCRConfigurationException(org.mycore.common.config.MCRConfigurationException)

Example 48 with MCRException

use of org.mycore.common.MCRException in project mycore by MyCoRe-Org.

the class MCRCategoryDAOImpl method buildCategoryFromPrefetchedList.

private static MCRCategoryImpl buildCategoryFromPrefetchedList(List<MCRCategoryDTO> list, MCRCategoryID returnID) {
    LOGGER.debug(() -> "using prefetched list: " + list);
    MCRCategoryImpl predecessor = null;
    for (MCRCategoryDTO entry : list) {
        predecessor = entry.merge(predecessor);
    }
    return MCRStreamUtils.flatten(predecessor.getRoot(), MCRCategory::getChildren, Collection::parallelStream).filter(c -> c.getId().equals(returnID)).findFirst().map(MCRCategoryImpl.class::cast).orElseThrow(() -> new MCRException("Could not find " + returnID + " in database result."));
}
Also used : NoResultException(javax.persistence.NoResultException) HashMap(java.util.HashMap) FlushModeType(javax.persistence.FlushModeType) Function(java.util.function.Function) TypedQuery(javax.persistence.TypedQuery) MCRException(org.mycore.common.MCRException) ArrayList(java.util.ArrayList) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) MCRCategoryDAO(org.mycore.datamodel.classifications2.MCRCategoryDAO) URI(java.net.URI) Collector(java.util.stream.Collector) MCRLabel(org.mycore.datamodel.classifications2.MCRLabel) Collection(java.util.Collection) MCRPersistenceException(org.mycore.common.MCRPersistenceException) Set(java.util.Set) MCRCategoryID(org.mycore.datamodel.classifications2.MCRCategoryID) MCRCategory(org.mycore.datamodel.classifications2.MCRCategory) EntityManager(javax.persistence.EntityManager) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) MCREntityManagerProvider(org.mycore.backend.jpa.MCREntityManagerProvider) AbstractMap(java.util.AbstractMap) List(java.util.List) Query(javax.persistence.Query) Logger(org.apache.logging.log4j.Logger) Optional(java.util.Optional) MCRStreamUtils(org.mycore.common.MCRStreamUtils) LogManager(org.apache.logging.log4j.LogManager) MCRCategory(org.mycore.datamodel.classifications2.MCRCategory) MCRException(org.mycore.common.MCRException) Collection(java.util.Collection)

Example 49 with MCRException

use of org.mycore.common.MCRException in project mycore by MyCoRe-Org.

the class MCRShutdownHandler method addCloseable.

public void addCloseable(MCRShutdownHandler.Closeable c) {
    Objects.requireNonNull(c);
    init();
    boolean hasShutDownLock;
    try {
        hasShutDownLock = shutdownLock.readLock().tryLock(ADD_CLOSEABLE_TIMEOUT, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
        throw new MCRException("Could not aquire shutdown lock in time", e);
    }
    try {
        if (hasShutDownLock && !shuttingDown) {
            requests.add(c);
        } else {
            throw new MCRException("Cannot register Closeable while shutting down application.");
        }
    } finally {
        if (hasShutDownLock) {
            shutdownLock.readLock().unlock();
        }
    }
}
Also used : MCRException(org.mycore.common.MCRException)

Example 50 with MCRException

use of org.mycore.common.MCRException in project mycore by MyCoRe-Org.

the class MCRBibUtilsTransformer method export.

private MCRContent export(File modsFile) {
    String[] arguments = buildCommandArguments(modsFile);
    MCRExternalProcess ep = null;
    try {
        ep = new MCRExternalProcess(arguments);
        ep.run();
        String errors = ep.getErrors();
        if (!errors.isEmpty())
            LOGGER.warn(errors);
        return ep.getOutput();
    } catch (Exception ex) {
        String msg = "Exception invoking external command " + command;
        throw new MCRException(msg, ex);
    }
}
Also used : MCRException(org.mycore.common.MCRException) IOException(java.io.IOException) MCRException(org.mycore.common.MCRException) MCRExternalProcess(org.mycore.frontend.cli.MCRExternalProcess)

Aggregations

MCRException (org.mycore.common.MCRException)131 IOException (java.io.IOException)39 Element (org.jdom2.Element)26 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)19 Document (org.jdom2.Document)18 MCRCommand (org.mycore.frontend.cli.annotation.MCRCommand)18 File (java.io.File)15 MCRConfigurationException (org.mycore.common.config.MCRConfigurationException)12 MCRObject (org.mycore.datamodel.metadata.MCRObject)12 ArrayList (java.util.ArrayList)11 JDOMException (org.jdom2.JDOMException)11 MCRAccessException (org.mycore.access.MCRAccessException)11 MCRPath (org.mycore.datamodel.niofs.MCRPath)10 SAXException (org.xml.sax.SAXException)9 InvocationTargetException (java.lang.reflect.InvocationTargetException)7 List (java.util.List)7 MCRActiveLinkException (org.mycore.datamodel.common.MCRActiveLinkException)7 SAXParseException (org.xml.sax.SAXParseException)7 URI (java.net.URI)6 Path (java.nio.file.Path)6