View Javadoc

1   /*
2    * Copyright (c) 2001-2004,
3    * RedVerst Group, ISP RAS http://www.ispras.ru
4    * All rights reserved.
5    *
6    * Redistribution and use in source and binary forms, with or without
7    * modification, are permitted provided that the following conditions are met:
8    *
9    * 1. Redistributions of source code must retain the above copyright notice, this
10   *    list of conditions and the following disclaimer.
11   *
12   * 2. Redistributions in binary form must reproduce the above copyright notice,
13   *    this list of conditions and the following disclaimer in the documentation
14   *    and/or other materials provided with the distribution.
15   *
16   * 3. The names "ATP", "TreeDL", "RedVerst", "ISP RAS"
17   *    may not be used to endorse or promote products derived from this software
18   *    without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
21   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
24   * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
27   * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30   */
31  
32  package com.unitesk.atp.tool;
33  
34  import java.io.InputStream;
35  import java.io.IOException;
36  import java.util.Iterator;
37  import java.util.List;
38  import java.util.Properties;
39  
40  import com.unitesk.atp.messages.MessageBox;
41  import com.unitesk.atp.text.localize.StringManager;
42  
43  /***
44   * Plugin manager default implementation.
45   *
46   * @author <A href="mailto:demakov@ispras.ru">Alexey Demakov</A>
47   * @version $Id: PluginManagerClass.java,v 1.3 2004/10/11 15:00:44 all-x Exp $
48   */
49  public class PluginManagerClass implements PluginManager
50  {
51      /***
52       * Plugin manager name.
53       */
54      protected String name;
55  
56      /***
57       * The message box for application messages.
58       */
59      protected MessageBox mbox;
60  
61      /***
62       * The set of application properties. Can be inited as constructor
63       * parameter or by overloading {@link #initConfig()} method.
64       */
65      protected Properties config = new Properties();
66  
67      /***
68       * The table of plugins initialized from resource file. Can be inited as
69       * constructor parameter or by overloading {@link #initPluginsTable()}
70       * method.
71       */
72      protected DynamicVariablesTable table;
73  
74      /***
75       * Constructs plugin manager with the specified parameters.
76       *
77       * @param  name    the plugin manager name
78       * @param  mbox    the plugin manager message box.
79       *                 If it is <code>null</code> no message box will be
80       *                 available for plugins.
81       * @param  config  the set of properties. It can be <code>null</code>.
82       * @param  in      the input stream to read plugin table from.
83       */
84      public PluginManagerClass( String name
85                               , MessageBox mbox
86                               , Properties config
87                               , InputStream in
88                               ) throws IOException, ClassNotFoundException
89      {
90          this.name = name;
91          this.mbox = mbox;
92          this.config = config != null ? config : new Properties();
93          loadPlugins( in );
94      }
95  
96      /***
97       * Default constructor. Calls {@link #initName()}, {@link #initConfig},
98       * {@link #initMessageBox()}, {@link #initPluginsTable}.
99       * These methods should be overriden by inheritor if it use this
100      * constructor.
101      */
102     public PluginManagerClass()
103     {
104         initName();
105         initConfig();
106         initMessageBox();
107         initPluginsTable();
108     }
109 
110     /*
111      * Init plugin manager name.
112      * This method should be overriden by inheritor class if it use plugin
113      * manager constructor without parameters.
114      *
115      * @see #name
116      */
117     protected void initName()
118     {
119         throw new UnsupportedOperationException();
120     }
121 
122     /*
123      * Init plugin manager properties set.
124      * This method should be overriden by inheritor class if it use plugin
125      * manager constructor without parameters.
126      *
127      * @see #config
128      */
129     protected void initConfig()
130     {
131         throw new UnsupportedOperationException();
132     }
133 
134     /*
135      * Init plugin manager message box.
136      * This method should be overriden by inheritor class if it use plugin
137      * manager constructor without parameters.
138      *
139      * @see #mbox
140      */
141     protected void initMessageBox()
142     {
143         throw new UnsupportedOperationException();
144     }
145 
146     /*
147      * Init plugin manager plugins table.
148      * This method should be overriden by inheritor class if it use plugin
149      * manager constructor without parameters.
150      *
151      * @see #table
152      */
153     protected void initPluginsTable()
154     {
155         throw new UnsupportedOperationException();
156     }
157 
158     /***
159      * {@inheritDoc}
160      */
161     public String getName() { return name; }
162 
163     /***
164      * {@inheritDoc}
165      */
166     public MessageBox getMessageBox() { return mbox; }
167 
168     /***
169      * {@inheritDoc}
170      */
171     public Object getPlugin( String name )
172     {
173         return table.get( name );
174     }
175 
176     /***
177      * Get property value for the specified property name.
178      * The default implementation of plugin manager searches in {@link #config}
179      * properties set then in system properties.
180      *
181      * @param  name    the specified property name.
182      * @return         property value or <code>null</code> if no such property
183      *                 specified.
184      */
185     public final String getProperty( String name )
186     {
187         String v = config.getProperty( name );
188 
189         if( v != null ) return v;
190 
191         return System.getProperty( name );
192     }
193 
194     /***
195      * Get property value for the specified property name.
196      * The default implementation of plugin manager searches in {@link #config}
197      * properties set then in system properties.
198      *
199      * @param  name    the specified property name.
200      * @param  value   the default property value.
201      * @return         property value or <code>value</code> if no such property
202      *                 specified.
203      */
204     public final String getProperty( String name, String value )
205     {
206         String v = config.getProperty( name );
207 
208         if( v != null ) return v;
209 
210         return System.getProperty( name, value );
211     }
212 
213     /***
214      * {@inheritDoc}
215      */
216     public String getPluginPropertyName( Object plugin, String name )
217     {
218         return table.getPropertyName( plugin, name );
219     }
220 
221     /***
222      * {@inheritDoc}
223      */
224     public String getPluginProperty( Object plugin, String name )
225     {
226         return getProperty( table.getPropertyName( plugin, name ) );
227     }
228 
229     /***
230      * {@inheritDoc}
231      */
232     public String getPluginProperty( Object plugin, String name, String value )
233     {
234         return getProperty( table.getPropertyName( plugin, name ), value );
235     }
236 
237     /***
238      * Creates plugins table and set {@link #table} field.
239      */
240     protected void createPluginsTable()
241     {
242         table = new DynamicVariablesTable( name );
243     }
244 
245     /***
246      * Loads plugins from the specified input stream.
247      * If plugins table is not created yet call {@link #createPluginsTable()}.
248      *
249      * @param in            the input stream to load plugins from.
250      * @throws IOException  if the was I/O error while reading input stream.
251      * @throws ClassNotFoundException
252      *                      if class specified in plugin table was not fount.
253      * @throws NullPointerException
254      *                      if <code>in</code> is <code>null</code>.
255      */
256     protected void loadPlugins( InputStream in )
257         throws IOException, ClassNotFoundException
258     {
259         if( in == null ) throw new NullPointerException( "in" );
260 
261         if( table == null ) createPluginsTable();
262 
263         table.load( in );
264 
265         Iterator names = table.names().iterator();
266 
267         while( names.hasNext() )
268         {
269             String name = (String)names.next();
270             Object obj = table.get( name );
271 
272             if( obj instanceof Plugin )
273             {
274                 Plugin p = ((Plugin)obj);
275                 if( p.getName() == null ) p.init( this, name );
276             }
277         }
278     }
279 
280     /***
281      * Prints help messages for plugins.
282      *
283      * @param pluginNames      the list of plugin names
284      *                         If pluginNames is null
285      *                         or pluginNames.size() is 0
286      *                         prints the list of available plugins
287      *                         otherwise - help message for each plugin
288      */
289     protected void help( List/*String*/ pluginNames )
290     {
291         StringBuffer sb = new StringBuffer();
292         StringManager sm = mbox.getStringManager();
293 
294         if(    pluginNames == null
295             || pluginNames.size() == 0
296           )
297         {
298             sb.append( endl );
299             sb.append( sm.getString( PluginManagerClass.class
300                                    , "defined_names"
301                                    , null
302                                    )
303                      );
304 
305             Iterator names = table.names().iterator();
306 
307             while( names.hasNext() )
308             {
309                 String name = (String)names.next();
310                 Object obj = table.get( name );
311 
312                 sb.append( name );
313 
314                 if( obj instanceof Plugin )
315                 {
316                     sb.append( ", " );
317                     sb.append( ((Plugin)obj).getVersion() );
318                     sb.append( " - " );
319                     sb.append( ((Plugin)obj).getKind() );
320                 }
321                 sb.append( endl );
322             }
323         } else {
324             for ( int i = 0; i < pluginNames.size(); i++ )
325             {
326                 String name = (String)pluginNames.get( i );
327                 Object obj = table.get( name );
328 
329                 sb.append( endl );
330                 if( obj == null )
331                 {
332                     sb.append( sm.getString( PluginManagerClass.class
333                                            , "name_not_defined"
334                                            , null
335                                            )
336                              );
337                     sb.append( name );
338                 } else {
339                     sb.append( name );
340                     if( obj instanceof Plugin )
341                     {
342                         sb.append( ", " );
343                         sb.append( ((Plugin)obj).getVersion() );
344                     }
345                     sb.append( ":" );
346                     sb.append( endl );
347 
348                     String pluginHelp = mbox.getStringManager().getString( "help", obj );
349                     if( pluginHelp != null )
350                     {
351                         sb.append( pluginHelp );
352                     } else {
353                         sb.append( sm.getString( PluginManagerClass.class
354                                                , "help_not_available"
355                                                , null
356                                                )
357                                  );
358                     }
359                     sb.append( endl );
360                 }
361             }
362         }
363         mbox.info( sb.toString() );
364     }
365 
366     private static final String endl = System.getProperty( "line.separator" );
367 }