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> mounts = Utils.newArrayList();
047
048 /** The class path. */
049 private List<File> classPath = Utils.newArrayList();
050
051 /** The base classloader. */
052 private ClassLoader baseLoader;
053
054 public Bootstrap(ClassLoader baseLoader) throws NullPointerException {
055 if (baseLoader == null) {
056 throw new NullPointerException("No null base loader accepted");
057 }
058 this.baseLoader = baseLoader;
059 }
060
061 public Bootstrap addToMounts(File file) {
062 mounts.add(file);
063 return this;
064 }
065
066 public Bootstrap addToClassPath(File file) {
067 classPath.add(file);
068 return this;
069 }
070
071 public void bootstrap() throws Exception {
072
073 // Compute the url classpath
074 URL[] urls = new URL[classPath.size()];
075 for (int i = 0;i < urls.length;i++) {
076 urls[i] = classPath.get(i).toURI().toURL();
077 }
078
079 // Create the classloader
080 URLClassLoader classLoader = new URLClassLoader(urls, baseLoader);
081
082 // Create the virtual file system
083 FS fs = new FS();
084 for (File file : mounts) {
085 fs.mount(file);
086 }
087 fs.mount(classLoader, Path.get("/crash/"));
088
089 // The service loader discovery
090 ServiceLoaderDiscovery discovery = new ServiceLoaderDiscovery(classLoader);
091
092 //
093 StringBuilder info = new StringBuilder("Booting crash with classpath=");
094 info.append(classPath).append(" and mounts=[");
095 for (int i = 0;i < mounts.size();i++) {
096 if (i > 0) {
097 info.append(',');
098 }
099 info.append(mounts.get(i).getAbsolutePath());
100 }
101 info.append(']');
102 log.info(info.toString());
103
104 //
105 PluginContext context = new PluginContext(discovery, fs, classLoader);
106 context.refresh();
107 start(context);
108 }
109
110 public void shutdown() {
111 stop();
112 }
113 }