001    /*
002     * Copyright (C) 2011 eXo Platform SAS.
003     *
004     * This is free software; you can redistribute it and/or modify it
005     * under the terms of the GNU Lesser General Public License as
006     * published by the Free Software Foundation; either version 2.1 of
007     * the License, or (at your option) any later version.
008     *
009     * This software is distributed in the hope that it will be useful,
010     * but WITHOUT ANY WARRANTY; without even the implied warranty of
011     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012     * Lesser General Public License for more details.
013     *
014     * You should have received a copy of the GNU Lesser General Public
015     * License along with this software; if not, write to the Free
016     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
017     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
018     */
019    package org.crsh.standalone;
020    
021    import org.crsh.cmdline.ClassDescriptor;
022    import org.crsh.cmdline.CommandFactory;
023    import org.crsh.cmdline.annotations.Argument;
024    import org.crsh.cmdline.annotations.Command;
025    import org.crsh.cmdline.annotations.Option;
026    import org.crsh.cmdline.matcher.CommandMatch;
027    import org.crsh.cmdline.matcher.InvocationContext;
028    import org.crsh.cmdline.matcher.Matcher;
029    import org.crsh.shell.impl.CRaSH;
030    import org.crsh.term.BaseTerm;
031    import org.crsh.term.Term;
032    import org.crsh.term.processor.Processor;
033    import org.crsh.term.spi.net.TermIOClient;
034    import org.slf4j.Logger;
035    import org.slf4j.LoggerFactory;
036    
037    import java.io.Closeable;
038    import java.io.File;
039    import java.lang.instrument.Instrumentation;
040    import java.util.List;
041    import java.util.Properties;
042    
043    /**
044     * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
045     */
046    public class Agent {
047    
048      /** . */
049      private static Logger log = LoggerFactory.getLogger(Agent.class);
050    
051      public static void agentmain(final String agentArgs, Instrumentation inst) throws Exception {
052        log.info("CRaSH agent loaded");
053    
054        //
055        Thread t = new Thread() {
056          @Override
057          public void run() {
058            try {
059              ClassDescriptor<Agent> c = CommandFactory.create(Agent.class);
060              Matcher<Agent> matcher = Matcher.createMatcher("main", c);
061              CommandMatch<Agent, ?, ?> match = matcher.match(agentArgs);
062              match.invoke(new InvocationContext(), new Agent());
063            } catch (Exception e) {
064              e.printStackTrace();
065            }
066          }
067        };
068    
069        //
070        t.start();
071        log.info("Spawned CRaSH thread " + t.getId() + " for further processing");
072      }
073    
074      @Command
075      public void main(
076        @Option(names={"j","jar"})
077        List<String> jars,
078        @Option(names={"c","cmd"})
079        List<String> cmds,
080        @Option(names={"conf"})
081        List<String> confs,
082        @Option(names={"p","property"})
083        List<String> properties,
084        @Argument(name = "port")
085        Integer port) throws Exception {
086    
087        //
088        Bootstrap bootstrap = new Bootstrap(Thread.currentThread().getContextClassLoader());
089    
090        //
091        if (cmds != null) {
092          for (String cmd : cmds) {
093            File cmdPath = new File(cmd);
094            bootstrap.addCmdPath(cmdPath);
095          }
096        }
097    
098        //
099        if (confs != null) {
100          for (String conf : confs) {
101            File confPath = new File(conf);
102            bootstrap.addConfPath(confPath);
103          }
104        }
105    
106        //
107        if (jars != null) {
108          for (String jar : jars) {
109            File jarFile = new File(jar);
110            bootstrap.addJarPath(jarFile);
111          }
112        }
113    
114        //
115        if (properties != null) {
116          Properties config = new Properties();
117          for (String property : properties) {
118            int index = property.indexOf('=');
119            if (index == -1) {
120              config.setProperty(property, "");
121            } else {
122              config.setProperty(property.substring(0, index), property.substring(index + 1));
123            }
124          }
125          bootstrap.setConfig(config);
126        }
127    
128        // Do bootstrap
129        bootstrap.bootstrap();
130    
131        //
132        try {
133          final TermIOClient client = new TermIOClient(port);
134          log.info("Callback back remote on port " + port);
135          client.connect();
136    
137          // Do stuff
138          Term term = new BaseTerm(client);
139          CRaSH crash = new CRaSH(bootstrap.getContext());
140          Processor processor = new Processor(term, crash.createSession());
141          processor.addListener(new Closeable() {
142            public void close() {
143              client.close();
144            }
145          });
146    
147          // Run
148          processor.run();
149        }
150        finally {
151          bootstrap.shutdown();
152        }
153      }
154    }