FELIX-1553: fileinstall bundle should have an optional import on org.osgi.service.log instead of exporting it

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@810936 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/fileinstall/pom.xml b/fileinstall/pom.xml
index eb949db..871b3fc 100644
--- a/fileinstall/pom.xml
+++ b/fileinstall/pom.xml
@@ -48,14 +48,22 @@
       <plugin>
         <groupId>org.apache.felix</groupId>
         <artifactId>maven-bundle-plugin</artifactId>
-        <version>1.4.3</version>
+        <version>2.0.0</version>
         <extensions>true</extensions>
         <configuration>
           <instructions>
-            <Export-Package>org.apache.felix.fileinstall.*,org.osgi.service.cm, org.osgi.service.log</Export-Package>
+            <Export-Package>
+                org.apache.felix.fileinstall*,
+                org.osgi.service.cm
+            </Export-Package>
+            <Import-Package>
+                org.osgi.service.log;resolution:=optional,
+                *
+            </Import-Package>
             <Bundle-Activator>org.apache.felix.fileinstall.FileInstall</Bundle-Activator>
             <Bundle-SymbolicName>${pom.artifactId}</Bundle-SymbolicName>
             <Bundle-Vendor>The Apache Software Foundation</Bundle-Vendor>
+            <_versionpolicy>[$(version;==;$(@)),$(version;+;$(@)))</_versionpolicy>
           </instructions>
         </configuration>
       </plugin>
diff --git a/fileinstall/src/main/java/org/apache/felix/fileinstall/util/Util.java b/fileinstall/src/main/java/org/apache/felix/fileinstall/util/Util.java
index 1e25b60..ecc6508 100644
--- a/fileinstall/src/main/java/org/apache/felix/fileinstall/util/Util.java
+++ b/fileinstall/src/main/java/org/apache/felix/fileinstall/util/Util.java
@@ -175,48 +175,92 @@
      */
     public static void log(BundleContext context, long debug, String message, Throwable e)
     {
-        LogService log = getLogService(context);
-        if (log == null)
+        getLogger(context).log(debug > 0, message, e);
+    }
+
+    private static Logger getLogger(BundleContext context)
+    {
+        if (logger != null)
         {
-            System.out.println(message + (e == null ? "" : ": " + e));
-            if (debug > 0 && e != null)
-            {
-                e.printStackTrace(System.out);
-            }
+            return logger;
         }
-        else
+        try
         {
-            if (e != null)
+            logger = new OsgiLogger(context);
+        }
+        catch (Throwable t)
+        {
+            logger = new StdOutLogger();
+        }
+        return logger;
+    }
+
+    private static Logger logger;
+
+    interface Logger
+    {
+        void log(boolean debug, java.lang.String message, java.lang.Throwable throwable);
+    }
+
+    static class StdOutLogger implements Logger
+    {
+        public void log(boolean debug, String message, Throwable throwable)
+        {
+            System.out.println(message + (throwable == null ? "" : ": " + throwable));
+            if (debug && throwable != null)
             {
-                log.log(LogService.LOG_ERROR, message, e);
-                if (debug > 0 && e != null)
-                {
-                    e.printStackTrace();
-                }
-            }
-            else
-            {
-                log.log(LogService.LOG_INFO, message);
+                throwable.printStackTrace(System.out);
             }
         }
     }
 
-    /**
-     * Answer the Log Service
-     *
-     * @return
-     */
-    private static LogService getLogService(BundleContext context)
+    static class OsgiLogger extends StdOutLogger
     {
-        ServiceReference ref = context.getServiceReference(LogService.class.getName());
-        if (ref != null)
+
+        private BundleContext context;
+
+        OsgiLogger(BundleContext context)
         {
-            LogService log = (LogService) context.getService(ref);
-            return log;
+            this.context = context;
         }
-        return null;
+
+        public void log(boolean debug, String message, Throwable throwable)
+        {
+            LogService log = getLogService();
+            if (log != null)
+            {
+                if (throwable != null)
+                {
+                    log.log(LogService.LOG_ERROR, message, throwable);
+                    if (debug)
+                    {
+                        throwable.printStackTrace();
+                    }
+                }
+                else
+                {
+                    log.log(LogService.LOG_INFO, message);
+                }
+            }
+            else
+            {
+                super.log(debug, message, throwable);
+            }
+        }
+
+        private LogService getLogService()
+        {
+            ServiceReference ref = context.getServiceReference(LogService.class.getName());
+            if (ref != null)
+            {
+                LogService log = (LogService) context.getService(ref);
+                return log;
+            }
+            return null;
+        }
     }
 
+
     /**
      * Jar up a directory
      *