/*
* Copyright (C) 2005-2010 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco 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 3 of the License, or
* (at your option) any later version.
*
* Alfresco 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 Alfresco. If not, see .
*/
package org.alfresco.repo.action;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.StringTokenizer;
import org.alfresco.repo.cache.SimpleCache;
import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork;
import org.alfresco.repo.transaction.AlfrescoTransactionSupport;
import org.alfresco.repo.transaction.TransactionListenerAdapter;
import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback;
import org.alfresco.service.cmr.action.Action;
import org.alfresco.service.cmr.action.ActionServiceTransientException;
import org.alfresco.service.cmr.action.ActionStatus;
import org.alfresco.service.cmr.action.ActionTrackingService;
import org.alfresco.service.cmr.action.CancellableAction;
import org.alfresco.service.cmr.action.ExecutionDetails;
import org.alfresco.service.cmr.action.ExecutionSummary;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.transaction.TransactionService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Action execution tracking service implementation
*
* @author Nick Burch
*/
public class ActionTrackingServiceImpl implements ActionTrackingService
{
/**
* The logger
*/
private static Log logger = LogFactory.getLog(ActionTrackingServiceImpl.class);
private SimpleCache executingActionsCache;
private NodeService nodeService;
private TransactionService transactionService;
private RuntimeActionService runtimeActionService;
/**
* Doesn't need to be cluster unique, is just used to try to reduce the
* chance of clashes in the quickest and easiest way.
*/
private short nextExecutionId = 1;
private short wrapExecutionIdAfter = Short.MAX_VALUE / 2;
/** How we separate bits of the cache key */
private static final char cacheKeyPartSeparator = '=';
/**
* Set the transaction service
*
* @param transactionService the transaction service
*/
public void setTransactionService(TransactionService transactionService)
{
this.transactionService = transactionService;
}
/**
* Set the node service
*
* @param nodeService the node service
*/
public void setNodeService(NodeService nodeService)
{
this.nodeService = nodeService;
}
/**
* Set the runtime action service
*
* @param runtimeActionService the runtime action service
*/
public void setRuntimeActionService(RuntimeActionService runtimeActionService)
{
this.runtimeActionService = runtimeActionService;
}
/**
* Sets the cache used to store details of currently executing actions,
* cluster wide.
*/
public void setExecutingActionsCache(SimpleCache executingActionsCache)
{
this.executingActionsCache = executingActionsCache;
}
/** Used by unit tests only */
protected void resetNextExecutionId()
{
this.nextExecutionId = 1;
}
public void recordActionPending(Action action)
{
recordActionPending((ActionImpl) action);
}
public void recordActionPending(ActionImpl action)
{
// Set the status
action.setExecutionStatus(ActionStatus.Pending);
// Mark it as not having started quite yet
action.setExecutionStartDate(null);
// Have it put into the cache, so we can tell it
// is waiting to be run
placeActionInCache(action);
}
public void recordActionComplete(Action action)
{
recordActionComplete((ActionImpl) action);
}
private void recordActionComplete(final ActionImpl action)
{
if (logger.isDebugEnabled() == true)
{
logger.debug("Action " + action + " has completed execution");
}
// Mark it as having worked
action.setExecutionEndDate(new Date());
action.setExecutionStatus(ActionStatus.Completed);
action.setExecutionFailureMessage(null);
// Do we need to update the persisted details?
if (action.getNodeRef() != null && nodeService.exists(action.getNodeRef()))
{
// Make sure we re-fetch the latest action details and save
// this version back into the repository
// (That way, if someone has a reference to the
// action and plays with it, we still save the
// correct information)
final Date startedAt = action.getExecutionStartDate();
final Date endedAt = action.getExecutionEndDate();
final NodeRef actionNode = action.getNodeRef();
AlfrescoTransactionSupport.bindListener(new TransactionListenerAdapter()
{
public void afterCommit()
{
transactionService.getRetryingTransactionHelper().doInTransaction(
new RetryingTransactionCallback