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 definePlugins();
42  
43      public static final List ALL;
44  
45      public static final Map MAP;
46      static
47      {
48          List all = new ArrayList( new BasePlugins().definePlugins() );
49          all.addAll( loadExtensionPlugins() );
50          ALL = Collections.unmodifiableList( all );
51  
52          Map map = new HashMap();
53          Iterator iter = ALL.iterator();
54          while ( iter.hasNext() )
55          {
56              Plugin plugin = (Plugin) iter.next();
57              map.put( plugin.getId(), plugin );
58          }
59          MAP = Collections.unmodifiableMap( map );
60      }
61  
62      /**
63       * get all the plugins defined (predefined and custom).
64       */
65      public static List getAllPlugins()
66      {
67          return ALL;
68      }
69  
70      public static String listAllPluginIds()
71      {
72          return FormatUtil.join( ", ", MAP.keySet().iterator() );
73      }
74  
75      public static Plugin getPlugin( String id )
76      {
77          return (Plugin) MAP.get( id );
78      }
79  
80      /**
81       * Instanciates a new distillation plugin from its distiller configuration.
82       *
83       * @param pluginConf the plugin distiller configuration
84       * @return the distillation plugin
85       * @throws PluginConfigException if the plugin type defined in the configuration does not exist
86       */
87      public static LogDistillation.Plugin newInstance( LogDistiller.Plugin pluginConf )
88      {
89          Plugin plugin = getPlugin( pluginConf.getType() );
90          if ( plugin == null )
91          {
92              throw new PluginConfigException( "plugin type '" + pluginConf.getType() + "' unknown, valid value: "
93                  + listAllPluginIds() );
94          }
95          return plugin.newInstance( pluginConf );
96      }
97  
98      /**
99       * Loads plugins defined by extension mechanism: if defined, <code>plugins</code> property in
100      * <code>logdistiller.properties</code> is the full class name of a concrete implementation of <code>Plugins</code>
101      * class.
102      *
103      * @return List the list of every <code>Plugin</code> defined by loaded <code>Plugins</code>
104      */
105     private static List loadExtensionPlugins()
106     {
107         Iterator iter = ExtensionHelper.findExtensions( "plugins" ).iterator();
108         List plugins = new ArrayList();
109         while ( iter.hasNext() )
110         {
111             String pluginsClass = (String) iter.next();
112             if ( pluginsClass != null )
113             {
114                 plugins.addAll( loadPlugins( pluginsClass ) );
115             }
116         }
117         return plugins;
118     }
119 
120     private static List loadPlugins( String pluginsClass )
121     {
122         try
123         {
124             Plugins plugins = (Plugins) Class.forName( pluginsClass ).newInstance();
125             return plugins.definePlugins();
126         }
127         catch ( ClassNotFoundException cnfe )
128         {
129             throw new RuntimeException( "unable to load plugins class " + pluginsClass, cnfe );
130         }
131         catch ( IllegalAccessException iae )
132         {
133             throw new RuntimeException( "unable to access plugins constructor for class " + pluginsClass, iae );
134         }
135         catch ( InstantiationException ie )
136         {
137             throw new RuntimeException( "unable to instanciate plugins class " + pluginsClass, ie );
138         }
139     }
140 }