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.plugin;
021    
022    import org.crsh.util.TypeResolver;
023    import org.slf4j.Logger;
024    import org.slf4j.LoggerFactory;
025    
026    /**
027     * <p>Base class for a plugin, that consist of a subclass of this class and the implementation
028     * of the business interface of the plugin. The business interface of the plugin is simply
029     * represented by the P generic parameter and its associated class <code>Class&lt;P&gt;></code>.</p>
030     *
031     * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
032     * @version $Revision$
033     * @param <P> the plugin type
034     */
035    public abstract class CRaSHPlugin<P> {
036    
037      /** . */
038      protected final Logger log = LoggerFactory.getLogger(getClass());
039    
040      /** . */
041      static final int FAILED = -1;
042    
043      /** . */
044      static final int CONSTRUCTED = 0;
045    
046      /** . */
047      static final int INITIALIZING = 1;
048    
049      /** . */
050      static final int INITIALIZED = 2;
051    
052      /** . */
053      PluginContext context;
054    
055      /** . */
056      int status;
057    
058      /** . */
059      private final Class<P> type;
060    
061      protected CRaSHPlugin() {
062        this.type = (Class<P>)TypeResolver.resolve(getClass(), CRaSHPlugin.class, 0);
063        this.status = CONSTRUCTED;
064        this.context = null;
065      }
066    
067      protected final PluginContext getContext() {
068        return context;
069      }
070    
071      public final Class<P> getType() {
072        return type;
073      }
074    
075      /**
076       * Returns the implementation.
077       *
078       * @return the implementation
079       */
080      public abstract P getImplementation();
081    
082      public void init() {
083      }
084    
085      public void destroy() {
086      }
087    
088      @Override
089      public String toString() {
090        return "Plugin[type=" + getClass().getSimpleName() + ",interface=" + type.getSimpleName() + "]";
091      }
092    }