blob: 91fa55912a547e71e21610d5944dfe292a2c2740 [file] [log] [blame]
Richard S. Hall930fecc2005-08-16 18:33:34 +00001/*
2 * $Header: /cvshome/build/org.osgi.framework/src/org/osgi/framework/BundleContext.java,v 1.13 2005/06/21 16:22:12 hargrave Exp $
3 *
4 * Copyright (c) OSGi Alliance (2000, 2005). All Rights Reserved.
5 *
6 * This program and the accompanying materials are made available under the
7 * terms of the Eclipse Public License v1.0 which accompanies this
8 * distribution, and is available at http://www.eclipse.org/legal/epl-v10.html.
9 */
10
11package org.osgi.framework;
12
13import java.io.File;
14import java.io.InputStream;
15import java.util.Dictionary;
16
17/**
18 * A bundle's execution context within the Framework. The context is used to
19 * grant access to other methods so that this bundle can interact with the
20 * Framework.
21 *
22 * <p>
23 * <code>BundleContext</code> methods allow a bundle to:
24 * <ul>
25 * <li>Subscribe to events published by the Framework.
26 * <li>Register service objects with the Framework service registry.
27 * <li>Retrieve <code>ServiceReferences</code> from the Framework service
28 * registry.
29 * <li>Get and release service objects for a referenced service.
30 * <li>Install new bundles in the Framework.
31 * <li>Get the list of bundles installed in the Framework.
32 * <li>Get the {@link Bundle} object for a bundle.
33 * <li>Create <code>File</code> objects for files in a persistent storage area
34 * provided for the bundle by the Framework.
35 * </ul>
36 *
37 * <p>
38 * A <code>BundleContext</code> object will be created and provided to the bundle
39 * associated with this context when it is started using the
40 * {@link BundleActivator#start} method. The same <code>BundleContext</code>
41 * object will be passed to the bundle associated with this context when it is
42 * stopped using the {@link BundleActivator#stop} method. A <code>BundleContext</code>
43 * object is generally for the private use of its associated bundle and is not
44 * meant to be shared with other bundles in the OSGi environment.
45 *
46 * <p>
47 * The <code>Bundle</code> object associated with a <code>BundleContext</code> object
48 * is called the <em>context bundle</em>.
49 *
50 * <p>
51 * The <code>BundleContext</code> object is only valid during the execution
52 * of its context bundle; that is, during the period from when the context
53 * bundle is in the <code>STARTING</code>, <code>STOPPING</code>, and
54 * <code>ACTIVE</code> bundle states. If the <code>BundleContext</code> object
55 * is used subsequently, an <code>IllegalStateException</code> must be thrown.
56 * The <code>BundleContext</code> object must never be reused after its
57 * context bundle is stopped.
58 *
59 * <p>
60 * The Framework is the only entity that can create <code>BundleContext</code>
61 * objects and they are only valid within the Framework that created them.
62 *
63 * @version $Revision: 1.13 $
64 */
65
66public abstract interface BundleContext {
67 /**
68 * Returns the value of the specified property. If the key is not found in
69 * the Framework properties, the system properties are then searched. The
70 * method returns <code>null</code> if the property is not found.
71 *
72 * <p>
73 * The Framework defines the following standard property keys:
74 * </p>
75 * <ul>
76 * <li>{@link Constants#FRAMEWORK_VERSION}- The OSGi Framework version.
77 * </li>
78 * <li>{@link Constants#FRAMEWORK_VENDOR}- The Framework implementation
79 * vendor.</li>
80 * <li>{@link Constants#FRAMEWORK_LANGUAGE}- The language being used. See
81 * ISO 639 for possible values.</li>
82 * <li>{@link Constants#FRAMEWORK_OS_NAME}- The host computer operating
83 * system.</li>
84 * <li>{@link Constants#FRAMEWORK_OS_VERSION}- The host computer operating
85 * system version number.</li>
86 * <li>{@link Constants#FRAMEWORK_PROCESSOR}- The host computer processor
87 * name.</li>
88 * </ul>
89 * <p>
90 * All bundles must have permission to read these properties.
91 *
92 * <p>
93 * Note: The last four standard properties are used by the
94 * {@link Constants#BUNDLE_NATIVECODE} <code>Manifest</code> header's matching
95 * algorithm for selecting native language code.
96 *
97 * @param key The name of the requested property.
98 * @return The value of the requested property, or <code>null</code> if the
99 * property is undefined.
100 * @exception java.lang.SecurityException If the caller does not have the
101 * appropriate <code>PropertyPermission</code> to read the
102 * property, and the Java Runtime Environment supports
103 * permissions.
104 */
105 public abstract String getProperty(String key);
106
107 /**
108 * Returns the <code>Bundle</code> object associated with this
109 * <code>BundleContext</code>. This bundle is called the context bundle.
110 *
111 * @return The <code>Bundle</code> object associated with this
112 * <code>BundleContext</code>.
113 * @exception java.lang.IllegalStateException If this BundleContext is no
114 * longer valid.
115 */
116 public abstract Bundle getBundle();
117
118 /**
119 * Installs a bundle from the specified location string. A bundle is
120 * obtained from <code>location</code> as interpreted by the Framework in an
121 * implementation dependent manner.
122 * <p>
123 * Every installed bundle is uniquely identified by its location string,
124 * typically in the form of a URL.
125 *
126 * <p>
127 * The following steps are required to install a bundle:
128 * <ol>
129 * <li>If a bundle containing the same location string is already
130 * installed, the <code>Bundle</code> object for that bundle is returned.
131 *
132 * <li>The bundle's content is read from the location string. If this
133 * fails, a {@link BundleException} is thrown.
134 *
135 * <li>The bundle's <code>Bundle-NativeCode</code> dependencies are resolved.
136 * If this fails, a <code>BundleException</code> is thrown.
137 *
138 * <li>The bundle's associated resources are allocated. The associated
139 * resources minimally consist of a unique identifier and a persistent
140 * storage area if the platform has file system support. If this step fails,
141 * a <code>BundleException</code> is thrown.
142 *
143 * <li>If the bundle has declared an Bundle-RequiredExecutionEnvironment
144 * header, then the listed execution environments must be verified against
145 * the installed execution environments. If they are not all present, a
146 * <code>BundleException</code> must be thrown.
147 *
148 * <li>The bundle's state is set to <code>INSTALLED</code>.
149 *
150 * <li>A bundle event of type {@link BundleEvent#INSTALLED} is broadcast.
151 *
152 * <li>The <code>Bundle</code> object for the newly or previously installed
153 * bundle is returned.
154 * </ol>
155 *
156 * <b>Postconditions, no exceptions thrown </b>
157 * <ul>
158 * <li><code>getState()</code> in {<code>INSTALLED</code>,<code>RESOLVED</code>}.
159 * <li>Bundle has a unique ID.
160 * </ul>
161 * <b>Postconditions, when an exception is thrown </b>
162 * <ul>
163 * <li>Bundle is not installed and no trace of the bundle exists.
164 * </ul>
165 *
166 * @param location The location identifier of the bundle to install.
167 * @return The <code>Bundle</code> object of the installed bundle.
168 * @exception BundleException If the installation failed.
169 * @exception java.lang.SecurityException If the caller does not have the
170 * appropriate <code>AdminPermission</code>, and the Java Runtime
171 * Environment supports permissions.
172 * @exception java.lang.IllegalStateException If this BundleContext is no
173 * longer valid.
174 */
175 public abstract Bundle installBundle(String location)
176 throws BundleException;
177
178 /**
179 * Installs a bundle from the specified <code>InputStream</code> object.
180 *
181 * <p>
182 * This method performs all of the steps listed in
183 * <code>BundleContext.installBundle(String location)</code>, except that the
184 * bundle's content will be read from the <code>InputStream</code> object. The
185 * location identifier string specified will be used as the identity of the
186 * bundle.
187 *
188 * <p>
189 * This method must always close the <code>InputStream</code> object, even if
190 * an exception is thrown.
191 *
192 * @param location The location identifier of the bundle to install.
193 * @param input The <code>InputStream</code> object from which this bundle
194 * will be read.
195 * @return The <code>Bundle</code> object of the installed bundle.
196 * @exception BundleException If the provided stream cannot be read or the
197 * installation failed.
198 * @exception java.lang.SecurityException If the caller does not have the
199 * appropriate <code>AdminPermission</code>, and the Java Runtime
200 * Environment supports permissions.
201 * @exception java.lang.IllegalStateException If this BundleContext is no
202 * longer valid.
203 * @see #installBundle(java.lang.String)
204 */
205 public abstract Bundle installBundle(String location, InputStream input)
206 throws BundleException;
207
208 /**
209 * Returns the bundle with the specified identifier.
210 *
211 * @param id The identifier of the bundle to retrieve.
212 * @return A <code>Bundle</code> object or <code>null</code> if the identifier
213 * does not match any installed bundle.
214 */
215 public abstract Bundle getBundle(long id);
216
217 /**
218 * Returns a list of all installed bundles.
219 * <p>
220 * This method returns a list of all bundles installed in the OSGi
221 * environment at the time of the call to this method. However, since the
222 * Framework is a very dynamic environment, bundles can be installed or
223 * uninstalled at anytime.
224 *
225 * @return An array of <code>Bundle</code> objects, one object per installed
226 * bundle.
227 */
228 public abstract Bundle[] getBundles();
229
230 /**
231 * Adds the specified <code>ServiceListener</code> object with the specified
232 * <code>filter</code> to the context bundle's list of listeners.
233 * See {@link Filter} for a description of the filter syntax.
234 * <code>ServiceListener</code> objects are notified when a service has a
235 * lifecycle state change.
236 *
237 * <p>
238 * If the context bundle's list of listeners already contains a listener
239 * <code>l</code> such that <code>(l==listener)</code>, then this method replaces
240 * that listener's filter (which may be <code>null</code>) with the specified
241 * one (which may be <code>null</code>).
242 *
243 * <p>
244 * The listener is called if the filter criteria is met. To filter based
245 * upon the class of the service, the filter should reference the
246 * {@link Constants#OBJECTCLASS} property. If <code>filter</code> is
247 * <code>null</code>, all services are considered to match the filter.
248 *
249 * <p>
250 * When using a <code>filter</code>, it is possible that the
251 * <code>ServiceEvent</code>s for the complete lifecycle of a service will
252 * not be delivered to the listener. For example, if the <code>filter</code>
253 * only matches when the property <code>x</code> has the value <code>1</code>,
254 * the listener will not be called if the service is registered with the
255 * property <code>x</code> not set to the value <code>1</code>. Subsequently,
256 * when the service is modified setting property <code>x</code> to the value
257 * <code>1</code>, the filter will match and the listener will be called with
258 * a <code>ServiceEvent</code> of type <code>MODIFIED</code>. Thus, the
259 * listener will not be called with a <code>ServiceEvent</code> of type
260 * <code>REGISTERED</code>.
261 *
262 * <p>
263 * If the Java Runtime Environment supports permissions, the
264 * <code>ServiceListener</code> object will be notified of a service event
265 * only if the bundle that is registering it has the
266 * <code>ServicePermission</code> to get the service using at least one of the
267 * named classes the service was registered under.
268 *
269 * @param listener The <code>ServiceListener</code> object to be added.
270 * @param filter The filter criteria.
271 *
272 * @exception InvalidSyntaxException If <code>filter</code> contains an
273 * invalid filter string that cannot be parsed.
274 * @exception java.lang.IllegalStateException If this BundleContext is no
275 * longer valid.
276 *
277 * @see ServiceEvent
278 * @see ServiceListener
279 * @see ServicePermission
280 */
281 public abstract void addServiceListener(ServiceListener listener,
282 String filter) throws InvalidSyntaxException;
283
284 /**
285 * Adds the specified <code>ServiceListener</code> object to the context
286 * bundle's list of listeners.
287 *
288 * <p>
289 * This method is the same as calling
290 * <code>BundleContext.addServiceListener(ServiceListener listener,
291 * String filter)</code>
292 * with <code>filter</code> set to <code>null</code>.
293 *
294 * @param listener The <code>ServiceListener</code> object to be added.
295 * @exception java.lang.IllegalStateException If this BundleContext is no
296 * longer valid.
297 *
298 * @see #addServiceListener(ServiceListener, String)
299 */
300 public abstract void addServiceListener(ServiceListener listener);
301
302 /**
303 * Removes the specified <code>ServiceListener</code> object from the context
304 * bundle's list of listeners.
305 *
306 * <p>
307 * If <code>listener</code> is not contained in this context bundle's list of
308 * listeners, this method does nothing.
309 *
310 * @param listener The <code>ServiceListener</code> to be removed.
311 * @exception java.lang.IllegalStateException If this BundleContext is no
312 * longer valid.
313 */
314 public abstract void removeServiceListener(ServiceListener listener);
315
316 /**
317 * Adds the specified <code>BundleListener</code> object to the context
318 * bundle's list of listeners if not already present. BundleListener
319 * objects are notified when a bundle has a lifecycle state change.
320 *
321 * <p>
322 * If the context bundle's list of listeners already contains a listener
323 * <code>l</code> such that <code>(l==listener)</code>, this method does
324 * nothing.
325 *
326 * @param listener The <code>BundleListener</code> to be added.
327 * @exception java.lang.IllegalStateException If this BundleContext is no
328 * longer valid.
329 *
330 * @see BundleEvent
331 * @see BundleListener
332 */
333 public abstract void addBundleListener(BundleListener listener);
334
335 /**
336 * Removes the specified <code>BundleListener</code> object from the context
337 * bundle's list of listeners.
338 *
339 * <p>
340 * If <code>listener</code> is not contained in the context bundle's list of
341 * listeners, this method does nothing.
342 *
343 * @param listener The <code>BundleListener</code> object to be removed.
344 * @exception java.lang.IllegalStateException If this BundleContext is no
345 * longer valid.
346 */
347 public abstract void removeBundleListener(BundleListener listener);
348
349 /**
350 * Adds the specified <code>FrameworkListener</code> object to the context
351 * bundle's list of listeners if not already present.
352 * FrameworkListeners are notified of general Framework events.
353 *
354 * <p>
355 * If the context bundle's list of listeners already contains a listener
356 * <code>l</code> such that <code>(l==listener)</code>, this method does
357 * nothing.
358 *
359 * @param listener The <code>FrameworkListener</code> object to be added.
360 * @exception java.lang.IllegalStateException If this BundleContext is no
361 * longer valid.
362 *
363 * @see FrameworkEvent
364 * @see FrameworkListener
365 */
366 public abstract void addFrameworkListener(FrameworkListener listener);
367
368 /**
369 * Removes the specified <code>FrameworkListener</code> object from the
370 * context bundle's list of listeners.
371 *
372 * <p>
373 * If <code>listener</code> is not contained in the context bundle's list of
374 * listeners, this method does nothing.
375 *
376 * @param listener The <code>FrameworkListener</code> object to be removed.
377 * @exception java.lang.IllegalStateException If this BundleContext is no
378 * longer valid.
379 */
380 public abstract void removeFrameworkListener(FrameworkListener listener);
381
382 /**
383 * Registers the specified service object with the specified properties
384 * under the specified class names into the Framework. A
385 * <code>ServiceRegistration</code> object is returned. The
386 * <code>ServiceRegistration</code> object is for the private use of the
387 * bundle registering the service and should not be shared with other
388 * bundles. The registering bundle is defined to be the context bundle.
389 * Other bundles can locate the service by using either the
390 * {@link #getServiceReferences} or {@link #getServiceReference} method.
391 *
392 * <p>
393 * A bundle can register a service object that implements the
394 * {@link ServiceFactory} interface to have more flexibility in providing
395 * service objects to other bundles.
396 *
397 * <p>
398 * The following steps are required to register a service:
399 * <ol>
400 * <li>If <code>service</code> is not a <code>ServiceFactory</code>, an
401 * <code>IllegalArgumentException</code> is thrown if <code>service</code> is
402 * not an <code>instanceof</code> all the classes named.
403 * <li>The Framework adds these service properties to the specified
404 * <code>Dictionary</code> (which may be <code>null</code>): a property named
405 * {@link Constants#SERVICE_ID} identifying the registration number of the
406 * service and a property named {@link Constants#OBJECTCLASS} containing
407 * all the specified classes. If any of these properties have already been
408 * specified by the registering bundle, their values will be overwritten by
409 * the Framework.
410 * <li>The service is added to the Framework service registry and may now
411 * be used by other bundles.
412 * <li>A service event of type {@link ServiceEvent#REGISTERED} is
413 * synchronously sent.
414 * <li>A <code>ServiceRegistration</code> object for this registration is
415 * returned.
416 * </ol>
417 *
418 * @param clazzes The class names under which the service can be located.
419 * The class names in this array will be stored in the service's
420 * properties under the key {@link Constants#OBJECTCLASS}.
421 * @param service The service object or a <code>ServiceFactory</code> object.
422 * @param properties The properties for this service. The keys in the
423 * properties object must all be <code>String</code> objects. See
424 * {@link Constants} for a list of standard service property keys.
425 * Changes should not be made to this object after calling this
426 * method. To update the service's properties the
427 * {@link ServiceRegistration#setProperties} method must be called.
428 * The set of properties may be <code>null</code> if the service has no
429 * properties.
430 *
431 * @return A <code>ServiceRegistration</code> object for use by the bundle
432 * registering the service to update the service's properties or to
433 * unregister the service.
434 *
435 * @exception java.lang.IllegalArgumentException If one of the following is
436 * true:
437 * <ul>
438 * <li><code>service</code> is <code>null</code>.
439 * <li><code>service</code> is not a <code>ServiceFactory</code>
440 * object and is not an instance of all the named classes in
441 * <code>clazzes</code>.
442 * <li><code>properties</code> contains case variants of the same
443 * key name.
444 * </ul>
445 *
446 * @exception java.lang.SecurityException If the caller does not have the
447 * <code>ServicePermission</code> to register the service for all
448 * the named classes and the Java Runtime Environment supports
449 * permissions.
450 *
451 * @exception java.lang.IllegalStateException If this BundleContext is no
452 * longer valid.
453 *
454 * @see ServiceRegistration
455 * @see ServiceFactory
456 */
457 public abstract ServiceRegistration registerService(String[] clazzes,
458 Object service, Dictionary properties);
459
460 /**
461 * Registers the specified service object with the specified properties
462 * under the specified class name with the Framework.
463 *
464 * <p>
465 * This method is otherwise identical to
466 * {@link #registerService(java.lang.String[], java.lang.Object,
467 * java.util.Dictionary)} and is provided as a convenience when
468 * <code>service</code> will only be registered under a single class name.
469 * Note that even in this case the value of the service's
470 * {@link Constants#OBJECTCLASS} property will be an array of strings,
471 * rather than just a single string.
472 *
473 * @exception java.lang.IllegalStateException If this BundleContext is no
474 * longer valid.
475 * @see #registerService(java.lang.String[], java.lang.Object,
476 * java.util.Dictionary)
477 */
478 public abstract ServiceRegistration registerService(String clazz,
479 Object service, Dictionary properties);
480
481 /**
482 * Returns an array of <code>ServiceReference</code> objects. The returned
483 * array of <code>ServiceReference</code> objects contains services that
484 * were registered under the specified class, match the specified filter
485 * criteria, and the packages for the class names under which the services
486 * were registered match the context bundle's packages as defined in
487 * {@link ServiceReference#isAssignableTo(Bundle, String)}.
488 *
489 * <p>
490 * The list is valid at the time of the call to this method, however since the
491 * Framework is a very dynamic environment, services can be modified or
492 * unregistered at anytime.
493 *
494 * <p>
495 * <code>filter</code> is used to select the registered service whose
496 * properties objects contain keys and values which satisfy the filter. See
497 * {@link Filter} for a description of the filter string syntax.
498 *
499 * <p>
500 * If <code>filter</code> is <code>null</code>, all registered services are
501 * considered to match the filter.
502 * If <code>filter</code> cannot be parsed, an {@link InvalidSyntaxException}
503 * will be thrown with a human readable message where the filter became
504 * unparsable.
505 *
506 * <p>
507 * The following steps are required to select a set of
508 * <code>ServiceReference</code> objects:
509 * <ol>
510 * <li>If the filter string is not <code>null</code>, the filter string is
511 * parsed and the set <code>ServiceReference</code> objects of registered
512 * services that satisfy the filter is produced. If the filter string is
513 * <code>null</code>, then all registered
514 * services are considered to satisfy the filter.
515 * <li>If the Java Runtime Environment supports permissions, the
516 * set of <code>ServiceReference</code> objects produced by the
517 * previous step is reduced by checking that the caller has
518 * the <code>ServicePermission</code> to get at least one
519 * of the class names under which the service was registered. If the caller
520 * does not have the correct permission for a particular
521 * <code>ServiceReference</code> object, then it is removed from the set.
522 * <li>If <code>clazz</code> is not <code>null</code>, the set is further
523 * reduced to those services that are an <code>instanceof</code> and were
524 * registered under the specified class. The complete list of classes of
525 * which a service is an instance and which were specified when the service
526 * was registered is available from the service's
527 * {@link Constants#OBJECTCLASS} property.
528 * <li>The set is reduced one final time by cycling through each
529 * <code>ServiceReference</code> object and calling
530 * {@link ServiceReference#isAssignableTo(Bundle, String)} with the
531 * context bundle and each class name under which the
532 * <code>ServiceReference</code> object was registered. For any
533 * given <code>ServiceReference</code> object, if any call to
534 * {@link ServiceReference#isAssignableTo(Bundle, String)} returns
535 * <code>false</code>, then it is removed from the set of
536 * <code>ServiceReference</code> objects.
537 * <li>An array of the remaining <code>ServiceReference</code> objects
538 * is returned.
539 * </ol>
540 *
541 * @param clazz The class name with which the service was registered or
542 * <code>null</code> for all services.
543 * @param filter The filter criteria.
544 * @return An array of <code>ServiceReference</code> objects or <code>null</code>
545 * if no services are registered which satisfy the search.
546 * @exception InvalidSyntaxException If <code>filter</code> contains an
547 * invalid filter string that cannot be parsed.
548 * @exception java.lang.IllegalStateException If this BundleContext is no
549 * longer valid.
550 */
551 public abstract ServiceReference[] getServiceReferences(String clazz,
552 String filter) throws InvalidSyntaxException;
553
554 /**
555 * Returns an array of <code>ServiceReference</code> objects. The returned
556 * array of <code>ServiceReference</code> objects contains services that
557 * were registered under the specified class and match the
558 * specified filter criteria.
559 *
560 * <p>
561 * The list is valid at the time of the call to this method, however since the
562 * Framework is a very dynamic environment, services can be modified or
563 * unregistered at anytime.
564 *
565 * <p>
566 * <code>filter</code> is used to select the registered service whose
567 * properties objects contain keys and values which satisfy the filter. See
568 * {@link Filter} for a description of the filter string syntax.
569 *
570 * <p>
571 * If <code>filter</code> is <code>null</code>, all registered services are
572 * considered to match the filter.
573 * If <code>filter</code> cannot be parsed, an {@link InvalidSyntaxException}
574 * will be thrown with a human readable message where the filter became
575 * unparsable.
576 *
577 * <p>
578 * The following steps are required to select a set of
579 * <code>ServiceReference</code> objects:
580 * <ol>
581 * <li>If the filter string is not <code>null</code>, the filter string is
582 * parsed and the set <code>ServiceReference</code> objects of registered
583 * services that satisfy the filter is produced. If the filter string is
584 * <code>null</code>, then all registered
585 * services are considered to satisfy the filter.
586 * <li>If the Java Runtime Environment supports permissions, the
587 * set of <code>ServiceReference</code> objects produced by the
588 * previous step is reduced by checking that the caller has
589 * the <code>ServicePermission</code> to get at least one
590 * of the class names under which the service was registered. If the caller
591 * does not have the correct permission for a particular
592 * <code>ServiceReference</code> object, then it is removed from the set.
593 * <li>If <code>clazz</code> is not <code>null</code>, the set is further
594 * reduced to those services that are an <code>instanceof</code> and were
595 * registered under the specified class. The complete list of classes of
596 * which a service is an instance and which were specified when the service
597 * was registered is available from the service's
598 * {@link Constants#OBJECTCLASS} property.
599 * <li>An array of the remaining <code>ServiceReference</code> objects
600 * is returned.
601 * </ol>
602 *
603 * @param clazz The class name with which the service was registered or
604 * <code>null</code> for all services.
605 * @param filter The filter criteria.
606 * @return An array of <code>ServiceReference</code> objects or <code>null</code>
607 * if no services are registered which satisfy the search.
608 * @exception InvalidSyntaxException If <code>filter</code> contains an
609 * invalid filter string that cannot be parsed.
610 * @exception java.lang.IllegalStateException If this BundleContext is no
611 * longer valid.
612 */
613 public abstract ServiceReference[] getAllServiceReferences(String clazz,
614 String filter) throws InvalidSyntaxException;
615
616 /**
617 * Returns a <code>ServiceReference</code> object for a service that
618 * implements and was registered under the specified class.
619 *
620 * <p>
621 * This <code>ServiceReference</code> object is valid at the time of the call
622 * to this method, however as the Framework is a very dynamic environment,
623 * services can be modified or unregistered at anytime.
624 *
625 * <p>
626 * This method is the same as calling
627 * {@link BundleContext#getServiceReferences(String, String)} with a
628 * <code>null</code> filter string. It is provided as a convenience for when
629 * the caller is interested in any service that implements the specified
630 * class.
631 * <p>
632 * If multiple such services exist, the service with the highest ranking (as
633 * specified in its {@link Constants#SERVICE_RANKING} property) is returned.
634 * <p>
635 * If there is a tie in ranking, the service with the lowest service ID (as
636 * specified in its {@link Constants#SERVICE_ID} property); that is, the
637 * service that was registered first is returned.
638 *
639 * @param clazz The class name with which the service was registered.
640 * @return A <code>ServiceReference</code> object, or <code>null</code> if no
641 * services are registered which implement the named class.
642 * @exception java.lang.IllegalStateException If this BundleContext is no
643 * longer valid.
644 * @see #getServiceReferences(String, String)
645 */
646 public abstract ServiceReference getServiceReference(String clazz);
647
648 /**
649 * Returns the specified service object for a service.
650 * <p>
651 * A bundle's use of a service is tracked by the bundle's use count of that
652 * service. Each time a service's service object is returned by
653 * {@link #getService(ServiceReference)} the context bundle's use count for
654 * that service is incremented by one. Each time the service is released by
655 * {@link #ungetService(ServiceReference)} the context bundle's use count
656 * for that service is decremented by one.
657 * <p>
658 * When a bundle's use count for a service drops to zero, the bundle should
659 * no longer use that service.
660 *
661 * <p>
662 * This method will always return <code>null</code> when the service
663 * associated with this <code>reference</code> has been unregistered.
664 *
665 * <p>
666 * The following steps are required to get the service object:
667 * <ol>
668 * <li>If the service has been unregistered, <code>null</code> is returned.
669 * <li>The context bundle's use count for this service is incremented by
670 * one.
671 * <li>If the context bundle's use count for the service is currently one
672 * and the service was registered with an object implementing the
673 * <code>ServiceFactory</code> interface, the
674 * {@link ServiceFactory#getService(Bundle, ServiceRegistration)} method
675 * is called to create a service object for the context bundle. This service
676 * object is cached by the Framework. While the context bundle's use count
677 * for the service is greater than zero, subsequent calls to get the
678 * services's service object for the context bundle will return the cached
679 * service object.
680 * <br>
681 * If the service object returned by the <code>ServiceFactory</code> object is
682 * not an <code>instanceof</code> all the classes named when the service was
683 * registered or the <code>ServiceFactory</code> object throws an exception,
684 * <code>null</code> is returned and a Framework event of type
685 * {@link FrameworkEvent#ERROR} is broadcast.
686 * <li>The service object for the service is returned.
687 * </ol>
688 *
689 * @param reference A reference to the service.
690 * @return A service object for the service associated with
691 * <code>reference</code> or <code>null</code> if the service is not
692 * registered or does not implement the classes under which it was
693 * registered in the case of a <code>ServiceFactory</code>.
694 * @exception java.lang.SecurityException If the caller does not have the
695 * <code>ServicePermission</code> to get the service using at least
696 * one of the named classes the service was registered under and
697 * the Java Runtime Environment supports permissions.
698 * @exception java.lang.IllegalStateException If this BundleContext is no
699 * longer valid.
700 * @see #ungetService(ServiceReference)
701 * @see ServiceFactory
702 */
703 public abstract Object getService(ServiceReference reference);
704
705 /**
706 * Releases the service object referenced by the specified
707 * <code>ServiceReference</code> object. If the context bundle's use count for
708 * the service is zero, this method returns <code>false</code>. Otherwise,
709 * the context bundle's use count for the service is decremented by one.
710 *
711 * <p>
712 * The service's service object should no longer be used and all references
713 * to it should be destroyed when a bundle's use count for the service drops
714 * to zero.
715 *
716 * <p>
717 * The following steps are required to unget the service object:
718 * <ol>
719 * <li>If the context bundle's use count for the service is zero or the
720 * service has been unregistered, <code>false</code> is returned.
721 * <li>The context bundle's use count for this service is decremented by
722 * one.
723 * <li>If the context bundle's use count for the service is currently zero
724 * and the service was registered with a <code>ServiceFactory</code> object,
725 * the {@link ServiceFactory#ungetService(Bundle, ServiceRegistration, Object)}
726 * method is called to release the service object for the context bundle.
727 * <li><code>true</code> is returned.
728 * </ol>
729 *
730 * @param reference A reference to the service to be released.
731 * @return <code>false</code> if the context bundle's use count for the
732 * service is zero or if the service has been unregistered;
733 * <code>true</code> otherwise.
734 * @exception java.lang.IllegalStateException If this BundleContext is no
735 * longer valid.
736 * @see #getService
737 * @see ServiceFactory
738 */
739 public abstract boolean ungetService(ServiceReference reference);
740
741 /**
742 * Creates a <code>File</code> object for a file in the persistent storage
743 * area provided for the bundle by the Framework. This method will return
744 * <code>null</code> if the platform does not have file system support.
745 *
746 * <p>
747 * A <code>File</code> object for the base directory of the persistent storage
748 * area provided for the context bundle by the Framework can be obtained by
749 * calling this method with an empty string as <code>filename</code>.
750 *
751 * <p>
752 * If the Java Runtime Environment supports permissions, the Framework will
753 * ensure that the bundle has the <code>java.io.FilePermission</code> with
754 * actions <code>read</code>,<code>write</code>,<code>delete</code> for all
755 * files (recursively) in the persistent storage area provided for the
756 * context bundle.
757 *
758 * @param filename A relative name to the file to be accessed.
759 * @return A <code>File</code> object that represents the requested file or
760 * <code>null</code> if the platform does not have file system
761 * support.
762 * @exception java.lang.IllegalStateException If this BundleContext is no
763 * longer valid.
764 */
765 public abstract File getDataFile(String filename);
766
767 /**
768 * Creates a <code>Filter</code> object. This <code>Filter</code> object may be
769 * used to match a <code>ServiceReference</code> object or a
770 * <code>Dictionary</code> object.
771 *
772 * <p>
773 * If the filter cannot be parsed, an {@link InvalidSyntaxException} will be
774 * thrown with a human readable message where the filter became unparsable.
775 *
776 * @param filter The filter string.
777 * @return A <code>Filter</code> object encapsulating the filter string.
778 * @exception InvalidSyntaxException If <code>filter</code> contains an
779 * invalid filter string that cannot be parsed.
780 * @exception NullPointerException If <code>filter</code> is null.
781 * @exception java.lang.IllegalStateException If this BundleContext is no
782 * longer valid.
783 *
784 * @since 1.1
785 * @see "Framework specification for a description of the filter string syntax."
786 */
787 public abstract Filter createFilter(String filter)
788 throws InvalidSyntaxException;
789}
790