Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4a4b3e98d460d5dbb5bef3f89716e58c5675a00a (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2008 Versant Corp.
 * 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:
 *     Markus Kuppe (mkuppe <at> versant <dot> com) - initial API and implementation
 ******************************************************************************/

package org.eclipse.ecf.internal.remoteservices.ui;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.ecf.core.ContainerCreateException;
import org.eclipse.ecf.core.ContainerFactory;
import org.eclipse.ecf.core.IContainer;
import org.eclipse.ecf.core.IContainerManager;
import org.eclipse.ecf.core.identity.ID;
import org.eclipse.ecf.core.identity.IDCreateException;
import org.eclipse.ecf.core.identity.IDFactory;
import org.eclipse.ecf.discovery.IServiceInfo;
import org.eclipse.ecf.discovery.ui.DiscoveryHandlerUtil;
import org.eclipse.ecf.remoteservice.Constants;
import org.eclipse.ecf.remoteservice.IRemoteServiceContainerAdapter;
import org.eclipse.ecf.remoteservice.IRemoteServiceReference;
import org.osgi.framework.InvalidSyntaxException;

public class RemoteServiceHandlerUtil {

	public static IRemoteServiceContainerAdapter getActiveIRemoteServiceContainerAdapterChecked(ExecutionEvent event) throws ExecutionException {
		final ID activeConnectId = getActiveConnectIDChecked(event);
		final IContainer container = getContainerWithConnectID(activeConnectId);
		if (container == null) {
			return null;
		}
		final IRemoteServiceContainerAdapter adapter = (IRemoteServiceContainerAdapter) container.getAdapter(IRemoteServiceContainerAdapter.class);
		return adapter;
	}
	
	public static IRemoteServiceReference[] getActiveIRemoteServiceReferencesChecked(ExecutionEvent event) throws ExecutionException {
		final IServiceInfo serviceInfo = DiscoveryHandlerUtil.getActiveIServiceInfoChecked(event);
		final IRemoteServiceContainerAdapter adapter = getActiveIRemoteServiceContainerAdapterChecked(event);
		try {
			return getRemoteServiceReferencesForRemoteServiceAdapter(adapter, serviceInfo);
		} catch (IDCreateException e) {
			throw new ExecutionException(e.getMessage(), e);
		} catch (InvalidSyntaxException e) {
			throw new ExecutionException(e.getMessage(), e);
		}
	}

	public static ID getActiveConnectIDChecked(ExecutionEvent event) throws ExecutionException {
		final IServiceInfo serviceInfo = DiscoveryHandlerUtil.getActiveIServiceInfoChecked(event);
		final String connectNamespace = getConnectNamespace(serviceInfo);
		final String connectId = getConnectID(serviceInfo);
		try {
			return IDFactory.getDefault().createID(connectNamespace, connectId);
		} catch (IDCreateException e) {
			throw new ExecutionException(e.getMessage(), e);
		}
	}

	public static IContainer getActiveIRemoteServiceContainerChecked(ExecutionEvent event) throws ExecutionException {
		final IServiceInfo serviceInfo = DiscoveryHandlerUtil.getActiveIServiceInfoChecked(event);
		final ID createConnectId = getActiveConnectIDChecked(event);
		final IContainer container = getContainerWithConnectID(createConnectId);
		if (container != null) {
			return container;
		}
		//TODO remove parameters once https://bugs.eclipse.org/bugs/show_bug.cgi?id=256586 is fixed
		final Object[] parameters = new Object[] { createConnectId };
		try {
		// If it's not there and already connected then create and return new one
			return ContainerFactory.getDefault().createContainer(getContainerFactory(serviceInfo), parameters);
		} catch (ContainerCreateException e) {
			throw new ExecutionException(e.getMessage(), e);
		}
	}
	
	//TODO push this functionality down into the ContainerManager
	private static IContainer getContainerWithConnectID(ID aConnectedID) {
		final IContainerManager containerManager = Activator.getDefault().getContainerManager();
		final IContainer[] containers = containerManager.getAllContainers();
		if (containers == null) {
			return null;
		}
		for(int i=0; i < containers.length; i++) {
			ID connectedId = containers[i].getConnectedID();
			if (connectedId != null && connectedId.equals(aConnectedID)) {
				return containers[i];
			}
		}
		return null;
	}

	private static IRemoteServiceReference[] getRemoteServiceReferencesForRemoteServiceAdapter(IRemoteServiceContainerAdapter adapter, IServiceInfo serviceInfo) throws InvalidSyntaxException, IDCreateException {
		if(adapter == null) {
			return null;
		}
		ID serviceID = null;
		final String serviceNamespace = getServiceNamespace(serviceInfo);
		final String serviceid = getServiceID(serviceInfo);
		if (serviceNamespace != null && serviceid != null) {
			serviceID = IDFactory.getDefault().createID(serviceNamespace, serviceid);
		}
		final ID[] targets = (serviceID == null) ? null : new ID[] {serviceID};
		return adapter.getRemoteServiceReferences(targets, getRemoteServiceClass(serviceInfo), getFilter(serviceInfo));
	}
	
	private static String getServiceNamespace(IServiceInfo serviceInfo) {
		return serviceInfo.getServiceProperties().getPropertyString(Constants.SERVICE_IDFILTER_NAMESPACE);
	}
	private static String getServiceID(IServiceInfo serviceInfo) {
		return serviceInfo.getServiceProperties().getPropertyString(Constants.SERVICE_IDFILTER_ID);
	}
	private static String getRemoteServiceClass(IServiceInfo serviceInfo) {
		return serviceInfo.getServiceProperties().getPropertyString(Constants.SERVICE_OBJECTCLASS);
	}
	private static String getFilter(IServiceInfo serviceInfo) {
		return serviceInfo.getServiceProperties().getPropertyString(Constants.SERVICE_FILTER_PROPERTY);
	}
	private static String getConnectNamespace(IServiceInfo serviceInfo) {
		return serviceInfo.getServiceProperties().getPropertyString(Constants.SERVICE_CONNECT_ID_NAMESPACE);
	}
	private static String getConnectID(IServiceInfo serviceInfo) {
		return serviceInfo.getServiceProperties().getPropertyString(Constants.SERVICE_CONNECT_ID);
	}
	private static String getContainerFactory(IServiceInfo serviceInfo) {
		return serviceInfo.getServiceProperties().getPropertyString(Constants.SERVICE_CONTAINER_FACTORY_NAME);
	}
}

Back to the top