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.shell.impl.CRaSHSession;
031 import org.crsh.term.processor.Processor;
032 import org.crsh.term.BaseTerm;
033 import org.crsh.term.Term;
034 import org.crsh.term.spi.net.TermIOClient;
035 import org.slf4j.Logger;
036 import org.slf4j.LoggerFactory;
037
038 import java.io.Closeable;
039 import java.io.File;
040 import java.lang.instrument.Instrumentation;
041 import java.util.List;
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("Spanned 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={"p","path"})
079 List<String> paths,
080 @Argument(name = "port")
081 Integer port) throws Exception {
082
083 //
084 Bootstrap bootstrap = new Bootstrap(Thread.currentThread().getContextClassLoader());
085
086 //
087 if (paths != null) {
088 for (String path : paths) {
089 File mount = new File(path);
090 bootstrap.addToMounts(mount);
091 }
092 }
093
094 //
095 if (jars != null) {
096 for (String jar : jars) {
097 File jarFile = new File(jar);
098 bootstrap.addToClassPath(jarFile);
099 }
100 }
101
102 // Do bootstrap
103 bootstrap.bootstrap();
104
105 //
106 try {
107 final TermIOClient client = new TermIOClient(port);
108 log.info("Callback back remote on port " + port);
109 client.connect();
110
111 // Do stuff
112 Term term = new BaseTerm(client);
113 CRaSH crash = new CRaSH(bootstrap.getContext());
114 Processor processor = new Processor(term, crash.createSession());
115 processor.addListener(new Closeable() {
116 public void close() {
117 client.close();
118 }
119 });
120
121 // Run
122 processor.run();
123 }
124 finally {
125 bootstrap.shutdown();
126 }
127 }
128 }