View Javadoc

1   /*
2    * Copyright 2007-2008 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * @author Marco Ruiz
38   * @since Dec 13, 2007
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  		// Add reinvocation aspect before the optional beating aspect
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