summaryrefslogtreecommitdiff
path: root/src/traffic-incidents-service/org.genivi.trafficinfo.dbus-java/src/main/java/org/freedesktop/dbus/ExportedObject.java
blob: 3afa54bcadf2f5cd496115f9a82bf421fac8f223 (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
   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.ref.Reference;
import java.lang.ref.WeakReference;

import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import org.freedesktop.dbus.exceptions.DBusException;
import org.freedesktop.dbus.exceptions.DBusExecutionException;

class ExportedObject
{
   @SuppressWarnings("unchecked")
   private String getAnnotations(AnnotatedElement c)
   {
      String ans = "";
      for (Annotation a: c.getDeclaredAnnotations()) {
         Class t = a.annotationType();
         String value = "";
         try {
            Method m = t.getMethod("value");
            value = m.invoke(a).toString();
         } catch (NoSuchMethodException NSMe) {
         } catch (InvocationTargetException ITe) {
         } catch (IllegalAccessException IAe) {}

         ans += "  <annotation name=\""+AbstractConnection.dollar_pattern.matcher(t.getName()).replaceAll(".")+"\" value=\""+value+"\" />\n";
      }
      return ans;
   }
   @SuppressWarnings("unchecked")
   private Map<MethodTuple,Method> getExportedMethods(Class c) throws DBusException
   {
      if (DBusInterface.class.equals(c)) return new HashMap<MethodTuple,Method>();
      Map<MethodTuple,Method> m = new HashMap<MethodTuple,Method>();
      for (Class i: c.getInterfaces())
         if (DBusInterface.class.equals(i)) {
            // add this class's public methods
            if (null != c.getAnnotation(DBusInterfaceName.class)) {
               String name = ((DBusInterfaceName) c.getAnnotation(DBusInterfaceName.class)).value();
               introspectiondata += " <interface name=\""+name+"\">\n";
               DBusSignal.addInterfaceMap(c.getName(), name);
            } else {
               // don't let people export things which don't have a
               // valid D-Bus interface name
               if (c.getName().equals(c.getSimpleName()))
                  throw new DBusException(_("DBusInterfaces cannot be declared outside a package"));
               if (c.getName().length() > DBusConnection.MAX_NAME_LENGTH) 
                  throw new DBusException(_("Introspected interface name exceeds 255 characters. Cannot export objects of type ")+c.getName());
               else
                  introspectiondata += " <interface name=\""+AbstractConnection.dollar_pattern.matcher(c.getName()).replaceAll(".")+"\">\n";
            }
            introspectiondata += getAnnotations(c);
            for (Method meth: c.getDeclaredMethods()) 
               if (Modifier.isPublic(meth.getModifiers())) {
                  String ms = "";
                  String name;
                  if (meth.isAnnotationPresent(DBusMemberName.class))
                     name = meth.getAnnotation(DBusMemberName.class).value();
                  else
                     name = meth.getName();
                  if (name.length() > DBusConnection.MAX_NAME_LENGTH) 
                     throw new DBusException(_("Introspected method name exceeds 255 characters. Cannot export objects with method ")+name);
                  introspectiondata += "  <method name=\""+name+"\" >\n";
                  introspectiondata += getAnnotations(meth);
                  for (Class ex: meth.getExceptionTypes())
                     if (DBusExecutionException.class.isAssignableFrom(ex))
                        introspectiondata +=
                           "   <annotation name=\"org.freedesktop.DBus.Method.Error\" value=\""+AbstractConnection.dollar_pattern.matcher(ex.getName()).replaceAll(".")+"\" />\n";
                  for (Type pt: meth.getGenericParameterTypes())
                     for (String s: Marshalling.getDBusType(pt)) {
                        introspectiondata += "   <arg type=\""+s+"\" direction=\"in\"/>\n";
                        ms += s;
                     }
                  if (!Void.TYPE.equals(meth.getGenericReturnType())) {
                     if (Tuple.class.isAssignableFrom((Class) meth.getReturnType())) {
                        ParameterizedType tc = (ParameterizedType) meth.getGenericReturnType();
                        Type[] ts = tc.getActualTypeArguments();

                        for (Type t: ts)
                           if (t != null)
                              for (String s: Marshalling.getDBusType(t))
                                 introspectiondata += "   <arg type=\""+s+"\" direction=\"out\"/>\n";
                     } else if (Object[].class.equals(meth.getGenericReturnType())) {
                        throw new DBusException(_("Return type of Object[] cannot be introspected properly"));
                     } else
                        for (String s: Marshalling.getDBusType(meth.getGenericReturnType()))
                        introspectiondata += "   <arg type=\""+s+"\" direction=\"out\"/>\n";
                  }
                  introspectiondata += "  </method>\n";
                  m.put(new MethodTuple(name, ms), meth);
               }
            for (Class sig: c.getDeclaredClasses()) 
               if (DBusSignal.class.isAssignableFrom(sig)) {
                  String name;
                  if (sig.isAnnotationPresent(DBusMemberName.class)) {
                     name = ((DBusMemberName) sig.getAnnotation(DBusMemberName.class)).value();
                     DBusSignal.addSignalMap(sig.getSimpleName(), name);
                  } else
                     name = sig.getSimpleName();
                  if (name.length() > DBusConnection.MAX_NAME_LENGTH) 
                     throw new DBusException(_("Introspected signal name exceeds 255 characters. Cannot export objects with signals of type ")+name);
                  introspectiondata += "  <signal name=\""+name+"\">\n";
                  Constructor con = sig.getConstructors()[0];
                  Type[] ts = con.getGenericParameterTypes();
                  for (int j = 1; j < ts.length; j++)
                     for (String s: Marshalling.getDBusType(ts[j]))
                        introspectiondata += "   <arg type=\""+s+"\" direction=\"out\" />\n";
                  introspectiondata += getAnnotations(sig);
                  introspectiondata += "  </signal>\n";

               }
            introspectiondata += " </interface>\n";
         } else {
            // recurse
            m.putAll(getExportedMethods(i));
         }
      return m;
   }
   Map<MethodTuple,Method> methods;
   Reference<DBusInterface> object;
   String introspectiondata;
   public ExportedObject(DBusInterface object, boolean weakreferences) throws DBusException
   {
      if (weakreferences)
         this.object = new WeakReference<DBusInterface>(object);
      else
         this.object = new StrongReference<DBusInterface>(object);
      introspectiondata = "";
      methods = getExportedMethods(object.getClass());
      introspectiondata += 
         " <interface name=\"org.freedesktop.DBus.Introspectable\">\n"+
         "  <method name=\"Introspect\">\n"+
         "   <arg type=\"s\" direction=\"out\"/>\n"+
         "  </method>\n"+
         " </interface>\n";
      introspectiondata += 
         " <interface name=\"org.freedesktop.DBus.Peer\">\n"+
         "  <method name=\"Ping\">\n"+
         "  </method>\n"+
         " </interface>\n";
   }
}