mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-07-24 17:32:48 +00:00
Moving to root below branch label
git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@2005 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
136
source/java/org/alfresco/util/debug/MethodCallLogAdvice.java
Normal file
136
source/java/org/alfresco/util/debug/MethodCallLogAdvice.java
Normal file
@@ -0,0 +1,136 @@
|
||||
/*
|
||||
* Copyright (C) 2005 Alfresco, Inc.
|
||||
*
|
||||
* Licensed under the Mozilla Public License version 1.1
|
||||
* with a permitted attribution clause. You may obtain a
|
||||
* copy of the License at
|
||||
*
|
||||
* http://www.alfresco.org/legal/license.txt
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||
* either express or implied. See the License for the specific
|
||||
* language governing permissions and limitations under the
|
||||
* License.
|
||||
*/
|
||||
package org.alfresco.util.debug;
|
||||
|
||||
import org.alfresco.repo.transaction.AlfrescoTransactionSupport;
|
||||
import org.aopalliance.intercept.MethodInterceptor;
|
||||
import org.aopalliance.intercept.MethodInvocation;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
/**
|
||||
* Performs writing to DEBUG of incoming arguments and outgoing results for a method call.<br>
|
||||
* If the method invocation throws an exception, then the incoming arguments are
|
||||
* logged to DEBUG as well.<br>
|
||||
* The implementation adds very little overhead to a normal method
|
||||
* call by only building log messages when required.
|
||||
* <p>
|
||||
* The logging is done against the logger retrieved using the names:
|
||||
* <p>
|
||||
* <pre>
|
||||
* org.alfresco.util.debug.MethodCallLogAdvice
|
||||
* AND
|
||||
* targetClassName
|
||||
* targetClassName.methodName
|
||||
* targetClassName.methodName.exception
|
||||
* </pre>
|
||||
* <p>
|
||||
* The following examples show how to control the log levels:
|
||||
* <p>
|
||||
* <pre>
|
||||
* org.alfresco.util.debug.MethodCallLogAdvice=DEBUG # activate method logging
|
||||
* AND
|
||||
* x.y.MyClass=DEBUG # log debug for all method calls on MyClass
|
||||
* x.y.MyClass.doSomething=DEBUG # log debug for all doSomething method calls
|
||||
* x.y.MyClass.doSomething.exception=DEBUG # only log debug for doSomething() upon exception
|
||||
* </pre>
|
||||
* <p>
|
||||
*
|
||||
* @author Derek Hulley
|
||||
*/
|
||||
public class MethodCallLogAdvice implements MethodInterceptor
|
||||
{
|
||||
private static final Log logger = LogFactory.getLog(MethodCallLogAdvice.class);
|
||||
|
||||
public Object invoke(MethodInvocation invocation) throws Throwable
|
||||
{
|
||||
if (logger.isDebugEnabled())
|
||||
{
|
||||
return invokeWithLogging(invocation);
|
||||
}
|
||||
else
|
||||
{
|
||||
// no logging required
|
||||
return invocation.proceed();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Only executes logging code if logging is required
|
||||
*/
|
||||
private Object invokeWithLogging(MethodInvocation invocation) throws Throwable
|
||||
{
|
||||
String methodName = invocation.getMethod().getName();
|
||||
String className = invocation.getMethod().getDeclaringClass().getName();
|
||||
|
||||
// execute as normal
|
||||
try
|
||||
{
|
||||
Object ret = invocation.proceed();
|
||||
// logging
|
||||
Log methodLogger = LogFactory.getLog(className + "." + methodName);
|
||||
if (methodLogger.isDebugEnabled())
|
||||
{
|
||||
// log success
|
||||
StringBuffer sb = getInvocationInfo(className, methodName, invocation.getArguments());
|
||||
sb.append(" Result: ").append(ret);
|
||||
methodLogger.debug(sb);
|
||||
}
|
||||
// done
|
||||
return ret;
|
||||
}
|
||||
catch (Throwable e)
|
||||
{
|
||||
Log exceptionLogger = LogFactory.getLog(className + "." + methodName + ".exception");
|
||||
if (exceptionLogger.isDebugEnabled())
|
||||
{
|
||||
StringBuffer sb = getInvocationInfo(className, methodName, invocation.getArguments());
|
||||
sb.append(" Failure: ").append(e.getClass().getName()).append(" - ").append(e.getMessage());
|
||||
exceptionLogger.debug(sb);
|
||||
}
|
||||
// rethrow
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return format:
|
||||
* <pre>
|
||||
* Method: className#methodName
|
||||
* Argument: arg0
|
||||
* Argument: arg1
|
||||
* ...
|
||||
* Argument: argN {newline}
|
||||
* </pre>
|
||||
*
|
||||
* @param className
|
||||
* @param methodName
|
||||
* @param args
|
||||
* @return Returns a StringBuffer containing the details of a method call
|
||||
*/
|
||||
private StringBuffer getInvocationInfo(String className, String methodName, Object[] args)
|
||||
{
|
||||
StringBuffer sb = new StringBuffer(250);
|
||||
sb.append("\nMethod: ").append(className).append("#").append(methodName).append("\n");
|
||||
sb.append(" Transaction: ").append(AlfrescoTransactionSupport.getTransactionId()).append("\n");
|
||||
for (Object arg : args)
|
||||
{
|
||||
sb.append(" Argument: ").append(arg).append("\n");
|
||||
}
|
||||
return sb;
|
||||
}
|
||||
}
|
166
source/java/org/alfresco/util/debug/NodeStoreInspector.java
Normal file
166
source/java/org/alfresco/util/debug/NodeStoreInspector.java
Normal file
@@ -0,0 +1,166 @@
|
||||
/*
|
||||
* Copyright (C) 2005 Alfresco, Inc.
|
||||
*
|
||||
* Licensed under the Mozilla Public License version 1.1
|
||||
* with a permitted attribution clause. You may obtain a
|
||||
* copy of the License at
|
||||
*
|
||||
* http://www.alfresco.org/legal/license.txt
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||
* either express or implied. See the License for the specific
|
||||
* language governing permissions and limitations under the
|
||||
* License.
|
||||
*/
|
||||
package org.alfresco.util.debug;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
import org.alfresco.service.cmr.repository.ChildAssociationRef;
|
||||
import org.alfresco.service.cmr.repository.InvalidNodeRefException;
|
||||
import org.alfresco.service.cmr.repository.AssociationRef;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.alfresco.service.cmr.repository.NodeService;
|
||||
import org.alfresco.service.cmr.repository.StoreRef;
|
||||
import org.alfresco.service.namespace.QName;
|
||||
import org.alfresco.service.namespace.RegexQNamePattern;
|
||||
|
||||
/**
|
||||
* Debug class that has methods to inspect the contents of a node store.
|
||||
*
|
||||
* @author Roy Wetherall
|
||||
*/
|
||||
public class NodeStoreInspector
|
||||
{
|
||||
/**
|
||||
* Dumps the contents of a store to a string.
|
||||
*
|
||||
* @param nodeService the node service
|
||||
* @param storeRef the store reference
|
||||
* @return string containing textual representation of the contents of the store
|
||||
*/
|
||||
public static String dumpNodeStore(NodeService nodeService, StoreRef storeRef)
|
||||
{
|
||||
StringBuilder builder = new StringBuilder();
|
||||
|
||||
if (nodeService.exists(storeRef) == true)
|
||||
{
|
||||
NodeRef rootNode = nodeService.getRootNode(storeRef);
|
||||
builder.append(outputNode(0, nodeService, rootNode));
|
||||
}
|
||||
else
|
||||
{
|
||||
builder.
|
||||
append("The store ").
|
||||
append(storeRef.toString()).
|
||||
append(" does not exist.");
|
||||
}
|
||||
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Output the node
|
||||
*
|
||||
* @param iIndent
|
||||
* @param nodeService
|
||||
* @param nodeRef
|
||||
* @return
|
||||
*/
|
||||
private static String outputNode(int iIndent, NodeService nodeService, NodeRef nodeRef)
|
||||
{
|
||||
StringBuilder builder = new StringBuilder();
|
||||
|
||||
try
|
||||
{
|
||||
QName nodeType = nodeService.getType(nodeRef);
|
||||
builder.
|
||||
append(getIndent(iIndent)).
|
||||
append("node: ").
|
||||
append(nodeRef.getId()).
|
||||
append(" (").
|
||||
append(nodeType.getLocalName());
|
||||
|
||||
Collection<QName> aspects = nodeService.getAspects(nodeRef);
|
||||
for (QName aspect : aspects)
|
||||
{
|
||||
builder.
|
||||
append(", ").
|
||||
append(aspect.getLocalName());
|
||||
}
|
||||
|
||||
builder.append(")\n");
|
||||
|
||||
Map<QName, Serializable> props = nodeService.getProperties(nodeRef);
|
||||
for (QName name : props.keySet())
|
||||
{
|
||||
String valueAsString = "null";
|
||||
Serializable value = props.get(name);
|
||||
if (value != null)
|
||||
{
|
||||
valueAsString = value.toString();
|
||||
}
|
||||
|
||||
builder.
|
||||
append(getIndent(iIndent+1)).
|
||||
append("@").
|
||||
append(name.getLocalName()).
|
||||
append(" = ").
|
||||
append(valueAsString).
|
||||
append("\n");
|
||||
|
||||
}
|
||||
|
||||
Collection<ChildAssociationRef> childAssocRefs = nodeService.getChildAssocs(nodeRef);
|
||||
for (ChildAssociationRef childAssocRef : childAssocRefs)
|
||||
{
|
||||
builder.
|
||||
append(getIndent(iIndent+1)).
|
||||
append("-> ").
|
||||
append(childAssocRef.getQName().toString()).
|
||||
append(" (").
|
||||
append(childAssocRef.getQName().toString()).
|
||||
append(")\n");
|
||||
|
||||
builder.append(outputNode(iIndent+2, nodeService, childAssocRef.getChildRef()));
|
||||
}
|
||||
|
||||
Collection<AssociationRef> assocRefs = nodeService.getTargetAssocs(nodeRef, RegexQNamePattern.MATCH_ALL);
|
||||
for (AssociationRef assocRef : assocRefs)
|
||||
{
|
||||
builder.
|
||||
append(getIndent(iIndent+1)).
|
||||
append("-> associated to ").
|
||||
append(assocRef.getTargetRef().getId()).
|
||||
append("\n");
|
||||
}
|
||||
}
|
||||
catch (InvalidNodeRefException invalidNode)
|
||||
{
|
||||
invalidNode.printStackTrace();
|
||||
}
|
||||
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the indent
|
||||
*
|
||||
* @param iIndent the indent value
|
||||
* @return the indent string
|
||||
*/
|
||||
private static String getIndent(int iIndent)
|
||||
{
|
||||
StringBuilder builder = new StringBuilder(iIndent*3);
|
||||
for (int i = 0; i < iIndent; i++)
|
||||
{
|
||||
builder.append(" ");
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright (C) 2005 Alfresco, Inc.
|
||||
*
|
||||
* Licensed under the Mozilla Public License version 1.1
|
||||
* with a permitted attribution clause. You may obtain a
|
||||
* copy of the License at
|
||||
*
|
||||
* http://www.alfresco.org/legal/license.txt
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||
* either express or implied. See the License for the specific
|
||||
* language governing permissions and limitations under the
|
||||
* License.
|
||||
*/
|
||||
package org.alfresco.util.debug;
|
||||
|
||||
import org.alfresco.service.cmr.repository.NodeService;
|
||||
import org.alfresco.service.cmr.repository.StoreRef;
|
||||
import org.alfresco.util.BaseSpringTest;
|
||||
|
||||
/**
|
||||
* @author Roy Wetherall
|
||||
*/
|
||||
public class OutputSpacesStoreSystemTest extends BaseSpringTest
|
||||
{
|
||||
/**
|
||||
* Dump the contents of the spaces store to standard out
|
||||
*/
|
||||
public void testDumpSpacesStore()
|
||||
{
|
||||
NodeService nodeService = (NodeService)this.applicationContext.getBean("nodeService");
|
||||
StoreRef spacesStore = new StoreRef(StoreRef.PROTOCOL_WORKSPACE, "SpacesStore");
|
||||
System.out.println(NodeStoreInspector.dumpNodeStore(nodeService, spacesStore));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user