📄 cmslockmanager.java
字号:
/**
* Proves if a resource is locked.<p>
*
* Use {@link org.opencms.lock.CmsLockManager#getLock(CmsDriverManager, CmsDbContext, CmsResource)}
* to obtain a CmsLock object for the specified resource to get further information
* about how the resource is locked.<p>
*
* @param driverManager the driver manager
* @param dbc the current database context
* @param resource the resource
*
* @return true, if and only if the resource is currently locked
* @throws CmsException if something goes wrong
*/
public boolean isLocked(CmsDriverManager driverManager, CmsDbContext dbc, CmsResource resource) throws CmsException {
CmsLock lock = getLock(driverManager, dbc, resource);
return !lock.isNullLock();
}
/**
* Removes a resource after it has been deleted by the driver manager.<p>
*
* @param driverManager the driver manager
* @param dbc the current database context
* @param resourceName the root path of the deleted resource
* @throws CmsException if something goes wrong
*/
public void removeDeletedResource(CmsDriverManager driverManager, CmsDbContext dbc, String resourceName)
throws CmsException {
boolean resourceExists;
try {
driverManager.getVfsDriver().readResource(dbc, dbc.currentProject().getId(), resourceName, false);
resourceExists = true;
} catch (CmsVfsResourceNotFoundException e) {
resourceExists = false;
}
if (resourceExists) {
throw new CmsLockException(Messages.get().container(
Messages.ERR_REMOVING_UNDELETED_RESOURCE_1,
dbc.getRequestContext().removeSiteRoot(resourceName)));
}
m_exclusiveLocks.remove(resourceName);
}
/**
* Removes a resource from the lock manager.<p>
*
* The forceUnlock option should be used with caution. forceUnlock will remove the lock
* by ignoring any rules which may cause wrong lock states.<p>
*
* @param driverManager the driver manager
* @param dbc the current database context
* @param resource the resource
* @param forceUnlock true, if a resource is forced to get unlocked, no matter by which user and in which project the resource is currently locked
*
* @return the previous CmsLock object of the resource, or null if the resource was unlocked
*
* @throws CmsException if something goes wrong
*/
public CmsLock removeResource(
CmsDriverManager driverManager,
CmsDbContext dbc,
CmsResource resource,
boolean forceUnlock) throws CmsException {
String resourcename = resource.getRootPath();
CmsLock lock = getLock(driverManager, dbc, resource);
CmsResource sibling = null;
// check some abort conditions first
if (lock.isNullLock()) {
// the resource isn't locked
return null;
}
if (!forceUnlock
&& (!lock.getUserId().equals(dbc.currentUser().getId()) || lock.getProjectId() != dbc.currentProject().getId())) {
// the resource is locked by another user
throw new CmsLockException(Messages.get().container(
Messages.ERR_RESOURCE_UNLOCK_1,
dbc.removeSiteRoot(resourcename)));
}
if (!forceUnlock
&& (lock.getType() == CmsLock.TYPE_INHERITED || lock.getType() == CmsLock.TYPE_SHARED_INHERITED || (getParentFolderLock(resourcename) != null))) {
// sub-resources of a locked folder can't be unlocked
throw new CmsLockException(Messages.get().container(
Messages.ERR_UNLOCK_LOCK_INHERITED_1,
dbc.removeSiteRoot(resourcename)));
}
// remove the lock and clean-up stuff
if (lock.getType() == CmsLock.TYPE_EXCLUSIVE) {
if (resource.isFolder()) {
// in case of a folder, remove any exclusive locks on sub-resources that probably have
// been upgraded from an inherited lock when the user edited a resource
Iterator i = m_exclusiveLocks.keySet().iterator();
String lockedPath = null;
while (i.hasNext()) {
lockedPath = (String)i.next();
if (lockedPath.startsWith(resourcename) && !lockedPath.equals(resourcename)) {
// remove the exclusive locked sub-resource
i.remove();
}
}
}
return (CmsLock)m_exclusiveLocks.remove(resourcename);
}
if (lock.getType() == CmsLock.TYPE_SHARED_EXCLUSIVE) {
// when a resource with a shared lock gets unlocked, fetch all siblings of the resource
// to the same content record to identify the exclusive locked sibling
List siblings = internalReadSiblings(driverManager, dbc, resource);
for (int i = 0; i < siblings.size(); i++) {
sibling = (CmsResource)siblings.get(i);
if (m_exclusiveLocks.containsKey(sibling.getRootPath())) {
// remove the exclusive locked sibling
m_exclusiveLocks.remove(sibling.getRootPath());
break;
}
}
return lock;
}
return lock;
}
/**
* Removes all resources that are exclusively locked in a project.<p>
*
* @param projectId the ID of the project where the resources have been locked
*/
public void removeResourcesInProject(int projectId) {
Iterator i = m_exclusiveLocks.keySet().iterator();
CmsLock currentLock = null;
while (i.hasNext()) {
currentLock = (CmsLock)m_exclusiveLocks.get(i.next());
if (currentLock.getProjectId() == projectId) {
// iterators are fail-fast!
i.remove();
}
}
}
/**
* Removes all exclusive temporary locks of a user.<p>
*
* @param userId the ID of the user whose locks are removed
*/
public void removeTempLocks(CmsUUID userId) {
Iterator i = m_exclusiveLocks.keySet().iterator();
CmsLock currentLock = null;
while (i.hasNext()) {
currentLock = (CmsLock)m_exclusiveLocks.get(i.next());
if (currentLock.getUserId().equals(userId) && currentLock.getMode() == CmsLock.TEMPORARY) {
// iterators are fail-fast!
i.remove();
}
}
}
/**
* Returns the number of exclusive locked resources.<p>
*
* @return the number of exclusive locked resources
*/
public int size() {
return m_exclusiveLocks.size();
}
/**
* @see java.lang.Object#toString()
*/
public String toString() {
// bring the list of locked resources into a human readable order first
List lockedResources = new ArrayList(m_exclusiveLocks.keySet());
Collections.sort(lockedResources);
Iterator i = lockedResources.iterator();
StringBuffer buf = new StringBuffer();
String lockedPath = null;
CmsLock currentLock = null;
while (i.hasNext()) {
lockedPath = (String)i.next();
currentLock = (CmsLock)m_exclusiveLocks.get(lockedPath);
buf.append(currentLock.toString()).append("\n");
}
return buf.toString();
}
/**
* @see java.lang.Object#finalize()
*/
protected void finalize() throws Throwable {
try {
if (m_exclusiveLocks != null) {
m_exclusiveLocks.clear();
m_exclusiveLocks = null;
sharedInstance = null;
}
} catch (Throwable t) {
// ignore
}
super.finalize();
}
/**
* Returns the lock of a possible locked parent folder of a resource.<p>
*
* @param resourcename the name of the resource
* @return the lock of a parent folder, or null if no parent folders are locked
*/
private CmsLock getParentFolderLock(String resourcename) {
String lockedPath = null;
List keys = new ArrayList(m_exclusiveLocks.keySet());
for (int i = 0; i < keys.size(); i++) {
lockedPath = (String)keys.get(i);
if (resourcename.startsWith(lockedPath) && !resourcename.equals(lockedPath) && lockedPath.endsWith("/")) {
return (CmsLock)m_exclusiveLocks.get(lockedPath);
}
}
return null;
}
/**
* Reads all siblings from a given resource.<p>
*
* The result is a list of <code>{@link CmsResource}</code> objects.
* It does NOT contain the resource itself, only the siblings of the resource.<p>
*
* @param driverManager the driver manager
* @param dbc the current database context
* @param resource the resource to find all siblings from
*
* @return a list of <code>{@link CmsResource}</code> Objects that
* are siblings to the specified resource,
* excluding the specified resource itself
*
* @throws CmsException if something goes wrong
*/
private List internalReadSiblings(CmsDriverManager driverManager, CmsDbContext dbc, CmsResource resource)
throws CmsException {
// reading siblings using the DriverManager methods while the lock state is checked would
// inevitably result in an infinite loop...
List siblings = driverManager.getVfsDriver().readSiblings(dbc, dbc.currentProject(), resource, true);
siblings.remove(resource);
return driverManager.updateContextDates(dbc, siblings);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -