summaryrefslogtreecommitdiff
path: root/src/traffic-incidents-service/org.genivi.trafficinfo.dbus-java/src/main/java/org/freedesktop/dbus/DBusAsyncReply.java
blob: 252cf0747c21db690f59129ab80840cf9bd41cf1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
   D-Bus Java Implementation
   Copyright (c) 2005-2006 Matthew Johnson

   This program is free software; you can redistribute it and/or modify it
   under the terms of either the GNU Lesser General Public License Version 2 or the
   Academic Free Licence Version 2.1.

   Full licence texts are included in the COPYING file with this program.
*/
package org.freedesktop.dbus;

import static org.freedesktop.dbus.Gettext._;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import org.freedesktop.DBus.Error.NoReply;
import org.freedesktop.dbus.exceptions.DBusException;
import org.freedesktop.dbus.exceptions.DBusExecutionException;

import cx.ath.matthew.debug.Debug;

/**
 * A handle to an asynchronous method call.
 */
public class DBusAsyncReply<ReturnType>
{
   /**
    * Check if any of a set of asynchronous calls have had a reply.
    * @param replies A Collection of handles to replies to check.
    * @return A Collection only containing those calls which have had replies.
    */
   public static Collection<DBusAsyncReply<? extends Object>> hasReply(Collection<DBusAsyncReply<? extends Object>> replies)
   {
      Collection<DBusAsyncReply<? extends Object>> c = new ArrayList<DBusAsyncReply<? extends Object>>(replies);
      Iterator<DBusAsyncReply<? extends Object>> i = c.iterator();
      while (i.hasNext())
         if (!i.next().hasReply()) i.remove();
      return c;
   }

   private ReturnType rval = null;
   private DBusExecutionException error = null;
   private MethodCall mc;
   private Method me;
   private AbstractConnection conn;
   DBusAsyncReply(MethodCall mc, Method me, AbstractConnection conn)
   {
      this.mc = mc;
      this.me = me;
      this.conn = conn;
   }
   @SuppressWarnings("unchecked")
   private synchronized void checkReply()
   {
      if (mc.hasReply()) {
         Message m = mc.getReply();
         if (m instanceof Error)
            error = ((Error) m).getException();
         else if (m instanceof MethodReturn) {
            try {
               rval = (ReturnType) RemoteInvocationHandler.convertRV(m.getSig(), m.getParameters(), me, conn);
            } catch (DBusExecutionException DBEe) {
               error = DBEe;
            } catch (DBusException DBe) {
               if (AbstractConnection.EXCEPTION_DEBUG && Debug.debug) Debug.print(Debug.ERR, DBe);
               error = new DBusExecutionException(DBe.getMessage());
            }
         }
      }
   }

   /**
    * Check if we've had a reply.
    * @return True if we have a reply
    */
   public boolean hasReply()
   {
      if (null != rval || null != error) return true;
      checkReply();
      return null != rval || null != error;
   }
   
   /**
    * Get the reply.
    * @return The return value from the method.
    * @throws DBusExecutionException if the reply to the method was an error.
    * @throws NoReply if the method hasn't had a reply yet
    */
   public ReturnType getReply() throws DBusExecutionException
   {
      if (null != rval) return rval;
      else if (null != error) throw error;
      checkReply();
      if (null != rval) return rval;
      else if (null != error) throw error;
      else throw new NoReply(_("Async call has not had a reply"));
   }

   public String toString()
   {
      return _("Waiting for: ")+mc;
   }
   Method getMethod() { return me; }
   AbstractConnection getConnection() { return conn; }
   MethodCall getCall() { return mc; }
}