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();
}
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());
}
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;
}
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;
}
}
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!");
}
}
Aggregations