/*
 * Copyright 2008-2011 Red Hat, Inc, and individual contributors.
 * 
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 * 
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.torquebox.bootstrap.as;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.jar.JarFile;

import org.jboss.as.controller.Extension;
import org.jboss.as.controller.ExtensionContext;
import org.jboss.as.controller.SubsystemRegistration;
import org.jboss.as.controller.parsing.ExtensionParsingContext;
import org.jboss.as.controller.registry.ManagementResourceRegistration;
import org.jboss.logging.Logger;
import org.jboss.modules.Module;
import org.jboss.modules.ModuleClassLoader;
import org.jboss.modules.ModuleLoader;
import org.jboss.modules.ResourceLoader;
import org.jboss.modules.ResourceLoaderSpec;
import org.jboss.modules.ResourceLoaders;
import org.torquebox.bootstrap.JRubyHomeLocator;

public class BootstrapExtension implements Extension {
    
    @Override
    public void initialize(ExtensionContext context) {
        /*
        log.info( "Bootstrapping TorqueBox" );
        final SubsystemRegistration registration = context.registerSubsystem( SUBSYSTEM_NAME );
        final ManagementResourceRegistration subsystem = registration.registerSubsystemModel( BootstrapSubsystemProviders.SUBSYSTEM );
        registration.registerXMLElementWriter( BootstrapSubsystemParser.getInstance() );

        String jrubyHome = JRubyHomeLocator.determineJRubyHome(true);

        if (jrubyHome == null) {
            log.fatal( "Unable to find a JRuby Home" );
            return;
        }
        
        log.info( "===> " + jrubyHome );
        
        System.setProperty( "jruby.home", jrubyHome );

        File libDir = new File( jrubyHome, "lib" );

        List<ResourceLoaderSpec> loaderSpecs = new ArrayList<ResourceLoaderSpec>();

        for (File child : libDir.listFiles()) {
            if (child.getName().endsWith( ".jar" )) {
                log.info( "Adding: " + child );
                try {
                    ResourceLoader loader = ResourceLoaders.createJarResourceLoader( child.getName(), new JarFile( child ) );
                    ResourceLoaderSpec loaderSpec = ResourceLoaderSpec.createResourceLoaderSpec( loader );
                    loaderSpecs.add( loaderSpec );
                } catch (IOException e) {
                    log.error( e );
                }
            }
        }

        swizzleResourceLoaders( loaderSpecs );
        */
    }

    /*
    private ResourceLoader[] getExistingResourceLoaders() throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException,
            InvocationTargetException {
        Module module = Module.forClass( BootstrapExtension.class );
        ModuleClassLoader cl = module.getClassLoader();

        Method method = ModuleClassLoader.class.getDeclaredMethod( "getResourceLoaders" );
        method.setAccessible( true );
        Object result = method.invoke( cl );

        return (ResourceLoader[]) result;

    }

    private void swizzleResourceLoaders(List<ResourceLoaderSpec> loaderSpecs) {

        Module module = Module.forClass( BootstrapExtension.class );
        ModuleLoader moduleLoader = module.getModuleLoader();

        try {
            for (ResourceLoader each : getExistingResourceLoaders()) {
                loaderSpecs.add( ResourceLoaderSpec.createResourceLoaderSpec( each ) );
            }
            
            Method method = ModuleLoader.class.getDeclaredMethod( "setAndRefreshResourceLoaders", Module.class, Collection.class );
            method.setAccessible( true );
            log.info( "Swizzle: " + loaderSpecs );
            method.invoke( moduleLoader, module, loaderSpecs );

            Method refreshMethod = ModuleLoader.class.getDeclaredMethod( "refreshResourceLoaders", Module.class );
            refreshMethod.setAccessible( true );
            refreshMethod.invoke( moduleLoader, module );

            Method relinkMethod = ModuleLoader.class.getDeclaredMethod( "relink", Module.class );
            relinkMethod.setAccessible( true );
            relinkMethod.invoke( moduleLoader, module );
        } catch (SecurityException e) {
            log.fatal( e.getMessage(), e );
        } catch (NoSuchMethodException e) {
            log.fatal( e.getMessage(), e );
        } catch (IllegalArgumentException e) {
            log.fatal( e.getMessage(), e );
        } catch (IllegalAccessException e) {
            log.fatal( e.getMessage(), e );
        } catch (InvocationTargetException e) {
            log.fatal( e.getMessage(), e );
        }

    }
    */
    
    @Override
    public void initializeParsers(ExtensionParsingContext context) {
        context.setSubsystemXmlMapping( Namespace.CURRENT.getUriString(), BootstrapSubsystemParser.getInstance() );
    }

    public static final String SUBSYSTEM_NAME = "torquebox-bootstrap";
    private static final Logger log = Logger.getLogger( "org.torquebox.bootstrap" );

}

@@ -887,253 +887,8 @@
 s;%0A%0A
-import java.io.File;%0Aimport java.io.IOException;%0Aimport java.lang.reflect.InvocationTargetException;%0Aimport java.lang.reflect.Method;%0Aimport java.util.ArrayList;%0Aimport java.util.Collection;%0Aimport java.util.List;%0Aimport java.util.jar.JarFile;%0A%0A
 impo
@@ -1200,301 +1200,8 @@
 ger;
-%0Aimport org.jboss.modules.Module;%0Aimport org.jboss.modules.ModuleClassLoader;%0Aimport org.jboss.modules.ModuleLoader;%0Aimport org.jboss.modules.ResourceLoader;%0Aimport org.jboss.modules.ResourceLoaderSpec;%0Aimport org.jboss.modules.ResourceLoaders;%0Aimport org.torquebox.bootstrap.JRubyHomeLocator;
 %0A%0Apu
@@ -1331,19 +1331,8 @@
 ) %7B%0A
-        /*%0A
     
@@ -1698,3245 +1698,14 @@
  );%0A
-%0A        String jrubyHome = JRubyHomeLocator.determineJRubyHome(true);%0A%0A        if (jrubyHome == null) %7B%0A            log.fatal( %22Unable to find a JRuby Home%22 );%0A            return;%0A        %7D%0A        %0A        log.info( %22===%3E %22 + jrubyHome );%0A        %0A        System.setProperty( %22jruby.home%22, jrubyHome );%0A%0A        File libDir = new File( jrubyHome, %22lib%22 );%0A%0A        List%3CResourceLoaderSpec%3E loaderSpecs = new ArrayList%3CResourceLoaderSpec%3E();%0A%0A        for (File child : libDir.listFiles()) %7B%0A            if (child.getName().endsWith( %22.jar%22 )) %7B%0A                log.info( %22Adding: %22 + child );%0A                try %7B%0A                    ResourceLoader loader = ResourceLoaders.createJarResourceLoader( child.getName(), new JarFile( child ) );%0A                    ResourceLoaderSpec loaderSpec = ResourceLoaderSpec.createResourceLoaderSpec( loader );%0A                    loaderSpecs.add( loaderSpec );%0A                %7D catch (IOException e) %7B%0A                    log.error( e );%0A                %7D%0A            %7D%0A        %7D%0A%0A        swizzleResourceLoaders( loaderSpecs );%0A        */%0A    %7D%0A%0A    /*%0A    private ResourceLoader%5B%5D getExistingResourceLoaders() throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException,%0A            InvocationTargetException %7B%0A        Module module = Module.forClass( BootstrapExtension.class );%0A        ModuleClassLoader cl = module.getClassLoader();%0A%0A        Method method = ModuleClassLoader.class.getDeclaredMethod( %22getResourceLoaders%22 );%0A        method.setAccessible( true );%0A        Object result = method.invoke( cl );%0A%0A        return (ResourceLoader%5B%5D) result;%0A%0A    %7D%0A%0A    private void swizzleResourceLoaders(List%3CResourceLoaderSpec%3E loaderSpecs) %7B%0A%0A        Module module = Module.forClass( BootstrapExtension.class );%0A        ModuleLoader moduleLoader = module.getModuleLoader();%0A%0A        try %7B%0A            for (ResourceLoader each : getExistingResourceLoaders()) %7B%0A                loaderSpecs.add( ResourceLoaderSpec.createResourceLoaderSpec( each ) );%0A            %7D%0A            %0A            Method method = ModuleLoader.class.getDeclaredMethod( %22setAndRefreshResourceLoaders%22, Module.class, Collection.class );%0A            method.setAccessible( true );%0A            log.info( %22Swizzle: %22 + loaderSpecs );%0A            method.invoke( moduleLoader, module, loaderSpecs );%0A%0A            Method refreshMethod = ModuleLoader.class.getDeclaredMethod( %22refreshResourceLoaders%22, Module.class );%0A            refreshMethod.setAccessible( true );%0A            refreshMethod.invoke( moduleLoader, module );%0A%0A            Method relinkMethod = ModuleLoader.class.getDeclaredMethod( %22relink%22, Module.class );%0A            relinkMethod.setAccessible( true );%0A            relinkMethod.invoke( moduleLoader, module );%0A        %7D catch (SecurityException e) %7B%0A            log.fatal( e.getMessage(), e );%0A        %7D catch (NoSuchMethodException e) %7B%0A            log.fatal( e.getMessage(), e );%0A        %7D catch (IllegalArgumentException e) %7B%0A            log.fatal( e.getMessage(), e );%0A        %7D catch (IllegalAccessException e) %7B%0A            log.fatal( e.getMessage(), e );%0A        %7D catch (InvocationTargetException e) %7B%0A            log.fatal( e.getMessage(), e );%0A        %7D%0A%0A    %7D%0A    */
+    %7D%0A
 %0A   

Clean up commented-out code.