livespace.osgi
Class OsgiUtil

java.lang.Object
  extended by livespace.osgi.OsgiUtil

public final class OsgiUtil
extends Object

OSGi utilty methods.

Author:
Matthew Phillips

Method Summary
static ClassLoader classLoaderFor(org.osgi.framework.Bundle bundle)
          Determine the class loader for a bundle.
static void copyBundleResourceToFile(org.osgi.framework.Bundle bundle, String resource, File file)
          Copies a bundle resource to a specified file on the file system.
static org.osgi.framework.Bundle findBundle(org.osgi.framework.BundleContext bundleContext, String bundleName)
          Return a bundle with a given name, or throw an exception if none found.
static String getHostCategory()
          Get the host's assigned category or "unknown" if not set.
static String getHostName()
          Get the local Livespace host's name.
static String getIPAddress()
          Get the local Livespace host's IP address.
static String getShortHostName()
           
static ClassLoader installBundleClassLoader(Class<?> bundleClass)
          Install the OSGi bundle class loader as the current thread's context class loader.
static ClassLoader installBundleClassLoader(ClassLoader classLoader)
          Install the OSGi bundle class loader as the current thread's context class loader.
static InputStream openUrl(URL url)
          Open an input stream on a URL.
static org.osgi.framework.Bundle peekBundle(org.osgi.framework.BundleContext bundleContext, String bundleName)
          Return a bundle with a given name, or null if none found.
static void restoreClassLoader(ClassLoader origLoader)
          Reverse the effect of installBundleClassLoader(Class).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

installBundleClassLoader

public static ClassLoader installBundleClassLoader(Class<?> bundleClass)
Install the OSGi bundle class loader as the current thread's context class loader. This can be used to ensure that bundle resources and classes will be seen. See this KF thread: http://sourceforge.net/forum/message.php?msg_id=2536560

Parameters:
bundleClass - A class that was loaded as part of the bundle.
Returns:
The orginal class loader. This should be passed to a call to restoreClassLoader(ClassLoader) after calling client code.
See Also:
restoreClassLoader(ClassLoader)

installBundleClassLoader

public static ClassLoader installBundleClassLoader(ClassLoader classLoader)
Install the OSGi bundle class loader as the current thread's context class loader. This can be used to ensure that bundle resources and classes will be seen. See this KF thread: http://sourceforge.net/forum/message.php?msg_id=2536560

Parameters:
classLoader - The class loader for classes in the bundle.
Returns:
The orginal class loader. This should be passed to a call to restoreClassLoader(ClassLoader) after calling client code.
See Also:
restoreClassLoader(ClassLoader)

restoreClassLoader

public static void restoreClassLoader(ClassLoader origLoader)
Reverse the effect of installBundleClassLoader(Class).

Parameters:
origLoader - The class loader returned by installBundleClassLoader.
See Also:
installBundleClassLoader(Class)

peekBundle

public static org.osgi.framework.Bundle peekBundle(org.osgi.framework.BundleContext bundleContext,
                                                   String bundleName)
Return a bundle with a given name, or null if none found.

Parameters:
bundleContext - Any valid bundle context.
bundleName - The bundle name.
See Also:
findBundle(BundleContext, String)

findBundle

public static org.osgi.framework.Bundle findBundle(org.osgi.framework.BundleContext bundleContext,
                                                   String bundleName)
                                            throws IllegalArgumentException
Return a bundle with a given name, or throw an exception if none found.

Parameters:
bundleContext - Any valid bundle context.
bundleName - The bundle name.
Throws:
IllegalArgumentException - if no matching bundle found.
See Also:
peekBundle(BundleContext, String)

getHostName

public static String getHostName()
                          throws IOException,
                                 UnknownHostException
Get the local Livespace host's name. This returns livespace.host.name if set, otherwise it tries first to get a host name, then falls back on the IP address.

Throws:
IOException - if no host name or IP can be found for the host. This could only really happen if the host is not on a network: clients should use livespace.host.name in this case.
UnknownHostException

getIPAddress

public static String getIPAddress()
                           throws IOException
Get the local Livespace host's IP address. This returns livespace.host.ip if set, otherwise it tries to get a host IP.

Throws:
IOException - if no host IP can be found for the host. This could only really happen if the host is not on a network: clients should use livespace.host.ip in this case.

getShortHostName

public static String getShortHostName()
                               throws IOException,
                                      UnknownHostException
Throws:
IOException
UnknownHostException

getHostCategory

public static String getHostCategory()
Get the host's assigned category or "unknown" if not set. This is usually set via the "livespace.host.category" system property.


openUrl

public static InputStream openUrl(URL url)
                           throws IOException
Open an input stream on a URL. Adds some logic to generate a FileNotFoundException for HTTP 404's.

Throws:
IOException

classLoaderFor

public static ClassLoader classLoaderFor(org.osgi.framework.Bundle bundle)
Determine the class loader for a bundle. Currently this has to use a hack as the OSGi API does not seem to support this directly.


copyBundleResourceToFile

public static void copyBundleResourceToFile(org.osgi.framework.Bundle bundle,
                                            String resource,
                                            File file)
                                     throws FileNotFoundException,
                                            IOException
Copies a bundle resource to a specified file on the file system.

Parameters:
bundle - the bundle that contains the resource to copy
resource - the name of the resource within the bundle that is to be copied
file - the target file which the bundle resource should be copied to
Throws:
FileNotFoundException - If the target file is not found
IOException - If there is a problem writing to the file or reading the resource from the bundle


Copyright © 2008 Commonwealth of Australia