1 /*
2 * @(#)$Id: Locator.java 795 2010-04-14 19:32:55Z bpuype $
3 *
4 * Author : Ueli Kurmann, igesture@uelikurmann.ch
5 *
6 *
7 * Purpose : Implementation of the locator pattern. A locator
8 * provides access to services which can be started and
9 * stopped. A default locator is available trough a
10 * singleton.
11 *
12 * -----------------------------------------------------------------------
13 *
14 * Revision Information:
15 *
16 * Date Who Reason
17 *
18 * 23.03.2008 ukurmann Initial Release
19 * 29.10.2008 bsigner Adapted to new RunnableService
20 *
21 * -----------------------------------------------------------------------
22 *
23 * Copyright 1999-2009 ETH Zurich. All Rights Reserved.
24 *
25 * This software is the proprietary information of ETH Zurich.
26 * Use is subject to license terms.
27 *
28 */
29
30
31 package org.ximtec.igesture.tool.locator;
32
33 import java.util.ArrayList;
34 import java.util.HashMap;
35 import java.util.List;
36 import java.util.Map;
37
38 import org.ximtec.igesture.io.GestureDevice;
39
40
41 /**
42 * Implementation of the locator pattern. A locator provides access to services
43 * which can be started and stopped. A default locator is available trough a
44 * singleton.
45 *
46 * @version 1.0, Mar 2008
47 * @author Ueli Kurmann, igesture@uelikurmann.ch
48 * @author Beat Signer, signer@inf.ethz.ch
49 */
50 public class Locator {
51
52 private static Locator defaultLocator;
53
54 private Map<String, Service> services;
55
56 private Map<String, GestureDevice<?,?>> sharedDevices;
57
58 public Locator() {
59 services = new HashMap<String, Service>();
60 sharedDevices = new HashMap<String, GestureDevice<?,?>>();
61 }
62
63
64 /**
65 * Registers a new service. The service's name can later be used to retrieve a
66 * specific service.
67 * @param service the service to be registered.
68 */
69 public void addService(Service service) {
70 services.put(service.getIdentifier(), service);
71 } // addService
72
73
74 /**
75 * Retrieves a specific service.
76 * @param identifier the identifier of the service to be retrieved.
77 * @return service for a given specifier.
78 */
79 public Service getService(String identifier) {
80 return services.get(identifier);
81 } // getService
82
83
84 /**
85 * Retrieves a specific service a casts it to the provided class type.
86 * @param identifier the identifier of the service to be retrieved.
87 * @param type the class type the returned service should be automatically
88 * casted to.
89 * @return service for a given identifier.
90 */
91 public <T> T getService(String identifier, Class<T> type) {
92 return type.cast(services.get(identifier));
93 } // getService
94
95
96 /**
97 * Returns all registered services.
98 * @return all registered services.
99 */
100 public List<Service> getServices() {
101 return new ArrayList<Service>(services.values());
102 } // getServices
103
104
105 /**
106 * Removes a service based on its unique identifier.
107 * @param service the service to be removed.
108 */
109 public void removeService(Service service) {
110 services.remove(service.getIdentifier());
111 } // removeService
112
113
114 /**
115 * Starts all registered services.
116 */
117 public void startAll() {
118 for (Service service : getServices()) {
119
120 if (service instanceof RunnableService) {
121 ((RunnableService)service).start();
122 }
123
124 }
125
126 } // startAll
127
128
129 /**
130 * Stops all registered services.
131 */
132 public void stopAll() {
133 for (Service service : getServices()) {
134
135 if (service instanceof RunnableService) {
136 ((RunnableService)service).stop();
137 }
138
139 }
140
141 } // stopAll
142
143
144 /**
145 * Returns a default locator (singleton).
146 * @return the default locator.
147 */
148 public static Locator getDefault() {
149 if (defaultLocator == null) {
150 defaultLocator = new Locator();
151 }
152
153 return defaultLocator;
154 } // getDefault
155
156 public void setSharedDevice(String identifier, GestureDevice<?,?> device)
157 {
158 sharedDevices.put(identifier, device);
159 }
160
161 public GestureDevice<?,?> getSharedDevice(String identifier)
162 {
163 return sharedDevices.get(identifier);
164 }
165
166 }