Search in sources :

Example 26 with MCRSession

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

the class MCRObjectIDLockTable method getLockingUserName.

public static String getLockingUserName(String objectId) {
    MCRObjectID objId = MCRObjectID.getInstance(objectId);
    MCRSession locker = MCRObjectIDLockTable.getLocker(objId);
    if (locker == null) {
        return null;
    }
    return locker.getUserInformation().getUserID();
}
Also used : MCRSession(org.mycore.common.MCRSession) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID)

Example 27 with MCRSession

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

the class MCRContainerLoginServlet method think.

/* (non-Javadoc)
     * @see org.mycore.frontend.servlets.MCRServlet#think(org.mycore.frontend.servlets.MCRServletJob)
     */
@Override
protected void think(MCRServletJob job) throws Exception {
    MCRSession session = MCRSessionMgr.getCurrentSession();
    session.setUserInformation(new ContainerUserInformation(session));
    LOGGER.info("Logged in: {}", session.getUserInformation().getUserID());
}
Also used : MCRSession(org.mycore.common.MCRSession)

Example 28 with MCRSession

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

the class MCRImageTiler method run.

/**
 * Starts local tiler threads ( {@link MCRTilingAction}) and gives {@link MCRTileJob} instances to them. Use
 * property <code>MCR.Module-iview2.TilingThreads</code> to specify how many concurrent threads should be running.
 */
public void run() {
    waiter = Thread.currentThread();
    Thread.currentThread().setName("TileMaster");
    // get this MCRSession a speaking name
    MCRSession mcrSession = MCRSessionMgr.getCurrentSession();
    mcrSession.setUserInformation(MCRSystemUserInformation.getSystemUserInstance());
    boolean activated = MCRConfiguration.instance().getBoolean(MCRIView2Tools.CONFIG_PREFIX + "LocalTiler.activated", true) && MCRHIBConnection.isEnabled();
    LOGGER.info("Local Tiling is {}", activated ? "activated" : "deactivated");
    ImageIO.scanForPlugins();
    LOGGER.info("Supported image file types for reading: {}", Arrays.toString(ImageIO.getReaderFormatNames()));
    MCRProcessableDefaultCollection imageTilerCollection = new MCRProcessableDefaultCollection("Image Tiler");
    MCRProcessableRegistry registry = MCRInjectorConfig.injector().getInstance(MCRProcessableRegistry.class);
    registry.register(imageTilerCollection);
    if (activated) {
        int tilingThreadCount = Integer.parseInt(MCRIView2Tools.getIView2Property("TilingThreads"));
        ThreadFactory slaveFactory = new ThreadFactory() {

            AtomicInteger tNum = new AtomicInteger();

            ThreadGroup tg = new ThreadGroup("MCR slave tiling thread group");

            public Thread newThread(Runnable r) {
                return new Thread(tg, r, "TileSlave#" + tNum.incrementAndGet());
            }
        };
        final AtomicInteger activeThreads = new AtomicInteger();
        final LinkedBlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<>();
        ThreadPoolExecutor baseExecutor = new ThreadPoolExecutor(tilingThreadCount, tilingThreadCount, 1, TimeUnit.DAYS, workQueue, slaveFactory) {

            @Override
            protected void afterExecute(Runnable r, Throwable t) {
                super.afterExecute(r, t);
                activeThreads.decrementAndGet();
            }

            @Override
            protected void beforeExecute(Thread t, Runnable r) {
                super.beforeExecute(t, r);
                activeThreads.incrementAndGet();
            }
        };
        this.tilingServe = MCRProcessableFactory.newPool(baseExecutor, imageTilerCollection);
        imageTilerCollection.setProperty("running", running);
        LOGGER.info("TilingMaster is started");
        while (running) {
            try {
                while (activeThreads.get() < tilingThreadCount) {
                    runLock.lock();
                    try {
                        if (!running) {
                            break;
                        }
                        Transaction transaction = null;
                        MCRTileJob job = null;
                        try (Session session = MCRHIBConnection.instance().getSession()) {
                            transaction = session.beginTransaction();
                            job = tq.poll();
                            imageTilerCollection.setProperty("queue", tq.stream().map(MCRTileJob::getPath).collect(Collectors.toList()));
                            transaction.commit();
                        } catch (HibernateException e) {
                            LOGGER.error("Error while getting next tiling job.", e);
                            if (transaction != null) {
                                try {
                                    transaction.rollback();
                                } catch (RuntimeException re) {
                                    LOGGER.warn("Could not rollback transaction.", re);
                                }
                            }
                        }
                        if (job != null && !tilingServe.getExecutor().isShutdown()) {
                            LOGGER.info("Creating:{}", job.getPath());
                            tilingServe.submit(getTilingAction(job));
                        } else {
                            try {
                                synchronized (tq) {
                                    if (running) {
                                        LOGGER.debug("No Picture in TilingQueue going to sleep");
                                        // fixes a race conditioned deadlock situation
                                        // do not wait longer than 60 sec. for a new MCRTileJob
                                        tq.wait(60000);
                                    }
                                }
                            } catch (InterruptedException e) {
                                LOGGER.error("Image Tiling thread was interrupted.", e);
                            }
                        }
                    } finally {
                        runLock.unlock();
                    }
                }
                // while(tilingServe.getActiveCount() < tilingServe.getCorePoolSize())
                if (activeThreads.get() < tilingThreadCount)
                    try {
                        LOGGER.info("Waiting for a tiling job to finish");
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        if (running) {
                            LOGGER.error("Image Tiling thread was interrupted.", e);
                        }
                    }
            } catch (Throwable e) {
                LOGGER.error("Keep running while catching exceptions.", e);
            }
        }
        // while(running)
        imageTilerCollection.setProperty("running", false);
    }
    LOGGER.info("Tiling thread finished");
    MCRSessionMgr.releaseCurrentSession();
    waiter = null;
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) HibernateException(org.hibernate.HibernateException) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) MCRSession(org.mycore.common.MCRSession) MCRProcessableRegistry(org.mycore.common.processing.MCRProcessableRegistry) Transaction(org.hibernate.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) MCRProcessableDefaultCollection(org.mycore.common.processing.MCRProcessableDefaultCollection) Session(org.hibernate.Session) MCRSession(org.mycore.common.MCRSession)

Example 29 with MCRSession

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

the class MCRMetsLock method isLocked.

/**
 * Checks if a Derivate is locked
 * @param derivateIdString the derivate id
 * @return true if the derivate is locked
 */
public static synchronized boolean isLocked(String derivateIdString) {
    MCRObjectID derivateId = MCRObjectID.getInstance(derivateIdString);
    if (MCRMetsLock.metsAccessSessionTable.containsKey(derivateId)) {
        String lastAccessID = MCRMetsLock.metsAccessSessionTable.get(derivateId);
        MCRSession lastSession = MCRSessionMgr.getSession(lastAccessID);
        LOGGER.debug(MessageFormat.format("{0} is locked : {1}", derivateIdString, lastSession != null));
        return lastSession != null;
    } else {
        LOGGER.debug(MessageFormat.format("{0} is not locked", derivateIdString));
        return false;
    }
}
Also used : MCRSession(org.mycore.common.MCRSession) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID)

Example 30 with MCRSession

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

the class MCRUserCommands method changeToUser.

/**
 * This command changes the user of the session context to a new user.
 *
 * @param user
 *            the new user ID
 * @param password
 *            the password of the new user
 */
@MCRCommand(syntax = "change to user {0} with {1}", help = "Changes to the user {0} with the given password {1}.", order = 10)
public static void changeToUser(String user, String password) {
    MCRSession session = MCRSessionMgr.getCurrentSession();
    System.out.println(SYSTEM + " The old user ID is " + session.getUserInformation().getUserID());
    if (MCRUserManager.login(user, password) != null) {
        System.out.println(SYSTEM + " The new user ID is " + session.getUserInformation().getUserID());
    } else {
        LOGGER.warn("Wrong password, no changes of user ID in session context!");
    }
}
Also used : MCRSession(org.mycore.common.MCRSession) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Aggregations

MCRSession (org.mycore.common.MCRSession)63 IOException (java.io.IOException)15 MCRUserInformation (org.mycore.common.MCRUserInformation)10 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)10 MCRPath (org.mycore.datamodel.niofs.MCRPath)6 SignedJWT (com.nimbusds.jwt.SignedJWT)5 Date (java.util.Date)5 EntityManager (javax.persistence.EntityManager)5 Path (java.nio.file.Path)4 Response (javax.ws.rs.core.Response)4 Test (org.junit.Test)4 MCRRestAPIException (org.mycore.restapi.v1.errors.MCRRestAPIException)4 SAXException (org.xml.sax.SAXException)4 UnknownHostException (java.net.UnknownHostException)3 Document (org.jdom2.Document)3 MCRAccessException (org.mycore.access.MCRAccessException)3 MCRException (org.mycore.common.MCRException)3 MCRPersistenceException (org.mycore.common.MCRPersistenceException)3 MCRDerivate (org.mycore.datamodel.metadata.MCRDerivate)3 MCRRestAPIError (org.mycore.restapi.v1.errors.MCRRestAPIError)3