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    import java.util.ArrayList;
027    import java.util.Collections;
028    import java.util.List;
029    
030    /**
031     * <p>Base class for a plugin, that consist of a subclass of this class and the implementation
032     * of the business interface of the plugin. The business interface of the plugin is simply
033     * represented by the P generic parameter and its associated class <code>Class&lt;P&gt;></code>.</p>
034     *
035     * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
036     * @version $Revision$
037     * @param <P> the plugin type
038     */
039    public abstract class CRaSHPlugin<P> {
040    
041      /** . */
042      protected final Logger log = LoggerFactory.getLogger(getClass());
043    
044      /** . */
045      static final int FAILED = -1;
046    
047      /** . */
048      static final int CONSTRUCTED = 0;
049    
050      /** . */
051      static final int INITIALIZING = 1;
052    
053      /** . */
054      static final int INITIALIZED = 2;
055    
056      /** . */
057      PluginContext context;
058    
059      /** . */
060      int status;
061    
062      /** . */
063      private final Class<P> type;
064    
065      /** . */
066      private List<PropertyDescriptor<?>> configurationCapabilities;
067    
068      protected CRaSHPlugin() {
069        this.type = (Class<P>)TypeResolver.resolveToClass(getClass(), CRaSHPlugin.class, 0);
070        this.status = CONSTRUCTED;
071        this.context = null;
072      }
073    
074      protected final PluginContext getContext() {
075        return context;
076      }
077    
078      public final Class<P> getType() {
079        return type;
080      }
081    
082      /**
083       * Returns a list of {@link PropertyDescriptor} this plugin requires for its configuration.
084       *
085       * @return the configuration capabilities
086       */
087      protected Iterable<PropertyDescriptor<?>> createConfigurationCapabilities() {
088        return Collections.emptyList();
089      }
090    
091      /**
092       * Returns a list of {@link PropertyDescriptor} this plugin requires for its configuration.
093       *
094       * @return the configuration capabilities
095       */
096      public final Iterable<PropertyDescriptor<?>> getConfigurationCapabilities() {
097        if (configurationCapabilities == null) {
098          List<PropertyDescriptor<?>> configurationCapabilities = Collections.emptyList();
099          for (PropertyDescriptor<?> pd : createConfigurationCapabilities()) {
100            if (configurationCapabilities.isEmpty()) {
101              configurationCapabilities = new ArrayList<PropertyDescriptor<?>>();
102            }
103            configurationCapabilities.add(pd);
104          }
105          this.configurationCapabilities = configurationCapabilities.isEmpty() ? configurationCapabilities : Collections.unmodifiableList(configurationCapabilities);
106        }
107        return configurationCapabilities;
108      }
109    
110      /**
111       * Returns the implementation.
112       *
113       * @return the implementation
114       */
115      public abstract P getImplementation();
116    
117      public void init() {
118      }
119    
120      public void destroy() {
121      }
122    
123      @Override
124      public String toString() {
125        return "Plugin[type=" + getClass().getSimpleName() + ",interface=" + type.getSimpleName() + "]";
126      }
127    }