org.apache.felix.ipojo
Class IPojoContext

java.lang.Object
  extended by org.apache.felix.ipojo.IPojoContext
All Implemented Interfaces:
ServiceContext, BundleContext

public class IPojoContext
extends Object
implements BundleContext, ServiceContext

The iPOJO Context is a BundleContext implementation allowing the separation between Bundle context and Service (Bundle) Context. This is used inside composition to differentiate the classloading context (i.e. Bundle) and the service registry access. This class delegates calls to the good internal context (either the BundleContext or the ServiceContext) according to the method. If the instance does not have a valid service context, the bundle context is always used.

Author:
Felix Project Team

Constructor Summary
IPojoContext(BundleContext context)
          Creates an iPOJO Context.
IPojoContext(BundleContext bundleContext, ServiceContext serviceContext)
          Creates an iPOJO Context.
 
Method Summary
 void addBundleListener(BundleListener listener)
          Adds a bundle listener.
 void addFrameworkListener(FrameworkListener listener)
          Adds a framework listener.
 void addServiceListener(ServiceListener listener)
          Add a service listener.
 void addServiceListener(ServiceListener listener, String filter)
          Adds a service listener.
 Filter createFilter(String filter)
          Creates a filter objects.
 ServiceReference[] getAllServiceReferences(String clazz, String filter)
          Gets the service references matching with the given query.
 Bundle getBundle()
          Gets the current bundle object.
 Bundle getBundle(long bundleId)
          Gets the bundle object with the given id.
 Bundle[] getBundles()
          Gets installed bundles.
 File getDataFile(String filename)
          Gets a data file.
 BundleContext getGlobalContext()
          Gets the global context, i.e.
 String getProperty(String key)
          Gets a property value.
 Object getService(ServiceReference reference)
          Gets a service object.
 ServiceContext getServiceContext()
          Gets the service context, i.e.
 ServiceReference getServiceReference(String clazz)
          Gets a service reference for the given interface.
 ServiceReference[] getServiceReferences(String clazz, String filter)
          Gets service reference list for the given query.
 Bundle installBundle(String location)
          Installs a bundle.
 Bundle installBundle(String location, InputStream input)
          Installs a bundle.
 ServiceRegistration registerService(String[] clazzes, Object service, Dictionary properties)
          Registers a service.
 ServiceRegistration registerService(String clazz, Object service, Dictionary properties)
          Registers a service.
 void removeBundleListener(BundleListener listener)
          Removes a bundle listener.
 void removeFrameworkListener(FrameworkListener listener)
          Removes a framework listener.
 void removeServiceListener(ServiceListener listener)
          Removes a service listener.
 boolean ungetService(ServiceReference reference)
          Ungets the service reference.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

IPojoContext

public IPojoContext(BundleContext context)
Creates an iPOJO Context. No service context is specified. This constructor is used when the instance lives in the global context.

Parameters:
context - the bundle context

IPojoContext

public IPojoContext(BundleContext bundleContext,
                    ServiceContext serviceContext)
Creates an iPOJO Context. A service context is used to refer to the service registry. The service context will be used for all service accesses.

Parameters:
bundleContext - the bundle context
serviceContext - the service context
Method Detail

addBundleListener

public void addBundleListener(BundleListener listener)
Adds a bundle listener.

Specified by:
addBundleListener in interface BundleContext
Parameters:
listener - the listener to add
See Also:
BundleContext.addBundleListener(org.osgi.framework.BundleListener)

addFrameworkListener

public void addFrameworkListener(FrameworkListener listener)
Adds a framework listener.

Specified by:
addFrameworkListener in interface BundleContext
Parameters:
listener - the listener object to add
See Also:
BundleContext.addFrameworkListener(org.osgi.framework.FrameworkListener)

addServiceListener

public void addServiceListener(ServiceListener listener,
                               String filter)
                        throws InvalidSyntaxException
Adds a service listener. This methods registers the listener on the service context if it specified. Otherwise, if the internal dispatcher is enabled, it registers the listener inside the internal dispatcher (if the filter match against the iPOJO Filter format match(String)). Finally, if the internal dispatcher is disabled, it uses the "regular" bundle context.

Specified by:
addServiceListener in interface ServiceContext
Specified by:
addServiceListener in interface BundleContext
Parameters:
listener - the service listener to add.
filter - the LDAP filter
Throws:
InvalidSyntaxException - if LDAP filter is malformed
See Also:
BundleContext.addServiceListener(org.osgi.framework.ServiceListener, java.lang.String)

addServiceListener

public void addServiceListener(ServiceListener listener)
Add a service listener. This methods registers the listener on the service context if it specified. Otherwise, it uses the "regular" bundle context.

Specified by:
addServiceListener in interface ServiceContext
Specified by:
addServiceListener in interface BundleContext
Parameters:
listener - the service listener to add.
See Also:
BundleContext.addServiceListener(org.osgi.framework.ServiceListener)

createFilter

public Filter createFilter(String filter)
                    throws InvalidSyntaxException
Creates a filter objects. This method always uses the bundle context.

Specified by:
createFilter in interface BundleContext
Parameters:
filter - the string form of the LDAP filter to create
Returns:
the filter object.
Throws:
InvalidSyntaxException - if the given filter is malformed
See Also:
BundleContext.createFilter(java.lang.String)

getAllServiceReferences

public ServiceReference[] getAllServiceReferences(String clazz,
                                                  String filter)
                                           throws InvalidSyntaxException
Gets the service references matching with the given query. Uses the service context if specified, used the bundle context otherwise.

Specified by:
getAllServiceReferences in interface ServiceContext
Specified by:
getAllServiceReferences in interface BundleContext
Parameters:
clazz - the required interface
filter - the LDAP filter
Returns:
the array of available service references
Throws:
InvalidSyntaxException - if the LDAP filter is malformed
See Also:
BundleContext.getAllServiceReferences(java.lang.String, java.lang.String)

getBundle

public Bundle getBundle()
Gets the current bundle object.

Specified by:
getBundle in interface BundleContext
Returns:
the bundle declaring the component type of the instance using the current IPojoContext.
See Also:
BundleContext.getBundle()

getBundle

public Bundle getBundle(long bundleId)
Gets the bundle object with the given id.

Specified by:
getBundle in interface BundleContext
Parameters:
bundleId - the bundle id
Returns:
the bundle object
See Also:
BundleContext.getBundle(long)

getBundles

public Bundle[] getBundles()
Gets installed bundles.

Specified by:
getBundles in interface BundleContext
Returns:
the list of installed bundles
See Also:
BundleContext.getBundles()

getDataFile

public File getDataFile(String filename)
Gets a data file.

Specified by:
getDataFile in interface BundleContext
Parameters:
filename - the file name.
Returns:
the File object
See Also:
BundleContext.getDataFile(java.lang.String)

getProperty

public String getProperty(String key)
Gets a property value.

Specified by:
getProperty in interface BundleContext
Parameters:
key - the key of the asked property
Returns:
the property value (object) or null if no property are associated with the given key
See Also:
BundleContext.getProperty(java.lang.String)

getService

public Object getService(ServiceReference reference)
Gets a service object. The given service reference must come from the same context than where the service is get. This method uses the service context if specified, the bundle context otherwise.

Specified by:
getService in interface ServiceContext
Specified by:
getService in interface BundleContext
Parameters:
reference - the required service reference
Returns:
the service object or null if the service reference is no more valid or if the service object is not accessible.
See Also:
BundleContext.getService(org.osgi.framework.ServiceReference)

getServiceReference

public ServiceReference getServiceReference(String clazz)
Gets a service reference for the given interface. This method uses the service context if specified, the bundle context otherwise.

Specified by:
getServiceReference in interface ServiceContext
Specified by:
getServiceReference in interface BundleContext
Parameters:
clazz - the required interface name
Returns:
a service reference on a available provider or null if no providers available
See Also:
BundleContext.getServiceReference(java.lang.String)

getServiceReferences

public ServiceReference[] getServiceReferences(String clazz,
                                               String filter)
                                        throws InvalidSyntaxException
Gets service reference list for the given query. This method uses the service context if specified, the bundle context otherwise.

Specified by:
getServiceReferences in interface ServiceContext
Specified by:
getServiceReferences in interface BundleContext
Parameters:
clazz - the name of the required service interface
filter - the LDAP filter to apply on service provider
Returns:
the array of consistent service reference or null if no available providers
Throws:
InvalidSyntaxException - if the LDAP filter is malformed
See Also:
BundleContext.getServiceReferences(java.lang.String, java.lang.String)

installBundle

public Bundle installBundle(String location)
                     throws BundleException
Installs a bundle.

Specified by:
installBundle in interface BundleContext
Parameters:
location - the URL of the bundle to install
Returns:
the installed bundle
Throws:
BundleException - if the bundle cannot be installed correctly
See Also:
BundleContext.installBundle(java.lang.String)

installBundle

public Bundle installBundle(String location,
                            InputStream input)
                     throws BundleException
Installs a bundle.

Specified by:
installBundle in interface BundleContext
Parameters:
location - the URL of the bundle to install
input - the input stream to load the bundle.
Returns:
the installed bundle
Throws:
BundleException - if the bundle cannot be installed correctly
See Also:
BundleContext.installBundle(java.lang.String, java.io.InputStream)

registerService

public ServiceRegistration registerService(String[] clazzes,
                                           Object service,
                                           Dictionary properties)
Registers a service. This method uses the service context if specified (and so, registers the service in this service registry), the bundle context otherwise (the service will be available to every global instances).

Specified by:
registerService in interface ServiceContext
Specified by:
registerService in interface BundleContext
Parameters:
clazzes - the interfaces provided by the service.
service - the service object.
properties - the service properties to publish
Returns:
the service registration for this service publication.
See Also:
ServiceContext.registerService(java.lang.String[], java.lang.Object, java.util.Dictionary)

registerService

public ServiceRegistration registerService(String clazz,
                                           Object service,
                                           Dictionary properties)
Registers a service. This method uses the service context if specified (and so, registers the service in this service registry), the bundle context otherwise (the service will be available to every global instances).

Specified by:
registerService in interface ServiceContext
Specified by:
registerService in interface BundleContext
Parameters:
clazz - the interface provided by the service.
service - the the service object.
properties - the service properties to publish.
Returns:
the service registration for this service publication.
See Also:
BundleContext.registerService(java.lang.String, java.lang.Object, java.util.Dictionary)

removeBundleListener

public void removeBundleListener(BundleListener listener)
Removes a bundle listener.

Specified by:
removeBundleListener in interface BundleContext
Parameters:
listener - the listener to remove
See Also:
BundleContext.removeBundleListener(org.osgi.framework.BundleListener)

removeFrameworkListener

public void removeFrameworkListener(FrameworkListener listener)
Removes a framework listener.

Specified by:
removeFrameworkListener in interface BundleContext
Parameters:
listener - the listener to remove
See Also:
BundleContext.removeFrameworkListener(org.osgi.framework.FrameworkListener)

removeServiceListener

public void removeServiceListener(ServiceListener listener)
Removes a service listener. Removes the service listener from where it was registered so either in the global context, or in the service context or in the internal dispatcher.

Specified by:
removeServiceListener in interface ServiceContext
Specified by:
removeServiceListener in interface BundleContext
Parameters:
listener - the service listener to remove
See Also:
ServiceContext.removeServiceListener(org.osgi.framework.ServiceListener), BundleContext.removeServiceListener(org.osgi.framework.ServiceListener)

ungetService

public boolean ungetService(ServiceReference reference)
Ungets the service reference. This method uses the service context if specified, the bundle context otherwise.

Specified by:
ungetService in interface ServiceContext
Specified by:
ungetService in interface BundleContext
Parameters:
reference - the reference to unget
Returns:
true if you are the last user of the reference
See Also:
BundleContext.ungetService(org.osgi.framework.ServiceReference)

getGlobalContext

public BundleContext getGlobalContext()
Gets the global context, i.e. the bundle context of the factory.

Returns:
the global bundle context.

getServiceContext

public ServiceContext getServiceContext()
Gets the service context, i.e. the composite context. Returns null if the instance does not live inside a composite.

Returns:
the service context or null.


Copyright © 2006-2011 Apache Software Foundation. All Rights Reserved.