1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package fi.vtt.simantics.procore.internal;
14 import gnu.trove.map.hash.THashMap;
16 import java.util.Iterator;
19 import org.simantics.db.Disposable;
20 import org.simantics.db.ServiceLocator;
21 import org.simantics.db.exception.ServiceNotFoundException;
24 * @author Tuukka Lehtonen
26 public class ServiceLocatorImpl implements ServiceLocator {
29 * The parent for this service locator. If a service can't be found in this
30 * locator, then the parent is asked. This value may be <code>null</code>
31 * if there is no parent.
33 private final ServiceLocator parent;
36 * The map of services maintained by the workbench window. These services
37 * are initialized during workbench window during the
38 * {@link #configureShell(Shell)}. This value is <code>null</code> until
39 * a service is registered.
41 private Map<Class<?>, Object> services = null;
44 * Constructs a service locator with no parent.
46 public ServiceLocatorImpl() {
51 * Constructs a service locator with the given parent.
54 * The parent for this service locator; this value may be
57 public ServiceLocatorImpl(final ServiceLocator parent) {
61 // public final void activate() {
62 // if (services != null) {
63 // final Iterator serviceItr = services.values().iterator();
64 // while (serviceItr.hasNext()) {
65 // final Object service = serviceItr.next();
66 // if (service instanceof INestable) {
67 // final INestable nestableService = (INestable) service;
68 // nestableService.activate();
74 // public final void deactivate() {
75 // if (services != null) {
76 // final Iterator serviceItr = services.values().iterator();
77 // while (serviceItr.hasNext()) {
78 // final Object service = serviceItr.next();
79 // if (service instanceof INestable) {
80 // final INestable nestableService = (INestable) service;
81 // nestableService.deactivate();
87 public final void dispose() {
88 Map<Class<?>, Object> s = null;
94 final Iterator<Object> serviceItr = s.values().iterator();
95 while (serviceItr.hasNext()) {
96 final Object object = serviceItr.next();
97 if (object instanceof Disposable) {
98 final Disposable service = (Disposable) object;
107 public final <T> T getService(final Class<T> key) {
108 T t = peekService(key);
110 throw new ServiceNotFoundException(this, key);
114 @SuppressWarnings("unchecked")
116 public final <T> T peekService(final Class<T> key) {
117 final Object service;
118 synchronized (this) {
119 if (services != null) {
120 service = services.get(key);
127 return parent.getService(key);
133 } catch (ClassCastException e) {
139 public final boolean hasService(final Class<?> key) {
140 if (services != null) {
141 if (services.containsKey(key)) {
150 * Registers a service with this locator. If there is an existing service
151 * matching the same <code>api</code> and it implements
152 * {@link Disposable}, it will be disposed.
155 * This is the interface that the service implements. Must not be
158 * The service to register. This must be some implementation of
159 * <code>api</code>. This value must not be <code>null</code>.
161 public final <T> void registerService(final Class<T> api, final T service) {
163 throw new NullPointerException("The service key cannot be null"); //$NON-NLS-1$
166 // if (!api.isInstance(service)) {
167 // throw new IllegalArgumentException(
168 // "The service does not implement the given interface"); //$NON-NLS-1$
171 synchronized (this) {
172 if (services == null) {
173 services = new THashMap<Class<?>, Object>();
176 final Object currentService = services.remove(api);
177 if (currentService instanceof Disposable) {
178 final Disposable disposable = (Disposable) currentService;
179 disposable.dispose();
182 if (service == null) {
183 if (services.isEmpty()) {
187 // System.out.println("register " + api.toString());
188 services.put(api, service);