1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.gwe.api;
18
19 import java.net.URISyntaxException;
20 import java.rmi.NotBoundException;
21 import java.rmi.Remote;
22 import java.rmi.RemoteException;
23 import java.rmi.registry.LocateRegistry;
24 import java.rmi.registry.Registry;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.gwe.drivers.netAccess.HostHandle;
29 import org.gwe.drivers.netAccess.tunneling.TunneledSocketFactory;
30 import org.gwe.persistence.model.HeadResourceInfo;
31 import org.gwe.utils.IOUtils;
32 import org.gwe.utils.concurrent.HeartBeater;
33 import org.gwe.utils.reinvoke.ReinvocationInterceptor;
34 import org.gwe.utils.security.ResourceLink;
35
36
37
38
39
40 public class ServerAPILink {
41
42 private static Log log = LogFactory.getLog(ServerAPILink.class);
43
44 public static <RT> String getRegistryId(Class<RT> remoteClass) {
45 return remoteClass.getName().replace('.', '/');
46 }
47
48 public static <RT extends Remote> String getFullRegistryId(HeadResourceInfo daemonInfo, Class<RT> remoteClass) {
49 return IOUtils.concatenatePaths(daemonInfo.getLocation(), getRegistryId(remoteClass));
50 }
51
52 private HeadResourceInfo daemonInfo;
53 private ResourceLink<HostHandle> hostLink;
54
55 public ServerAPILink(HeadResourceInfo daemonInfo, ResourceLink<HostHandle> link) {
56 this.daemonInfo = daemonInfo;
57 this.hostLink = link;
58 }
59
60 public HeadResourceInfo getDaemonInfo() {
61 return daemonInfo;
62 }
63
64 public ResourceLink<HostHandle> getHostLink() {
65 return hostLink;
66 }
67
68 public Session4MonitorAPI createEventMonitor() throws Exception {
69 return new Session4MonitorAPI(this);
70 }
71
72 public <RT extends Remote> RT createAPIProxy(Class<RT> remoteClass) throws URISyntaxException, RemoteException, NotBoundException {
73 String remoteObjURI = getFullRegistryId(daemonInfo, remoteClass);
74 log.info("Trying to establish a link to " + remoteObjURI + " ...");
75 Registry registry = LocateRegistry.getRegistry(daemonInfo.getHost(), daemonInfo.getRegistryPort(hostLink.getAccountInfo()), new TunneledSocketFactory());
76 log.info("Registry obtained! " + registry + ". Retrieving " + remoteObjURI + " ...");
77 String registryId = IOUtils.concatenatePaths("", daemonInfo.generateRMIPath(), getRegistryId(remoteClass)).substring(1);
78 RT serverAPI = (RT) registry.lookup(registryId);
79 log.info("Remote proxy object created: '" + serverAPI + "' for " + remoteObjURI);
80
81 serverAPI = ReinvocationInterceptor.createProxy(serverAPI);
82 return PulsingServerAPIProxyCreator.createProxyIfNecessary(serverAPI, new RemoteInvocatorHeartBeater());
83 }
84
85 class RemoteInvocatorHeartBeater implements HeartBeater<PulsingServerAPI> {
86 public boolean beatAndReportIfMustContinue(Object id, PulsingServerAPI heartContainer) {
87 try {
88 heartContainer.heartBeat(id);
89 } catch (RemoteException e) {
90 }
91 return true;
92 }
93 };
94 }
95
96