1*2be43276SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*2be43276SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*2be43276SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*2be43276SAndrew Rist * distributed with this work for additional information 6*2be43276SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*2be43276SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*2be43276SAndrew Rist * "License"); you may not use this file except in compliance 9*2be43276SAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 11*2be43276SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 13*2be43276SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*2be43276SAndrew Rist * software distributed under the License is distributed on an 15*2be43276SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*2be43276SAndrew Rist * KIND, either express or implied. See the License for the 17*2be43276SAndrew Rist * specific language governing permissions and limitations 18*2be43276SAndrew Rist * under the License. 19cdf0e10cSrcweir * 20*2be43276SAndrew Rist *************************************************************/ 21*2be43276SAndrew Rist 22*2be43276SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir package com.sun.star.lib.uno.environments.remote; 25cdf0e10cSrcweir 26cdf0e10cSrcweir 27cdf0e10cSrcweir import java.io.PrintWriter; 28cdf0e10cSrcweir import java.io.StringWriter; 29cdf0e10cSrcweir 30cdf0e10cSrcweir 31cdf0e10cSrcweir import java.lang.reflect.InvocationTargetException; 32cdf0e10cSrcweir 33cdf0e10cSrcweir import com.sun.star.lib.uno.typedesc.MethodDescription; 34cdf0e10cSrcweir import com.sun.star.uno.Any; 35cdf0e10cSrcweir import com.sun.star.uno.IMethodDescription; 36cdf0e10cSrcweir import com.sun.star.uno.Type; 37cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 38cdf0e10cSrcweir import com.sun.star.uno.XCurrentContext; 39cdf0e10cSrcweir 40cdf0e10cSrcweir /** 41cdf0e10cSrcweir * The Job is an abstraction for tasks which have to be done 42cdf0e10cSrcweir * remotely because of a method invocation. 43cdf0e10cSrcweir * <p> 44cdf0e10cSrcweir * @version $Revision: 1.17 $ $ $Date: 2008-04-11 11:21:00 $ 45cdf0e10cSrcweir * @author Kay Ramme 46cdf0e10cSrcweir * @see com.sun.star.lib.uno.environments.remote.ThreadID 47cdf0e10cSrcweir * @see com.sun.star.lib.uno.environments.remote.IReceiver 48cdf0e10cSrcweir * @since UDK1.0 49cdf0e10cSrcweir */ 50cdf0e10cSrcweir public class Job { 51cdf0e10cSrcweir protected Job _next; 52cdf0e10cSrcweir 53cdf0e10cSrcweir protected IReceiver _iReceiver; 54cdf0e10cSrcweir protected Message _iMessage; 55cdf0e10cSrcweir Object _disposeId; 56cdf0e10cSrcweir 57cdf0e10cSrcweir protected Object _object; 58cdf0e10cSrcweir Job(Object object, IReceiver iReceiver, Message iMessage)59cdf0e10cSrcweir public Job(Object object, IReceiver iReceiver, Message iMessage) { 60cdf0e10cSrcweir _object = object; 61cdf0e10cSrcweir _iReceiver = iReceiver; 62cdf0e10cSrcweir _iMessage = iMessage; 63cdf0e10cSrcweir } 64cdf0e10cSrcweir 65cdf0e10cSrcweir /** 66cdf0e10cSrcweir * Dispatches a <code>queryInterface</code> call 67cdf0e10cSrcweir * <p> 68cdf0e10cSrcweir * @return the result of the call (should be an <code>Any</code>) 69cdf0e10cSrcweir * @param message the parameter for the call 70cdf0e10cSrcweir * @param resultClass the result type as an out parameter 71cdf0e10cSrcweir * @param status the status as an out parameter 72cdf0e10cSrcweir * @param o_outs the out parameters of the call as out parameters 73cdf0e10cSrcweir * @param o_out_sig the out signature as an out parameter 74cdf0e10cSrcweir */ dispatch_queryInterface(Type type)75cdf0e10cSrcweir protected Object dispatch_queryInterface(Type type) { 76cdf0e10cSrcweir Class zInterface = type.getTypeDescription().getZClass(); 77cdf0e10cSrcweir 78cdf0e10cSrcweir Object result = null; 79cdf0e10cSrcweir 80cdf0e10cSrcweir Object face = UnoRuntime.queryInterface(zInterface, _object); 81cdf0e10cSrcweir // the hell knows why, but empty interfaces a given back as void anys 82cdf0e10cSrcweir if(face != null) 83cdf0e10cSrcweir result = new Any(type, face); 84cdf0e10cSrcweir return result; 85cdf0e10cSrcweir } 86cdf0e10cSrcweir 87cdf0e10cSrcweir /** 88cdf0e10cSrcweir * Execute the job. 89cdf0e10cSrcweir * 90cdf0e10cSrcweir * @return the result of the message. 91cdf0e10cSrcweir */ execute()92cdf0e10cSrcweir public Object execute() throws Throwable { 93cdf0e10cSrcweir Object msgResult = _iMessage.getResult(); 94cdf0e10cSrcweir if (_iMessage.isRequest()) { 95cdf0e10cSrcweir Object result = null; 96cdf0e10cSrcweir Throwable exception = null; 97cdf0e10cSrcweir IMethodDescription md = _iMessage.getMethod(); 98cdf0e10cSrcweir Object[] args = _iMessage.getArguments(); 99cdf0e10cSrcweir XCurrentContext oldCC = UnoRuntime.getCurrentContext(); 100cdf0e10cSrcweir UnoRuntime.setCurrentContext(_iMessage.getCurrentContext()); 101cdf0e10cSrcweir try { 102cdf0e10cSrcweir result = md.getIndex() == MethodDescription.ID_QUERY_INTERFACE 103cdf0e10cSrcweir ? dispatch_queryInterface((Type) args[0]) 104cdf0e10cSrcweir : md.getMethod().invoke(_object, args); 105cdf0e10cSrcweir } catch (InvocationTargetException e) { 106cdf0e10cSrcweir exception = e.getTargetException(); 107cdf0e10cSrcweir if (exception == null) { 108cdf0e10cSrcweir exception = e; 109cdf0e10cSrcweir } 110cdf0e10cSrcweir } catch (Exception e) { 111cdf0e10cSrcweir exception = e; 112cdf0e10cSrcweir } finally { 113cdf0e10cSrcweir UnoRuntime.setCurrentContext(oldCC); 114cdf0e10cSrcweir } 115cdf0e10cSrcweir if (_iMessage.isSynchronous()) { 116cdf0e10cSrcweir if (exception == null) { 117cdf0e10cSrcweir _iReceiver.sendReply( 118cdf0e10cSrcweir false, _iMessage.getThreadId(), result); 119cdf0e10cSrcweir } else { 120cdf0e10cSrcweir // Here we have to be aware of non-UNO exceptions, because 121cdf0e10cSrcweir // they may kill a remote side which does not know anything 122cdf0e10cSrcweir // about their types: 123cdf0e10cSrcweir if (exception != null 124cdf0e10cSrcweir && !(exception instanceof com.sun.star.uno.Exception) 125cdf0e10cSrcweir && !(exception instanceof 126cdf0e10cSrcweir com.sun.star.uno.RuntimeException)) 127cdf0e10cSrcweir { 128cdf0e10cSrcweir StringWriter writer = new StringWriter(); 129cdf0e10cSrcweir exception.printStackTrace(new PrintWriter(writer)); 130cdf0e10cSrcweir exception = new com.sun.star.uno.RuntimeException( 131cdf0e10cSrcweir "Java exception: <" + writer + ">", null); 132cdf0e10cSrcweir } 133cdf0e10cSrcweir _iReceiver.sendReply( 134cdf0e10cSrcweir true, _iMessage.getThreadId(), exception); 135cdf0e10cSrcweir } 136cdf0e10cSrcweir } 137cdf0e10cSrcweir return null; 138cdf0e10cSrcweir } else if (_iMessage.isAbnormalTermination()) { 139cdf0e10cSrcweir throw remoteUnoRequestRaisedException(_iMessage.getResult()); 140cdf0e10cSrcweir } else { 141cdf0e10cSrcweir return _iMessage.getResult(); 142cdf0e10cSrcweir } 143cdf0e10cSrcweir } 144cdf0e10cSrcweir getThreadId()145cdf0e10cSrcweir public ThreadId getThreadId() { 146cdf0e10cSrcweir return _iMessage.getThreadId(); 147cdf0e10cSrcweir } 148cdf0e10cSrcweir isRequest()149cdf0e10cSrcweir public boolean isRequest() { 150cdf0e10cSrcweir return _iMessage.isRequest(); 151cdf0e10cSrcweir } 152cdf0e10cSrcweir isSynchronous()153cdf0e10cSrcweir public boolean isSynchronous() { 154cdf0e10cSrcweir return _iMessage.isSynchronous(); 155cdf0e10cSrcweir } 156cdf0e10cSrcweir dispose()157cdf0e10cSrcweir public void dispose() { 158cdf0e10cSrcweir // _oId = null; 159cdf0e10cSrcweir // _iReceiver = null; 160cdf0e10cSrcweir // _threadId = null; 161cdf0e10cSrcweir // _object = null; 162cdf0e10cSrcweir // _operation = null; 163cdf0e10cSrcweir // _param = null; 164cdf0e10cSrcweir // _exception = null; 165cdf0e10cSrcweir // _zInterface = null; 166cdf0e10cSrcweir // _disposeId = null; 167cdf0e10cSrcweir } 168cdf0e10cSrcweir 169cdf0e10cSrcweir // The name of this method is chosen to generate a somewhat self-explanatory 170cdf0e10cSrcweir // stack trace: remoteUnoRequestRaisedException(Object exception)171cdf0e10cSrcweir private Exception remoteUnoRequestRaisedException(Object exception) { 172cdf0e10cSrcweir Exception e = (Exception) exception; 173cdf0e10cSrcweir e.fillInStackTrace(); 174cdf0e10cSrcweir return e; 175cdf0e10cSrcweir } 176cdf0e10cSrcweir } 177