Migrate the composite support, iPOJO API and Online manipulator to the new manipulator API (FELIX-4509).

The iPOJO API build a classloader around the given bundle context.
The composite use the class's classloader (should not be used anyway as the generated code is pretty simple)
The online manipulaotr use a bridge loading classes from the original bundle (under deployment) and from the set of deployed bundles

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1592771 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/ipojo/manipulator/online-manipulator/src/main/java/org/apache/felix/ipojo/online/manipulator/BridgeClassLoader.java b/ipojo/manipulator/online-manipulator/src/main/java/org/apache/felix/ipojo/online/manipulator/BridgeClassLoader.java
new file mode 100644
index 0000000..28987a8
--- /dev/null
+++ b/ipojo/manipulator/online-manipulator/src/main/java/org/apache/felix/ipojo/online/manipulator/BridgeClassLoader.java
@@ -0,0 +1,65 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.ipojo.online.manipulator;
+
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+
+import java.io.File;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLClassLoader;
+
+/**
+ * A classloader trying to load classes from a given jar files and then from bundles.
+ * This classloader must only be used for the iPOJO manipulator (in order to compute bytecode frames).
+ */
+public class BridgeClassLoader extends ClassLoader {
+
+    private final URLClassLoader m_loader;
+    private final BundleContext m_context;
+
+    public BridgeClassLoader(File original, BundleContext context) throws MalformedURLException {
+        m_loader = new URLClassLoader(new URL[]{original.toURI().toURL()}, null);
+        m_context = context;
+    }
+
+    @Override
+    public Class<?> loadClass(String name) throws ClassNotFoundException {
+        // Try to load it using the url classloader
+        try {
+            return m_loader.loadClass(name);
+        } catch (ClassNotFoundException e) {
+            // Not there, try somewhere else.
+        }
+
+        for (Bundle bundle : m_context.getBundles()) {
+            if (bundle.getState() >= Bundle.RESOLVED) {
+                try {
+                    return bundle.loadClass(name);
+                } catch (ClassNotFoundException e) {
+                    // Try next one.
+                }
+            }
+        }
+
+        // Still nothing, delegate to parent
+        return super.loadClass(name);
+    }
+}
diff --git a/ipojo/manipulator/online-manipulator/src/main/java/org/apache/felix/ipojo/online/manipulator/IPOJOURLHandler.java b/ipojo/manipulator/online-manipulator/src/main/java/org/apache/felix/ipojo/online/manipulator/IPOJOURLHandler.java
index 403581c..dc0e838 100644
--- a/ipojo/manipulator/online-manipulator/src/main/java/org/apache/felix/ipojo/online/manipulator/IPOJOURLHandler.java
+++ b/ipojo/manipulator/online-manipulator/src/main/java/org/apache/felix/ipojo/online/manipulator/IPOJOURLHandler.java
@@ -212,10 +212,11 @@
             composite.addMetadataProvider(provider);
         }
 
+        ClassLoader classloader = new BridgeClassLoader(original, m_context);
         // Pojoization
         Pojoization pojoizator = new Pojoization(createModuleProvider());
         try {
-            pojoizator.pojoization(store, composite, createVisitor(store, reporter));
+            pojoizator.pojoization(store, composite, createVisitor(store, reporter), classloader);
         } catch (Exception e) {
             if (!pojoizator.getErrors().isEmpty()) {
                 throw new IOException("Errors occurred during the manipulation : " + pojoizator.getErrors(), e);
diff --git a/ipojo/runtime/api/src/main/java/org/apache/felix/ipojo/api/PrimitiveComponentType.java b/ipojo/runtime/api/src/main/java/org/apache/felix/ipojo/api/PrimitiveComponentType.java
index 00bb278..53a0931 100644
--- a/ipojo/runtime/api/src/main/java/org/apache/felix/ipojo/api/PrimitiveComponentType.java
+++ b/ipojo/runtime/api/src/main/java/org/apache/felix/ipojo/api/PrimitiveComponentType.java
@@ -448,7 +448,16 @@
      * @return the manipulated class
      */
     private byte[] manipulate() {
-        Manipulator manipulator = new Manipulator();
+        Manipulator manipulator = new Manipulator(new ClassLoader() {
+            @Override
+            public Class<?> loadClass(String name) throws ClassNotFoundException {
+                try {
+                    return m_context.getBundle().loadClass(name);
+                } catch (ClassNotFoundException e) {
+                    return this.getClass().getClassLoader().loadClass(name);
+                }
+            }
+        });
         try {
             byte[] array = getClassByteArray();
 
diff --git a/ipojo/runtime/composite/src/main/java/org/apache/felix/ipojo/composite/service/provides/CompositionMetadata.java b/ipojo/runtime/composite/src/main/java/org/apache/felix/ipojo/composite/service/provides/CompositionMetadata.java
index e5728bb..39c497d 100644
--- a/ipojo/runtime/composite/src/main/java/org/apache/felix/ipojo/composite/service/provides/CompositionMetadata.java
+++ b/ipojo/runtime/composite/src/main/java/org/apache/felix/ipojo/composite/service/provides/CompositionMetadata.java
@@ -234,7 +234,7 @@
             return null;

         }

         byte[] pojo = POJOWriter.dump(clazz, m_name, getFieldList(), getMethodList(), m_handler);

-        Manipulator manipulator = new Manipulator();

+        Manipulator manipulator = new Manipulator(this.getClass().getClassLoader());

         try {

             manipulator.prepare(pojo);

             byte[] newclazz = manipulator.manipulate(pojo);