Permalink
Browse files

Reverting this for a bit to fix the build until I correct it.

Revert "XWIKI-7767: Moved deletion of locks from AuthServiceImpl to XHS."

This reverts commit 5d14b16.
  • Loading branch information...
1 parent 5d14b16 commit dcfe1ea3d156938c6382e7255b7aebc647169426 Caleb James DeLisle committed May 3, 2012
@@ -248,13 +248,6 @@
<version>2.0.3</version>
</dependency>
- <!-- UnexpectedException -->
- <dependency>
- <groupId>org.xwiki.platform</groupId>
- <artifactId>xwiki-platform-store-api</artifactId>
- <version>${project.version}</version>
- </dependency>
-
<!-- Authentication -->
<dependency>
<groupId>securityfilter</groupId>
@@ -55,9 +55,7 @@
import org.hibernate.mapping.Property;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import org.xwiki.bridge.event.ActionExecutingEvent;
import org.xwiki.component.annotation.Component;
-import org.xwiki.component.phase.Initializable;
import org.xwiki.context.Execution;
import org.xwiki.context.ExecutionContext;
import org.xwiki.model.EntityType;
@@ -67,12 +65,8 @@
import org.xwiki.model.reference.EntityReferenceSerializer;
import org.xwiki.model.reference.SpaceReference;
import org.xwiki.model.reference.WikiReference;
-import org.xwiki.observation.event.Event;
-import org.xwiki.observation.EventListener;
-import org.xwiki.observation.ObservationManager;
import org.xwiki.query.QueryManager;
import org.xwiki.rendering.syntax.Syntax;
-import org.xwiki.store.UnexpectedException;
import com.xpn.xwiki.XWiki;
import com.xpn.xwiki.XWikiContext;
@@ -119,8 +113,7 @@
@Component
@Named("hibernate")
@Singleton
-public class XWikiHibernateStore extends XWikiHibernateBaseStore
- implements XWikiStoreInterface, Initializable
+public class XWikiHibernateStore extends XWikiHibernateBaseStore implements XWikiStoreInterface
{
private static final Logger log = LoggerFactory.getLogger(XWikiHibernateStore.class);
@@ -132,10 +125,6 @@
@Inject
private QueryManager queryManager;
- /** Needed so we can register an event to trap logout and delete held locks. */
- @Inject
- private ObservationManager observationManager;
-
/**
* Used to convert a string into a proper Document Reference.
*/
@@ -214,12 +203,6 @@ public XWikiHibernateStore()
initValidColumTypes();
}
- @Override
- public void initialize()
- {
- this.registerLogoutListener();
- }
-
/**
* This initializes the valid custom types Used for Custom Mapping
*/
@@ -1572,10 +1555,6 @@ private void saveAttachment(XWikiAttachment attachment, boolean parentUpdate, XW
}
}
- // ---------------------------------------
- // Locks
- // ---------------------------------------
-
@Override
public XWikiLock loadLock(long docId, XWikiContext context, boolean bTransaction) throws XWikiException
{
@@ -1673,80 +1652,6 @@ public void deleteLock(XWikiLock lock, XWikiContext context, boolean bTransactio
}
}
- private void registerLogoutListener()
- {
- this.observationManager.addListener(new EventListener() {
- private final Event ev = new ActionExecutingEvent();
-
- public String getName()
- {
- return "deleteLocksOnLogoutListener";
- }
-
- public List<Event> getEvents()
- {
- return Collections.<Event> singletonList(this.ev);
- }
-
- public void onEvent(Event event, Object source, Object data)
- {
- if ("logout".equals(((ActionExecutingEvent) event).getActionName())) {
- final XWikiContext ctx = (XWikiContext) data;
- if (ctx.getUserReference() != null) {
- releaseAllLocksForCurrentUser(ctx);
- }
- }
- }
- });
- }
-
- /**
- * 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);
- }
-
- // If we're in a non-main wiki & the user is global,
- // switch to the global wiki and delete locks held there.
- if (ctx.isMainWiki()
- && ctx.isMainWiki(ctx.getUserReference().getWikiReference().getName()))
- {
- final String cdb = ctx.getDatabase();
- try {
- ctx.setDatabase(ctx.getMainXWiki());
- this.releaseAllLocksForCurrentUser(ctx);
- } finally {
- ctx.setDatabase(cdb);
- }
- }
- }
-
- // ---------------------------------------
- // Links
- // ---------------------------------------
-
@Override
public List<XWikiLink> loadLinks(long docId, XWikiContext context, boolean bTransaction) throws XWikiException
{
@@ -243,6 +243,60 @@ public XWikiUser checkAuth(XWikiContext context) throws XWikiException
return null;
}
+ // TODO : This code need to be moved elsewhere (appropriately).
+ String userName = getContextUserName(wrappedRequest.getUserPrincipal(), context);
+
+ if (context.getAction().equals("logout") && userName != null) {
+ // Release all active locks on current wiki.
+ try {
+ xwiki.getHibernateStore().beginTransaction(context);
+ Session session = xwiki.getHibernateStore().getSession(context);
+ String sql = "delete from XWikiLock as lock where lock.userName=:userName";
+ Query query = session.createQuery(sql);
+ query.setString("userName", userName);
+ query.executeUpdate();
+ } catch (Exception e) {
+ throw new XWikiException(XWikiException.MODULE_XWIKI_STORE,
+ XWikiException.ERROR_XWIKI_STORE_HIBERNATE_DELETING_LOCK,
+ "Error while deleting active locks held by logging out user.");
+ } finally {
+ try {
+ xwiki.getHibernateStore().endTransaction(context, true);
+ } catch (Exception e) {
+
+ }
+ }
+ // If we're in a non-main wiki & the user is global,
+ // switch to the global wiki and delete locks held there.
+ if (xwiki.isVirtualMode() && !context.getMainXWiki().equals(context.getDatabase())
+ && userName.startsWith(context.getMainXWiki() + ":")) {
+ String cdb = context.getDatabase();
+ // switch to main wiki.
+ context.setDatabase(context.getMainXWiki());
+ try {
+ xwiki.getHibernateStore().beginTransaction(context);
+ Session session = xwiki.getHibernateStore().getSession(context);
+ String sql = "delete from XWikiLock as lock where lock.userName=:userName";
+ Query query = session.createQuery(sql);
+ String localName = userName.substring(userName.indexOf(":") + 1);
+ query.setString("userName", localName);
+ query.executeUpdate();
+ } catch (Exception e) {
+ throw new XWikiException(XWikiException.MODULE_XWIKI_STORE,
+ XWikiException.ERROR_XWIKI_STORE_HIBERNATE_DELETING_LOCK,
+ "Error while deleting active locks held by logging out user.");
+ } finally {
+ try {
+ xwiki.getHibernateStore().endTransaction(context, true);
+ } catch (Exception e) {
+
+ }
+ // switch back to original wiki.
+ context.setDatabase(cdb);
+ }
+ }
+ }
+
// Process logout (this only works with Forms)
if (auth.processLogout(wrappedRequest, response, xwiki.getUrlPatternMatcher())) {
if (LOGGER.isInfoEnabled()) {
@@ -252,7 +306,6 @@ public XWikiUser checkAuth(XWikiContext context) throws XWikiException
return null;
}
- final String userName = getContextUserName(wrappedRequest.getUserPrincipal(), context);
if (LOGGER.isInfoEnabled()) {
if (userName != null) {
LOGGER.info("User " + userName + " is authentified");
@@ -1,131 +0,0 @@
-/*
- * See the NOTICE file distributed with this work for additional
- * information regarding copyright ownership.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-package com.xpn.xwiki.store;
-
-import com.xpn.xwiki.store.hibernate.HibernateSessionFactory;
-import com.xpn.xwiki.store.migration.DataMigrationManager;
-import com.xpn.xwiki.test.AbstractBridgedComponentTestCase;
-import com.xpn.xwiki.web.Utils;
-import com.xpn.xwiki.XWikiContext;
-import org.hamcrest.BaseMatcher;
-import org.hamcrest.Description;
-import org.hibernate.FlushMode;
-import org.hibernate.Session;
-import org.hibernate.SessionFactory;
-import org.hibernate.Query;
-import org.hibernate.Transaction;
-import org.jmock.Expectations;
-import org.jmock.lib.action.CustomAction;
-import org.jmock.Mockery;
-import org.junit.Assert;
-import org.junit.Test;
-import org.xwiki.bridge.event.ActionExecutingEvent;
-import org.xwiki.model.reference.DocumentReference;
-import org.xwiki.observation.EventListener;
-import org.xwiki.observation.ObservationManager;
-import org.xwiki.test.AbstractMockingComponentTestCase;
-import org.xwiki.test.annotation.MockingRequirement;
-
-
-/**
- * Make sure the user's locks are released when they logout.
- *
- * @version $Id$
- * @since 4.1M1
- */
-public class XWikiHibernateStoreLocksTest extends AbstractMockingComponentTestCase
-{
- @MockingRequirement
- private XWikiHibernateStore xhs;
-
- private ObservationManager observationManager;
-
- private final EventListener[] listener = new EventListener[1];
-
- @Override
- public void configure() throws Exception
- {
- // Needed because XHS has initializers which depend on Utils.
- Utils.setComponentManager(this.getComponentManager());
-
- final ObservationManager om =
- this.getComponentManager().getInstance(ObservationManager.class);
- this.observationManager = om;
- this.getMockery().checking(new Expectations() {{
- oneOf(om).addListener(with(new BaseMatcher<EventListener>() {
- public void describeTo(final Description d)
- {
- d.appendText("See if the listener is a deleteLocksOnLogoutListener.");
- }
-
- public boolean matches(final Object o)
- {
- return ((EventListener) o).getName().equals("deleteLocksOnLogoutListener");
- }
- }));
- will(new CustomAction("grab the EventListener so it can be called") {
- public Object invoke(org.jmock.api.Invocation invocation) throws Exception
- {
- listener[0] = (EventListener) invocation.getParameter(0);
- return null;
- }
- });
- }});
-
- final HibernateSessionFactory xhsf =
- this.getComponentManager().getInstance(HibernateSessionFactory.class);
- final SessionFactory hsf = this.getMockery().mock(SessionFactory.class, "hsf");
- final Session session = this.getMockery().mock(org.hibernate.classic.Session.class);
- this.getMockery().checking(new Expectations() {{
- oneOf(xhsf).getSessionFactory(); will(returnValue(hsf));
- oneOf(hsf).openSession(); will(returnValue(session));
- }});
-
- final Query mockQuery = this.getMockery().mock(Query.class);
- final Transaction mockTransaction = this.getMockery().mock(Transaction.class);
- this.getMockery().checking(new Expectations() {{
- exactly(2).of(session).setFlushMode(FlushMode.COMMIT);
- oneOf(session).createQuery("delete from XWikiLock as lock where lock.userName=:userName");
- will(returnValue(mockQuery));
- oneOf(mockQuery).setString("userName", "XWiki.LoggerOutter");
- oneOf(mockQuery).executeUpdate();
- oneOf(session).beginTransaction();
- will(returnValue(mockTransaction));
- oneOf(mockTransaction).commit();
- oneOf(session).close();
- }});
-
- // setDatabase() is called for each transaction and that calls checkDatabase().
- final DataMigrationManager dmm =
- this.getComponentManager().getInstance(DataMigrationManager.class, "hibernate");
- this.getMockery().checking(new Expectations() {{
- oneOf(dmm).checkDatabase();
- }});
- }
-
- @Test
- public void testLocksAreReleasedOnLogout()
- {
- Assert.assertNotNull(this.listener[0]);
- final XWikiContext xc = new XWikiContext();
- xc.setUserReference(new DocumentReference("xwiki", "XWiki", "LoggerOutter"));
- this.listener[0].onEvent(new ActionExecutingEvent("logout"), null, xc);
- }
-}

0 comments on commit dcfe1ea

Please sign in to comment.