001/**
002 * Copyright (c) 2011, The University of Southampton and the individual contributors.
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 *
008 *   *  Redistributions of source code must retain the above copyright notice,
009 *      this list of conditions and the following disclaimer.
010 *
011 *   *  Redistributions in binary form must reproduce the above copyright notice,
012 *      this list of conditions and the following disclaimer in the documentation
013 *      and/or other materials provided with the distribution.
014 *
015 *   *  Neither the name of the University of Southampton nor the names of its
016 *      contributors may be used to endorse or promote products derived from this
017 *      software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030package org.openimaj.tools;
031
032import java.lang.reflect.Constructor;
033import java.lang.reflect.InvocationTargetException;
034
035import javassist.ClassPool;
036import javassist.Loader;
037
038import org.kohsuke.args4j.CmdLineException;
039import org.openimaj.aop.MultiTransformClassFileTransformer;
040import org.openimaj.aop.classloader.ClassLoaderTransform;
041import org.openimaj.citation.ReferencesClassTransformer;
042
043/**
044 * A tool for running another program and extracting a bibliography for all the
045 * annotated methods and classes that are used during the programs execution.
046 * 
047 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
048 */
049public class ReferencesTool {
050        /**
051         * Create an {@link OutputWorker} with the specified classloader
052         * 
053         * @param cl
054         *            the classloader
055         * @param args
056         *            the arguments string
057         * @return the new {@link OutputWorker} as a {@link Runnable}.
058         * @throws ClassNotFoundException
059         * @throws InstantiationException
060         * @throws IllegalAccessException
061         * @throws SecurityException
062         * @throws NoSuchMethodException
063         * @throws IllegalArgumentException
064         * @throws InvocationTargetException
065         */
066        private static Runnable loadOutputWorker(Loader cl, String[] args)
067                        throws ClassNotFoundException, InstantiationException, IllegalAccessException, SecurityException,
068                        NoSuchMethodException, IllegalArgumentException, InvocationTargetException
069        {
070                final Class<?> clz = cl.loadClass(OutputWorker.class.getName());
071                final Constructor<?> ctr = clz.getConstructor(String[].class);
072
073                return (Runnable) ctr.newInstance((Object) args);
074        }
075
076        /**
077         * The main method.
078         * 
079         * @param args
080         *            the arguments
081         * 
082         * @throws Throwable
083         *             if an error occurs
084         */
085        public static void main(String[] args) throws Throwable {
086                ReferencesToolOpts options = null;
087                try {
088                        options = new ReferencesToolOpts(args);
089                        options.validate();
090                } catch (final CmdLineException e) {
091                        System.err.println(e.getMessage());
092                        System.err.println();
093                        System.err.println("Usage:");
094                        options.parser.printUsage(System.err);
095                        System.err.println();
096                        System.err.println("Examples:");
097                        System.err
098                                        .println("java -jar ReferencesTool.jar [references output options] -jar jarFile [tool arguments and options]");
099                        System.err
100                                        .println("java -jar ReferencesTool.jar [references output options] -cp classpath mainClass [tool arguments and options]");
101                        System.err
102                                        .println("java -jar ReferencesTool.jar [references output options] mainClass [tool arguments and options]");
103
104                        return;
105                }
106
107                final MultiTransformClassFileTransformer transformer = new MultiTransformClassFileTransformer(
108                                new ReferencesClassTransformer());
109
110                Loader cl = null;
111                if (options.isJar()) {
112                        cl = ClassLoaderTransform.run(ClassPool.getDefault(), transformer, options.jarFile,
113                                        options.arguments.toArray(new String[options.arguments.size()]));
114                } else {
115                        cl = ClassLoaderTransform.run(ClassPool.getDefault(), transformer, options.classpath, options.mainMethod,
116                                        options.arguments.toArray(new String[options.arguments.size()]));
117                }
118
119                Runtime.getRuntime().addShutdownHook(new Thread(loadOutputWorker(cl, args)));
120        }
121}