View Javadoc

1   /*
2   Copyright 2011-2014 AGH-UST, http://www.agh.edu.pl
3   Faculty of Computer Science, Electronics and Telecommunications
4   Department of Computer Science 
5   
6   See the NOTICE file distributed with this work for additional
7   information regarding copyright ownership
8   
9   Licensed under the Apache License, Version 2.0 (the "License");
10  you may not use this file except in compliance with the License.
11  You may obtain a copy of the License at
12  
13    http://www.apache.org/licenses/LICENSE-2.0
14  
15  Unless required by applicable law or agreed to in writing, software
16  distributed under the License is distributed on an "AS IS" BASIS,
17  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  See the License for the specific language governing permissions and
19  limitations under the License.
20  */
21  package org.universAAL.ri.gateway.eimanager.impl.importing;
22  
23  import java.io.IOException;
24  import java.util.HashSet;
25  import java.util.Set;
26  
27  import org.universAAL.middleware.container.ModuleContext;
28  import org.universAAL.middleware.service.ServiceCall;
29  import org.universAAL.middleware.service.ServiceCallee;
30  import org.universAAL.middleware.service.ServiceResponse;
31  import org.universAAL.middleware.service.owls.profile.ServiceProfile;
32  import org.universAAL.ri.gateway.eimanager.impl.ProxyBusMember;
33  
34  public class ProxyServiceCallee extends ProxyBusMember {
35  
36  	private ServiceCallee callee;
37  	private Set<ServiceProfile> profilesSet;
38  
39  	public ProxyServiceCallee(final ServiceProfile[] realizedServices,
40  			final ImportedProxyManager manager, final String targetId, final String remoteBusMemberId,
41  			final ModuleContext mc) {
42  		super(manager, targetId, remoteBusMemberId, mc);
43  		profilesSet = new HashSet<ServiceProfile>();
44  		for (ServiceProfile profile : realizedServices) {
45  			profilesSet.add(profile);
46  		}
47  		callee = new CustomServiceCalee(mc, realizedServices);
48  	}
49  
50  	@Override
51  	public void removeProxy() {
52  		callee.close();
53  	}
54  
55  	@Override
56  	public String getId() {
57  		return callee.getMyID();
58  	}
59  
60  	public void refreshProfiles(final ServiceProfile[] profiles) {
61  		ServiceCallee old = callee;
62  		callee = new CustomServiceCalee(mc, profiles);
63  		old.close();
64  
65  		profilesSet.clear();
66  		for (ServiceProfile profile : profiles) {
67  			profilesSet.add(profile);
68  		}
69  
70  	}
71  
72  	public Set<ServiceProfile> getProfilesSet() {
73  		return profilesSet;
74  	}
75  
76  	public void setProfilesSet(Set<ServiceProfile> profilesSet) {
77  		this.profilesSet = profilesSet;
78  	}
79  
80  	private class CustomServiceCalee extends ServiceCallee {
81  		protected CustomServiceCalee(final ModuleContext context,
82  				final ServiceProfile[] realizedServices) {
83  			super(context, realizedServices);
84  		}
85  
86  		@Override
87  		public ServiceResponse handleCall(final ServiceCall call) {
88  			ServiceResponse response;
89  			try {
90  				response = ((ImportedProxyManager) getManager())
91  						.realizeRemoteServiceRequest(targetId,
92  								call, remoteBusMemberId);
93  				return response;
94  			} catch (IOException e) {
95  				// TODO Auto-generated catch block
96  				e.printStackTrace();
97  			} catch (ClassNotFoundException e) {
98  				// TODO Auto-generated catch block
99  				e.printStackTrace();
100 			}
101 			return null;
102 		}
103 
104 		@Override
105 		public void communicationChannelBroken() {
106 		}
107 	}
108 
109 }