1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.microemu.microedition.io;
23
24 import java.lang.reflect.InvocationHandler;
25 import java.lang.reflect.InvocationTargetException;
26 import java.lang.reflect.Method;
27 import java.security.AccessControlContext;
28 import java.security.AccessController;
29 import java.security.PrivilegedActionException;
30 import java.security.PrivilegedExceptionAction;
31
32 import javax.microedition.io.Connection;
33
34 import org.microemu.log.Logger;
35
36
37
38
39
40
41
42
43 public class ConnectionInvocationHandler implements InvocationHandler {
44
45 private Connection originalConnection;
46
47
48 private AccessControlContext acc;
49
50 static {
51 Logger.addLogOrigin(ConnectionInvocationHandler.class);
52 }
53
54 public ConnectionInvocationHandler(Connection con, boolean needPrivilegedCalls) {
55 this.originalConnection = con;
56 if (needPrivilegedCalls) {
57 this.acc = AccessController.getContext();
58 }
59 }
60
61
62
63
64 public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
65 if (ConnectorImpl.debugConnectionInvocations) {
66 Logger.debug("invoke", method);
67 }
68 try {
69 if (this.acc != null) {
70 return AccessController.doPrivileged(new PrivilegedExceptionAction() {
71 public Object run() throws InvocationTargetException, IllegalAccessException {
72 return method.invoke(originalConnection, args);
73 }
74 }, acc);
75 } else {
76 return method.invoke(this.originalConnection, args);
77 }
78 } catch (PrivilegedActionException e) {
79 if (e.getCause() instanceof InvocationTargetException) {
80 if (ConnectorImpl.debugConnectionInvocations) {
81 Logger.error("Connection." + method.getName(), e.getCause().getCause());
82 }
83 throw e.getCause().getCause();
84 } else {
85 if (ConnectorImpl.debugConnectionInvocations) {
86 Logger.error("Connection." + method.getName(), e.getCause());
87 }
88 throw e.getCause();
89 }
90 } catch (InvocationTargetException e) {
91 if (ConnectorImpl.debugConnectionInvocations) {
92 Logger.error("Connection." + method.getName(), e.getCause());
93 }
94 throw e.getCause();
95 }
96 }
97
98 }