/*
 * Copyright (c) 2010-2012 Célio Cidral Junior.
 *
 *     Licensed 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.tomighty.plugin.impl;

import com.google.inject.Injector;
import org.tomighty.plugin.Plugin;
import org.tomighty.plugin.PluginLoader;
import org.tomighty.plugin.PluginPack;

import javax.inject.Inject;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLClassLoader;
import java.util.Properties;

public class DefaultPluginLoader implements PluginLoader {

    @Inject
    private Injector injector;

    @Override
    public Plugin load(PluginPack pluginPack) {
        URLClassLoader classLoader = new URLClassLoader(pluginPack.jars());
        Class<? extends Plugin> pluginClass = loadPluginClass(classLoader);
        return injector.getInstance(pluginClass);
    }

    private Class<? extends Plugin> loadPluginClass(ClassLoader classLoader) {
        String pluginClassName = getPluginClassName(classLoader);
        try {
            return (Class<? extends Plugin>) classLoader.loadClass(pluginClassName);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    private String getPluginClassName(ClassLoader classLoader) {
        InputStream inputStream = classLoader.getResourceAsStream("tomighty-plugin.properties");
        Properties properties = loadProperties(inputStream);
        return properties.getProperty("class");
    }

    private Properties loadProperties(InputStream inputStream) {
        if(inputStream == null)
            throw new IllegalArgumentException("Input stream must not be null");

        Properties properties = new Properties();
        try {
            properties.load(inputStream);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return properties;
    }

}

@@ -728,24 +728,83 @@
 .Injector;%0D%0A
+import org.slf4j.Logger;%0D%0Aimport org.slf4j.LoggerFactory;%0D%0A
 import org.t
@@ -998,16 +998,38 @@
 tream;%0D%0A
+import java.net.URL;%0D%0A
 import j
@@ -1190,24 +1190,98 @@
 njector;%0D%0A%0D%0A
+    private final Logger logger = LoggerFactory.getLogger(getClass());%0D%0A%0D%0A
     @Overrid
@@ -1328,24 +1328,79 @@
 ginPack) %7B%0D%0A
+        logger.info(%22Loading plugin %7B%7D%22, pluginPack);%0D%0A
         URLC
@@ -1428,15 +1428,14 @@
 r = 
-new URL
+create
 Clas
@@ -1456,15 +1456,8 @@
 Pack
-.jars()
 );%0D%0A
@@ -1585,32 +1585,273 @@
 ass);%0D%0A    %7D%0D%0A%0D%0A
+    private URLClassLoader createClassLoader(PluginPack pluginPack) %7B%0D%0A        URL%5B%5D jars = pluginPack.jars();%0D%0A        for(URL jar : jars)%0D%0A            logger.info(%22Loading jar %7B%7D%22, jar);%0D%0A        return new URLClassLoader(jars);%0D%0A    %7D%0D%0A%0D%0A
     private Clas
@@ -1973,32 +1973,98 @@
 (classLoader);%0D%0A
+        logger.info(%22Loading plugin class %7B%7D%22, pluginClassName);%0D%0A
         try %7B%0D%0A 

Write some log messages during plugin load time.