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<P>></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 public static final int FAILED = -1;
046
047 /** . */
048 public static final int CONSTRUCTED = 0;
049
050 /** . */
051 public static final int INITIALIZING = 1;
052
053 /** . */
054 public 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 /**
079 * Returns the current plugin status.
080 *
081 * @return the plugin status
082 */
083 public int getStatus() {
084 return status;
085 }
086
087 /**
088 * Returns the plugin type.
089 *
090 * @return the plugin type
091 */
092 public final Class<P> getType() {
093 return type;
094 }
095
096 /**
097 * Returns a list of {@link PropertyDescriptor} this plugin requires for its configuration.
098 *
099 * @return the configuration capabilities
100 */
101 protected Iterable<PropertyDescriptor<?>> createConfigurationCapabilities() {
102 return Collections.emptyList();
103 }
104
105 /**
106 * Returns a list of {@link PropertyDescriptor} this plugin requires for its configuration.
107 *
108 * @return the configuration capabilities
109 */
110 public final Iterable<PropertyDescriptor<?>> getConfigurationCapabilities() {
111 if (configurationCapabilities == null) {
112 List<PropertyDescriptor<?>> configurationCapabilities = Collections.emptyList();
113 for (PropertyDescriptor<?> pd : createConfigurationCapabilities()) {
114 if (configurationCapabilities.isEmpty()) {
115 configurationCapabilities = new ArrayList<PropertyDescriptor<?>>();
116 }
117 configurationCapabilities.add(pd);
118 }
119 this.configurationCapabilities = configurationCapabilities.isEmpty() ? configurationCapabilities : Collections.unmodifiableList(configurationCapabilities);
120 }
121 return configurationCapabilities;
122 }
123
124 /**
125 * Returns the implementation.
126 *
127 * @return the implementation
128 */
129 public abstract P getImplementation();
130
131 /**
132 * Implement this method to know about init life cycle callback.
133 */
134 public void init() {
135 }
136
137 /**
138 * Implement this method to know about destroy life cycle callback.
139 */
140 public void destroy() {
141 }
142
143 @Override
144 public String toString() {
145 return "Plugin[type=" + getClass().getSimpleName() + ",interface=" + type.getSimpleName() + "]";
146 }
147 }