Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: bdb0832e649066416287d6e8acf66b4f8363d291 (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
134
135
136
137
138
139
140
141
/*******************************************************************************
 * Copyright (c) 2012, 2017 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.osgi.tests.container.dummys;

import java.util.EnumSet;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.atomic.AtomicBoolean;
import org.eclipse.osgi.container.Module;
import org.eclipse.osgi.container.Module.Settings;
import org.eclipse.osgi.container.ModuleCollisionHook;
import org.eclipse.osgi.container.ModuleContainer;
import org.eclipse.osgi.container.ModuleContainerAdaptor;
import org.eclipse.osgi.container.SystemModule;
import org.eclipse.osgi.service.debug.DebugOptions;
import org.eclipse.osgi.tests.container.dummys.DummyModuleDatabase.DummyContainerEvent;
import org.eclipse.osgi.tests.container.dummys.DummyModuleDatabase.DummyModuleEvent;
import org.osgi.framework.FrameworkListener;
import org.osgi.framework.hooks.resolver.ResolverHookFactory;

public class DummyContainerAdaptor extends ModuleContainerAdaptor {
	private AtomicBoolean slowdownEvents = new AtomicBoolean(false);
	private final ModuleCollisionHook collisionHook;
	private final Map<String, String> configuration;
	private final DummyModuleDatabase moduleDatabase;
	private final ModuleContainer container;
	private final ResolverHookFactory resolverHookFactory;
	private final DebugOptions debugOptions;
	private volatile Executor resolverExecutor;
	private volatile ScheduledExecutorService timeoutExecutor;

	public DummyContainerAdaptor(ModuleCollisionHook collisionHook, Map<String, String> configuration) {
		this(collisionHook, configuration, new DummyResolverHookFactory());
	}

	public DummyContainerAdaptor(ModuleCollisionHook collisionHook, Map<String, String> configuration, ResolverHookFactory resolverHookFactory) {
		this(collisionHook, configuration, resolverHookFactory, null);
	}

	public DummyContainerAdaptor(ModuleCollisionHook collisionHook, Map<String, String> configuration, ResolverHookFactory resolverHookFactory, DebugOptions debugOptions) {
		this.collisionHook = collisionHook;
		this.configuration = configuration;
		this.resolverHookFactory = resolverHookFactory;
		this.moduleDatabase = new DummyModuleDatabase(this);
		this.debugOptions = debugOptions;
		this.container = new ModuleContainer(this, moduleDatabase);
	}

	@Override
	public ModuleCollisionHook getModuleCollisionHook() {
		return collisionHook;
	}

	@Override
	public ResolverHookFactory getResolverHookFactory() {
		return resolverHookFactory;
	}

	@Override
	public void publishContainerEvent(ContainerEvent type, Module module, Throwable error, FrameworkListener... listeners) {
		moduleDatabase.addEvent(new DummyContainerEvent(type, module, error, listeners));

	}

	@Override
	public String getProperty(String key) {
		return configuration.get(key);
	}

	@Override
	public Module createModule(String location, long id, EnumSet<Settings> settings, int startlevel) {
		return new DummyModule(id, location, container, settings, startlevel);
	}

	@Override
	public SystemModule createSystemModule() {
		return new DummySystemModule(container);
	}

	public ModuleContainer getContainer() {
		return container;
	}

	public DummyModuleDatabase getDatabase() {
		return moduleDatabase;
	}

	public void setSlowdownEvents(boolean slowdown) {
		slowdownEvents.set(slowdown);
	}

	@Override
	public void publishModuleEvent(ModuleEvent type, Module module, Module origin) {
		if (type == ModuleEvent.STARTING && slowdownEvents.get()) {
			try {
				Thread.sleep(6000);
			} catch (InterruptedException e) {
				// ignore
				Thread.currentThread().interrupt();
			}
		}
		moduleDatabase.addEvent(new DummyModuleEvent(module, type, module.getState()));
	}

	@Override
	public DebugOptions getDebugOptions() {
		return this.debugOptions;
	}

	public void setResolverExecutor(Executor executor) {
		this.resolverExecutor = executor;
	}

	@Override
	public Executor getResolverExecutor() {
		Executor current = this.resolverExecutor;
		if (current != null) {
			return current;
		}
		return super.getResolverExecutor();
	}

	public void setTimeoutExecutor(ScheduledExecutorService timeoutExecutor) {
		this.timeoutExecutor = timeoutExecutor;
	}

	@Override
	public ScheduledExecutorService getScheduledExecutor() {
		return this.timeoutExecutor;
	}

}

Back to the top