Formatted Alfresco-style

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@22322 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Derek Hulley
2010-09-08 10:39:51 +00:00
parent 808ff739a3
commit 7f4402ac58

View File

@@ -26,7 +26,6 @@ import java.util.Date;
import java.util.List;
import java.util.StringTokenizer;
import org.alfresco.repo.cache.EhCacheAdapter;
import org.alfresco.repo.cache.SimpleCache;
import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork;
@@ -57,18 +56,15 @@ public class ActionTrackingServiceImpl implements ActionTrackingService
private static Log logger = LogFactory.getLog(ActionTrackingServiceImpl.class);
private SimpleCache<String, ExecutionDetails> executingActionsCache;
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.
* 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 = '=';
@@ -93,25 +89,25 @@ public class ActionTrackingServiceImpl implements ActionTrackingService
}
/**
* Sets the cache used to store details of
* currently executing actions, cluster wide.
* Sets the cache used to store details of currently executing actions,
* cluster wide.
*/
public void setExecutingActionsCache(SimpleCache<String, ExecutionDetails> executingActionsCache)
{
this.executingActionsCache = executingActionsCache;
}
/** Used by unit tests only */
protected void resetNextExecutionId() {
protected void resetNextExecutionId()
{
this.nextExecutionId = 1;
}
public void recordActionPending(Action action)
{
recordActionPending((ActionImpl) action);
}
public void recordActionPending(ActionImpl action)
{
// Set the status
@@ -129,6 +125,7 @@ public class ActionTrackingServiceImpl implements ActionTrackingService
{
recordActionComplete((ActionImpl) action);
}
private void recordActionComplete(ActionImpl action)
{
if (logger.isDebugEnabled() == true)
@@ -154,11 +151,13 @@ public class ActionTrackingServiceImpl implements ActionTrackingService
{
recordActionExecuting((ActionImpl) action);
}
private void recordActionExecuting(ActionImpl action)
{
if (logger.isDebugEnabled() == true)
{
logger.debug("Action " + action + " with provisional key " + generateCacheKey(action) + " has begun exection");
logger.debug("Action " + action + " with provisional key " + generateCacheKey(action)
+ " has begun exection");
}
// Grab what status it was before
@@ -180,13 +179,11 @@ public class ActionTrackingServiceImpl implements ActionTrackingService
ExecutionDetails details = executingActionsCache.get(key);
// Check it's really there, warn + fix if not
if(details == null) {
logger.warn(
"Went to mark the start of execution of " +
action + " with key " + key +
" but it wasn't in the running actions cache! " +
"Your running actions cache is probably too small"
);
if (details == null)
{
logger.warn("Went to mark the start of execution of " + action + " with key " + key
+ " but it wasn't in the running actions cache! "
+ "Your running actions cache is probably too small");
}
// Update and save into the cache
@@ -196,9 +193,8 @@ public class ActionTrackingServiceImpl implements ActionTrackingService
}
/**
* For an action that needs to go into the cache
* (async action that is pending, or sync action
* that is running), assign an execution instance
* For an action that needs to go into the cache (async action that is
* pending, or sync action that is running), assign an execution instance
* and put into the cache
*/
private void placeActionInCache(ActionImpl action)
@@ -208,21 +204,26 @@ public class ActionTrackingServiceImpl implements ActionTrackingService
// increase nextExecutionId until it isn't)
String key = null;
boolean assigned = false;
while(!assigned) {
while (!assigned)
{
// Try
action.setExecutionInstance(nextExecutionId++);
key = generateCacheKey(action);
// Is it ok?
if(executingActionsCache.get(key) == null) {
if (executingActionsCache.get(key) == null)
{
assigned = true;
}
// Do we need to wrap?
// (Wrap before absolutely needed, makes things simpler)
if(nextExecutionId > wrapExecutionIdAfter) {
synchronized (this) {
while(nextExecutionId > wrapExecutionIdAfter) {
if (nextExecutionId > wrapExecutionIdAfter)
{
synchronized (this)
{
while (nextExecutionId > wrapExecutionIdAfter)
{
nextExecutionId -= wrapExecutionIdAfter;
}
}
@@ -240,8 +241,8 @@ public class ActionTrackingServiceImpl implements ActionTrackingService
}
/**
* Schedule the recording of the action failure to occur
* in another transaction
* Schedule the recording of the action failure to occur in another
* transaction
*/
public void recordActionFailure(Action action, Throwable exception)
{
@@ -253,7 +254,8 @@ public class ActionTrackingServiceImpl implements ActionTrackingService
}
else
{
logger.debug("Will shortly record failure of action " + action + " due to " + exception.getMessage());
logger.debug("Will shortly record failure of action " + action + " due to "
+ exception.getMessage());
}
}
@@ -291,8 +293,8 @@ public class ActionTrackingServiceImpl implements ActionTrackingService
// Have the details updated on the action as soon
// as the transaction has finished rolling back
AlfrescoTransactionSupport.bindListener(
new TransactionListenerAdapter() {
AlfrescoTransactionSupport.bindListener(new TransactionListenerAdapter()
{
public void afterRollback()
{
transactionService.getRetryingTransactionHelper().doInTransaction(
@@ -301,12 +303,14 @@ public class ActionTrackingServiceImpl implements ActionTrackingService
public Object execute() throws Throwable
{
// Update the action as the system user
return AuthenticationUtil.runAs(new RunAsWork<Action>() {
return AuthenticationUtil.runAs(new RunAsWork<Action>()
{
public Action doWork() throws Exception
{
// Grab the latest version of the action
ActionImpl action = (ActionImpl)
runtimeActionService.createAction(actionNode);
// Grab the latest version of the
// action
ActionImpl action = (ActionImpl) runtimeActionService
.createAction(actionNode);
// Update it
action.setExecutionStartDate(startedAt);
@@ -317,7 +321,9 @@ public class ActionTrackingServiceImpl implements ActionTrackingService
if (logger.isDebugEnabled() == true)
{
logger.debug("Recorded failure of action " + actionId + ", node " + actionNode + " due to " + message);
logger.debug("Recorded failure of action "
+ actionId + ", node " + actionNode
+ " due to " + message);
}
// All done
@@ -325,11 +331,9 @@ public class ActionTrackingServiceImpl implements ActionTrackingService
}
}, AuthenticationUtil.SYSTEM_USER_NAME);
}
}, false, true
);
}, false, true);
}
}
);
});
}
}
@@ -341,17 +345,14 @@ public class ActionTrackingServiceImpl implements ActionTrackingService
// (Probably means the cache is too small)
String key = generateCacheKey(action);
ExecutionDetails details = getExecutionDetails(buildExecutionSummary(key));
if(details == null) {
if (details == null)
{
Exception e = new Exception("Cancellation status missing from cache");
e.fillInStackTrace();
logger.warn(
"Unable to check cancellation status for running action " +
action + " with execution key " + key +
" as it wasn't in the running actions cache! " +
"Your running actions cache is probably too small",
e
);
logger.warn("Unable to check cancellation status for running action " + action
+ " with execution key " + key + " as it wasn't in the running actions cache! "
+ "Your running actions cache is probably too small", e);
// Re-generate
details = buildExecutionDetails(action);
@@ -368,16 +369,12 @@ public class ActionTrackingServiceImpl implements ActionTrackingService
public void requestActionCancellation(CancellableAction action)
{
requestActionCancellation(
generateCacheKey(action)
);
requestActionCancellation(generateCacheKey(action));
}
public void requestActionCancellation(ExecutionSummary executionSummary)
{
requestActionCancellation(
generateCacheKey(executionSummary)
);
requestActionCancellation(generateCacheKey(executionSummary));
}
private void requestActionCancellation(String actionKey)
@@ -385,7 +382,8 @@ public class ActionTrackingServiceImpl implements ActionTrackingService
// See if the action is in the cache
ExecutionDetails details = executingActionsCache.get(actionKey);
if(details == null) {
if (details == null)
{
// It isn't in the cache, so nothing to do
return;
}
@@ -397,46 +395,53 @@ public class ActionTrackingServiceImpl implements ActionTrackingService
executingActionsCache.put(actionKey, details);
}
public List<ExecutionSummary> getAllExecutingActions() {
public List<ExecutionSummary> getAllExecutingActions()
{
Collection<String> actions = executingActionsCache.getKeys();
List<ExecutionSummary> details = new ArrayList<ExecutionSummary>(actions.size());
for(String key : actions) {
for (String key : actions)
{
details.add(buildExecutionSummary(key));
}
return details;
}
public List<ExecutionSummary> getExecutingActions(Action action) {
public List<ExecutionSummary> getExecutingActions(Action action)
{
Collection<String> actions = executingActionsCache.getKeys();
List<ExecutionSummary> details = new ArrayList<ExecutionSummary>();
String match = action.getActionDefinitionName() + cacheKeyPartSeparator +
action.getId() + cacheKeyPartSeparator;
for(String key : actions) {
if(key.startsWith(match)) {
String match = action.getActionDefinitionName() + cacheKeyPartSeparator + action.getId()
+ cacheKeyPartSeparator;
for (String key : actions)
{
if (key.startsWith(match))
{
details.add(buildExecutionSummary(key));
}
}
return details;
}
public List<ExecutionSummary> getExecutingActions(String type) {
public List<ExecutionSummary> getExecutingActions(String type)
{
Collection<String> actions = executingActionsCache.getKeys();
List<ExecutionSummary> details = new ArrayList<ExecutionSummary>();
String match = type + cacheKeyPartSeparator;
for(String key : actions) {
if(key.startsWith(match)) {
for (String key : actions)
{
if (key.startsWith(match))
{
details.add(buildExecutionSummary(key));
}
}
return details;
}
public ExecutionDetails getExecutionDetails(ExecutionSummary executionSummary) {
ExecutionDetails details = executingActionsCache.get(
generateCacheKey(executionSummary)
);
if(details != null) {
public ExecutionDetails getExecutionDetails(ExecutionSummary executionSummary)
{
ExecutionDetails details = executingActionsCache.get(generateCacheKey(executionSummary));
if (details != null)
{
details.setExecutionSummary(executionSummary);
}
return details;
@@ -447,66 +452,59 @@ public class ActionTrackingServiceImpl implements ActionTrackingService
*/
protected static String generateCacheKey(Action action)
{
return
action.getActionDefinitionName() + cacheKeyPartSeparator +
action.getId() + cacheKeyPartSeparator +
((ActionImpl)action).getExecutionInstance()
;
return action.getActionDefinitionName() + cacheKeyPartSeparator + action.getId()
+ cacheKeyPartSeparator + ((ActionImpl) action).getExecutionInstance();
}
protected static String generateCacheKey(ExecutionSummary summary)
{
return
summary.getActionType() + cacheKeyPartSeparator +
summary.getActionId() + cacheKeyPartSeparator +
summary.getExecutionInstance()
;
return summary.getActionType() + cacheKeyPartSeparator + summary.getActionId()
+ cacheKeyPartSeparator + summary.getExecutionInstance();
}
/**
* Builds up the details to be stored in a cache
* for a specific action
* Builds up the details to be stored in a cache for a specific action
*/
protected static ExecutionDetails buildExecutionDetails(Action action)
{
// Where are we running?
if(machineName == null) {
try {
if (machineName == null)
{
try
{
InetAddress localhost = InetAddress.getLocalHost();
machineName = localhost.getHostAddress() + " : " +
localhost.getHostName();
} catch(UnknownHostException e) {
machineName = localhost.getHostAddress() + " : " + localhost.getHostName();
}
catch (UnknownHostException e)
{
machineName = "(machine details unavailable - server IP not known)";
}
}
// Generate
return new ExecutionDetails(
buildExecutionSummary(action),
action.getNodeRef(), machineName,
action.getExecutionStartDate(), false
);
return new ExecutionDetails(buildExecutionSummary(action), action.getNodeRef(),
machineName, action.getExecutionStartDate(), false);
}
private static String machineName = null;
/**
* Turns a cache key back into its constituent
* parts, for easier access.
* Turns a cache key back into its constituent parts, for easier access.
*/
protected static ExecutionSummary buildExecutionSummary(String key)
{
StringTokenizer st = new StringTokenizer(key, new String(new char[]{cacheKeyPartSeparator}));
StringTokenizer st = new StringTokenizer(key, new String(
new char[] { cacheKeyPartSeparator }));
String actionType = st.nextToken();
String actionId = st.nextToken();
int executionInstance = Integer.parseInt(st.nextToken());
return new ExecutionSummary(actionType, actionId, executionInstance);
}
protected static ExecutionSummary buildExecutionSummary(Action action)
{
return new ExecutionSummary(
action.getActionDefinitionName(),
action.getId(),
((ActionImpl)action).getExecutionInstance()
);
return new ExecutionSummary(action.getActionDefinitionName(), action.getId(),
((ActionImpl) action).getExecutionInstance());
}
}