1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
112
113
114
115
116
117 protected void initName()
118 {
119 throw new UnsupportedOperationException();
120 }
121
122
123
124
125
126
127
128
129 protected void initConfig()
130 {
131 throw new UnsupportedOperationException();
132 }
133
134
135
136
137
138
139
140
141 protected void initMessageBox()
142 {
143 throw new UnsupportedOperationException();
144 }
145
146
147
148
149
150
151
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
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 }