blob: 85a6c4ab3a219a7c1454c4e64ee75b22e56e468a [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.Dictionary;
20import java.util.List;
21
22import org.osgi.framework.ServiceRegistration;
23
24/**
25 * Service interface.
26 *
27 * @author Marcel Offermans
28 */
29public interface Service {
30 /**
31 * Adds a new dependency to this service.
32 *
33 * @param dependency the dependency to add
34 * @return this service
35 */
36 public Service add(Dependency dependency);
37
38 /**
39 * Removes a dependency from this service.
40 *
41 * @param dependency the dependency to remove
42 * @return this service
43 */
44 public Service remove(Dependency dependency);
45
46 /**
47 * Sets the public interface under which this service should be registered
48 * in the OSGi service registry.
49 *
50 * @param serviceName the name of the service interface
51 * @param properties the properties for this service
52 * @return this service
53 */
54 public Service setInterface(String serviceName, Dictionary properties);
55 /**
56 * Sets the public interfaces under which this service should be registered
57 * in the OSGi service registry.
58 *
59 * @param serviceNames the names of the service interface
60 * @param properties the properties for this service
61 * @return this service
62 */
63 public Service setInterface(String[] serviceNames, Dictionary properties);
64
65 /**
66 * Sets the implementation for this service. You can actually specify
67 * an instance you have instantiated manually, or a <code>Class</code>
68 * that will be instantiated using its default constructor when the
69 * required dependencies are resolved (effectively giving you a lazy
70 * instantiation mechanism).
71 *
72 * There are four special methods that are called when found through
73 * reflection to give you some life-cycle management options:
74 * <ol>
75 * <li><code>init()</code> when the implementation should be initialized,
76 * before it is actually registered as a service (if at all)</li>
77 * <li><code>start()</code> when the implementation has been registered
78 * as a service (if at all)</li>
79 * <li><code>stop()</code> when the implementation will be stopped, just
80 * before the service will go away (if it had been registered)</li>
81 * <li><code>destroy()</code>after the service has gone away (if it had
82 * been registered)</li>
83 * </ol>
84 * In short, this allows you to initialize your instance before it is
85 * registered, perform some post-initialization and pre-destruction code
86 * as well as final cleanup. If a method is not defined, it simply is not
87 * called, so you can decide which one(s) you need.
88 *
89 * @param implementation the implementation
90 * @return this service
91 */
92 public Service setImplementation(Object implementation);
93
94 /**
95 * Returns a list of dependencies.
96 *
97 * @return a list of dependencies
98 */
99 public List getDependencies();
100
101 /**
102 * Returns the service registration for this service. The method
103 * will return <code>null</code> if no service registration is
104 * available.
105 *
106 * @return the service registration
107 */
108 public ServiceRegistration getServiceRegistration();
109
110 /**
111 * Returns the service instance for this service. The method will
112 * return <code>null</code> if no service instance is available.
113 *
114 * @return the service instance
115 */
116 public Object getService();
117
118 /**
119 * Returns the service properties associated with the service.
120 *
121 * @return the properties or <code>null</code> if there are none
122 */
123 public Dictionary getServiceProperties();
124
125 /**
126 * Sets the service properties associated with the service. If the service
127 * was already registered, it will be updated.
128 *
129 * @param serviceProperties the properties
130 */
131 public void setServiceProperties(Dictionary serviceProperties);
132
133 /**
134 * Sets the names of the methods used as callbacks. These methods, when found, are
135 * invoked as part of the life-cycle management of the service implementation. The
136 * methods should not have any parameters.
137 *
138 * @param init the name of the init method
139 * @param start the name of the start method
140 * @param stop the name of the stop method
141 * @param destroy the name of the destroy method
142 * @return the service instance
143 */
144 public Service setCallbacks(String init, String start, String stop, String destroy);
145
146 // listener
147 /**
148 * Adds a service state listener to this service.
149 *
150 * @param listener the state listener
151 */
152 public void addStateListener(ServiceStateListener listener);
153
154 /**
155 * Removes a service state listener from this service.
156 *
157 * @param listener the state listener
158 */
159 public void removeStateListener(ServiceStateListener listener);
160
161 // events, must be fired when the dependency is started/active
162
163 /**
164 * Will be called when the dependency becomes available.
165 *
166 * @param dependency the dependency
167 */
168 public void dependencyAvailable(Dependency dependency);
169
170 /**
171 * Will be called when the dependency changes.
172 *
173 * @param dependency the dependency
174 */
175 public void dependencyUnavailable(Dependency dependency);
176
177 /**
178 * Will be called when the dependency becomes unavailable.
179 *
180 * @param dependency the dependency
181 */
182 public void dependencyChanged(Dependency dependency);
183
184 /**
185 * Starts the service. This activates the dependency tracking mechanism
186 * for this service.
187 */
188 public void start();
189
190 /**
191 * Stops the service. This deactivates the dependency tracking mechanism
192 * for this service.
193 */
194 public void stop();
195}