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
|
/*******************************************************************************
* Copyright (c) 2009 EclipseSource and others. All rights reserved. This
* program and the accompanying materials are made available under the terms of
* the Eclipse Public License v1.0 which accompanies this distribution, and is
* available at http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* EclipseSource - initial API and implementation
******************************************************************************/
package org.eclipse.ecf.remoteservice;
import org.eclipse.core.runtime.Assert;
import org.eclipse.ecf.core.ContainerConnectException;
import org.eclipse.ecf.core.IContainer;
import org.eclipse.ecf.core.identity.ID;
import org.eclipse.ecf.core.identity.IDFactory;
import org.osgi.framework.InvalidSyntaxException;
/**
* @since 3.0
*/
public class RemoteServiceContainer implements IRemoteServiceContainer {
private final IContainer container;
private final IRemoteServiceContainerAdapter containerAdapter;
public RemoteServiceContainer(IContainer container, IRemoteServiceContainerAdapter containerAdapter) {
Assert.isNotNull(container);
Assert.isNotNull(containerAdapter);
this.container = container;
this.containerAdapter = containerAdapter;
}
public IContainer getContainer() {
return container;
}
public IRemoteServiceContainerAdapter getContainerAdapter() {
return containerAdapter;
}
public IRemoteService getRemoteService(String targetLocation, String serviceInterfaceClass, String filter) throws ContainerConnectException, InvalidSyntaxException {
ID targetID = null;
if (targetLocation != null) {
targetID = IDFactory.getDefault().createID(getContainer().getConnectNamespace(), targetLocation);
}
IRemoteServiceReference serviceReference = getServiceReference(targetID, serviceInterfaceClass, filter);
if (serviceReference == null)
return null;
return getContainerAdapter().getRemoteService(serviceReference);
}
protected IRemoteServiceReference getServiceReference(ID targetId, String serviceInterfaceClass, String filter) throws ContainerConnectException, InvalidSyntaxException {
IRemoteServiceReference[] references = getContainerAdapter().getRemoteServiceReferences(targetId, serviceInterfaceClass, filter);
if (references == null || references.length == 0)
return null;
return selectReference(references);
}
protected IRemoteServiceReference selectReference(IRemoteServiceReference[] references) {
int length = (references == null) ? 0 : references.length;
if (length == 0) { /* if no service is being tracked */
return null;
}
int index = 0;
if (length > 1) { /* if more than one service, select highest ranking */
int rankings[] = new int[length];
int count = 0;
int maxRanking = Integer.MIN_VALUE;
for (int i = 0; i < length; i++) {
Object property = references[i].getProperty(org.eclipse.ecf.remoteservice.Constants.SERVICE_RANKING);
int ranking = (property instanceof Integer) ? ((Integer) property).intValue() : 0;
rankings[i] = ranking;
if (ranking > maxRanking) {
index = i;
maxRanking = ranking;
count = 1;
} else {
if (ranking == maxRanking) {
count++;
}
}
}
if (count > 1) { /* if still more than one service, select lowest id */
long minId = Long.MAX_VALUE;
for (int i = 0; i < length; i++) {
if (rankings[i] == maxRanking) {
long id = ((Long) (references[i].getProperty(org.eclipse.ecf.remoteservice.Constants.SERVICE_ID))).longValue();
if (id < minId) {
index = i;
minId = id;
}
}
}
}
}
return references[index];
}
public IRemoteService getRemoteService(String targetLocation, String serviceInterfaceClass) throws ContainerConnectException {
try {
return getRemoteService(targetLocation, serviceInterfaceClass, null);
} catch (InvalidSyntaxException e) {
// Cannot happen
return null;
}
}
public IRemoteService getRemoteService(String serviceInterfaceClass) {
try {
return getRemoteService(null, serviceInterfaceClass);
} catch (ContainerConnectException e) {
// if targetLocation is null, cannot happen
return null;
}
}
}
|