001    /*
002     * Copyright (C) 2003-2009 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.telnet;
021    
022    import org.crsh.plugin.*;
023    import org.crsh.telnet.term.TelnetLifeCycle;
024    import org.crsh.vfs.Resource;
025    
026    import java.io.IOException;
027    import java.net.URL;
028    import java.util.Collections;
029    
030    /**
031     * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
032     * @version $Revision$
033     */
034    public class TelnetPlugin extends CRaSHPlugin<TelnetPlugin> {
035    
036      /** . */
037      public static final PropertyDescriptor<Integer> TELNET_PORT = PropertyDescriptor.create("telnet.port", 5000, "The telnet port");
038    
039      /** . */
040      private TelnetLifeCycle lifeCycle;
041    
042      @Override
043      public TelnetPlugin getImplementation() {
044        return this;
045      }
046    
047      @Override
048      protected Iterable<PropertyDescriptor<?>> createConfigurationCapabilities() {
049        return Collections.<PropertyDescriptor<?>>singletonList(TELNET_PORT);
050      }
051    
052      @Override
053      public void init() {
054        PluginContext context = getContext();
055    
056        //
057        Resource config = null;
058    
059        //
060        URL configURL = TelnetPlugin.class.getResource("telnet.properties");
061        if (configURL != null) {
062          try {
063            log.debug("Found embedded telnet config url " + configURL);
064            config = new Resource(configURL);
065          }
066          catch (IOException e) {
067            log.debug("Could not load embedded telnet config url " + configURL + " will bypass it", e);
068          }
069        }
070    
071        // Override from config if any
072        Resource res = getContext().loadResource("telnet.properties", ResourceKind.CONFIG);
073        if (res != null) {
074          config = res;
075          log.debug("Found telnet config url " + configURL);
076        }
077    
078        //
079        if (configURL == null) {
080          log.info("Could not boot Telnet due to missing config");
081          return;
082        }
083    
084        //
085        TelnetLifeCycle lifeCycle = new TelnetLifeCycle(context);
086        lifeCycle.setConfig(config);
087        Integer port = context.getProperty(TELNET_PORT);
088        if (port != null) {
089          lifeCycle.setPort(port);
090        }
091    
092        //
093        lifeCycle.init();
094    
095        //
096        this.lifeCycle = lifeCycle;
097      }
098    
099      @Override
100      public void destroy() {
101        if (lifeCycle != null) {
102          lifeCycle.destroy();
103        }
104      }
105    }