blob: 3c78f45558848cf321a1c38621bb0fad0b82aad7 [file] [log] [blame]
Pierre De Rop19476fe2010-05-23 08:13:58 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19package org.apache.felix.dm.impl;
20
21import java.util.ArrayList;
22import java.util.Dictionary;
23import java.util.Iterator;
24import java.util.List;
25
Marcel Offermansea89b862010-06-24 13:14:43 +000026import org.apache.felix.dm.DependencyManager;
Pierre De Rop19476fe2010-05-23 08:13:58 +000027import org.apache.felix.dm.dependencies.Dependency;
28import org.apache.felix.dm.service.Service;
29import org.apache.felix.dm.service.ServiceStateListener;
30import org.osgi.framework.ServiceRegistration;
31
32/**
33 * This class allows to filter a Service interface. All Aspect/Adapters extends this class
34 * in order to add functionality to the default Service implementation.
35 */
36public class FilterService implements Service
37{
38 protected ServiceImpl m_service;
39 protected List m_stateListeners = new ArrayList();
40 protected String m_init = "init";
41 protected String m_start = "start";
42 protected String m_stop = "stop";
43 protected String m_destroy = "destroy";
44 protected Object m_callbackObject;
45 protected Object m_compositionInstance;
46 protected String m_compositionMethod;
47 protected String[] m_serviceInterfaces;
48 protected Object m_serviceImpl;
49 protected Object m_factory;
50 protected String m_factoryCreateMethod;
51 protected Dictionary m_serviceProperties;
52
53 public FilterService(Service service)
54 {
55 m_service = (ServiceImpl) service;
56 }
57
58 public Service add(Dependency dependency)
59 {
60 m_service.add(dependency);
61 // Add the dependency (if optional) to all already instantiated services.
62 // If the dependency is required, our internal service will be stopped/restarted, so in this case
63 // we have nothing to do.
64 if (! dependency.isRequired()) {
65 AbstractDecorator ad = (AbstractDecorator) m_service.getService();
66 if (ad != null)
67 {
68 ad.addDependency(dependency);
69 }
70 }
71 return this;
72 }
73
74 public Service add(List dependencies)
75 {
76 m_service.add(dependencies);
77 // Add the dependencies to all already instantiated services.
78 // If one dependency from the list is required, we have nothing to do, since our internal
79 // service will be stopped/restarted.
80 Iterator it = dependencies.iterator();
81 while (it.hasNext()) {
82 if (((Dependency) it.next()).isRequired()) {
83 return this;
84 }
85 }
86 // Ok, the list contains no required dependencies: add optionals dependencies in already instantiated
87 // services.
88 AbstractDecorator ad = (AbstractDecorator) m_service.getService();
89 if (ad != null)
90 {
91 ad.addDependencies(dependencies);
92 }
93 return this;
94 }
95
96 public void addStateListener(ServiceStateListener listener)
97 {
98 synchronized (this)
99 {
100 m_stateListeners.add(listener);
101 }
102 // Add the listener to all already instantiated services.
103 AbstractDecorator ad = (AbstractDecorator) m_service.getService();
104 if (ad != null)
105 {
106 ad.addStateListener(listener);
107 }
108 }
109
110 public List getDependencies()
111 {
112 return m_service.getDependencies();
113 }
114
115 public Object getService()
116 {
117 return m_service.getService();
118 }
119
120 public synchronized Dictionary getServiceProperties()
121 {
122 return m_serviceProperties;
123 }
124
125 public ServiceRegistration getServiceRegistration()
126 {
127 return m_service.getServiceRegistration();
128 }
129
130 public Service remove(Dependency dependency)
131 {
132 m_service.remove(dependency);
133 // Remove the dependency (if optional) from all already instantiated services.
134 // If the dependency is required, our internal service will be stopped, so in this case
135 // we have nothing to do.
136 if (!dependency.isRequired())
137 {
138 AbstractDecorator ad = (AbstractDecorator) m_service.getService();
139 if (ad != null)
140 {
141 ad.removeDependency(dependency);
142 }
143 }
144 return this;
145 }
146
147 public void removeStateListener(ServiceStateListener listener)
148 {
149 synchronized (this)
150 {
151 m_stateListeners.remove(listener);
152 }
153 // Remove the listener from all already instantiated services.
154 AbstractDecorator ad = (AbstractDecorator) m_service.getService();
155 if (ad != null)
156 {
157 ad.removeStateListener(listener);
158 }
159 }
160
161 public synchronized Service setCallbacks(Object instance, String init, String start, String stop,
162 String destroy)
163 {
164 m_service.ensureNotActive();
165 m_callbackObject = instance;
166 m_init = init;
167 m_start = start;
168 m_stop = stop;
169 m_destroy = destroy;
170 return this;
171 }
172
173 public Service setCallbacks(String init, String start, String stop, String destroy)
174 {
175 setCallbacks(null, init, start, stop, destroy);
176 return this;
177 }
178
179 public synchronized Service setComposition(Object instance, String getMethod)
180 {
181 m_service.ensureNotActive();
182 m_compositionInstance = instance;
183 m_compositionMethod = getMethod;
184 return this;
185 }
186
187 public synchronized Service setComposition(String getMethod)
188 {
189 m_service.ensureNotActive();
190 m_compositionMethod = getMethod;
191 return this;
192 }
193
194 public synchronized Service setFactory(Object factory, String createMethod)
195 {
196 m_service.ensureNotActive();
197 m_factory = factory;
198 m_factoryCreateMethod = createMethod;
199 return this;
200 }
201
202 public Service setFactory(String createMethod)
203 {
204 return setFactory(null, createMethod);
205 }
206
207 public synchronized Service setImplementation(Object implementation)
208 {
209 m_service.ensureNotActive();
210 m_serviceImpl = implementation;
211 return this;
212 }
213
214 public Service setInterface(String serviceName, Dictionary properties)
215 {
216 return setInterface(new String[] { serviceName }, properties);
217 }
218
219 public synchronized Service setInterface(String[] serviceInterfaces, Dictionary properties) {
220 m_service.ensureNotActive();
221 if (serviceInterfaces != null) {
222 m_serviceInterfaces = new String[serviceInterfaces.length];
223 System.arraycopy(serviceInterfaces, 0, m_serviceInterfaces, 0, serviceInterfaces.length);
224 m_serviceProperties = properties;
225 }
226 return this;
227 }
228
229 public Service setServiceProperties(Dictionary serviceProperties)
230 {
231 synchronized (this)
232 {
233 m_serviceProperties = serviceProperties;
234 }
235 // Set the properties to all already instantiated services.
236 if (serviceProperties != null) {
237 AbstractDecorator ad = (AbstractDecorator) m_service.getService();
238 if (ad != null)
239 {
240 ad.setServiceProperties(serviceProperties);
241 }
242 }
243 return this;
244 }
245
246 public void start()
247 {
248 m_service.start();
249 }
250
251 public void stop()
252 {
253 m_service.stop();
254 }
Marcel Offermansea89b862010-06-24 13:14:43 +0000255
256 public Object[] getCompositionInstances() {
257 return m_service.getCompositionInstances();
258 }
259
260 public DependencyManager getDependencyManager() {
261 return m_service.getDependencyManager();
262 }
Pierre De Rop19476fe2010-05-23 08:13:58 +0000263}