Remove dependency on Java 1.4 inside the iPOJO Runtime.
Improve factory import inside composite to point directly on the global context.

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@555072 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/ipojo/core/src/main/java/org/apache/felix/ipojo/IPojoContext.java b/ipojo/core/src/main/java/org/apache/felix/ipojo/IPojoContext.java
index fca2832..e789f14 100644
--- a/ipojo/core/src/main/java/org/apache/felix/ipojo/IPojoContext.java
+++ b/ipojo/core/src/main/java/org/apache/felix/ipojo/IPojoContext.java
@@ -301,5 +301,13 @@
     public boolean ungetService(ServiceReference reference) {
         return m_serviceContext.ungetService(reference);
     }
+    
+    /**
+     * Get the global context, i.e. the bundle context of the factory.
+     * @return the global bundle context.
+     */
+    public BundleContext getGlobalContext() {
+        return m_bundleContext;
+    }
 
 }
diff --git a/ipojo/core/src/main/java/org/apache/felix/ipojo/composite/CompositeServiceContext.java b/ipojo/core/src/main/java/org/apache/felix/ipojo/composite/CompositeServiceContext.java
index 26304e7..3ea9a4c 100644
--- a/ipojo/core/src/main/java/org/apache/felix/ipojo/composite/CompositeServiceContext.java
+++ b/ipojo/core/src/main/java/org/apache/felix/ipojo/composite/CompositeServiceContext.java
@@ -25,6 +25,7 @@
 
 import org.apache.felix.ipojo.ComponentInstance;
 import org.apache.felix.ipojo.Factory;
+import org.apache.felix.ipojo.IPojoContext;
 import org.apache.felix.ipojo.ServiceContext;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.Constants;
@@ -79,6 +80,11 @@
      * Component Instance who creates this registry.
      */
     private ComponentInstance m_instance;
+    
+    /**
+     * Global service context.
+     */
+    private BundleContext m_global;
 
     /**
      * Constructor. This constructor instantiate a service registry with the
@@ -89,6 +95,11 @@
     public CompositeServiceContext(BundleContext bc) {
         m_registry = new ServiceRegistry(bc);
         m_parent = bc;
+        if (m_parent instanceof IPojoContext) {
+            m_global = ((IPojoContext) m_parent).getGlobalContext();
+        } else {
+            m_global = m_parent; // the parent context is the global context
+        }
     }
 
     /**
@@ -99,7 +110,6 @@
      */
     public CompositeServiceContext(BundleContext bc, ComponentInstance ci) {
         this(bc);
-        m_parent = bc;
         m_instance = ci;
     }
 
@@ -219,7 +229,7 @@
      */
     private void importFactories() {
         try {
-            ServiceReference[] refs = m_parent.getServiceReferences(Factory.class.getName(), null);
+            ServiceReference[] refs = m_global.getServiceReferences(Factory.class.getName(), null);
             if (refs != null) {
                 for (int i = 0; i < refs.length; i++) {
                     importFactory(refs[i]);
@@ -242,7 +252,7 @@
         for (int j = 0; j < ref.getPropertyKeys().length; j++) {
             dict.put(ref.getPropertyKeys()[j], ref.getProperty(ref.getPropertyKeys()[j]));
         }
-        rec.m_fact = new FactoryProxy((Factory) m_parent.getService(ref), this);
+        rec.m_fact = new FactoryProxy((Factory) m_global.getService(ref), this);
         rec.m_reg = registerService(Factory.class.getName(), rec.m_fact, dict);
         rec.m_ref = ref;
     }
@@ -258,7 +268,7 @@
             if (rec.m_ref == ref) {
                 rec.m_reg.unregister();
                 rec.m_fact = null;
-                m_parent.ungetService(rec.m_ref);
+                m_global.ungetService(rec.m_ref);
                 m_factories.remove(rec);
                 return;
             }
@@ -268,13 +278,12 @@
     /**
      * Start the registry management.
      */
-    public void start() {
+    public synchronized void start() {
         importFactories();
         try {
-            m_parent.addServiceListener(this, "(" + Constants.OBJECTCLASS + "=" + Factory.class.getName() + ")");
+            m_global.addServiceListener(this, "(" + Constants.OBJECTCLASS + "=" + Factory.class.getName() + ")");
         } catch (InvalidSyntaxException e) {
-            // Should not happen
-            e.printStackTrace();
+            e.printStackTrace(); // Should not happen
         }
     }
 
@@ -282,7 +291,7 @@
      * Stop the registry management.
      */
     public synchronized void stop() {
-        m_parent.removeServiceListener(this);
+        m_global.removeServiceListener(this);
         m_registry.reset();
         for (int i = 0; i < m_factories.size(); i++) {
             Record rec = (Record) m_factories.get(i);
diff --git a/ipojo/core/src/main/java/org/apache/felix/ipojo/handlers/lifecycle/callback/LifecycleCallbackHandler.java b/ipojo/core/src/main/java/org/apache/felix/ipojo/handlers/lifecycle/callback/LifecycleCallbackHandler.java
index 4add20e..d4b431d 100644
--- a/ipojo/core/src/main/java/org/apache/felix/ipojo/handlers/lifecycle/callback/LifecycleCallbackHandler.java
+++ b/ipojo/core/src/main/java/org/apache/felix/ipojo/handlers/lifecycle/callback/LifecycleCallbackHandler.java
@@ -200,7 +200,7 @@
                     m_manager.getFactory().getLogger().log(
                             Logger.ERROR,
                             "[" + m_manager.getClassName() + "] The callback method " + m_callbacks[i].getMethod() + " has throws an exception : "
-                                    + e.getMessage() + " -> " + e.getCause());
+                                    + e.getMessage());
                 }
             }
         }
diff --git a/ipojo/core/src/main/java/org/apache/felix/ipojo/parser/ParseUtils.java b/ipojo/core/src/main/java/org/apache/felix/ipojo/parser/ParseUtils.java
index 7da9a9e..f1ec6e7 100644
--- a/ipojo/core/src/main/java/org/apache/felix/ipojo/parser/ParseUtils.java
+++ b/ipojo/core/src/main/java/org/apache/felix/ipojo/parser/ParseUtils.java
@@ -18,6 +18,8 @@
  */
 package org.apache.felix.ipojo.parser;
 
+import java.util.StringTokenizer;
+
 /**
  * Parse Utils Methods.
  * 
@@ -40,7 +42,7 @@
             if (m.length() == 0) {
                 return new String[0];
             }
-            String[] values = m.split(",");
+            String[] values = split(m, ",");
             for (int i = 0; i < values.length; i++) {
                 values[i] = values[i].trim();
             }
@@ -49,5 +51,23 @@
             return new String[] { str };
         }
     }
+    
+    /**
+     * Split method. 
+     * This method is equivalent of the String.split in java 1.4
+     * @param toSplit : String to split
+     * @param separator : separator
+     * @return the token array 
+     */
+    public static String[] split(String toSplit, String separator) {
+        StringTokenizer st = new StringTokenizer(toSplit, separator);
+        String[] result = new String[st.countTokens()];
+        int i = 0;
+        while (st.hasMoreElements()) {
+            result[i] = st.nextToken();
+            i++;
+        }
+        return result;
+    }
 
 }
diff --git a/ipojo/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/FieldAdapter.java b/ipojo/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/FieldAdapter.java
index 4eb313c..3dc3cc2 100644
--- a/ipojo/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/FieldAdapter.java
+++ b/ipojo/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/FieldAdapter.java
@@ -18,8 +18,6 @@
  */

 package org.apache.felix.ipojo.manipulation;

 

-import java.util.logging.Level;

-

 import org.objectweb.asm.ClassAdapter;

 import org.objectweb.asm.ClassVisitor;

 import org.objectweb.asm.FieldVisitor;

@@ -524,7 +522,7 @@
                 break;

 

             default:

-                ManipulationProperty.getLogger().log(Level.SEVERE, "Manipulation problem in " + m_owner + " : a type is not implemented : " + type);

+                ManipulationProperty.getLogger().log(ManipulationProperty.SEVERE, "Manipulation problem in " + m_owner + " : a type is not implemented : " + type);

                 break;

         }

 

@@ -646,7 +644,7 @@
                 mv.visitInsn(RETURN);

                 break;

             default:

-                ManipulationProperty.getLogger().log(Level.SEVERE, "Manipulation Error : Cannot create the setter method for the field : " + name + " (" + type + ")");

+                ManipulationProperty.getLogger().log(ManipulationProperty.SEVERE, "Manipulation Error : Cannot create the setter method for the field : " + name + " (" + type + ")");

                 break;

         }

 

diff --git a/ipojo/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/ManipulationProperty.java b/ipojo/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/ManipulationProperty.java
index d8b6c4b..c0af374 100644
--- a/ipojo/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/ManipulationProperty.java
+++ b/ipojo/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/ManipulationProperty.java
@@ -18,10 +18,6 @@
  */

 package org.apache.felix.ipojo.manipulation;

 

-import java.util.logging.Level;

-import java.util.logging.Logger;

-

-

 /**

  * Store properties for the manipulation process.

  * 

@@ -29,6 +25,47 @@
  * 

  */

 public class ManipulationProperty {

+    

+    /**

+     * Logger info level.

+     */

+    public static final int INFO = 0;

+    

+    /**

+     * Logger warning level. 

+     */

+    public static final int WARNING = 1;

+    

+    /**

+     * Logger severe level. 

+     */

+    public static final int SEVERE = 2;

+    

+    /**

+     * Internal logger implementation.

+     */

+    protected static class Logger {

+        /**

+         * Log method.

+         * @param level : level

+         * @param message : message to log

+         */

+        public void log(int level, String message) {

+            if (level >= m_logLevel) {

+                switch (level) {

+                    case INFO:

+                        System.err.println("[INFO] " + message);

+                        break;

+                    case WARNING:

+                        System.err.println("[WARNING] " + message);

+                        break;

+                    case SEVERE:

+                        System.err.println("[SEVERE] " + message);

+                        break;

+                }

+            }

+        }

+    }

 

     /**

      * Manipulator logger.

@@ -38,7 +75,7 @@
     /**

      * Default logger level.

      */

-    private static Level m_logLevel = Level.WARNING;

+    private static int m_logLevel = WARNING;

 

     /**

      * Get the manipulator logger.

@@ -46,9 +83,7 @@
      */

     public static Logger getLogger() {

         if (m_logger == null) {

-            String name = "org.apache.felix.ipojo.manipulator";

-            m_logger = Logger.getLogger(name);

-            m_logger.setLevel(m_logLevel);

+            m_logger = new Logger();

         }

         return m_logger;

     }

diff --git a/ipojo/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/Manipulator.java b/ipojo/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/Manipulator.java
index 3d7bbab..ca51a7d 100644
--- a/ipojo/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/Manipulator.java
+++ b/ipojo/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/Manipulator.java
@@ -28,7 +28,6 @@
 import java.util.HashMap;

 import java.util.Iterator;

 import java.util.List;

-import java.util.logging.Level;

 

 import org.apache.felix.ipojo.metadata.Attribute;

 import org.apache.felix.ipojo.metadata.Element;

@@ -79,10 +78,10 @@
             return false;

         }

 

-        URL url = clazz.toURI().toURL();

+        URL url = clazz.toURL();

 

         // if (url == null) { throw new ClassNotFoundException(name); }

-        ManipulationProperty.getLogger().log(Level.INFO, "Manipulate the class file : " + clazz.getAbsolutePath());

+        ManipulationProperty.getLogger().log(ManipulationProperty.INFO, "Manipulate the class file : " + clazz.getAbsolutePath());

 

         InputStream is1 = url.openStream();

 

@@ -138,7 +137,7 @@
                 fos.write(cw0.toByteArray());

 

                 fos.close();

-                ManipulationProperty.getLogger().log(Level.INFO, "Put the file " + clazz.getAbsolutePath() + " in the jar file");

+                ManipulationProperty.getLogger().log(ManipulationProperty.INFO, "Put the file " + clazz.getAbsolutePath() + " in the jar file");

             } catch (Exception e) {

                 System.err.println("Problem to write the adapted class on the file system " + " [ " + clazz.getAbsolutePath() + " ] " + e.getMessage());

             }

diff --git a/ipojo/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/MethodCodeAdapter.java b/ipojo/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/MethodCodeAdapter.java
index be9288d..4108af5 100644
--- a/ipojo/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/MethodCodeAdapter.java
+++ b/ipojo/manipulator/src/main/java/org/apache/felix/ipojo/manipulation/MethodCodeAdapter.java
@@ -18,8 +18,6 @@
  */

 package org.apache.felix.ipojo.manipulation;

 

-import java.util.logging.Level;

-

 import org.objectweb.asm.Label;

 import org.objectweb.asm.MethodVisitor;

 import org.objectweb.asm.Opcodes;

@@ -68,12 +66,10 @@
     public void visitFieldInsn(final int opcode, final String owner, final String name, final String desc) {

         if (owner.equals(m_owner)) {

             if (opcode == GETFIELD) {

-                ManipulationProperty.getLogger().log(Level.INFO, "Manipulate a GETFIELD on : " + name);

                 String gDesc = "()" + desc;

                 visitMethodInsn(INVOKEVIRTUAL, owner, "_get" + name, gDesc);

                 return;

             } else if (opcode == PUTFIELD) {

-                ManipulationProperty.getLogger().log(Level.INFO, "Manipulate a PUTFIELD on : " + name);

                 String sDesc = "(" + desc + ")V";

                 visitMethodInsn(INVOKESPECIAL, owner, "_set" + name, sDesc);

                 return;

diff --git a/ipojo/manipulator/src/main/java/org/apache/felix/ipojo/manipulator/Pojoization.java b/ipojo/manipulator/src/main/java/org/apache/felix/ipojo/manipulator/Pojoization.java
index d38998a..a56f198 100644
--- a/ipojo/manipulator/src/main/java/org/apache/felix/ipojo/manipulator/Pojoization.java
+++ b/ipojo/manipulator/src/main/java/org/apache/felix/ipojo/manipulator/Pojoization.java
@@ -29,7 +29,6 @@
 import java.util.Enumeration;

 import java.util.HashMap;

 import java.util.Iterator;

-import java.util.LinkedHashMap;

 import java.util.List;

 import java.util.Map;

 import java.util.TreeMap;

@@ -211,7 +210,7 @@
                 }

             }

         } catch (IOException e) {

-            error("Cannot manipulate the Jar file : " + e.getMessage() + " - Cause : " + e.getCause());

+            error("Cannot manipulate the Jar file : " + e.getMessage());

             return;

         }

 

@@ -222,7 +221,7 @@
             jos = null;

             fos = null;

         } catch (IOException e) {

-            error("Cannot close the new Jar file : " + e.getMessage() + " - Cause : " + e.getCause());

+            error("Cannot close the new Jar file : " + e.getMessage());

             return;

         }

     }

@@ -443,7 +442,7 @@
             return new HashMap();

         }

 

-        Map result = new LinkedHashMap();

+        Map result = new HashMap();

         QuotedTokenizer qt = new QuotedTokenizer(value, ";=,");

         char del;

         do {

@@ -525,7 +524,7 @@
         URL url;

         Element[] meta = null;

         try {

-            url = metadata.toURI().toURL();

+            url = metadata.toURL();

             if (url == null) {

                 error("Cannot find the metdata file : " + path);

                 return null;