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    
020    package org.crsh.plugin;
021    
022    import org.crsh.util.TypeResolver;
023    
024    import java.util.ArrayList;
025    import java.util.Collections;
026    import java.util.List;
027    import java.util.logging.Logger;
028    
029    public abstract class CRaSHPlugin<P> {
030    
031      /** . */
032      protected final Logger log = Logger.getLogger(getClass().getName());
033    
034      /** . */
035      public static final int FAILED = -1;
036    
037      /** . */
038      public static final int CONSTRUCTED = 0;
039    
040      /** . */
041      public static final int INITIALIZING = 1;
042    
043      /** . */
044      public static final int INITIALIZED = 2;
045    
046      /** . */
047      PluginContext context;
048    
049      /** . */
050      int status;
051    
052      /** . */
053      private final Class<P> type;
054    
055      /** . */
056      private List<PropertyDescriptor<?>> configurationCapabilities;
057    
058      protected CRaSHPlugin() {
059        this.type = (Class<P>)TypeResolver.resolveToClass(getClass(), CRaSHPlugin.class, 0);
060        this.status = CONSTRUCTED;
061        this.context = null;
062      }
063    
064      protected final PluginContext getContext() {
065        return context;
066      }
067    
068      /**
069       * Returns the current plugin status.
070       *
071       * @return the plugin status
072       */
073      public int getStatus() {
074        return status;
075      }
076    
077      /**
078       * Returns the plugin type.
079       *
080       * @return the plugin type
081       */
082      public final Class<P> getType() {
083        return type;
084      }
085    
086      /**
087       * Returns a list of {@link PropertyDescriptor} this plugin requires for its configuration.
088       *
089       * @return the configuration capabilities
090       */
091      protected Iterable<PropertyDescriptor<?>> createConfigurationCapabilities() {
092        return Collections.emptyList();
093      }
094    
095      /**
096       * Returns a list of {@link PropertyDescriptor} this plugin requires for its configuration.
097       *
098       * @return the configuration capabilities
099       */
100      public final Iterable<PropertyDescriptor<?>> getConfigurationCapabilities() {
101        if (configurationCapabilities == null) {
102          List<PropertyDescriptor<?>> configurationCapabilities = Collections.emptyList();
103          for (PropertyDescriptor<?> pd : createConfigurationCapabilities()) {
104            if (configurationCapabilities.isEmpty()) {
105              configurationCapabilities = new ArrayList<PropertyDescriptor<?>>();
106            }
107            configurationCapabilities.add(pd);
108          }
109          this.configurationCapabilities = configurationCapabilities.isEmpty() ? configurationCapabilities : Collections.unmodifiableList(configurationCapabilities);
110        }
111        return configurationCapabilities;
112      }
113    
114      /**
115       * Returns the implementation.
116       *
117       * @return the implementation
118       */
119      public abstract P getImplementation();
120    
121      /**
122       * Implement this method to know about init life cycle callback.
123       */
124      public void init() {
125      }
126    
127      /**
128       * Implement this method to know about destroy life cycle callback.
129       */
130      public void destroy() {
131      }
132    
133      @Override
134      public String toString() {
135        return "Plugin[type=" + getClass().getSimpleName() + ",interface=" + type.getSimpleName() + "]";
136      }
137    }