Browse Source
- Removed backport-util-concurrent - Removed commons attributes supportpull/23217/head
13 changed files with 28 additions and 816 deletions
@ -1,225 +0,0 @@
@@ -1,225 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2008 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* 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.springframework.core; |
||||
|
||||
import java.lang.reflect.Method; |
||||
import java.lang.reflect.Modifier; |
||||
import java.util.LinkedList; |
||||
|
||||
import org.apache.commons.logging.Log; |
||||
import org.apache.commons.logging.LogFactory; |
||||
|
||||
import org.springframework.util.Assert; |
||||
import org.springframework.util.CachingMapDecorator; |
||||
import org.springframework.util.ReflectionUtils; |
||||
|
||||
/** |
||||
* Helper implementation for a reflective visitor. |
||||
* Mainly for internal use within the framework. |
||||
* |
||||
* <p>To use, call <code>invokeVisit</code>, passing a Visitor object |
||||
* and the data argument to accept (double-dispatch). For example: |
||||
* |
||||
* <pre> |
||||
* public String styleValue(Object value) { |
||||
* reflectiveVistorSupport.invokeVisit(this, value) |
||||
* } |
||||
* |
||||
* // visit call back will be invoked via reflection
|
||||
* String visit(<valueType> arg) { |
||||
* // process argument of type <valueType>
|
||||
* } |
||||
* </pre> |
||||
* |
||||
* See the {@link org.springframework.core.style.DefaultValueStyler} class
|
||||
* for a concrete usage of this visitor helper. |
||||
* |
||||
* @author Keith Donald |
||||
* @author Juergen Hoeller |
||||
* @since 1.2.2 |
||||
* @deprecated as of Spring 2.5, to be removed in Spring 3.0 |
||||
*/ |
||||
public class ReflectiveVisitorHelper { |
||||
|
||||
private static final String VISIT_METHOD = "visit"; |
||||
|
||||
private static final String VISIT_NULL = "visitNull"; |
||||
|
||||
private static final Log logger = LogFactory.getLog(ReflectiveVisitorHelper.class); |
||||
|
||||
|
||||
private final CachingMapDecorator visitorClassVisitMethods = new CachingMapDecorator() { |
||||
public Object create(Object key) { |
||||
return new ClassVisitMethods((Class) key); |
||||
} |
||||
}; |
||||
|
||||
|
||||
/** |
||||
* Use reflection to call the appropriate <code>visit</code> method |
||||
* on the provided visitor, passing in the specified argument. |
||||
* @param visitor the visitor encapsulating the logic to process the argument |
||||
* @param argument the argument to dispatch |
||||
* @throws IllegalArgumentException if the visitor parameter is null |
||||
*/ |
||||
public Object invokeVisit(Object visitor, Object argument) { |
||||
Assert.notNull(visitor, "The visitor to visit is required"); |
||||
// Perform call back on the visitor through reflection.
|
||||
Method method = getMethod(visitor.getClass(), argument); |
||||
if (method == null) { |
||||
if (logger.isWarnEnabled()) { |
||||
logger.warn("No method found by reflection for visitor class [" + visitor.getClass().getName() |
||||
+ "] and argument of type [" + (argument != null ? argument.getClass().getName() : "") + "]"); |
||||
} |
||||
return null; |
||||
} |
||||
try { |
||||
Object[] args = null; |
||||
if (argument != null) { |
||||
args = new Object[] {argument}; |
||||
} |
||||
if (!Modifier.isPublic(method.getModifiers())) { |
||||
method.setAccessible(true); |
||||
} |
||||
return method.invoke(visitor, args); |
||||
} |
||||
catch (Exception ex) { |
||||
ReflectionUtils.handleReflectionException(ex); |
||||
throw new IllegalStateException("Should never get here"); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Determines the most appropriate visit method for the |
||||
* given visitor class and argument. |
||||
*/ |
||||
private Method getMethod(Class visitorClass, Object argument) { |
||||
ClassVisitMethods visitMethods = (ClassVisitMethods) this.visitorClassVisitMethods.get(visitorClass); |
||||
return visitMethods.getVisitMethod(argument != null ? argument.getClass() : null); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Internal class caching visitor methods by argument class. |
||||
*/ |
||||
private static class ClassVisitMethods { |
||||
|
||||
private final Class visitorClass; |
||||
|
||||
private final CachingMapDecorator visitMethodCache = new CachingMapDecorator() { |
||||
public Object create(Object argumentClazz) { |
||||
if (argumentClazz == null) { |
||||
return findNullVisitorMethod(); |
||||
} |
||||
Method method = findVisitMethod((Class) argumentClazz); |
||||
if (method == null) { |
||||
method = findDefaultVisitMethod(); |
||||
} |
||||
return method; |
||||
} |
||||
}; |
||||
|
||||
public ClassVisitMethods(Class visitorClass) { |
||||
this.visitorClass = visitorClass; |
||||
} |
||||
|
||||
private Method findNullVisitorMethod() { |
||||
for (Class clazz = this.visitorClass; clazz != null; clazz = clazz.getSuperclass()) { |
||||
try { |
||||
return clazz.getDeclaredMethod(VISIT_NULL, (Class[]) null); |
||||
} |
||||
catch (NoSuchMethodException ex) { |
||||
} |
||||
} |
||||
return findDefaultVisitMethod(); |
||||
} |
||||
|
||||
private Method findDefaultVisitMethod() { |
||||
final Class[] args = {Object.class}; |
||||
for (Class clazz = this.visitorClass; clazz != null; clazz = clazz.getSuperclass()) { |
||||
try { |
||||
return clazz.getDeclaredMethod(VISIT_METHOD, args); |
||||
} |
||||
catch (NoSuchMethodException ex) { |
||||
} |
||||
} |
||||
if (logger.isWarnEnabled()) { |
||||
logger.warn("No default '" + VISIT_METHOD + "' method found. Returning <null>."); |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
/** |
||||
* Gets a cached visitor method for the specified argument type. |
||||
*/ |
||||
private Method getVisitMethod(Class argumentClass) { |
||||
return (Method) this.visitMethodCache.get(argumentClass); |
||||
} |
||||
|
||||
/** |
||||
* Traverses class hierarchy looking for applicable visit() method. |
||||
*/ |
||||
private Method findVisitMethod(Class rootArgumentType) { |
||||
if (rootArgumentType == Object.class) { |
||||
return null; |
||||
} |
||||
LinkedList classQueue = new LinkedList(); |
||||
classQueue.addFirst(rootArgumentType); |
||||
|
||||
while (!classQueue.isEmpty()) { |
||||
Class argumentType = (Class) classQueue.removeLast(); |
||||
// Check for a visit method on the visitor class matching this
|
||||
// argument type.
|
||||
try { |
||||
if (logger.isTraceEnabled()) { |
||||
logger.trace("Looking for method " + VISIT_METHOD + "(" + argumentType + ")"); |
||||
} |
||||
return findVisitMethod(this.visitorClass, argumentType); |
||||
} |
||||
catch (NoSuchMethodException e) { |
||||
// Queue up the argument super class if it's not of type Object.
|
||||
if (!argumentType.isInterface() && (argumentType.getSuperclass() != Object.class)) { |
||||
classQueue.addFirst(argumentType.getSuperclass()); |
||||
} |
||||
// Queue up argument's implemented interfaces.
|
||||
Class[] interfaces = argumentType.getInterfaces(); |
||||
for (int i = 0; i < interfaces.length; i++) { |
||||
classQueue.addFirst(interfaces[i]); |
||||
} |
||||
} |
||||
} |
||||
// No specific method found -> return the default.
|
||||
return findDefaultVisitMethod(); |
||||
} |
||||
|
||||
private Method findVisitMethod(Class visitorClass, Class argumentType) throws NoSuchMethodException { |
||||
try { |
||||
return visitorClass.getDeclaredMethod(VISIT_METHOD, new Class[] {argumentType}); |
||||
} |
||||
catch (NoSuchMethodException ex) { |
||||
// Try visitorClass superclasses.
|
||||
if (visitorClass.getSuperclass() != Object.class) { |
||||
return findVisitMethod(visitorClass.getSuperclass(), argumentType); |
||||
} |
||||
else { |
||||
throw ex; |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -1,98 +0,0 @@
@@ -1,98 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2005 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* 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.springframework.metadata; |
||||
|
||||
import java.lang.reflect.Field; |
||||
import java.lang.reflect.Method; |
||||
import java.util.Collection; |
||||
|
||||
/** |
||||
* Interface for accessing attributes at runtime. This is a facade, |
||||
* which can accommodate any attributes API such as Jakarta Commons Attributes, |
||||
* or (possibly in future) a Spring attributes implementation. |
||||
* |
||||
* <p>The purpose of using this interface is to decouple Spring code from any |
||||
* specific attributes implementation. Even once JSR-175 is available, there |
||||
* is still value in such a facade interface, as it allows for hierarchical |
||||
* attribute sources: for example, an XML file or properties file might override |
||||
* some attributes defined in source-level metadata with JSR-175 or another framework. |
||||
* |
||||
* @author Mark Pollack |
||||
* @author Rod Johnson |
||||
* @since 30.09.2003 |
||||
* @see org.springframework.metadata.commons.CommonsAttributes |
||||
*/ |
||||
public interface Attributes { |
||||
|
||||
/** |
||||
* Return the class attributes of the target class. |
||||
* @param targetClass the class that contains attribute information |
||||
* @return a collection of attributes, possibly an empty collection, never <code>null</code> |
||||
*/ |
||||
Collection getAttributes(Class targetClass); |
||||
|
||||
/** |
||||
* Return the class attributes of the target class of a given type. |
||||
* <p>The class attributes are filtered by providing a <code>Class</code> |
||||
* reference to indicate the type to filter on. This is useful if you know |
||||
* the type of the attribute you are looking for and don't want to sort |
||||
* through the unfiltered Collection yourself. |
||||
* @param targetClass the class that contains attribute information |
||||
* @param filter specify that only this type of class should be returned |
||||
* @return return only the Collection of attributes that are of the filter type |
||||
*/ |
||||
Collection getAttributes(Class targetClass, Class filter); |
||||
|
||||
/** |
||||
* Return the method attributes of the target method. |
||||
* @param targetMethod the method that contains attribute information |
||||
* @return a Collection of attributes, possibly an empty Collection, never <code>null</code> |
||||
*/ |
||||
Collection getAttributes(Method targetMethod); |
||||
|
||||
/** |
||||
* Return the method attributes of the target method of a given type. |
||||
* <p>The method attributes are filtered by providing a <code>Class</code> |
||||
* reference to indicate the type to filter on. This is useful if you know |
||||
* the type of the attribute you are looking for and don't want to sort |
||||
* through the unfiltered Collection yourself. |
||||
* @param targetMethod the method that contains attribute information |
||||
* @param filter specify that only this type of class should be returned |
||||
* @return a Collection of attributes, possibly an empty Collection, never <code>null</code> |
||||
*/ |
||||
Collection getAttributes(Method targetMethod, Class filter); |
||||
|
||||
/** |
||||
* Return the field attributes of the target field. |
||||
* @param targetField the field that contains attribute information |
||||
* @return a Collection of attribute, possibly an empty Collection, never <code>null</code> |
||||
*/ |
||||
Collection getAttributes(Field targetField); |
||||
|
||||
/** |
||||
* Return the field attributes of the target method of a given type. |
||||
* <p>The field attributes are filtered by providing a <code>Class</code> |
||||
* reference to indicate the type to filter on. This is useful if you know |
||||
* the type of the attribute you are looking for and don't want to sort |
||||
* through the unfiltered Collection yourself. |
||||
* @param targetField the field that contains attribute information |
||||
* @param filter specify that only this type of class should be returned |
||||
* @return a Collection of attributes, possibly an empty Collection, never <code>null</code> |
||||
*/ |
||||
Collection getAttributes(Field targetField, Class filter); |
||||
|
||||
} |
||||
@ -1,87 +0,0 @@
@@ -1,87 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2005 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* 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.springframework.metadata.commons; |
||||
|
||||
import java.lang.reflect.Field; |
||||
import java.lang.reflect.Method; |
||||
import java.util.Collection; |
||||
|
||||
import org.springframework.metadata.Attributes; |
||||
|
||||
/** |
||||
* Implementation of the Spring Attributes facade for Commons Attributes. |
||||
* |
||||
* <p>Please see the |
||||
* <a href="http://jakarta.apache.org/commons/sandbox/attributes"> |
||||
* Commons Attributes documentation</a> for information on how to use the |
||||
* attribute compiler. |
||||
* |
||||
* <p>As of December 2003, follow the Javadocs to the AttributeCompiler class
|
||||
* to see how the Ant task works. Note that you need to put the following jars |
||||
* in your $ANT_HOME/lib directory for the Common Attributes compiler to work: |
||||
* <ul> |
||||
* <li>Commons Attributes compiler jar |
||||
* <li>the xjavadoc Jar (from XDoclet) |
||||
* <li>commons-collection.jar (from Jakarta Commons) |
||||
* </ul> |
||||
* |
||||
* <p>You need to perform the attribute compilation step before compiling your source. |
||||
* |
||||
* <p>See build.xml in the tests for package org.springframework.aop.autoproxy.metadata |
||||
* for an example of the required Ant scripting. The header of this build script |
||||
* includes some quick, and hopefully useful, hints on using Commons Attributes. |
||||
* The source files in the same package (TxClass and TxClassWithClassAttribute) |
||||
* illustrate attribute usage in source files. |
||||
* |
||||
* <p>The Spring Framework project does not provide support usage of specific |
||||
* attributes implementations. Please refer to the appropriate site and mailing |
||||
* list of the attributes implementation. |
||||
* |
||||
* @author Rod Johnson |
||||
*/ |
||||
public class CommonsAttributes implements Attributes { |
||||
|
||||
/* |
||||
* Commons Attributes caches attributes, so we don't need to cache here |
||||
* as well. |
||||
*/ |
||||
|
||||
public Collection getAttributes(Class targetClass) { |
||||
return org.apache.commons.attributes.Attributes.getAttributes(targetClass); |
||||
} |
||||
|
||||
public Collection getAttributes(Class targetClass, Class filter) { |
||||
return org.apache.commons.attributes.Attributes.getAttributes(targetClass, filter); |
||||
} |
||||
|
||||
public Collection getAttributes(Method targetMethod) { |
||||
return org.apache.commons.attributes.Attributes.getAttributes(targetMethod); |
||||
} |
||||
|
||||
public Collection getAttributes(Method targetMethod, Class filter) { |
||||
return org.apache.commons.attributes.Attributes.getAttributes(targetMethod, filter); |
||||
} |
||||
|
||||
public Collection getAttributes(Field targetField) { |
||||
return org.apache.commons.attributes.Attributes.getAttributes(targetField); |
||||
} |
||||
|
||||
public Collection getAttributes(Field targetField, Class filter) { |
||||
return org.apache.commons.attributes.Attributes.getAttributes(targetField, filter); |
||||
} |
||||
|
||||
} |
||||
@ -1,8 +0,0 @@
@@ -1,8 +0,0 @@
|
||||
<html> |
||||
<body> |
||||
|
||||
Attributes wrapper for |
||||
<a href="http://jakarta.apache.org/commons/sandbox/attributes">Commons Attributes</a>. |
||||
|
||||
</body> |
||||
</html> |
||||
@ -1,8 +0,0 @@
@@ -1,8 +0,0 @@
|
||||
<html> |
||||
<body> |
||||
|
||||
Package defining a facade for accessing source-level |
||||
metadata attributes at runtime. |
||||
|
||||
</body> |
||||
</html> |
||||
@ -1,100 +0,0 @@
@@ -1,100 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2008 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* 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.springframework.util; |
||||
|
||||
/** |
||||
* Utility class for diagnostic purposes, to analyze the |
||||
* ClassLoader hierarchy for any given object or class loader. |
||||
* |
||||
* @author Rod Johnson |
||||
* @author Juergen Hoeller |
||||
* @since 02 April 2001 |
||||
* @deprecated as of Spring 2.5, to be removed in Spring 3.0 |
||||
* @see java.lang.ClassLoader |
||||
*/ |
||||
public abstract class ClassLoaderUtils { |
||||
|
||||
/** |
||||
* Show the class loader hierarchy for this class. |
||||
* Uses default line break and tab text characters. |
||||
* @param obj object to analyze loader hierarchy for |
||||
* @param role a description of the role of this class in the application |
||||
* (e.g., "servlet" or "EJB reference") |
||||
* @return a String showing the class loader hierarchy for this class
|
||||
*/ |
||||
public static String showClassLoaderHierarchy(Object obj, String role) { |
||||
return showClassLoaderHierarchy(obj, role, "\n", "\t"); |
||||
} |
||||
|
||||
/** |
||||
* Show the class loader hierarchy for this class. |
||||
* @param obj object to analyze loader hierarchy for |
||||
* @param role a description of the role of this class in the application |
||||
* (e.g., "servlet" or "EJB reference") |
||||
* @param lineBreak line break |
||||
* @param tabText text to use to set tabs |
||||
* @return a String showing the class loader hierarchy for this class
|
||||
*/ |
||||
public static String showClassLoaderHierarchy(Object obj, String role, String lineBreak, String tabText) { |
||||
String s = "object of " + obj.getClass() + ": role is " + role + lineBreak; |
||||
return s + showClassLoaderHierarchy(obj.getClass().getClassLoader(), lineBreak, tabText, 0); |
||||
} |
||||
|
||||
/** |
||||
* Show the class loader hierarchy for the given class loader. |
||||
* Uses default line break and tab text characters. |
||||
* @param cl class loader to analyze hierarchy for |
||||
* @return a String showing the class loader hierarchy for this class
|
||||
*/ |
||||
public static String showClassLoaderHierarchy(ClassLoader cl) { |
||||
return showClassLoaderHierarchy(cl, "\n", "\t"); |
||||
} |
||||
|
||||
/** |
||||
* Show the class loader hierarchy for the given class loader. |
||||
* @param cl class loader to analyze hierarchy for |
||||
* @param lineBreak line break |
||||
* @param tabText text to use to set tabs |
||||
* @return a String showing the class loader hierarchy for this class
|
||||
*/ |
||||
public static String showClassLoaderHierarchy(ClassLoader cl, String lineBreak, String tabText) { |
||||
return showClassLoaderHierarchy(cl, lineBreak, tabText, 0); |
||||
} |
||||
|
||||
/** |
||||
* Show the class loader hierarchy for the given class loader. |
||||
* @param cl class loader to analyze hierarchy for |
||||
* @param lineBreak line break |
||||
* @param tabText text to use to set tabs |
||||
* @param indent nesting level (from 0) of this loader; used in pretty printing |
||||
* @return a String showing the class loader hierarchy for this class
|
||||
*/ |
||||
private static String showClassLoaderHierarchy(ClassLoader cl, String lineBreak, String tabText, int indent) { |
||||
if (cl == null) { |
||||
ClassLoader ccl = Thread.currentThread().getContextClassLoader(); |
||||
return "context class loader=[" + ccl + "] hashCode=" + ccl.hashCode(); |
||||
} |
||||
StringBuffer buf = new StringBuffer(); |
||||
for (int i = 0; i < indent; i++) { |
||||
buf.append(tabText); |
||||
} |
||||
buf.append("[").append(cl).append("] hashCode=").append(cl.hashCode()).append(lineBreak); |
||||
ClassLoader parent = cl.getParent(); |
||||
return buf.toString() + showClassLoaderHierarchy(parent, lineBreak, tabText, indent + 1); |
||||
} |
||||
|
||||
} |
||||
@ -1,55 +0,0 @@
@@ -1,55 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2008 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* 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.springframework.util; |
||||
|
||||
/** |
||||
* Interface implemented by objects that can provide performance information |
||||
* as well as a record of the number of times they are accessed. |
||||
* |
||||
* <p>Implementing objects must ensure that implementing this interface
|
||||
* does <b>not</b> compromise thread safety. However, it may be acceptable |
||||
* for slight innaccuracies in reported statistics to result from the |
||||
* avoidance of synchronization: performance may be well be more important |
||||
* than exact reporting, so long as the errors are not likely to be misleading. |
||||
* |
||||
* @author Rod Johnson |
||||
* @since November 21, 2000 |
||||
* @deprecated as of Spring 2.5, to be removed in Spring 3.0 |
||||
*/ |
||||
public interface ResponseTimeMonitor { |
||||
|
||||
/** |
||||
* Return the number of accesses to this resource. |
||||
*/ |
||||
int getAccessCount(); |
||||
|
||||
/** |
||||
* Return the average response time in milliseconds. |
||||
*/ |
||||
int getAverageResponseTimeMillis(); |
||||
|
||||
/** |
||||
* Return the best (quickest) response time in milliseconds. |
||||
*/ |
||||
int getBestResponseTimeMillis(); |
||||
|
||||
/** |
||||
* Return the worst (slowest) response time in milliseconds. |
||||
*/ |
||||
int getWorstResponseTimeMillis(); |
||||
|
||||
} |
||||
@ -1,121 +0,0 @@
@@ -1,121 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2008 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* 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.springframework.util; |
||||
|
||||
import java.util.Date; |
||||
|
||||
/** |
||||
* Default implementation of {@link ResponseTimeMonitor}. |
||||
* |
||||
* @author Rod Johnson |
||||
* @since November 21, 2000 |
||||
* @deprecated as of Spring 2.5, to be removed in Spring 3.0 |
||||
*/ |
||||
public class ResponseTimeMonitorImpl implements ResponseTimeMonitor { |
||||
|
||||
/** The system time at which this object was initialized */ |
||||
private final long initedMillis = System.currentTimeMillis(); |
||||
|
||||
/** The number of operations recorded by this object */ |
||||
private volatile int accessCount; |
||||
|
||||
/** The sum of the response times for all operations */ |
||||
private volatile int totalResponseTimeMillis = 0; |
||||
|
||||
/** The best response time this object has recorded */ |
||||
private volatile int bestResponseTimeMillis = Integer.MAX_VALUE; |
||||
|
||||
/** The worst response time this object has recorded */ |
||||
private volatile int worstResponseTimeMillis = Integer.MIN_VALUE; |
||||
|
||||
|
||||
/** |
||||
* Return the date when this object was loaded. |
||||
*/ |
||||
public Date getLoadDate() { |
||||
return new Date(this.initedMillis); |
||||
} |
||||
|
||||
/** |
||||
* Return the number of hits this object has handled. |
||||
*/ |
||||
public int getAccessCount() { |
||||
return this.accessCount; |
||||
} |
||||
|
||||
/** |
||||
* Return the number of milliseconds since this object was loaded. |
||||
*/ |
||||
public long getUptimeMillis() { |
||||
return System.currentTimeMillis() - this.initedMillis; |
||||
} |
||||
|
||||
/** |
||||
* Return the average response time achieved by this object. |
||||
*/ |
||||
public int getAverageResponseTimeMillis() { |
||||
int count = getAccessCount(); |
||||
// avoid division by 0
|
||||
return (count != 0 ? this.totalResponseTimeMillis / count : 0); |
||||
} |
||||
|
||||
/** |
||||
* Return the best (lowest) response time achieved by this object. |
||||
*/ |
||||
public int getBestResponseTimeMillis() { |
||||
return this.bestResponseTimeMillis; |
||||
} |
||||
|
||||
/** |
||||
* Return the worst (slowest) response time achieved by this object. |
||||
*/ |
||||
public int getWorstResponseTimeMillis() { |
||||
return this.worstResponseTimeMillis; |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Utility method to record this response time, updating |
||||
* the best and worst response times if necessary. |
||||
* @param responseTimeMillis the response time of this request |
||||
*/ |
||||
public synchronized void recordResponseTime(long responseTimeMillis) { |
||||
++this.accessCount; |
||||
int iResponseTime = (int) responseTimeMillis; |
||||
this.totalResponseTimeMillis += iResponseTime; |
||||
if (iResponseTime < this.bestResponseTimeMillis) { |
||||
this.bestResponseTimeMillis = iResponseTime; |
||||
} |
||||
if (iResponseTime > this.worstResponseTimeMillis) { |
||||
this.worstResponseTimeMillis = iResponseTime; |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Return a human-readable string showing the performance |
||||
* data recorded by this object. |
||||
*/ |
||||
public synchronized String toString() { |
||||
StringBuffer sb = new StringBuffer(); |
||||
sb.append("hits=[").append(getAccessCount()).append("]; "); |
||||
sb.append("average=[").append(getAverageResponseTimeMillis()).append("ms]; "); |
||||
sb.append("best=[").append(getBestResponseTimeMillis()).append("ms]; "); |
||||
sb.append("worst=[").append(getWorstResponseTimeMillis()).append("ms]"); |
||||
return sb.toString(); |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue