<commit_before>/*
 * Copyright 2008-2012 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.immutant.core.processors;

import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.immutant.core.ApplicationBootstrapUtils;
import org.immutant.core.ClojureMetaData;
import org.immutant.core.Immutant;
import org.immutant.core.as.CoreServices;
import org.jboss.as.server.deployment.Attachments;
import org.jboss.as.server.deployment.DeploymentPhaseContext;
import org.jboss.as.server.deployment.DeploymentUnit;
import org.jboss.as.server.deployment.DeploymentUnitProcessingException;
import org.jboss.as.server.deployment.DeploymentUnitProcessor;
import org.jboss.as.server.deployment.module.ModuleRootMarker;
import org.jboss.as.server.deployment.module.MountHandle;
import org.jboss.as.server.deployment.module.ResourceRoot;
import org.jboss.as.server.deployment.module.TempFileProviderService;
import org.jboss.logging.Logger;
import org.jboss.vfs.VFS;
import org.jboss.vfs.VirtualFile;
import org.projectodd.polyglot.core.as.ArchivedDeploymentMarker;

public class AppJarScanningProcessor implements DeploymentUnitProcessor {

    @Override
    public void deploy(DeploymentPhaseContext phaseContext) throws DeploymentUnitProcessingException {
        DeploymentUnit unit = phaseContext.getDeploymentUnit();

        ClojureMetaData metaData = unit.getAttachment( ClojureMetaData.ATTACHMENT_KEY );
        if (metaData == null && 
                !ArchivedDeploymentMarker.isMarked( unit )) {
            return;
        }

        ResourceRoot resourceRoot = unit.getAttachment( Attachments.DEPLOYMENT_ROOT );
        VirtualFile root = resourceRoot.getRoot();

        try {
            List<File> dependencyJars = ApplicationBootstrapUtils.getDependencies( root.getPhysicalFile() );

            boolean clojureProvided = false;

            for (File each : dependencyJars) {
                if (each.getName().matches( "^clojure(-\\d.\\d.\\d)?\\.jar$" )) {
                    clojureProvided = true;
                }
                mount( each, unit );
            }

            if (!clojureProvided) {
                Immutant immutant = (Immutant)phaseContext.getServiceRegistry().getService( CoreServices.IMMUTANT ).getValue();

                log.warn( "No clojure.jar found within " + metaData.getApplicationName() + 
                        ", Using built-in clojure.jar (v" + immutant.getClojureVersion() + ")" );

                // borrow the shipped clojure.jar
                String jarPath = System.getProperty( "jboss.home.dir" ) + "/modules/org/immutant/core/main/clojure.jar";
                mount( new File( jarPath ), unit );
            }

            for(String each : DIR_ROOTS) {
                final ResourceRoot childResource = new ResourceRoot( root.getChild( each ), null );
                ModuleRootMarker.mark(childResource);
                unit.addToAttachmentList( Attachments.RESOURCE_ROOTS, childResource );
            }

        } catch (Exception e) {
            throw new DeploymentUnitProcessingException( e );
        }

    }

    private void mount(File file, DeploymentUnit unit) throws IOException {
        VirtualFile mountPath = VFS.getChild( File.createTempFile( file.getName(), ".jar", tmpMountDir( unit ) ).toURI() );
        final ResourceRoot childResource = new ResourceRoot( mountPath, 
                new MountHandle( VFS.mountZip( file, mountPath, TempFileProviderService.provider() ) ) );
        ModuleRootMarker.mark(childResource);
        unit.addToAttachmentList( Attachments.RESOURCE_ROOTS, childResource );
    }

    private File tmpMountDir(DeploymentUnit unit) throws IOException {
        File dir = new File( unit.getAttachment( ClojureMetaData.ATTACHMENT_KEY ).getRoot(), ".tmp_jar_mounts" );
        dir.mkdir();
        return dir;
    }
    
    @Override
    public void undeploy(DeploymentUnit context) {
        try {
            FileUtils.deleteDirectory( tmpMountDir( context ) );
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @SuppressWarnings("serial")
    private static final List<String> DIR_ROOTS = new ArrayList<String>() {
        {
            // FIXME: we really should pull this dynamically from lein's project.clj
            add( "src" );
            add( "resources" );
            add( "classes" );<commit_msg>Log out the jar mounts to debug jimbo's issue.<commit_after>/*
 * Copyright 2008-2012 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.immutant.core.processors;

import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.immutant.core.ApplicationBootstrapUtils;
import org.immutant.core.ClojureMetaData;
import org.immutant.core.Immutant;
import org.immutant.core.as.CoreServices;
import org.jboss.as.server.deployment.Attachments;
import org.jboss.as.server.deployment.DeploymentPhaseContext;
import org.jboss.as.server.deployment.DeploymentUnit;
import org.jboss.as.server.deployment.DeploymentUnitProcessingException;
import org.jboss.as.server.deployment.DeploymentUnitProcessor;
import org.jboss.as.server.deployment.module.ModuleRootMarker;
import org.jboss.as.server.deployment.module.MountHandle;
import org.jboss.as.server.deployment.module.ResourceRoot;
import org.jboss.as.server.deployment.module.TempFileProviderService;
import org.jboss.logging.Logger;
import org.jboss.vfs.VFS;
import org.jboss.vfs.VirtualFile;
import org.projectodd.polyglot.core.as.ArchivedDeploymentMarker;

public class AppJarScanningProcessor implements DeploymentUnitProcessor {

    @Override
    public void deploy(DeploymentPhaseContext phaseContext) throws DeploymentUnitProcessingException {
        DeploymentUnit unit = phaseContext.getDeploymentUnit();

        ClojureMetaData metaData = unit.getAttachment( ClojureMetaData.ATTACHMENT_KEY );
        if (metaData == null && 
                !ArchivedDeploymentMarker.isMarked( unit )) {
            return;
        }

        ResourceRoot resourceRoot = unit.getAttachment( Attachments.DEPLOYMENT_ROOT );
        VirtualFile root = resourceRoot.getRoot();

        try {
            List<File> dependencyJars = ApplicationBootstrapUtils.getDependencies( root.getPhysicalFile() );

            boolean clojureProvided = false;

            for (File each : dependencyJars) {
                if (each.getName().matches( "^clojure(-\\d.\\d.\\d)?\\.jar$" )) {
                    clojureProvided = true;
                }
                mount( each, unit );
            }

            if (!clojureProvided) {
                Immutant immutant = (Immutant)phaseContext.getServiceRegistry().getService( CoreServices.IMMUTANT ).getValue();

                log.warn( "No clojure.jar found within " + metaData.getApplicationName() + 
                        ", Using built-in clojure.jar (v" + immutant.getClojureVersion() + ")" );

                // borrow the shipped clojure.jar
                String jarPath = System.getProperty( "jboss.home.dir" ) + "/modules/org/immutant/core/main/clojure.jar";
                mount( new File( jarPath ), unit );
            }

            for(String each : DIR_ROOTS) {
                final ResourceRoot childResource = new ResourceRoot( root.getChild( each ), null );
                ModuleRootMarker.mark(childResource);
                unit.addToAttachmentList( Attachments.RESOURCE_ROOTS, childResource );
            }

        } catch (Exception e) {
            throw new DeploymentUnitProcessingException( e );
        }

    }

    private void mount(File file, DeploymentUnit unit) throws IOException {
        VirtualFile mountPath = VFS.getChild( File.createTempFile( file.getName(), ".jar", tmpMountDir( unit ) ).toURI() );
        log.info( unit.getName() + ": mounting " + file );
        final ResourceRoot childResource = new ResourceRoot( mountPath, 
                new MountHandle( VFS.mountZip( file, mountPath, TempFileProviderService.provider() ) ) );
        ModuleRootMarker.mark(childResource);
        unit.addToAttachmentList( Attachments.RESOURCE_ROOTS, childResource );
    }

    private File tmpMountDir(DeploymentUnit unit) throws IOException {
        File dir = new File( unit.getAttachment( ClojureMetaData.ATTACHMENT_KEY ).getRoot(), ".tmp_jar_mounts" );
        dir.mkdir();
        return dir;
    }
    
    @Override
    public void undeploy(DeploymentUnit context) {
        try {
            FileUtils.deleteDirectory( tmpMountDir( context ) );
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @SuppressWarnings("serial")
    private static final List<String> DIR_ROOTS = new ArrayList<String>() {
        {
            // FIXME: we really should pull this dynamically from lein's project.clj
            add( "src" );
            add( "resources" );
            add( "classes" );
{'fraction_non_alphanumeric': 0.09566854990583804, 'fraction_numerical': 0.0039548022598870055, 'mean_word_length': 2.8976146788990826, 'pattern_counts': {'":': 1, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6081936', 'n_tokens_mistral': 3032, 'n_tokens_neox': 2896, 'n_words': 754}