001: /*
002: * $Id: NestedInvocationHandler.java 10790 2008-02-12 20:53:32Z dfeist $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010:
011: package org.mule.routing.nested;
012:
013: import org.mule.DefaultMuleMessage;
014: import org.mule.RequestContext;
015: import org.mule.api.MuleEvent;
016: import org.mule.api.MuleMessage;
017: import org.mule.api.routing.NestedRouter;
018: import org.mule.config.i18n.CoreMessages;
019: import org.mule.util.StringMessageUtils;
020:
021: import java.lang.reflect.InvocationHandler;
022: import java.lang.reflect.Method;
023: import java.util.Map;
024:
025: import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap;
026:
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029:
030: public class NestedInvocationHandler implements InvocationHandler {
031:
032: public static final String DEFAULT_METHOD_NAME_TOKEN = "default";
033:
034: protected static Log logger = LogFactory
035: .getLog(NestedInvocationHandler.class);
036:
037: protected Map routers = new ConcurrentHashMap();
038:
039: protected NestedInvocationHandler(NestedRouter router) {
040: addRouterForInterface(router);
041: }
042:
043: public void addRouterForInterface(NestedRouter router) {
044: if (router.getMethod() == null) {
045: if (routers.size() == 0) {
046: routers.put(DEFAULT_METHOD_NAME_TOKEN, router);
047: } else {
048: throw new IllegalArgumentException(CoreMessages
049: .mustSetMethodNamesOnBinding().getMessage());
050: }
051: } else {
052: routers.put(router.getMethod(), router);
053: }
054: }
055:
056: public Object invoke(Object proxy, Method method, Object[] args)
057: throws Throwable {
058: if (method.getName().equals("toString")) {
059: return toString();
060: }
061:
062: MuleMessage message;
063:
064: if (args.length == 1) {
065: message = new DefaultMuleMessage(args[0]);
066: } else {
067: message = new DefaultMuleMessage(args);
068: }
069:
070: NestedRouter router = (NestedRouter) routers.get(method
071: .getName());
072: if (router == null) {
073: router = (NestedRouter) routers
074: .get(DEFAULT_METHOD_NAME_TOKEN);
075: }
076:
077: if (router == null) {
078: throw new IllegalArgumentException(CoreMessages
079: .cannotFindBindingForMethod(method.getName())
080: .toString());
081: }
082:
083: MuleMessage reply;
084: MuleEvent currentEvent = RequestContext.getEvent();
085: reply = router.route(message, currentEvent.getSession(), router
086: .getEndpoint().isSynchronous());
087:
088: if (reply != null) {
089: if (reply.getExceptionPayload() != null) {
090: throw reply.getExceptionPayload().getException();
091: } else {
092: return reply.getPayload();
093: }
094: } else {
095: return null;
096: }
097: }
098:
099: public String toString() {
100: final StringBuffer sb = new StringBuffer();
101: sb.append("NestedInvocation");
102: sb.append("{routers='").append(
103: StringMessageUtils.toString(routers));
104: sb.append('}');
105: return sb.toString();
106: }
107: }
|