KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > services > ServiceLocator


1 /*******************************************************************************
2  * Copyright (c) 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  ******************************************************************************/

11
12 package org.eclipse.ui.internal.services;
13
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import org.eclipse.swt.widgets.Shell;
19 import org.eclipse.ui.services.IDisposable;
20 import org.eclipse.ui.services.IServiceLocator;
21
22 /**
23  * @since 3.2
24  *
25  */

26 public final class ServiceLocator implements IDisposable, INestable,
27         IServiceLocator {
28
29     /**
30      * The parent for this service locator. If a service can't be found in this
31      * locator, then the parent is asked. This value may be <code>null</code>
32      * if there is no parent.
33      */

34     private final IServiceLocator parent;
35
36     /**
37      * The map of services maintained by the workbench window. These services
38      * are initialized during workbench window during the
39      * {@link #configureShell(Shell)}. This value is <code>null</code> until
40      * a service is registered.
41      */

42     private Map JavaDoc services = null;
43
44     /**
45      * Constructs a service locator with no parent.
46      */

47     public ServiceLocator() {
48         this(null);
49     }
50
51     /**
52      * Constructs a service locator with the given parent.
53      *
54      * @param parent
55      * The parent for this service locator; this value may be
56      * <code>null</code>.
57      */

58     public ServiceLocator(final IServiceLocator parent) {
59         this.parent = parent;
60     }
61
62     public final void activate() {
63         if (services != null) {
64             final Iterator JavaDoc serviceItr = services.values().iterator();
65             while (serviceItr.hasNext()) {
66                 final Object JavaDoc service = serviceItr.next();
67                 if (service instanceof INestable) {
68                     final INestable nestableService = (INestable) service;
69                     nestableService.activate();
70                 }
71             }
72         }
73     }
74
75     public final void deactivate() {
76         if (services != null) {
77             final Iterator JavaDoc serviceItr = services.values().iterator();
78             while (serviceItr.hasNext()) {
79                 final Object JavaDoc service = serviceItr.next();
80                 if (service instanceof INestable) {
81                     final INestable nestableService = (INestable) service;
82                     nestableService.deactivate();
83                 }
84             }
85         }
86     }
87
88     public final void dispose() {
89         if (services != null) {
90             final Iterator JavaDoc serviceItr = services.values().iterator();
91             while (serviceItr.hasNext()) {
92                 final Object JavaDoc object = serviceItr.next();
93                 if (object instanceof IDisposable) {
94                     final IDisposable service = (IDisposable) object;
95                     service.dispose();
96                 }
97             }
98             services = null;
99         }
100     }
101
102     public final Object JavaDoc getService(final Class JavaDoc key) {
103         final Object JavaDoc service;
104         if (services != null) {
105             service = services.get(key);
106         } else {
107             service = null;
108         }
109         if ((service == null) && (parent != null)) {
110             return parent.getService(key);
111         }
112
113         return service;
114     }
115
116     public final boolean hasService(final Class JavaDoc key) {
117         if (services != null) {
118             if (services.containsKey(key)) {
119                 return true;
120             }
121         }
122
123         return false;
124     }
125
126     /**
127      * Registers a service with this locator. If there is an existing service
128      * matching the same <code>api</code> and it implements
129      * {@link IDisposable}, it will be disposed.
130      *
131      * @param api
132      * This is the interface that the service implements. Must not be
133      * <code>null</code>.
134      * @param service
135      * The service to register. This must be some implementation of
136      * <code>api</code>. This value must not be <code>null</code>.
137      */

138     public final void registerService(final Class JavaDoc api, final Object JavaDoc service) {
139         if (api == null) {
140             throw new NullPointerException JavaDoc("The service key cannot be null"); //$NON-NLS-1$
141
}
142
143         if (!api.isInstance(service)) {
144             throw new IllegalArgumentException JavaDoc(
145                     "The service does not implement the given interface"); //$NON-NLS-1$
146
}
147
148         if (services == null) {
149             services = new HashMap JavaDoc();
150         }
151
152         if (services.containsKey(api)) {
153             final Object JavaDoc currentService = services.remove(api);
154             if (currentService instanceof IDisposable) {
155                 final IDisposable disposable = (IDisposable) currentService;
156                 disposable.dispose();
157             }
158         }
159
160         if (service == null) {
161             if (services.isEmpty()) {
162                 services = null;
163             }
164         } else {
165             services.put(api, service);
166         }
167     }
168
169 }
170
Popular Tags