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
020package org.crsh.spring;
021
022import org.crsh.plugin.Embedded;
023import org.crsh.plugin.PluginDiscovery;
024import org.crsh.util.Utils;
025import org.crsh.vfs.spi.FSMountFactory;
026import org.crsh.vfs.spi.file.FileMountFactory;
027import org.crsh.vfs.spi.url.ClassPathMountFactory;
028import org.springframework.beans.BeansException;
029import org.springframework.beans.factory.BeanClassLoaderAware;
030import org.springframework.beans.factory.BeanFactory;
031import org.springframework.beans.factory.BeanFactoryAware;
032import org.springframework.beans.factory.DisposableBean;
033import org.springframework.beans.factory.InitializingBean;
034import org.springframework.beans.factory.ListableBeanFactory;
035
036import java.util.Collections;
037import java.util.HashMap;
038import java.util.Map;
039import java.util.logging.Level;
040
041public class SpringBootstrap extends Embedded implements
042    BeanClassLoaderAware,
043    BeanFactoryAware,
044    InitializingBean,
045    DisposableBean {
046
047  /** . */
048  private ClassLoader loader;
049
050  /** . */
051  private BeanFactory factory;
052
053  /** . */
054  protected final HashMap<String, FSMountFactory<?>> drivers = new HashMap<String, FSMountFactory<?>>();
055
056  /** . */
057  private String cmdMountPointConfig;
058
059  /** . */
060  private String confMountPointConfig;
061
062  public SpringBootstrap() {
063  }
064
065  public String getCmdMountPointConfig() {
066    return cmdMountPointConfig;
067  }
068
069  public void setCmdMountPointConfig(String cmdMountPointConfig) {
070    this.cmdMountPointConfig = cmdMountPointConfig;
071  }
072
073  public String getConfMountPointConfig() {
074    return confMountPointConfig;
075  }
076
077  public void setConfMountPointConfig(String confMountPointConfig) {
078    this.confMountPointConfig = confMountPointConfig;
079  }
080
081  public void setBeanClassLoader(ClassLoader loader) {
082    this.loader = loader;
083  }
084
085  public void setBeanFactory(BeanFactory factory) throws BeansException {
086    this.factory = factory;
087  }
088
089  public void afterPropertiesSet() throws Exception {
090
091    // Initialise the registerable drivers
092    try {
093      drivers.put("classpath", new ClassPathMountFactory(loader));
094      drivers.put("file", new FileMountFactory(Utils.getCurrentDirectory()));
095    }
096    catch (Exception e) {
097      log.log(Level.SEVERE, "Coult not initialize classpath driver", e);
098      return;
099    }
100
101    // List beans
102    Map<String,Object> attributes = new HashMap<String, Object>();
103    attributes.put("factory", factory);
104    if (factory instanceof ListableBeanFactory) {
105      ListableBeanFactory listable = (ListableBeanFactory)factory;
106      attributes.put("beans", new SpringMap(listable));
107    }
108
109    //
110    PluginDiscovery discovery = new SpringPluginDiscovery(loader, factory);
111
112    //
113    start(Collections.unmodifiableMap(attributes), discovery, loader);
114  }
115
116  @Override
117  protected Map<String, FSMountFactory<?>> getMountFactories() {
118    return drivers;
119  }
120
121  @Override
122  protected String resolveConfMountPointConfig() {
123    return confMountPointConfig != null ? confMountPointConfig : getDefaultConfMountPointConfig();
124  }
125
126  @Override
127  protected String resolveCmdMountPointConfig() {
128    return cmdMountPointConfig != null ? cmdMountPointConfig : getDefaultCmdMountPointConfig();
129  }
130
131  protected String getDefaultCmdMountPointConfig() {
132    return "classpath:/crash/commands/";
133  }
134
135  protected String getDefaultConfMountPointConfig() {
136    return "classpath:/crash/";
137  }
138
139  public void destroy() throws Exception {
140    stop();
141  }
142}