blob: 71fd6e9557cc93e936142f2124bd77a09a273187 [file] [log] [blame]
Marcel Offermansa962bc92009-11-21 17:59:33 +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 */
Pierre De Ropae4a5db2009-12-04 22:08:05 +000019package org.apache.felix.dm;
Marcel Offermansa962bc92009-11-21 17:59:33 +000020
Marcel Offermans80eeafe2009-12-01 22:12:26 +000021import java.util.Dictionary;
Marcel Offermansa962bc92009-11-21 17:59:33 +000022import java.util.List;
23
Pierre De Ropae4a5db2009-12-04 22:08:05 +000024import org.apache.felix.dm.dependencies.BundleDependency;
25import org.apache.felix.dm.dependencies.ConfigurationDependency;
Pierre De Ropa0204f52010-03-06 22:23:57 +000026import org.apache.felix.dm.dependencies.PropertyMetaData;
Marcel Offermansd665eaf2010-02-16 15:56:35 +000027import org.apache.felix.dm.dependencies.ResourceDependency;
Pierre De Ropae4a5db2009-12-04 22:08:05 +000028import org.apache.felix.dm.dependencies.ServiceDependency;
29import org.apache.felix.dm.dependencies.TemporalServiceDependency;
30import org.apache.felix.dm.impl.Logger;
31import org.apache.felix.dm.impl.ServiceImpl;
32import org.apache.felix.dm.service.Service;
Marcel Offermansa962bc92009-11-21 17:59:33 +000033import org.osgi.framework.BundleActivator;
34import org.osgi.framework.BundleContext;
35
36/**
37 * Base bundle activator class. Subclass this activator if you want to use dependency
38 * management in your bundle. There are two methods you should implement:
39 * <code>init()</code> and <code>destroy()</code>. Both methods take two arguments,
40 * the bundle context and the dependency manager. The dependency manager can be used
41 * to define all the dependencies.
42 *
43 * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
44 */
45public abstract class DependencyActivatorBase implements BundleActivator {
46 private BundleContext m_context;
47 private DependencyManager m_manager;
48 private Logger m_logger;
49
50 /**
51 * Initialize the dependency manager. Here you can add all services and their dependencies.
52 * If something goes wrong and you do not want your bundle to be started, you can throw an
53 * exception. This exception will be passed on to the <code>start()</code> method of the
54 * bundle activator, causing the bundle not to start.
55 *
56 * @param context the bundle context
57 * @param manager the dependency manager
58 * @throws Exception if the initialization fails
59 */
60 public abstract void init(BundleContext context, DependencyManager manager) throws Exception;
61
62 /**
63 * Destroy the dependency manager. Here you can remove all services and their dependencies.
64 * Actually, the base class will clean up your dependencies anyway, so most of the time you
65 * don't need to do anything here.
66 * If something goes wrong and you do not want your bundle to be stopped, you can throw an
67 * exception. This exception will be passed on to the <code>stop()</code> method of the
68 * bundle activator, causing the bundle not to stop.
69 *
70 * @param context the bundle context
71 * @param manager the dependency manager
72 * @throws Exception if the destruction fails
73 */
74 public abstract void destroy(BundleContext context, DependencyManager manager) throws Exception;
75
76 /**
77 * Start method of the bundle activator. Initializes the dependency manager
78 * and calls <code>init()</code>.
79 *
80 * @param context the bundle context
81 */
82 public void start(BundleContext context) throws Exception {
83 m_context = context;
84 m_logger = new Logger(context);
85 m_manager = new DependencyManager(context, m_logger);
86 init(m_context, m_manager);
87 }
88
89 /**
90 * Stop method of the bundle activator. Calls the <code>destroy()</code> method
91 * and cleans up all left over dependencies.
92 *
93 * @param context the bundle context
94 */
95 public void stop(BundleContext context) throws Exception {
96 destroy(m_context, m_manager);
97 cleanup(m_manager);
98 m_manager = null;
99 m_context = null;
100 }
101
102 /**
103 * Creates a new service.
104 *
105 * @return the new service
106 */
107 public Service createService() {
Marcel Offermanse14b3422009-11-25 23:04:32 +0000108 return m_manager.createService();
Marcel Offermansa962bc92009-11-21 17:59:33 +0000109 }
110
111 /**
112 * Creates a new service dependency.
113 *
114 * @return the service dependency
115 */
116 public ServiceDependency createServiceDependency() {
Marcel Offermanse14b3422009-11-25 23:04:32 +0000117 return m_manager.createServiceDependency();
Marcel Offermansa962bc92009-11-21 17:59:33 +0000118 }
119
120 /**
Marcel Offermans74363c32009-11-23 19:56:08 +0000121 * Creates a new temporal service dependency.
122 *
123 * @param timeout the max number of milliseconds to wait for a service availability.
124 * @return the service dependency
125 */
126 public TemporalServiceDependency createTemporalServiceDependency() {
Marcel Offermanse14b3422009-11-25 23:04:32 +0000127 return m_manager.createTemporalServiceDependency();
Marcel Offermans74363c32009-11-23 19:56:08 +0000128 }
129
130 /**
Marcel Offermansa962bc92009-11-21 17:59:33 +0000131 * Creates a new configuration dependency.
132 *
133 * @return the configuration dependency
134 */
135 public ConfigurationDependency createConfigurationDependency() {
Marcel Offermansd66c5ce2009-11-26 09:58:44 +0000136 return m_manager.createConfigurationDependency();
137 }
Pierre De Ropa0204f52010-03-06 22:23:57 +0000138
139 /**
140 * Creates a new configuration property MetaData.
141 * @return a new configuration property MetaData
142 */
143 public PropertyMetaData createPropertyMetaData() {
144 return m_manager.createPropertyMetaData();
145 }
Marcel Offermansd66c5ce2009-11-26 09:58:44 +0000146
147 /**
148 * Creates a new bundle dependency.
149 *
150 * @return the bundle dependency
151 */
152 public BundleDependency createBundleDependency() {
153 return m_manager.createBundleDependency();
Marcel Offermansa962bc92009-11-21 17:59:33 +0000154 }
Marcel Offermansd665eaf2010-02-16 15:56:35 +0000155
156 public ResourceDependency createResourceDependency() {
157 return m_manager.createResourceDependency();
158 }
Marcel Offermansa962bc92009-11-21 17:59:33 +0000159
Marcel Offermansad760672010-03-03 15:30:01 +0000160 public Service createAspectService(Class serviceInterface, String serviceFilter, int ranking, Object aspectImplementation, Dictionary properties) {
161 return m_manager.createAspectService(serviceInterface, serviceFilter, ranking, aspectImplementation, properties);
162 }
163 public Service createAspectService(Class serviceInterface, String serviceFilter, int ranking, Object factory, String factoryCreateMethod, Dictionary properties) {
164 return m_manager.createAspectService(serviceInterface, serviceFilter, ranking, factory, factoryCreateMethod, properties);
Marcel Offermans80eeafe2009-12-01 22:12:26 +0000165 }
Marcel Offermansd665eaf2010-02-16 15:56:35 +0000166
Marcel Offermans61a81142010-04-02 15:16:50 +0000167 public Service createAdapterService(Class serviceInterface, String serviceFilter, String adapterInterface, Object adapterImplementation, Dictionary adapterProperties) {
168 return m_manager.createAdapterService(serviceInterface, serviceFilter, adapterInterface, adapterImplementation, adapterProperties);
169 }
170 public Service createAdapterService(Class serviceInterface, String serviceFilter, String[] adapterInterface, Object adapterImplementation, Dictionary adapterProperties) {
Marcel Offermansd665eaf2010-02-16 15:56:35 +0000171 return m_manager.createAdapterService(serviceInterface, serviceFilter, adapterInterface, adapterImplementation, adapterProperties);
172 }
173
Marcel Offermans61a81142010-04-02 15:16:50 +0000174 public Service createResourceAdapter(String resourceFilter, String adapterInterface, Dictionary adapterProperties, Object adapterImplementation, boolean propagate) {
175 return m_manager.createResourceAdapterService(resourceFilter, adapterInterface, adapterProperties, adapterImplementation, propagate);
176 }
177 public Service createResourceAdapter(String resourceFilter, String[] adapterInterface, Dictionary adapterProperties, Object adapterImplementation, boolean propagate) {
Marcel Offermansd665eaf2010-02-16 15:56:35 +0000178 return m_manager.createResourceAdapterService(resourceFilter, adapterInterface, adapterProperties, adapterImplementation, propagate);
179 }
180
181 public Service createBundleAdapterService(int bundleStateMask, String bundleFilter, Object adapterImplementation, String adapterInterface, Dictionary adapterProperties, boolean propagate) {
182 return m_manager.createBundleAdapterService(bundleStateMask, bundleFilter, adapterImplementation, adapterInterface, adapterProperties, propagate);
183 }
Marcel Offermans61a81142010-04-02 15:16:50 +0000184 public Service createBundleAdapterService(int bundleStateMask, String bundleFilter, Object adapterImplementation, String[] adapterInterface, Dictionary adapterProperties, boolean propagate) {
185 return m_manager.createBundleAdapterService(bundleStateMask, bundleFilter, adapterImplementation, adapterInterface, adapterProperties, propagate);
186 }
Marcel Offermans80eeafe2009-12-01 22:12:26 +0000187
Marcel Offermansa962bc92009-11-21 17:59:33 +0000188 /**
189 * Cleans up all services and their dependencies.
190 *
191 * @param manager the dependency manager
192 */
193 private void cleanup(DependencyManager manager) {
194 List services = manager.getServices();
195 for (int i = services.size() - 1; i >= 0; i--) {
196 Service service = (Service) services.get(i);
197 manager.remove(service);
198 // remove any state listeners that are still registered
199 if (service instanceof ServiceImpl) {
200 ServiceImpl si = (ServiceImpl) service;
201 si.removeStateListeners();
202 }
203 }
204 }
205}