bump product version to 5.0.4.1
[LibreOffice.git] / jurt / com / sun / star / lib / uno / environments / remote / Job.java
bloba9cd28d5c0dfedc4a027502d08d4b770c0891e81
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package com.sun.star.lib.uno.environments.remote;
22 import java.io.PrintWriter;
23 import java.io.StringWriter;
25 import java.lang.reflect.InvocationTargetException;
27 import com.sun.star.lib.uno.typedesc.MethodDescription;
28 import com.sun.star.uno.Any;
29 import com.sun.star.uno.IMethodDescription;
30 import com.sun.star.uno.Type;
31 import com.sun.star.uno.UnoRuntime;
32 import com.sun.star.uno.XCurrentContext;
34 /**
35 * The Job is an abstraction for tasks which have to be done
36 * remotely because of a method invocation.
38 * @see com.sun.star.lib.uno.environments.remote.ThreadId
39 * @see com.sun.star.lib.uno.environments.remote.IReceiver
40 * @since UDK1.0
42 public class Job {
43 protected Job _next;
45 protected IReceiver _iReceiver;
46 protected Message _iMessage;
47 Object _disposeId;
49 protected Object _object;
51 public Job(Object object, IReceiver iReceiver, Message iMessage) {
52 _object = object;
53 _iReceiver = iReceiver;
54 _iMessage = iMessage;
57 /**
58 * Dispatches a <code>queryInterface</code> call.
60 * @return the result of the call (should be an <code>Any</code>).
62 protected Object dispatch_queryInterface(Type type) {
63 Class<?> zInterface = type.getTypeDescription().getZClass();
65 Object result = null;
67 Object face = UnoRuntime.queryInterface(zInterface, _object);
68 // the hell knows why, but empty interfaces a given back as void anys
69 if(face != null)
70 result = new Any(type, face);
71 return result;
74 /**
75 * Execute the job.
77 * @return the result of the message.
79 public Object execute() throws Throwable {
80 if (_iMessage.isRequest()) {
81 Object result = null;
82 Throwable exception = null;
83 IMethodDescription md = _iMessage.getMethod();
84 Object[] args = _iMessage.getArguments();
85 XCurrentContext oldCC = UnoRuntime.getCurrentContext();
86 UnoRuntime.setCurrentContext(_iMessage.getCurrentContext());
87 try {
88 result = md.getIndex() == MethodDescription.ID_QUERY_INTERFACE
89 ? dispatch_queryInterface((Type) args[0])
90 : md.getMethod().invoke(_object, args);
91 } catch (InvocationTargetException e) {
92 exception = e.getCause();
93 if (exception == null) {
94 exception = e;
96 } catch (Exception e) {
97 exception = e;
98 } finally {
99 UnoRuntime.setCurrentContext(oldCC);
101 if (_iMessage.isSynchronous()) {
102 if (exception == null) {
103 _iReceiver.sendReply(
104 false, _iMessage.getThreadId(), result);
105 } else {
106 // Here we have to be aware of non-UNO exceptions, because
107 // they may kill a remote side which does not know anything
108 // about their types:
109 if (!(exception instanceof com.sun.star.uno.Exception)
110 && !(exception instanceof
111 com.sun.star.uno.RuntimeException))
113 StringWriter writer = new StringWriter();
114 exception.printStackTrace(new PrintWriter(writer));
115 exception = new com.sun.star.uno.RuntimeException(
116 "Java exception: <" + writer + ">", null);
118 _iReceiver.sendReply(
119 true, _iMessage.getThreadId(), exception);
122 return null;
123 } else if (_iMessage.isAbnormalTermination()) {
124 throw remoteUnoRequestRaisedException(_iMessage.getResult());
125 } else {
126 return _iMessage.getResult();
130 public ThreadId getThreadId() {
131 return _iMessage.getThreadId();
134 public boolean isRequest() {
135 return _iMessage.isRequest();
138 public boolean isSynchronous() {
139 return _iMessage.isSynchronous();
142 public void dispose() {
143 // _oId = null;
144 // _iReceiver = null;
145 // _threadId = null;
146 // _object = null;
147 // _operation = null;
148 // _param = null;
149 // _exception = null;
150 // _zInterface = null;
151 // _disposeId = null;
155 * The name of this method is chosen to generate a somewhat self-explanatory
156 * stack trace.
158 private Exception remoteUnoRequestRaisedException(Object exception) {
159 Exception e = (Exception) exception;
160 e.fillInStackTrace();
161 return e;