View Javadoc
1   package net.sf.logdistiller;
2   
3   /*
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  import java.util.*;
18  
19  import net.sf.logdistiller.plugins.BasePlugins;
20  import net.sf.logdistiller.util.ExtensionHelper;
21  import net.sf.logdistiller.util.FormatUtil;
22  
23  /**
24   * Definition of available plugins. By default, some plugins come {@link net.sf.logdistiller.plugins.BasePlugins
25   * predefined} with <b>LogDistiller</b>. You can add custom plugins:
26   * <ul>
27   * <li>1. create a new <code>LogDistillation.Plugin</code> class</li>
28   * <li>2. create a new <code>Plugins</code> class defining previous <code>Plugin</code> with a default constructor</li>
29   * <li>3. create a <code>logdistiller.properties</code> resource file which <code>plugins</code> property is the full
30   * <code>Plugins</code> class name</li>
31   * </ul>
32   */
33  public abstract class Plugins
34  {
35      /**
36       * get the <code>Plugin</code> instances defined by this extension.
37       *
38       * @return the list of plugins
39       * @see Plugin
40       */
41      public abstract List<Plugin> definePlugins();
42  
43      public static final List<Plugin> ALL;
44  
45      public static final Map<String, Plugin> MAP;
46      static
47      {
48          List<Plugin> all = new ArrayList<Plugin>( new BasePlugins().definePlugins() );
49          all.addAll( loadExtensionPlugins() );
50          ALL = Collections.unmodifiableList( all );
51  
52          Map<String, Plugin> map = new HashMap<String, Plugin>();
53          for ( Plugin plugin : ALL )
54          {
55              map.put( plugin.getId(), plugin );
56          }
57          MAP = Collections.unmodifiableMap( map );
58      }
59  
60      /**
61       * get all the plugins defined (predefined and custom).
62       */
63      public static List<Plugin> getAllPlugins()
64      {
65          return ALL;
66      }
67  
68      public static String listAllPluginIds()
69      {
70          return FormatUtil.join( ", ", MAP.keySet().iterator() );
71      }
72  
73      public static Plugin getPlugin( String id )
74      {
75          return MAP.get( id );
76      }
77  
78      /**
79       * Instanciates a new distillation plugin from its distiller configuration.
80       *
81       * @param pluginConf the plugin distiller configuration
82       * @return the distillation plugin
83       * @throws PluginConfigException if the plugin type defined in the configuration does not exist
84       */
85      public static LogDistillation.Plugin newInstance( LogDistiller.Plugin pluginConf )
86      {
87          Plugin plugin = getPlugin( pluginConf.getType() );
88          if ( plugin == null )
89          {
90              throw new PluginConfigException( "plugin type '" + pluginConf.getType() + "' unknown, valid value: "
91                  + listAllPluginIds() );
92          }
93          return plugin.newInstance( pluginConf );
94      }
95  
96      /**
97       * Loads plugins defined by extension mechanism: if defined, <code>plugins</code> property in
98       * <code>logdistiller.properties</code> is the full class name of a concrete implementation of <code>Plugins</code>
99       * class.
100      *
101      * @return List the list of every <code>Plugin</code> defined by loaded <code>Plugins</code>
102      */
103     private static List<Plugin> loadExtensionPlugins()
104     {
105         List<Plugin> plugins = new ArrayList<Plugin>();
106         for ( String pluginsClass : ExtensionHelper.findExtensions( "plugins" ) )
107         {
108             if ( pluginsClass != null )
109             {
110                 plugins.addAll( loadPlugins( pluginsClass ) );
111             }
112         }
113         return plugins;
114     }
115 
116     private static List<Plugin> loadPlugins( String pluginsClass )
117     {
118         try
119         {
120             Plugins plugins = (Plugins) Class.forName( pluginsClass ).newInstance();
121             return plugins.definePlugins();
122         }
123         catch ( ClassNotFoundException cnfe )
124         {
125             throw new RuntimeException( "unable to load plugins class " + pluginsClass, cnfe );
126         }
127         catch ( IllegalAccessException iae )
128         {
129             throw new RuntimeException( "unable to access plugins constructor for class " + pluginsClass, iae );
130         }
131         catch ( InstantiationException ie )
132         {
133             throw new RuntimeException( "unable to instanciate plugins class " + pluginsClass, ie );
134         }
135     }
136 }