My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
167
168
/**
* BlueCove - Java library for Bluetooth
* Copyright (C) 2008-2009 Michael Lifshits
* Copyright (C) 2008-2009 Vlad Skarzhevskyy
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
* @author vlads
* @version $Id$
*/
package com.intel.bluetooth.rmi;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.security.AccessControlContext;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.List;
import java.util.Vector;

public class Client {

private static final String rmiRegistryHostDefault = "localhost";

private static final int rmiRegistryPortDefault = Server.rmiRegistryPortDefault;

private static RemoteService remoteService;

private static class ServiceProxy implements InvocationHandler {

private AccessControlContext accessControlContext;

private ServiceProxy() {
accessControlContext = AccessController.getContext();
}

public Object invoke(Object proxy, final Method m, Object[] args) throws Throwable {
final ServiceRequest request = new ServiceRequest(m.getDeclaringClass().getCanonicalName(), m.getName(), m
.getParameterTypes(), args);
ServiceResponse response;
try {
response = AccessController.doPrivileged(new PrivilegedExceptionAction<ServiceResponse>() {
public ServiceResponse run() throws RuntimeException {
return execute(request, m);
}
}, accessControlContext);
} catch (PrivilegedActionException e) {
Throwable cause = e.getCause();
if (cause != null) {
throw new RuntimeException(cause.getMessage(), cause);
} else {
throw e;
}
}
if (response.getException() == null) {
return response.getReturnValue();
} else {
Throwable t = response.getException();
// Build combined StackTrace
StackTraceElement[] remote = t.getStackTrace();
StackTraceElement[] curreent = Thread.currentThread().getStackTrace();
List<StackTraceElement> combined = new Vector<StackTraceElement>();
for (int i = 0; i < remote.length; i++) {
combined.add(remote[i]);
if ((remote[i].getMethodName().equals(m.getName()))
&& (remote[i].getClassName().startsWith(m.getDeclaringClass().getCanonicalName()))) {
break;
}
}
int startClient = curreent.length;
for (int i = 0; i < curreent.length; i++) {
if (curreent[i].getClassName().equals(this.getClass().getName())) {
startClient = i + 1;
break;
}
}
for (int i = startClient; i < curreent.length; i++) {
combined.add(curreent[i]);
}
t.setStackTrace(combined.toArray(new StackTraceElement[combined.size()]));
throw t;
}
}
}

private static String getRemoteExceptionMessage(RemoteException e) {
String message = e.getMessage();
int idx = message.indexOf("; nested exception is:");
if (idx != -1) {
return message.substring(0, idx);
}
return message;
}

public synchronized static Object getService(Class<?> interfaceClass, boolean isMaster, String host, String port)
throws RuntimeException {
if (remoteService == null) {
try {
if (isMaster) {
if ((host != null) && (!"localhost".equals(host))) {
throw new IllegalArgumentException("Can't start RMI registry while connecting to remote host "
+ host);
}
Server.start(port);
}
remoteService = getRemoteService(host, port);
remoteService.verify(interfaceClass.getCanonicalName());
} catch (RemoteException e) {
Throwable t = (e.getCause() != null) ? e.getCause() : e;
throw new RuntimeException(getRemoteExceptionMessage(e), t);
} catch (NotBoundException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
Class<?>[] allInterfaces = new Class[interfaceClass.getInterfaces().length + 1];
allInterfaces[0] = interfaceClass;
System.arraycopy(interfaceClass.getInterfaces(), 0, allInterfaces, 1, interfaceClass.getInterfaces().length);
return Proxy.newProxyInstance(interfaceClass.getClassLoader(), allInterfaces, new ServiceProxy());
}

private static ServiceResponse execute(ServiceRequest request, Method method) throws RuntimeException {
try {
return remoteService.execute(request);
} catch (RemoteException e) {
Throwable t = (e.getCause() != null) ? e.getCause() : e;
throw new RuntimeException(getRemoteExceptionMessage(e), t);
}
}

private static RemoteService getRemoteService(String host, String port) throws RemoteException, NotBoundException {
String rmiHost = rmiRegistryHostDefault;
if ((host != null) && (host.length() > 0)) {
rmiHost = host;
}
int rmiPort = rmiRegistryPortDefault;
if ((port != null) && (port.length() > 0)) {
rmiPort = Integer.parseInt(port);
}
if (rmiPort == 0) {
// in process server
return new RemoteServiceImpl();
} else {
Registry registry = LocateRegistry.getRegistry(rmiHost, rmiPort);
return (RemoteService) registry.lookup(RemoteService.SERVICE_NAME);
}
}
}

Change log

r2917 by skarzhevskyy on Mar 13, 2009   Diff
Updated javadocs and Copyright
Go to: 
Project members, sign in to write a code review

Older revisions

r2471 by skarzhevskyy on Nov 30, 2008   Diff
Change license to Apache License,
Version 2.0, Update headers
r2408 by skarzhevskyy on Oct 9, 2008   Diff
organize product to modules
r2252 by skarzhevskyy on May 31, 2008   Diff
make it work under security manager
All revisions of this file

File info

Size: 5953 bytes, 168 lines

File properties

svn:mime-type
text/plain
svn:eol-style
native
svn:keywords
Date Author Id Revision
Powered by Google Project Hosting