Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

aboutsummaryrefslogtreecommitdiffstats
blob: aafda18f5bb80f7fc0b16da3460ae75a96f6b323 (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
/****************************************************************************
 * Copyright (c) 2014 Composent, Inc.
 *
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 *
 * Contributors: Scott Lewis - initial API and implementation
 *
 * SPDX-License-Identifier: EPL-2.0
 *****************************************************************************/
package com.mycorp.examples.timeservice.host.generic.auth;

import java.security.PermissionCollection;
import java.util.Dictionary;
import java.util.Hashtable;

import org.eclipse.ecf.core.IContainer;
import org.eclipse.ecf.core.IContainerManager;
import org.eclipse.ecf.core.identity.ID;
import org.eclipse.ecf.core.security.IConnectHandlerPolicy;
import org.eclipse.ecf.core.sharedobject.ISharedObjectContainerGroupManager;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import org.osgi.util.tracker.ServiceTracker;

import com.mycorp.examples.timeservice.ITimeService;

public class Activator implements BundleActivator {

	
	private IContainer hostContainer;
	
	public void start(BundleContext context) throws Exception {
		
		// One way to setup authentication is to create/configure the ECF generic provider container
		createAndConfigureHostContainer(context);
		
		// Create remote service properties...see createRemoteServiceProperties()
		Dictionary<String,Object> props = new Hashtable<String,Object>();
		props.put("service.exported.interfaces", "*");
		props.put("service.exported.configs","ecf.generic.server");
		//props.put("ecf.endpoint.connecttarget.id", hostContainer.getID().getName());
		// Create MyTimeService impl and register/export as a remote service
		ServiceRegistration<ITimeService> timeServiceRegistration = context
				.registerService(ITimeService.class, new TimeServiceImpl(),
						props);
		
		// Print out that ITimeService remote service registration
		System.out.println("MyTimeService host registered with registration="
				+ timeServiceRegistration);
	}

	private void createAndConfigureHostContainer(BundleContext context) throws Exception {
		// Get IContainerManager singleton
		ServiceTracker<IContainerManager,IContainerManager> containerManagerTracker = new ServiceTracker<IContainerManager,IContainerManager>(context,IContainerManager.class.getName(), null);
		containerManagerTracker.open();
		IContainerManager containerManager = containerManagerTracker.getService();
		if (containerManager == null) throw new NullPointerException("Cannot get IContainerManager service");
		containerManagerTracker.close();
		
		// Now create a hostContainer instance
		hostContainer = containerManager.getContainerFactory().createContainer("ecf.generic.server");
		// Get the ISharedObjectContainerGroupManager adapter interface
		ISharedObjectContainerGroupManager hostManager = (ISharedObjectContainerGroupManager) hostContainer.getAdapter(ISharedObjectContainerGroupManager.class);
		// Set connect policy
		hostManager.setConnectPolicy(new IConnectHandlerPolicy() {
			public void refresh() {
			}
			public PermissionCollection checkConnect(Object address, ID fromID,
					ID targetID, String targetGroup, Object connectData)
					throws Exception {
				// What we will do when we receive a check connect call is to call
				// verifyClientConnect
				verifyClientConnect(fromID, connectData);
				return null;
			}});
	}

	void verifyClientConnect(ID clientID, Object connectData) throws Exception {
		// Simply print out the data.  This would/should check the values in the
		// connectData (a map:  "username"->username, "password"->password
		// And check these values against appropriate values from db, or some other source
		// and throw some appropriate exception if things do not match
		System.out.println("clientID="+clientID+",connectData="+connectData);
	}
	
	public void stop(BundleContext context) throws Exception {
		if (hostContainer != null) {
			hostContainer.disconnect();
			hostContainer.dispose();
			hostContainer = null;
		}
	}

}

Back to the top