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.PluginContext;
023import org.crsh.plugin.PluginDiscovery;
024import org.crsh.plugin.PluginLifeCycle;
025import org.crsh.plugin.ServiceLoaderDiscovery;
026import org.crsh.vfs.FS;
027import org.crsh.vfs.Path;
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.io.IOException;
037import java.net.URISyntaxException;
038import java.util.Collections;
039import java.util.HashMap;
040import java.util.Map;
041
042public class SpringBootstrap extends PluginLifeCycle implements
043    BeanClassLoaderAware,
044    BeanFactoryAware,
045    InitializingBean,
046    DisposableBean {
047
048  /** . */
049  private ClassLoader loader;
050
051  /** . */
052  private BeanFactory factory;
053
054  public SpringBootstrap() {
055  }
056
057  public void setBeanClassLoader(ClassLoader loader) {
058    this.loader = loader;
059  }
060
061  public void setBeanFactory(BeanFactory factory) throws BeansException {
062    this.factory = factory;
063  }
064
065  public void afterPropertiesSet() throws Exception {
066
067    // List beans
068    Map<String,Object> attributes = new HashMap<String, Object>();
069    attributes.put("factory", factory);
070    if (factory instanceof ListableBeanFactory) {
071      ListableBeanFactory listable = (ListableBeanFactory)factory;
072      attributes.put("beans", new SpringMap(listable));
073    }
074
075    //
076    PluginDiscovery discovery = new SpringPluginDiscovery(loader, factory);
077
078    //
079    FS cmdFS = createCommandFS();
080
081    //
082    FS confFS = createConfFS();
083
084    //
085    PluginContext context = new PluginContext(
086        discovery,
087        Collections.unmodifiableMap(attributes),
088        cmdFS,
089        confFS,
090        loader);
091
092    //
093    context.refresh();
094
095    //
096    start(context);
097  }
098
099  protected FS createCommandFS() throws IOException, URISyntaxException {
100    FS cmdFS = new FS();
101    cmdFS.mount(loader, Path.get("/crash/commands/"));
102    return cmdFS;
103  }
104
105  protected FS createConfFS() throws IOException, URISyntaxException {
106    FS confFS = new FS();
107    confFS.mount(loader, Path.get("/crash/"));
108    return confFS;
109  }
110
111  public void destroy() throws Exception {
112    stop();
113  }
114}