Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

Collection of OSGi conversion tips

Table of Contents
minLevel3

Lets collect tips on osgi conversion here

jars with xmlbeans compilation

...

Code Block
Index: src/main/java/org/apache/felix/framework/searchpolicy/Resolver.java
===================================================================
--- src/main/java/org/apache/felix/framework/searchpolicy/Resolver.java	(revision 831350)
+++ src/main/java/org/apache/felix/framework/searchpolicy/Resolver.java	(working copy)
@@ -332,8 +332,12 @@
                     }
                     else
                     {
+                        System.out.println(
+                            "Constraint violation for " + importer
+                            + " detected; module can see "
+                            + rp + " and " + rpUses);
                         m_logger.log(
-                            Logger.LOG_DEBUG,
+                            Logger.LOG_ERROR,
                             "Constraint violation for " + importer
                             + " detected; module can see "
                             + rp + " and " + rpUses);
@@ -705,7 +709,7 @@
         catch (ResolveException ex)
         {
             m_logger.log(
-                Logger.LOG_DEBUG,
+                Logger.LOG_ERROR,
                 "Constraint violation for " + targetModule + " detected.",
                 ex);
             return false;
@@ -789,7 +793,7 @@
         catch (ResolveException ex)
         {
             m_logger.log(
-                Logger.LOG_DEBUG,
+                Logger.LOG_ERROR,
                 "Constraint violation for " + targetModule + " detected.",
                 ex);
             return false;
@@ -824,7 +828,7 @@
         catch (ResolveException ex)
         {
             m_logger.log(
-                Logger.LOG_DEBUG,
+                Logger.LOG_ERROR,
                 "Constraint violation for " + targetModule + " detected.",
                 ex);
             return false;
@@ -875,7 +879,7 @@
                     else
                     {
                         m_logger.log(
-                            Logger.LOG_DEBUG,
+                            Logger.LOG_ERROR,
                             "Constraint violation for " + targetModule
                             + " detected; module can see "
                             + rp + " and " + rpUses);
Index: src/main/java/org/apache/felix/framework/ModuleImpl.java
===================================================================
--- src/main/java/org/apache/felix/framework/ModuleImpl.java	(revision 831350)
+++ src/main/java/org/apache/felix/framework/ModuleImpl.java	(working copy)
@@ -1295,7 +1295,7 @@
 
     public String toString()
     {
-        return m_id;
+        return getSymbolicName() + " (" + m_id + ")";
     }
 
     private synchronized ModuleClassLoader getClassLoader()
@@ -1590,12 +1590,12 @@
 
             try
             {
-                dexFileClassLoadDex = dexFileClass.getMethod("loadDex", 
+                dexFileClassLoadDex = dexFileClass.getMethod("loadDex",
                     new Class[]{String.class, String.class, Integer.TYPE});
             }
             catch (Exception ex)
             {
-                // Nothing we need to do 
+                // Nothing we need to do
             }
             dexFileClassConstructor = dexFileClass.getConstructor(
                 new Class[] { java.io.File.class });
@@ -1717,7 +1717,7 @@
 
                         if (clazz == null)
                         {
-                            int activationPolicy = 
+                            int activationPolicy =
                                 ((BundleImpl) getBundle()).isDeclaredActivationPolicyUsed()
                                 ? ((BundleImpl) getBundle()).getCurrentModule().getDeclaredActivationPolicy()
                                 : IModule.EAGER_ACTIVATION;
@@ -1873,8 +1873,8 @@
                 {
                     if (m_dexFileClassLoadDex != null)
                     {
-                        dexFile = m_dexFileClassLoadDex.invoke(null, 
-                            new Object[]{content.getFile().getAbsolutePath(), 
+                        dexFile = m_dexFileClassLoadDex.invoke(null,
+                            new Object[]{content.getFile().getAbsolutePath(),
                                 content.getFile().getAbsolutePath() + ".dex", new Integer(0)});
                     }
                     else

Classloading problems

Frequently, you'll see build problems with attempting to load a class when starting a configuration. Most of the time, this is caused by a bundle resolution problem that occurs earlier, but the information has been swallowed. If you use the -X option on the build, the resolution error will be given earlier in the build and you can generally figure out what's missing from that.

Occasionally, the resolution error will be a very generic "Constraint violation" without much information on what actually failed. Turning on debug logging in Felix will give some information, but I've found that building Felix with the following patch applied helps diagnose the problem more quickly:

Code Block

Index: src/main/java/org/apache/felix/framework/searchpolicy/Resolver.java
===================================================================
--- src/main/java/org/apache/felix/framework/searchpolicy/Resolver.java	(revision 831350)
+++ src/main/java/org/apache/felix/framework/searchpolicy/Resolver.java	(working copy)
@@ -332,8 +332,12 @@
                     }
                     else
                     {
+                        System.out.println(
+                            "Constraint violation for " + importer
+                            + " detected; module can see "
+                            + rp + " and " + rpUses);
                         m_logger.log(
-                            Logger.LOG_DEBUG,
+                            Logger.LOG_ERROR,
                             "Constraint violation for " + importer
                             + " detected; module can see "
                             + rp + " and " + rpUses);
@@ -705,7 +709,7 @@
         catch (ResolveException ex)
         {
             m_logger.log(
-                Logger.LOG_DEBUG,
+                Logger.LOG_ERROR,
                 "Constraint violation for " + targetModule + " detected.",
                 ex);
             return false;
@@ -789,7 +793,7 @@
         catch (ResolveException ex)
         {
             m_logger.log(
-                Logger.LOG_DEBUG,
+                Logger.LOG_ERROR,
                 "Constraint violation for " + targetModule + " detected.",
                 ex);
             return false;
@@ -824,7 +828,7 @@
         catch (ResolveException ex)
         {
             m_logger.log(
-                Logger.LOG_DEBUG,
+                Logger.LOG_ERROR,
                 "Constraint violation for " + targetModule + " detected.",
                 ex);
             return false;
@@ -875,7 +879,7 @@
                     else
                     {
                         m_logger.log(
-                            Logger.LOG_DEBUG,
+                            Logger.LOG_ERROR,
                             "Constraint violation for " + targetModule
                             + " detected; module can see "
                             + rp + " and " + rpUses);
Index: src/main/java/org/apache/felix/framework/ModuleImpl.java
===================================================================
--- src/main/java/org/apache/felix/framework/ModuleImpl.java	(revision 831350)
+++ src/main/java/org/apache/felix/framework/ModuleImpl.java	(working copy)
@@ -1295,7 +1295,7 @@
 
     public String toString()
     {
-        return m_id;
+        return getSymbolicName() + " (" + m_id + ")";
     }
 
     private synchronized ModuleClassLoader getClassLoader()
@@ -1590,12 +1590,12 @@
 
             try
             {
-                dexFileClassLoadDex = dexFileClass.getMethod("loadDex", 
+                dexFileClassLoadDex = dexFileClass.getMethod("loadDex",
                     new Class[]{String.class, String.class, Integer.TYPE});
             }
             catch (Exception ex)
             {
-                // Nothing we need to do 
+                // Nothing we need to do
             }
             dexFileClassConstructor = dexFileClass.getConstructor(
                 new Class[] { java.io.File.class });
@@ -1717,7 +1717,7 @@
 
                         if (clazz == null)
                         {
-                            int activationPolicy = 
+                            int activationPolicy =
                                 ((BundleImpl) getBundle()).isDeclaredActivationPolicyUsed()
                                 ? ((BundleImpl) getBundle()).getCurrentModule().getDeclaredActivationPolicy()
                                 : IModule.EAGER_ACTIVATION;
@@ -1873,8 +1873,8 @@
                 {
                     if (m_dexFileClassLoadDex != null)
                     {
-                        dexFile = m_dexFileClassLoadDex.invoke(null, 
-                            new Object[]{content.getFile().getAbsolutePath(), 
+                        dexFile = m_dexFileClassLoadDex.invoke(null,
+                            new Object[]{content.getFile().getAbsolutePath(),
                                 content.getFile().getAbsolutePath() + ".dex", new Integer(0)});
                     }
                     else

Logging conversion

Logging conversion

When converting plugins make sure to avoid the following dependencies: log4j, jcl-over-slf4j,
jul-to-slf4j, slf4j-api, or slf4j-log4j12. Instead use pax-logging-api. This When converting plugins make sure to avoid the following dependencies: log4j, jcl-over-slf4j,
jul-to-slf4j, slf4j-api, or slf4j-log4j12. Instead use pax-logging-api. This bundle exports all of these logging API.

...

The pax-logging-api together with pax-logging-service provides a logging service in the OSGi environment that works with all commonly used logging API.

General OSGi tips

  • The Bundle-ClassPath can refer to a directory within the bundle. Make sure there is an entry just for that directory in the JAR file of the bundle. Otherwise, classes might not be loaded from that directory.
  • Always specify some wild mark in the filePattern (the second argument) in the bundle.findEntries() call to ensure the same set of entries is returned on different OSGi frameworks.