001: /*
002: * Copyright 2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.ws.server.endpoint;
018:
019: import java.lang.reflect.InvocationTargetException;
020: import java.lang.reflect.Method;
021:
022: import org.springframework.util.Assert;
023:
024: /**
025: * Represents a bean method that will be invoked as part of an incoming Web service message.
026: * <p/>
027: * Consists of a {@link Method}, and a bean {@link Object}.
028: *
029: * @author Arjen Poutsma
030: * @since 1.0.0
031: */
032: public final class MethodEndpoint {
033:
034: private Object bean;
035:
036: private Method method;
037:
038: /**
039: * Constructs a new method endpoint with the given bean and method.
040: *
041: * @param bean the object bean
042: * @param method the method
043: */
044: public MethodEndpoint(Object bean, Method method) {
045: Assert.notNull(bean, "bean must not be null");
046: Assert.notNull(method, "method must not be null");
047: this .bean = bean;
048: this .method = method;
049: }
050:
051: /**
052: * Constructs a new method endpoint with the given bean, method name and parameters.
053: *
054: * @param bean the object bean
055: * @param methodName the method name
056: * @param parameterTypes the method parameter types
057: * @throws NoSuchMethodException when the method cannot be found
058: */
059: public MethodEndpoint(Object bean, String methodName,
060: Class[] parameterTypes) throws NoSuchMethodException {
061: Assert.notNull(bean, "bean must not be null");
062: Assert.notNull(methodName, "method must not be null");
063: this .bean = bean;
064: this .method = bean.getClass().getMethod(methodName,
065: parameterTypes);
066: }
067:
068: /** Returns the object bean for this method endpoint. */
069: public Object getBean() {
070: return this .bean;
071: }
072:
073: /** Returns the method for this method endpoint. */
074: public Method getMethod() {
075: return this .method;
076: }
077:
078: /**
079: * Invokes this method endpoint with the given arguments.
080: *
081: * @param args the arguments
082: * @return the invocation result
083: * @throws Exception when the method invocation results in an exception
084: */
085: public Object invoke(Object[] args) throws Exception {
086: try {
087: return this .method.invoke(this .bean, args);
088: } catch (InvocationTargetException ex) {
089: handleInvocationTargetException(ex);
090: throw new IllegalStateException(
091: "Unexpected exception thrown by method - "
092: + ex.getTargetException().getClass()
093: .getName() + ": "
094: + ex.getTargetException().getMessage());
095: }
096: }
097:
098: private void handleInvocationTargetException(
099: InvocationTargetException ex) throws Exception {
100: if (ex.getTargetException() instanceof RuntimeException) {
101: throw (RuntimeException) ex.getTargetException();
102: }
103: if (ex.getTargetException() instanceof Error) {
104: throw (Error) ex.getTargetException();
105: }
106: if (ex.getTargetException() instanceof Exception) {
107: throw (Exception) ex.getTargetException();
108: }
109:
110: }
111:
112: public boolean equals(Object o) {
113: if (this == o) {
114: return true;
115: }
116: if (o != null && o instanceof MethodEndpoint) {
117: MethodEndpoint other = (MethodEndpoint) o;
118: return this .bean.equals(other.bean)
119: && this .method.equals(other.method);
120: }
121: return false;
122: }
123:
124: public int hashCode() {
125: return 31 * this .bean.hashCode() + this .method.hashCode();
126: }
127:
128: public String toString() {
129: return this.method.toString();
130: }
131: }
|