001: /*
002: * JBoss, Home of Professional Open Source
003: * Copyright 2005, JBoss Inc., and individual contributors as indicated
004: * by the @authors tag. See the copyright.txt in the distribution for a
005: * full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.aspects.remoting;
023:
024: import java.io.ObjectStreamException;
025:
026: import org.jboss.logging.Logger;
027: import org.jboss.remoting.Client;
028: import org.jboss.remoting.InvokerLocator;
029:
030: /**
031: * Checks to see if this object is local in VM
032: *
033: * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
034: * @version $Revision: 59458 $
035: */
036: public class InvokeRemoteInterceptor implements
037: org.jboss.aop.advice.Interceptor, java.io.Serializable {
038: private static final Logger log = Logger
039: .getLogger(InvokeRemoteInterceptor.class);
040:
041: private static final long serialVersionUID = -145166951731929406L;
042:
043: public static final InvokeRemoteInterceptor singleton = new InvokeRemoteInterceptor();
044: public static final String REMOTING = "REMOTING";
045: public static final String INVOKER_LOCATOR = "INVOKER_LOCATOR";
046: public static final String SUBSYSTEM = "SUBSYSTEM";
047:
048: public String getName() {
049: return "InvokeRemoteInterceptor";
050: }
051:
052: public Object invoke(org.jboss.aop.joinpoint.Invocation invocation)
053: throws Throwable {
054: InvokerLocator locator = (InvokerLocator) invocation
055: .getMetaData(REMOTING, INVOKER_LOCATOR);
056: if (locator == null) {
057: throw new RuntimeException(
058: "No InvokerLocator supplied. Can't invoke remotely!");
059: }
060: String subsystem = (String) invocation.getMetaData(REMOTING,
061: SUBSYSTEM);
062: if (subsystem == null)
063: subsystem = "AOP";
064: Client client = new Client(locator, subsystem);
065:
066: try {
067: client.connect();
068: org.jboss.aop.joinpoint.InvocationResponse response = (org.jboss.aop.joinpoint.InvocationResponse) client
069: .invoke(invocation, null);
070: invocation
071: .setResponseContextInfo(response.getContextInfo());
072: return response.getResponse();
073: } catch (Exception e) {
074: StackTraceElement[] serverStackTrace;
075: if (e.getCause() != null)
076: serverStackTrace = e.getCause().getStackTrace();
077: else
078: serverStackTrace = e.getStackTrace();
079:
080: Exception clientException = new Exception();
081: StackTraceElement[] clientStackTrace = clientException
082: .getStackTrace();
083: StackTraceElement[] completeStackTrace = new StackTraceElement[serverStackTrace.length
084: + clientStackTrace.length];
085: System.arraycopy(serverStackTrace, 0, completeStackTrace,
086: 0, serverStackTrace.length);
087: System.arraycopy(clientStackTrace, 0, completeStackTrace,
088: serverStackTrace.length, clientStackTrace.length);
089:
090: if (e.getCause() != null)
091: e.getCause().setStackTrace(completeStackTrace);
092: else
093: e.setStackTrace(completeStackTrace);
094:
095: throw e;
096: } finally {
097: client.disconnect();
098: }
099: }
100:
101: Object readResolve() throws ObjectStreamException {
102: return singleton;
103: }
104: }
|