001    /*
002     * Copyright (C) 2010 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    
020    package org.crsh.standalone;
021    
022    import org.crsh.plugin.PluginContext;
023    import org.crsh.plugin.PluginLifeCycle;
024    import org.crsh.plugin.ServiceLoaderDiscovery;
025    import org.crsh.util.Utils;
026    import org.crsh.vfs.FS;
027    import org.crsh.vfs.Path;
028    import org.slf4j.Logger;
029    import org.slf4j.LoggerFactory;
030    
031    import java.io.File;
032    import java.net.URL;
033    import java.net.URLClassLoader;
034    import java.util.List;
035    
036    /**
037     * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
038     * @version $Revision$
039     */
040    public class Bootstrap extends PluginLifeCycle {
041    
042      /** . */
043      protected final Logger log = LoggerFactory.getLogger(getClass());
044    
045      /** The mounted path on the file system. */
046      private List<File> bins = Utils.newArrayList();
047    
048      /** The mounted path on the file system. */
049      private List<File> confs = Utils.newArrayList();
050    
051      /** The class path. */
052      private List<File> jars = Utils.newArrayList();
053    
054      /** The base classloader. */
055      private ClassLoader baseLoader;
056    
057      public Bootstrap(ClassLoader baseLoader) throws NullPointerException {
058        if (baseLoader == null) {
059          throw new NullPointerException("No null base loader accepted");
060        }
061        this.baseLoader = baseLoader;
062      }
063    
064      public Bootstrap addConfPath(File file) {
065        confs.add(file);
066        return this;
067      }
068    
069      public Bootstrap addCmdPath(File file) {
070        bins.add(file);
071        return this;
072      }
073    
074      public Bootstrap addJarPath(File file) {
075        jars.add(file);
076        return this;
077      }
078    
079      public void bootstrap() throws Exception {
080    
081        // Compute the url classpath
082        URL[] urls = new URL[jars.size()];
083        for (int i = 0;i < urls.length;i++) {
084          urls[i] = jars.get(i).toURI().toURL();
085        }
086    
087        // Create the classloader
088        URLClassLoader classLoader = new URLClassLoader(urls, baseLoader);
089    
090        // Create the bin file system
091        FS binFS = new FS();
092        for (File bin : bins) {
093          binFS.mount(bin);
094        }
095        binFS.mount(classLoader, Path.get("/crash/commands/"));
096    
097        // Create the conf file system
098        FS confFS = new FS();
099        for (File conf : confs) {
100          confFS.mount(conf);
101        }
102        confFS.mount(classLoader, Path.get("/crash/"));
103    
104        // The service loader discovery
105        ServiceLoaderDiscovery discovery = new ServiceLoaderDiscovery(classLoader);
106    
107        //
108        StringBuilder info = new StringBuilder("Booting crash with classpath=");
109        info.append(jars).append(" and mounts=[");
110        for (int i = 0;i < bins.size();i++) {
111          if (i > 0) {
112            info.append(',');
113          }
114          info.append(bins.get(i).getAbsolutePath());
115        }
116        info.append(']');
117        log.info(info.toString());
118    
119        //
120        PluginContext context = new PluginContext(discovery, binFS, confFS, classLoader);
121        context.refresh();
122        start(context);
123      }
124    
125      public void shutdown() {
126        stop();
127      }
128    }