use of org.opencms.main.CmsEvent in project opencms-core by alkacon.
the class CmsPublishEngine method publishJobFinished.
/**
* Signalizes that the publish thread finishes.<p>
*
* @param publishJob the finished publish job
*/
protected void publishJobFinished(CmsPublishJobInfoBean publishJob) {
// in order to avoid not removable publish locks, unlock all assigned resources again
try {
unlockPublishList(publishJob);
} catch (Throwable t) {
// log failure, most likely a database problem
LOG.error(t.getLocalizedMessage(), t);
}
// trigger the old event mechanism
CmsDbContext dbc = m_dbContextFactory.getDbContext(publishJob.getCmsObject().getRequestContext());
try {
// fire an event that a project has been published
Map<String, Object> eventData = new HashMap<String, Object>();
eventData.put(I_CmsEventListener.KEY_REPORT, publishJob.getPublishReport());
eventData.put(I_CmsEventListener.KEY_PUBLISHID, publishJob.getPublishList().getPublishHistoryId().toString());
eventData.put(I_CmsEventListener.KEY_PROJECTID, dbc.currentProject().getUuid());
eventData.put(I_CmsEventListener.KEY_DBCONTEXT, dbc);
CmsEvent afterPublishEvent = new CmsEvent(I_CmsEventListener.EVENT_PUBLISH_PROJECT, eventData);
OpenCms.fireCmsEvent(afterPublishEvent);
} catch (Throwable t) {
if (dbc != null) {
dbc.rollback();
}
LOG.error(t.getLocalizedMessage(), t);
// catch every thing including runtime exceptions
publishJob.getPublishReport().println(t);
} finally {
if (dbc != null) {
try {
dbc.clear();
} catch (Throwable t) {
// ignore
}
dbc = null;
}
}
try {
// fire the publish finish event
m_listeners.fireFinish(new CmsPublishJobRunning(publishJob));
} catch (Throwable t) {
// log failure, most likely a database problem
LOG.error(t.getLocalizedMessage(), t);
}
try {
// finish the job
publishJob.finish();
} catch (Throwable t) {
// log failure, most likely a database problem
LOG.error(t.getLocalizedMessage(), t);
}
try {
// put the publish job into the history list
m_publishHistory.add(publishJob);
} catch (Throwable t) {
// log failure, most likely a database problem
LOG.error(t.getLocalizedMessage(), t);
}
if (Thread.currentThread() == m_currentPublishThread) {
// wipe the dead thread, only if this thread has not been abandoned
m_currentPublishThread = null;
}
// clear the published resources cache
OpenCms.getMemoryMonitor().flushCache(CmsMemoryMonitor.CacheType.PUBLISHED_RESOURCES);
// try to start a new publish job
checkCurrentPublishJobThread();
}
use of org.opencms.main.CmsEvent in project opencms-core by alkacon.
the class CmsImageCacheCleanDialog method flushCache.
/**
* Clears the Image Cache according to value of date field.<p>
*/
void flushCache() {
float age = (System.currentTimeMillis() - m_dateField.getDate().getTime()) / (60f * 60f * 1000f);
OpenCms.fireCmsEvent(new CmsEvent(I_CmsEventListener.EVENT_CLEAR_CACHES, Collections.<String, Object>singletonMap(CmsImageLoader.PARAM_CLEAR_IMAGES_CACHE, "" + age)));
}
use of org.opencms.main.CmsEvent in project opencms-core by alkacon.
the class CmsDriverManager method undoContentChanges.
/**
* Undoes all content changes of a resource.<p>
*
* @param dbc the database context
* @param onlineProject the online project
* @param offlineResource the offline resource, or <code>null</code> if deleted
* @param onlineResource the online resource
* @param newState the new resource state
* @param moveUndone is a move operation on the same resource has been made
*
* @throws CmsException if something goes wrong
*/
private void undoContentChanges(CmsDbContext dbc, CmsProject onlineProject, CmsResource offlineResource, CmsResource onlineResource, CmsResourceState newState, boolean moveUndone) throws CmsException {
String path = ((moveUndone || (offlineResource == null)) ? onlineResource.getRootPath() : offlineResource.getRootPath());
// change folder or file?
I_CmsUserDriver userDriver = getUserDriver(dbc);
I_CmsVfsDriver vfsDriver = getVfsDriver(dbc);
if (onlineResource.isFolder()) {
CmsFolder restoredFolder = new CmsFolder(onlineResource.getStructureId(), onlineResource.getResourceId(), path, onlineResource.getTypeId(), onlineResource.getFlags(), dbc.currentProject().getUuid(), newState, onlineResource.getDateCreated(), onlineResource.getUserCreated(), onlineResource.getDateLastModified(), onlineResource.getUserLastModified(), onlineResource.getDateReleased(), onlineResource.getDateExpired(), // version number does not matter since it will be computed later
onlineResource.getVersion());
// write the folder in the offline project
// this sets a flag so that the folder date is not set to the current time
restoredFolder.setDateLastModified(onlineResource.getDateLastModified());
// write the folder
vfsDriver.writeResource(dbc, dbc.currentProject().getUuid(), restoredFolder, NOTHING_CHANGED);
// restore the properties from the online project
vfsDriver.deletePropertyObjects(dbc, dbc.currentProject().getUuid(), restoredFolder, CmsProperty.DELETE_OPTION_DELETE_STRUCTURE_AND_RESOURCE_VALUES);
List<CmsProperty> propertyInfos = vfsDriver.readPropertyObjects(dbc, onlineProject, onlineResource);
vfsDriver.writePropertyObjects(dbc, dbc.currentProject(), restoredFolder, propertyInfos);
// restore the access control entries from the online project
userDriver.removeAccessControlEntries(dbc, dbc.currentProject(), onlineResource.getResourceId());
ListIterator<CmsAccessControlEntry> aceList = userDriver.readAccessControlEntries(dbc, onlineProject, onlineResource.getResourceId(), false).listIterator();
while (aceList.hasNext()) {
CmsAccessControlEntry ace = aceList.next();
userDriver.createAccessControlEntry(dbc, dbc.currentProject(), onlineResource.getResourceId(), ace.getPrincipal(), ace.getPermissions().getAllowedPermissions(), ace.getPermissions().getDeniedPermissions(), ace.getFlags());
}
} else {
byte[] onlineContent = vfsDriver.readContent(dbc, CmsProject.ONLINE_PROJECT_ID, onlineResource.getResourceId());
CmsFile restoredFile = new CmsFile(onlineResource.getStructureId(), onlineResource.getResourceId(), path, onlineResource.getTypeId(), onlineResource.getFlags(), dbc.currentProject().getUuid(), newState, onlineResource.getDateCreated(), onlineResource.getUserCreated(), onlineResource.getDateLastModified(), onlineResource.getUserLastModified(), onlineResource.getDateReleased(), onlineResource.getDateExpired(), 0, onlineResource.getLength(), onlineResource.getDateContent(), // version number does not matter since it will be computed later
onlineResource.getVersion(), onlineContent);
// write the file in the offline project
// this sets a flag so that the file date is not set to the current time
restoredFile.setDateLastModified(onlineResource.getDateLastModified());
// collect the old properties
List<CmsProperty> properties = vfsDriver.readPropertyObjects(dbc, onlineProject, onlineResource);
if (offlineResource != null) {
// bug fix 1020: delete all properties (inclum_rejectStructureIdded shared),
// shared properties will be recreated by the next call of #createResource(...)
vfsDriver.deletePropertyObjects(dbc, dbc.currentProject().getUuid(), onlineResource, CmsProperty.DELETE_OPTION_DELETE_STRUCTURE_AND_RESOURCE_VALUES);
// implementation notes:
// undo changes can become complex e.g. if a resource was deleted, and then
// another resource was copied over the deleted file as a sibling
// therefore we must "clean" delete the offline resource, and then create
// an new resource with the create method
// note that this does NOT apply to folders, since a folder cannot be replaced
// like a resource anyway
deleteResource(dbc, offlineResource, CmsResource.DELETE_PRESERVE_SIBLINGS);
}
CmsResource res = createResource(dbc, restoredFile.getRootPath(), restoredFile, restoredFile.getContents(), properties, false);
// copy the access control entries from the online project
if (offlineResource != null) {
userDriver.removeAccessControlEntries(dbc, dbc.currentProject(), onlineResource.getResourceId());
}
ListIterator<CmsAccessControlEntry> aceList = userDriver.readAccessControlEntries(dbc, onlineProject, onlineResource.getResourceId(), false).listIterator();
while (aceList.hasNext()) {
CmsAccessControlEntry ace = aceList.next();
userDriver.createAccessControlEntry(dbc, dbc.currentProject(), res.getResourceId(), ace.getPrincipal(), ace.getPermissions().getAllowedPermissions(), ace.getPermissions().getDeniedPermissions(), ace.getFlags());
}
vfsDriver.deleteUrlNameMappingEntries(dbc, false, CmsUrlNameMappingFilter.ALL.filterStructureId(res.getStructureId()).filterStates(CmsUrlNameMappingEntry.MAPPING_STATUS_NEW, CmsUrlNameMappingEntry.MAPPING_STATUS_REPLACE_ON_PUBLISH));
// restore the state to unchanged
res.setState(newState);
m_vfsDriver.writeResourceState(dbc, dbc.currentProject(), res, UPDATE_ALL, false);
}
// delete all offline relations
if (offlineResource != null) {
vfsDriver.deleteRelations(dbc, dbc.currentProject().getUuid(), offlineResource, CmsRelationFilter.TARGETS);
}
// get online relations
List<CmsRelation> relations = vfsDriver.readRelations(dbc, CmsProject.ONLINE_PROJECT_ID, onlineResource, CmsRelationFilter.TARGETS);
// write offline relations
Iterator<CmsRelation> itRelations = relations.iterator();
while (itRelations.hasNext()) {
CmsRelation relation = itRelations.next();
vfsDriver.createRelation(dbc, dbc.currentProject().getUuid(), relation);
}
// update the cache
m_monitor.clearResourceCache();
m_monitor.flushCache(CmsMemoryMonitor.CacheType.PROPERTY, CmsMemoryMonitor.CacheType.PROPERTY_LIST);
if ((offlineResource == null) || offlineResource.getRootPath().equals(onlineResource.getRootPath())) {
log(dbc, new CmsLogEntry(dbc, onlineResource.getStructureId(), CmsLogEntryType.RESOURCE_RESTORED, new String[] { onlineResource.getRootPath() }), false);
} else {
log(dbc, new CmsLogEntry(dbc, offlineResource.getStructureId(), CmsLogEntryType.RESOURCE_MOVE_RESTORED, new String[] { offlineResource.getRootPath(), onlineResource.getRootPath() }), false);
}
if (offlineResource != null) {
OpenCms.fireCmsEvent(new CmsEvent(I_CmsEventListener.EVENT_RESOURCE_AND_PROPERTIES_MODIFIED, Collections.<String, Object>singletonMap(I_CmsEventListener.KEY_RESOURCE, offlineResource)));
} else {
OpenCms.fireCmsEvent(new CmsEvent(I_CmsEventListener.EVENT_RESOURCE_AND_PROPERTIES_MODIFIED, Collections.<String, Object>singletonMap(I_CmsEventListener.KEY_RESOURCE, onlineResource)));
}
}
use of org.opencms.main.CmsEvent in project opencms-core by alkacon.
the class CmsDriverManager method restoreDeletedResource.
/**
* Restores a deleted resource identified by its structure id from the historical archive.<p>
*
* @param dbc the current database context
* @param structureId the structure id of the resource to restore
*
* @throws CmsException if something goes wrong
*
* @see CmsObject#restoreDeletedResource(CmsUUID)
*/
public void restoreDeletedResource(CmsDbContext dbc, CmsUUID structureId) throws CmsException {
// get the last version, which should be the deleted one
int version = getHistoryDriver(dbc).readLastVersion(dbc, structureId);
// get that version
I_CmsHistoryResource histRes = getHistoryDriver(dbc).readResource(dbc, structureId, version);
// check the parent path
CmsResource parent;
try {
// try to read the parent resource by id
parent = getVfsDriver(dbc).readResource(dbc, dbc.currentProject().getUuid(), histRes.getParentId(), true);
} catch (CmsVfsResourceNotFoundException e) {
// if not found try to read the parent resource by name
try {
// try to read the parent resource by id
parent = getVfsDriver(dbc).readResource(dbc, dbc.currentProject().getUuid(), CmsResource.getParentFolder(histRes.getRootPath()), true);
} catch (CmsVfsResourceNotFoundException e1) {
// if not found try to restore the parent resource
restoreDeletedResource(dbc, histRes.getParentId());
parent = readResource(dbc, histRes.getParentId(), CmsResourceFilter.IGNORE_EXPIRATION);
}
}
// check write permissions
m_securityManager.checkPermissions(dbc, parent, CmsPermissionSet.ACCESS_WRITE, false, CmsResourceFilter.IGNORE_EXPIRATION);
// check the name
String path = parent.getRootPath();
// name
String resName = CmsResource.getName(histRes.getRootPath());
String ext = "";
if (resName.charAt(resName.length() - 1) == '/') {
resName = resName.substring(0, resName.length() - 1);
} else {
// extension
ext = CmsFileUtil.getExtension(resName);
}
// name without extension
String nameWOExt = resName.substring(0, resName.length() - ext.length());
for (int i = 1; true; i++) {
try {
readResource(dbc, path + resName, CmsResourceFilter.ALL);
resName = nameWOExt + "_" + i + ext;
// try the next resource name with following schema: path/name_{i}.ext
} catch (CmsVfsResourceNotFoundException e) {
// ok, we found a not used resource name
break;
}
}
// check structure id
CmsUUID id = structureId;
if (getVfsDriver(dbc).validateStructureIdExists(dbc, dbc.currentProject().getUuid(), structureId)) {
// should never happen, but if already exists create a new one
id = new CmsUUID();
}
byte[] contents = null;
boolean isFolder = true;
// do we need the contents?
if (histRes instanceof CmsFile) {
contents = ((CmsFile) histRes).getContents();
if ((contents == null) || (contents.length == 0)) {
contents = getHistoryDriver(dbc).readContent(dbc, histRes.getResourceId(), histRes.getPublishTag());
}
isFolder = false;
}
// now read the historical properties
List<CmsProperty> properties = getHistoryDriver(dbc).readProperties(dbc, histRes);
// create the object to create
CmsResource newResource = new CmsResource(id, histRes.getResourceId(), path + resName, histRes.getTypeId(), isFolder, histRes.getFlags(), dbc.currentProject().getUuid(), CmsResource.STATE_NEW, histRes.getDateCreated(), histRes.getUserCreated(), histRes.getDateLastModified(), dbc.currentUser().getId(), histRes.getDateReleased(), histRes.getDateExpired(), histRes.getSiblingCount(), histRes.getLength(), histRes.getDateContent(), histRes.getVersion());
// log it
log(dbc, new CmsLogEntry(dbc, newResource.getStructureId(), CmsLogEntryType.RESOURCE_RESTORE_DELETED, new String[] { newResource.getRootPath() }), false);
// prevent the date last modified is set to the current time
newResource.setDateLastModified(newResource.getDateLastModified());
// restore the resource!
CmsResource resource = createResource(dbc, path + resName, newResource, contents, properties, true);
// set resource state to changed
newResource.setState(CmsResource.STATE_CHANGED);
getVfsDriver(dbc).writeResourceState(dbc, dbc.currentProject(), newResource, UPDATE_RESOURCE_STATE, false);
newResource.setState(CmsResource.STATE_NEW);
// fire the event
Map<String, Object> data = new HashMap<String, Object>(2);
data.put(I_CmsEventListener.KEY_RESOURCE, resource);
data.put(I_CmsEventListener.KEY_CHANGE, new Integer(CHANGED_RESOURCE | CHANGED_CONTENT));
OpenCms.fireCmsEvent(new CmsEvent(I_CmsEventListener.EVENT_RESOURCE_MODIFIED, data));
}
use of org.opencms.main.CmsEvent in project opencms-core by alkacon.
the class CmsDriverManager method deleteResource.
/**
* Deletes a resource.<p>
*
* The <code>siblingMode</code> parameter controls how to handle siblings
* during the delete operation.
* Possible values for this parameter are:
* <ul>
* <li><code>{@link CmsResource#DELETE_REMOVE_SIBLINGS}</code></li>
* <li><code>{@link CmsResource#DELETE_PRESERVE_SIBLINGS}</code></li>
* </ul><p>
*
* @param dbc the current database context
* @param resource the name of the resource to delete (full path)
* @param siblingMode indicates how to handle siblings of the deleted resource
*
* @throws CmsException if something goes wrong
*
* @see CmsObject#deleteResource(String, CmsResource.CmsResourceDeleteMode)
* @see I_CmsResourceType#deleteResource(CmsObject, CmsSecurityManager, CmsResource, CmsResource.CmsResourceDeleteMode)
*/
public void deleteResource(CmsDbContext dbc, CmsResource resource, CmsResource.CmsResourceDeleteMode siblingMode) throws CmsException {
// upgrade a potential inherited, non-shared lock into a common lock
CmsLock currentLock = getLock(dbc, resource);
if (currentLock.getEditionLock().isDirectlyInherited()) {
// upgrade the lock status if required
lockResource(dbc, resource, CmsLockType.EXCLUSIVE);
}
// check if siblings of the resource exist and must be deleted as well
if (resource.isFolder()) {
// folder can have no siblings
siblingMode = CmsResource.DELETE_PRESERVE_SIBLINGS;
}
// if selected, add all siblings of this resource to the list of resources to be deleted
boolean allSiblingsRemoved;
List<CmsResource> resources;
if (siblingMode == CmsResource.DELETE_REMOVE_SIBLINGS) {
resources = new ArrayList<CmsResource>(readSiblings(dbc, resource, CmsResourceFilter.ALL));
allSiblingsRemoved = true;
// ensure that the resource requested to be deleted is the last resource that gets actually deleted
// to keep the shared locks of the siblings while those get deleted.
resources.remove(resource);
resources.add(resource);
} else {
// only delete the resource, no siblings
resources = Collections.singletonList(resource);
allSiblingsRemoved = false;
}
int size = resources.size();
// if we have only one resource no further check is required
if (size > 1) {
CmsMultiException me = new CmsMultiException();
// ensure that each sibling is unlocked or locked by the current user
for (int i = 0; i < size; i++) {
CmsResource currentResource = resources.get(i);
currentLock = getLock(dbc, currentResource);
if (!currentLock.getEditionLock().isUnlocked() && !currentLock.isOwnedBy(dbc.currentUser())) {
// the resource is locked by a user different from the current user
CmsRequestContext context = dbc.getRequestContext();
me.addException(new CmsLockException(org.opencms.lock.Messages.get().container(org.opencms.lock.Messages.ERR_SIBLING_LOCKED_2, context.getSitePath(currentResource), context.getSitePath(resource))));
}
}
if (!me.getExceptions().isEmpty()) {
throw me;
}
}
boolean removeAce = true;
if (resource.isFolder()) {
// check if the folder has any resources in it
Iterator<CmsResource> childResources = getVfsDriver(dbc).readChildResources(dbc, dbc.currentProject(), resource, true, true).iterator();
CmsUUID projectId = CmsProject.ONLINE_PROJECT_ID;
if (dbc.currentProject().isOnlineProject()) {
// HACK: to get an offline project id
projectId = CmsUUID.getOpenCmsUUID();
}
// collect the names of the resources inside the folder, excluding the moved resources
StringBuffer errorResNames = new StringBuffer(128);
while (childResources.hasNext()) {
CmsResource errorRes = childResources.next();
if (errorRes.getState().isDeleted()) {
continue;
}
// if deleting offline, or not moved, or just renamed inside the deleted folder
// so, it may remain some orphan online entries for moved resources
// which will be fixed during the publishing of the moved resources
boolean error = !dbc.currentProject().isOnlineProject();
if (!error) {
try {
String originalPath = getVfsDriver(dbc).readResource(dbc, projectId, errorRes.getRootPath(), true).getRootPath();
error = originalPath.equals(errorRes.getRootPath()) || originalPath.startsWith(resource.getRootPath());
} catch (CmsVfsResourceNotFoundException e) {
// ignore
}
}
if (error) {
if (errorResNames.length() != 0) {
errorResNames.append(", ");
}
errorResNames.append("[" + dbc.removeSiteRoot(errorRes.getRootPath()) + "]");
}
}
// the current implementation only deletes empty folders
if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(errorResNames.toString())) {
throw new CmsVfsException(org.opencms.db.generic.Messages.get().container(org.opencms.db.generic.Messages.ERR_DELETE_NONEMTY_FOLDER_2, dbc.removeSiteRoot(resource.getRootPath()), errorResNames.toString()));
}
}
// delete all collected resources
for (int i = 0; i < size; i++) {
CmsResource currentResource = resources.get(i);
// is to be removed without write permissions, ie. while deleting a folder
if (!currentResource.equals(resource) && (I_CmsPermissionHandler.PERM_ALLOWED != m_securityManager.hasPermissions(dbc, currentResource, CmsPermissionSet.ACCESS_WRITE, LockCheck.yes, CmsResourceFilter.ALL))) {
// no write access to sibling - must keep ACE (see below)
allSiblingsRemoved = false;
} else {
// write access to sibling granted
boolean existsOnline = (getVfsDriver(dbc).validateStructureIdExists(dbc, CmsProject.ONLINE_PROJECT_ID, currentResource.getStructureId()) || !(currentResource.getState().equals(CmsResource.STATE_NEW)));
if (!existsOnline) {
// the resource does not exist online => remove the resource
// this means the resource is "new" (blue) in the offline project
// delete all properties of this resource
deleteAllProperties(dbc, currentResource.getRootPath());
if (currentResource.isFolder()) {
getVfsDriver(dbc).removeFolder(dbc, dbc.currentProject(), currentResource);
} else {
// check labels
if (currentResource.isLabeled() && !labelResource(dbc, currentResource, null, 2)) {
// update the resource flags to "un label" the other siblings
int flags = currentResource.getFlags();
flags &= ~CmsResource.FLAG_LABELED;
currentResource.setFlags(flags);
}
getVfsDriver(dbc).removeFile(dbc, dbc.currentProject().getUuid(), currentResource);
}
// ensure an exclusive lock is removed in the lock manager for a deleted new resource,
// otherwise it would "stick" in the lock manager, preventing other users from creating
// a file with the same name (issue with temp files in editor)
m_lockManager.removeDeletedResource(dbc, currentResource.getRootPath());
// delete relations
getVfsDriver(dbc).deleteRelations(dbc, dbc.currentProject().getUuid(), currentResource, CmsRelationFilter.TARGETS);
getVfsDriver(dbc).deleteUrlNameMappingEntries(dbc, false, CmsUrlNameMappingFilter.ALL.filterStructureId(currentResource.getStructureId()));
getVfsDriver(dbc).deleteAliases(dbc, dbc.currentProject(), new CmsAliasFilter(null, null, currentResource.getStructureId()));
log(dbc, new CmsLogEntry(dbc, currentResource.getStructureId(), CmsLogEntryType.RESOURCE_NEW_DELETED, new String[] { currentResource.getRootPath() }), true);
} else {
// the resource exists online => mark the resource as deleted
// structure record is removed during next publish
// if one (or more) siblings are not removed, the ACE can not be removed
removeAce = false;
// set resource state to deleted
currentResource.setState(CmsResource.STATE_DELETED);
getVfsDriver(dbc).writeResourceState(dbc, dbc.currentProject(), currentResource, UPDATE_STRUCTURE, false);
// update the project ID
getVfsDriver(dbc).writeLastModifiedProjectId(dbc, dbc.currentProject(), dbc.currentProject().getUuid(), currentResource);
// log it
log(dbc, new CmsLogEntry(dbc, currentResource.getStructureId(), CmsLogEntryType.RESOURCE_DELETED, new String[] { currentResource.getRootPath() }), true);
}
}
}
if ((resource.getSiblingCount() <= 1) || allSiblingsRemoved) {
if (removeAce) {
// remove the access control entries
getUserDriver(dbc).removeAccessControlEntries(dbc, dbc.currentProject(), resource.getResourceId());
}
}
// flush all caches
m_monitor.clearAccessControlListCache();
m_monitor.flushCache(CmsMemoryMonitor.CacheType.PROPERTY, CmsMemoryMonitor.CacheType.PROPERTY_LIST, CmsMemoryMonitor.CacheType.PROJECT_RESOURCES);
Map<String, Object> eventData = new HashMap<String, Object>();
eventData.put(I_CmsEventListener.KEY_RESOURCES, resources);
eventData.put(I_CmsEventListener.KEY_DBCONTEXT, dbc);
OpenCms.fireCmsEvent(new CmsEvent(I_CmsEventListener.EVENT_RESOURCE_DELETED, eventData));
}
Aggregations