Search in sources :

Example 1 with UnexpectedException

use of org.xwiki.store.UnexpectedException in project xwiki-platform by xwiki.

the class XWikiHibernateStore method releaseAllLocksForCurrentUser.

/**
 * Release all of the locks held by the currently logged in user.
 *
 * @param ctx the XWikiContext, used to start the connection and get the user name.
 */
private void releaseAllLocksForCurrentUser(final XWikiContext ctx) {
    try {
        this.beginTransaction(ctx);
        Session session = this.getSession(ctx);
        final Query query = session.createQuery("delete from XWikiLock as lock where lock.userName=:userName");
        // Using deprecated getUser() because this is how locks are created.
        // It would be a maintainibility disaster to use different code paths
        // for calculating names when creating and removing.
        query.setString("userName", ctx.getUser());
        query.executeUpdate();
        this.endTransaction(ctx, true);
    } catch (Exception e) {
        String msg = "Error while deleting active locks held by user.";
        try {
            this.endTransaction(ctx, false);
        } catch (Exception utoh) {
            msg += " Failed to commit OR rollback [" + utoh.getMessage() + "]";
        }
        throw new UnexpectedException(msg, e);
    }
    // switch to the global wiki and delete locks held there.
    if (!ctx.isMainWiki() && ctx.isMainWiki(ctx.getUserReference().getWikiReference().getName())) {
        final String cdb = ctx.getWikiId();
        try {
            ctx.setWikiId(ctx.getMainXWiki());
            this.releaseAllLocksForCurrentUser(ctx);
        } finally {
            ctx.setWikiId(cdb);
        }
    }
}
Also used : UnexpectedException(org.xwiki.store.UnexpectedException) Query(org.hibernate.Query) XWikiException(com.xpn.xwiki.XWikiException) InitializationException(org.xwiki.component.phase.InitializationException) MigrationRequiredException(com.xpn.xwiki.store.migration.MigrationRequiredException) ObjectNotFoundException(org.hibernate.ObjectNotFoundException) QueryException(org.xwiki.query.QueryException) UnexpectedException(org.xwiki.store.UnexpectedException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) SQLException(java.sql.SQLException) Session(org.hibernate.Session)

Example 2 with UnexpectedException

use of org.xwiki.store.UnexpectedException in project xwiki-platform by xwiki.

the class XWikiAttachmentContent method getNewFileItem.

/**
 * @return a new FileItem for temporarily storing attachment content.
 * @since 4.2M3
 */
private static FileItem getNewFileItem() {
    final Environment env = Utils.getComponent(Environment.class);
    final File dir = new File(env.getTemporaryDirectory(), "attachment-cache");
    try {
        if (!dir.mkdirs() && !dir.exists()) {
            throw new UnexpectedException("Failed to create directory for attachments " + dir);
        }
        final DiskFileItem dfi = new DiskFileItem(null, null, false, null, 10000, dir);
        // This causes the temp file to be created.
        dfi.getOutputStream().close();
        return dfi;
    } catch (IOException e) {
        throw new UnexpectedException("Failed to create new attachment temporary file.", e);
    }
}
Also used : UnexpectedException(org.xwiki.store.UnexpectedException) Environment(org.xwiki.environment.Environment) IOException(java.io.IOException) File(java.io.File) DiskFileItem(org.apache.commons.fileupload.disk.DiskFileItem)

Aggregations

UnexpectedException (org.xwiki.store.UnexpectedException)2 XWikiException (com.xpn.xwiki.XWikiException)1 MigrationRequiredException (com.xpn.xwiki.store.migration.MigrationRequiredException)1 File (java.io.File)1 IOException (java.io.IOException)1 SQLException (java.sql.SQLException)1 DiskFileItem (org.apache.commons.fileupload.disk.DiskFileItem)1 ObjectNotFoundException (org.hibernate.ObjectNotFoundException)1 Query (org.hibernate.Query)1 Session (org.hibernate.Session)1 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)1 InitializationException (org.xwiki.component.phase.InitializationException)1 Environment (org.xwiki.environment.Environment)1 QueryException (org.xwiki.query.QueryException)1