merge the formfield patch from ooo-build
[ooovba.git] / jurt / com / sun / star / lib / uno / environments / remote / Job.java
blob895fba59e83e79363e0f0437b6360b28f1eaf16c
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: Job.java,v $
10 * $Revision: 1.17 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 package com.sun.star.lib.uno.environments.remote;
34 import java.io.PrintWriter;
35 import java.io.StringWriter;
38 import java.lang.reflect.InvocationTargetException;
40 import com.sun.star.lib.uno.typedesc.MethodDescription;
41 import com.sun.star.uno.Any;
42 import com.sun.star.uno.IMethodDescription;
43 import com.sun.star.uno.Type;
44 import com.sun.star.uno.UnoRuntime;
45 import com.sun.star.uno.XCurrentContext;
47 /**
48 * The Job is an abstraction for tasks which have to be done
49 * remotely because of a method invocation.
50 * <p>
51 * @version $Revision: 1.17 $ $ $Date: 2008-04-11 11:21:00 $
52 * @author Kay Ramme
53 * @see com.sun.star.lib.uno.environments.remote.ThreadID
54 * @see com.sun.star.lib.uno.environments.remote.IReceiver
55 * @since UDK1.0
57 public class Job {
58 protected Job _next;
60 protected IReceiver _iReceiver;
61 protected Message _iMessage;
62 Object _disposeId;
64 protected Object _object;
66 public Job(Object object, IReceiver iReceiver, Message iMessage) {
67 _object = object;
68 _iReceiver = iReceiver;
69 _iMessage = iMessage;
72 /**
73 * Dispatches a <code>queryInterface</code> call
74 * <p>
75 * @return the result of the call (should be an <code>Any</code>)
76 * @param message the parameter for the call
77 * @param resultClass the result type as an out parameter
78 * @param status the status as an out parameter
79 * @param o_outs the out parameters of the call as out parameters
80 * @param o_out_sig the out signature as an out parameter
82 protected Object dispatch_queryInterface(Type type) {
83 Class zInterface = type.getTypeDescription().getZClass();
85 Object result = null;
87 Object face = UnoRuntime.queryInterface(zInterface, _object);
88 // the hell knows why, but empty interfaces a given back as void anys
89 if(face != null)
90 result = new Any(type, face);
91 return result;
94 /**
95 * Execute the job.
97 * @return the result of the message.
99 public Object execute() throws Throwable {
100 Object msgResult = _iMessage.getResult();
101 if (_iMessage.isRequest()) {
102 Object result = null;
103 Throwable exception = null;
104 IMethodDescription md = _iMessage.getMethod();
105 Object[] args = _iMessage.getArguments();
106 XCurrentContext oldCC = UnoRuntime.getCurrentContext();
107 UnoRuntime.setCurrentContext(_iMessage.getCurrentContext());
108 try {
109 result = md.getIndex() == MethodDescription.ID_QUERY_INTERFACE
110 ? dispatch_queryInterface((Type) args[0])
111 : md.getMethod().invoke(_object, args);
112 } catch (InvocationTargetException e) {
113 exception = e.getTargetException();
114 if (exception == null) {
115 exception = e;
117 } catch (Exception e) {
118 exception = e;
119 } finally {
120 UnoRuntime.setCurrentContext(oldCC);
122 if (_iMessage.isSynchronous()) {
123 if (exception == null) {
124 _iReceiver.sendReply(
125 false, _iMessage.getThreadId(), result);
126 } else {
127 // Here we have to be aware of non-UNO exceptions, because
128 // they may kill a remote side which does not know anything
129 // about their types:
130 if (exception != null
131 && !(exception instanceof com.sun.star.uno.Exception)
132 && !(exception instanceof
133 com.sun.star.uno.RuntimeException))
135 StringWriter writer = new StringWriter();
136 exception.printStackTrace(new PrintWriter(writer));
137 exception = new com.sun.star.uno.RuntimeException(
138 "Java exception: <" + writer + ">", null);
140 _iReceiver.sendReply(
141 true, _iMessage.getThreadId(), exception);
144 return null;
145 } else if (_iMessage.isAbnormalTermination()) {
146 throw remoteUnoRequestRaisedException(_iMessage.getResult());
147 } else {
148 return _iMessage.getResult();
152 public ThreadId getThreadId() {
153 return _iMessage.getThreadId();
156 public boolean isRequest() {
157 return _iMessage.isRequest();
160 public boolean isSynchronous() {
161 return _iMessage.isSynchronous();
164 public void dispose() {
165 // _oId = null;
166 // _iReceiver = null;
167 // _threadId = null;
168 // _object = null;
169 // _operation = null;
170 // _param = null;
171 // _exception = null;
172 // _zInterface = null;
173 // _disposeId = null;
176 // The name of this method is chosen to generate a somewhat self-explanatory
177 // stack trace:
178 private Exception remoteUnoRequestRaisedException(Object exception) {
179 Exception e = (Exception) exception;
180 e.fillInStackTrace();
181 return e;