From do-not-reply at jboss.org Wed Feb 29 01:53:12 2012 Content-Type: multipart/mixed; boundary="===============5248880915461622558==" MIME-Version: 1.0 From: do-not-reply at jboss.org To: exo-jcr-commits at lists.jboss.org Subject: [exo-jcr-commits] exo-jcr SVN: r5705 - jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/lock. Date: Wed, 29 Feb 2012 01:53:12 -0500 Message-ID: <201202290653.q1T6rCCc015181@svn01.web.mwc.hst.phx2.redhat.com> --===============5248880915461622558== Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Author: tolusha Date: 2012-02-29 01:53:12 -0500 (Wed, 29 Feb 2012) New Revision: 5705 Added: jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/= jcr/impl/core/lock/AbstractLockTableHandler.java Log: EXOJCR-1762: auto-repair for VS and Lock inconsistency Added: jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/servi= ces/jcr/impl/core/lock/AbstractLockTableHandler.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services= /jcr/impl/core/lock/AbstractLockTableHandler.java (= rev 0) +++ jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services= /jcr/impl/core/lock/AbstractLockTableHandler.java 2012-02-29 06:53:12 UTC (= rev 5705) @@ -0,0 +1,159 @@ +/* + * Copyright (C) 2012 eXo Platform SAS. + * + * 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 org.exoplatform.services.jcr.impl.core.lock; + +import org.exoplatform.commons.utils.SecurityHelper; +import org.exoplatform.services.jcr.config.LockManagerEntry; +import org.exoplatform.services.jcr.config.WorkspaceEntry; +import org.exoplatform.services.jcr.impl.checker.InspectionQuery; +import org.exoplatform.services.jcr.impl.storage.jdbc.JDBCUtils; + +import java.security.PrivilegedExceptionAction; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.HashSet; +import java.util.Set; + +import javax.naming.InitialContext; +import javax.naming.NamingException; +import javax.sql.DataSource; + +/** + * @author Anatoliy Bazko + * @version $Id: AbstractLockTableHandler.java 34360 2009-07-22 23:58:59Z = tolusha $ + */ +public abstract class AbstractLockTableHandler implements LockTableHandler +{ + + protected final WorkspaceEntry workspaceEntry; + + protected final LockManagerEntry lockManagerEntry; + + /** + * AbstractLockTableHandler constructor. + */ + public AbstractLockTableHandler(WorkspaceEntry workspaceEntry) + { + this.workspaceEntry =3D workspaceEntry; + this.lockManagerEntry =3D workspaceEntry.getLockManager(); + } + + /** + * {@inheritDoc} + */ + public Set getLockedNodesIds() throws SQLException + { + Set lockedNodesIds =3D new HashSet(); + + ResultSet resultSet =3D null; + PreparedStatement preparedStatement =3D null; + + Connection jdbcConnection =3D openConnection(); + try + { + InspectionQuery query =3D getSelectQuery(); + + preparedStatement =3D query.prepareStatement(jdbcConnection); + resultSet =3D preparedStatement.executeQuery(); + + while (resultSet.next()) + { + String idColumn =3D query.getFieldNames()[0]; + String idValue =3D resultSet.getString(idColumn); + + lockedNodesIds.add(extractNodeId(idValue)); + } + } + finally + { + JDBCUtils.freeResources(resultSet, preparedStatement, jdbcConnect= ion); + } + + return lockedNodesIds; + } + + /** + * {@inheritDoc} + */ + public void removeLockedNode(String nodeId) throws SQLException + { + ResultSet resultSet =3D null; + PreparedStatement preparedStatement =3D null; + + Connection jdbcConnection =3D openConnection(); + try + { + InspectionQuery query =3D getDeleteQuery(nodeId); + + preparedStatement =3D query.prepareStatement(jdbcConnection); + preparedStatement.executeUpdate(); + } + finally + { + JDBCUtils.freeResources(resultSet, preparedStatement, jdbcConnect= ion); + } + } + + /** + * Opens connection to database. + */ + protected Connection openConnection() throws SQLException + { + final DataSource ds; + try + { + ds =3D (DataSource)new InitialContext().lookup(getDataSourceName(= )); + } + catch (NamingException e) + { + throw new SQLException(e); + } + + return SecurityHelper.doPrivilegedSQLExceptionAction(new PrivilegedE= xceptionAction() + { + public Connection run() throws SQLException + { + return ds.getConnection(); + } + }); + } + + /** + * Returns node identifier from ID column's value {@link DataSource}. + */ + protected abstract String extractNodeId(String value); + + /** + * Returns the name corresponding {@link DataSource}. + */ + protected abstract String getDataSourceName() throws SQLException; + + /** + * Returns {@link InspectionQuery} for removing row from LOCK table. + */ + protected abstract InspectionQuery getDeleteQuery(String nodeId) throws= SQLException; + + /** + * Returns {@link InspectionQuery} for selecting all rows from LOCK tab= le. + */ + protected abstract InspectionQuery getSelectQuery() throws SQLException; + +} --===============5248880915461622558==--