001 /* 002 * Copyright (C) 2012 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.plugin; 020 021 import org.crsh.vfs.FS; 022 import org.crsh.vfs.spi.FSMountFactory; 023 024 import java.io.IOException; 025 import java.util.Collections; 026 import java.util.Map; 027 import java.util.logging.Level; 028 029 /** 030 * @author Julien Viet 031 */ 032 public class Embedded extends PluginLifeCycle { 033 034 /** 035 * Create the plugin context, allow subclasses to customize it. 036 * 037 * @param discovery the plugin discovery 038 * @return the plugin context 039 */ 040 protected PluginContext create(Map<String, Object> attributes, PluginDiscovery discovery, ClassLoader loader) { 041 042 // 043 FS cmdFS; 044 FS confFS; 045 try { 046 cmdFS = createCommandFS(); 047 confFS = createConfFS(); 048 } 049 catch (IOException e) { 050 log.log(Level.SEVERE, "Coult not initialize the file system", e); 051 return null; 052 } 053 054 // 055 return new PluginContext(discovery, attributes, cmdFS, confFS, loader); 056 } 057 058 /** 059 * Create and start the plugin context. 060 * 061 * @param discovery the plugin discovery 062 * @return the plugin context 063 */ 064 protected PluginContext start(Map<String, Object> attributes, PluginDiscovery discovery, ClassLoader loader) { 065 PluginContext context = create(attributes, discovery, loader); 066 if (context != null) { 067 context.refresh(); 068 start(context); 069 } 070 return context; 071 } 072 073 /** 074 * Create the command file system from the <code>crash.mountpointconfig.cmd</code> servlet context parameter. 075 * 076 * @return the command file system 077 */ 078 protected FS createCommandFS() throws IOException { 079 return createFS(resolveCmdMountPointConfig()); 080 } 081 082 /** 083 * Create the conf file system from the <code>crash.mountpointconfig.conf</code> servlet context parameter. 084 * 085 * @return the conf file system 086 */ 087 protected FS createConfFS() throws IOException { 088 return createFS(resolveConfMountPointConfig()); 089 } 090 091 /** 092 * @return the registered drivers, by default an empty map is returned, subclasses can override to customize 093 */ 094 protected Map<String, FSMountFactory<?>> getMountFactories() { 095 return Collections.emptyMap(); 096 } 097 098 /** 099 * Create a new file system, configured by a the argument <code>mountPointConfig</code>: when the mount point 100 * configuration is not null, it is mounted on the returned file system. 101 * 102 * @param mountPointConfig the mount point configuration 103 * @return the configured file system 104 * @throws IOException any io exception 105 */ 106 protected FS createFS(String mountPointConfig) throws IOException { 107 FS.Builder builder = new FS.Builder(); 108 for (Map.Entry<String, FSMountFactory<?>> driver : getMountFactories().entrySet()) { 109 builder.register(driver.getKey(), driver.getValue()); 110 } 111 if (mountPointConfig != null) { 112 builder.mount(mountPointConfig); 113 } 114 return builder.build(); 115 } 116 117 protected String resolveConfMountPointConfig() { 118 return null; 119 } 120 121 protected String resolveCmdMountPointConfig() { 122 return null; 123 } 124 }