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.Collections; 035 import java.util.List; 036 import java.util.Map; 037 038 /** 039 * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a> 040 * @version $Revision$ 041 */ 042 public class Bootstrap extends PluginLifeCycle { 043 044 /** . */ 045 protected final Logger log = LoggerFactory.getLogger(getClass()); 046 047 /** The mounted path on the file system. */ 048 private List<File> cmds = Utils.newArrayList(); 049 050 /** The mounted path on the file system. */ 051 private List<File> confs = Utils.newArrayList(); 052 053 /** The class path. */ 054 private List<File> jars = Utils.newArrayList(); 055 056 /** The base classloader. */ 057 private ClassLoader baseLoader; 058 059 /** The attributes. */ 060 private Map<String, ?> attributes; 061 062 public Bootstrap(ClassLoader baseLoader) throws NullPointerException { 063 if (baseLoader == null) { 064 throw new NullPointerException("No null base loader accepted"); 065 } 066 this.baseLoader = baseLoader; 067 this.attributes = Collections.emptyMap(); 068 } 069 070 public void setAttributes(Map<String, ?> attributes) { 071 this.attributes = attributes; 072 } 073 074 public Bootstrap addConfPath(File file) { 075 confs.add(file); 076 return this; 077 } 078 079 public Bootstrap addCmdPath(File file) { 080 cmds.add(file); 081 return this; 082 } 083 084 public Bootstrap addJarPath(File file) { 085 jars.add(file); 086 return this; 087 } 088 089 public void bootstrap() throws Exception { 090 091 // Compute the url classpath 092 URL[] urls = new URL[jars.size()]; 093 for (int i = 0;i < urls.length;i++) { 094 urls[i] = jars.get(i).toURI().toURL(); 095 } 096 097 // Create the classloader 098 URLClassLoader classLoader = new URLClassLoader(urls, baseLoader); 099 100 // Create the cmd file system 101 FS cmdFS = new FS(); 102 for (File cmd : cmds) { 103 cmdFS.mount(cmd); 104 } 105 106 // Add the classloader 107 cmdFS.mount(classLoader, Path.get("/crash/commands/")); 108 109 // Create the conf file system 110 FS confFS = new FS(); 111 for (File conf : confs) { 112 confFS.mount(conf); 113 } 114 confFS.mount(classLoader, Path.get("/crash/")); 115 116 // The service loader discovery 117 ServiceLoaderDiscovery discovery = new ServiceLoaderDiscovery(classLoader); 118 119 // 120 StringBuilder info = new StringBuilder("Booting crash with classpath="); 121 info.append(jars).append(" and mounts=["); 122 for (int i = 0;i < cmds.size();i++) { 123 if (i > 0) { 124 info.append(','); 125 } 126 info.append(cmds.get(i).getAbsolutePath()); 127 } 128 info.append(']'); 129 log.info(info.toString()); 130 131 // 132 PluginContext context = new PluginContext( 133 discovery, 134 attributes, 135 cmdFS, 136 confFS, 137 classLoader); 138 139 // 140 context.refresh(); 141 142 // 143 start(context); 144 } 145 146 public void shutdown() { 147 stop(); 148 } 149 }