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