blob: 38847b07ada4b0273a49b2378bf66cd1b9c07e85 [file] [log] [blame]
Marcel Offermans516d38d2006-03-25 20:46:19 +00001/*
2 * Copyright 2006 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17package org.apache.felix.dependencymanager;
18
19import java.util.List;
20
21import org.osgi.framework.BundleActivator;
22import org.osgi.framework.BundleContext;
23
24/**
25 * Base bundle activator class. Subclass this activator if you want to use dependency
26 * management in your bundle. There are two methods you should implement:
27 * <code>init()</code> and <code>destroy()</code>. Both methods take two arguments,
28 * the bundle context and the dependency manager. The dependency manager can be used
29 * to define all the dependencies.
30 *
31 * @author Marcel Offermans
32 */
33public abstract class DependencyActivatorBase implements BundleActivator {
34 private BundleContext m_context;
35 private DependencyManager m_manager;
36
37 /**
38 * Initialize the dependency manager. Here you can add all services and their dependencies.
39 * If something goes wrong and you do not want your bundle to be started, you can throw an
40 * exception. This exception will be passed on to the <code>start()</code> method of the
41 * bundle activator, causing the bundle not to start.
42 *
43 * @param context the bundle context
44 * @param manager the dependency manager
45 * @throws Exception if the initialization fails
46 */
47 public abstract void init(BundleContext context, DependencyManager manager) throws Exception;
48
49 /**
50 * Destroy the dependency manager. Here you can remove all services and their dependencies.
51 * Actually, the base class will clean up your dependencies anyway, so most of the time you
52 * don't need to do anything here.
53 * If something goes wrong and you do not want your bundle to be stopped, you can throw an
54 * exception. This exception will be passed on to the <code>stop()</code> method of the
55 * bundle activator, causing the bundle not to stop.
56 *
57 * @param context the bundle context
58 * @param manager the dependency manager
59 * @throws Exception if the destruction fails
60 */
61 public abstract void destroy(BundleContext context, DependencyManager manager) throws Exception;
62
63 /**
64 * Start method of the bundle activator. Initializes the dependency manager
65 * and calls <code>init()</code>.
66 *
67 * @param context the bundle context
68 */
69 public void start(BundleContext context) throws Exception {
70 m_context = context;
71 m_manager = new DependencyManager(context);
72 init(m_context, m_manager);
73 }
74
75 /**
76 * Stop method of the bundle activator. Calls the <code>destroy()</code> method
77 * and cleans up all left over dependencies.
78 *
79 * @param context the bundle context
80 */
81 public void stop(BundleContext context) throws Exception {
82 destroy(m_context, m_manager);
83 cleanup(m_manager);
84 m_manager = null;
85 m_context = null;
86 }
87
88 /**
89 * Creates a new service.
90 *
91 * @return the new service
92 */
93 public Service createService() {
94 return new ServiceImpl(m_context);
95 }
96
97 /**
98 * Creates a new service dependency.
99 *
100 * @return the service dependency
101 */
102 public ServiceDependency createServiceDependency() {
103 return new ServiceDependency(m_context);
104 }
105
106 /**
107 * Cleans up all services and their dependencies.
108 *
109 * @param manager the dependency manager
110 */
111 private void cleanup(DependencyManager manager) {
112 List services = manager.getServices();
113 for (int i = services.size() - 1; i >= 0; i--) {
114 Service service = (Service) services.get(i);
115 manager.remove(service);
116 // remove any state listeners that are still registered
117 if (service instanceof ServiceImpl) {
118 ServiceImpl si = (ServiceImpl) service;
119 si.removeStateListeners();
120 }
121 }
122 }
123}