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.io.File;
37  import java.io.FileInputStream;
38  import java.io.FileNotFoundException;
39  
40  import java.util.Properties;
41  
42  import com.unitesk.atp.messages.*;
43  
44  /***
45   * The framework of an application with plugins.
46   *
47   * @author <A href="mailto:demakov@ispras.ru">Alexey Demakov</A>
48   * @version $Id: Tool.java,v 1.4 2005/07/31 19:58:29 all-x Exp $
49   */
50  public abstract class Tool extends PluginManagerClass
51  {
52      protected String version;
53  
54      protected String properties_file_property_name;
55      protected String default_properties_file_name;
56      protected String properties_file_name;
57      protected FileStatus properties_file_status;
58  
59      protected String plugins_file_property_name;
60      protected String default_plugins_file_name;
61      protected String plugins_file_name;
62      protected FileStatus plugins_file_status;
63  
64      protected String mbox_class_property_name;
65      protected String default_mbox_class;
66  
67      /***
68       * Returns tool version.
69       */
70      public final String getVersion()
71      {
72          return version;
73      }
74  
75      /***
76       * In addition to {@link #name} initialization this method should initialize
77       * {@link #version}
78       */
79      protected abstract void initName();
80  
81      /***
82       * {@inheritDoc}
83       */
84      protected void initConfig()
85      {
86          properties_file_property_name = name + ".properties.file";
87          default_properties_file_name = name + ".properties";
88  
89          properties_file_name
90              = System.getProperty( properties_file_property_name
91                                  , default_properties_file_name
92                                  );
93          properties_file_status = NotFoundFileStatus.status;
94  
95          config = new Properties();
96  
97          try
98          {
99              InputStream in = findFile( properties_file_name );
100 
101             if( in != null )
102             {
103                 config.load( in );
104                 properties_file_status = FoundFileStatus.status;
105             }
106         }
107         catch( IOException e )
108         {
109             mbox = new PrintStreamMessageBox();
110             mbox.fatal( new IOErrorMessage( new File( properties_file_name ) ) );
111         }
112     }
113 
114     /***
115      * Initializes tool message box by instance of class with name specified
116      * by property &lt;tool&gt;.mbox.class.
117      * Default: {@link com.unitesk.atp.messages.PrintStreamMessageBox}.
118      * Class should have constructor without parameters.
119      */
120     protected void initMessageBox()
121     {
122         mbox_class_property_name = name + ".mbox.class";
123         default_mbox_class
124             = "com.unitesk.atp.messages.PrintStreamMessageBox";
125         String mbox_class
126             = getProperty( mbox_class_property_name, default_mbox_class );
127         try
128         {
129             mbox = (MessageBox)Class.forName( mbox_class ).newInstance();
130         }
131         catch( Exception e )
132         {
133             mbox = new PrintStreamMessageBox();
134             mbox.fatal( new NameNotFoundMessage
135                             ( null
136                             , null
137                             ,  mbox
138                               .getStringManager()
139                               .getString( Tool.class, "message.class" )
140                             , mbox_class
141                             )
142                       );
143         }
144     }
145 
146     /***
147      * Initializes tool plugins table from file with name specified by property
148      * &lt;tool&gt;.plugins.table. Default: &lt;tool&gt;.plugins.
149      * If file not found sends warning message and creates empty table.
150      */
151     protected void initPluginsTable()
152     {
153         plugins_file_property_name = name + ".plugins.file";
154         default_plugins_file_name = name + ".plugins";
155 
156         plugins_file_name = getProperty( plugins_file_property_name
157                                        , default_plugins_file_name
158                                        );
159         plugins_file_status = NotFoundFileStatus.status;
160 
161         try
162         {
163             InputStream in = findFile( plugins_file_name );
164             if( in != null )
165             {
166                 loadPlugins( in );
167                 plugins_file_status = FoundFileStatus.status;
168             } else {
169                 mbox.warning( new FileNotFoundMessage
170                                   ( new File( plugins_file_name ) )
171                             );
172                 createPluginsTable();
173                 return;
174             }
175         }
176         catch( IOException e )
177         {
178             mbox.fatal( new IOErrorMessage( new File( plugins_file_name ) ) );
179         }
180         catch( ClassNotFoundException e )
181         {
182             mbox.fatal( new NameNotFoundMessage
183                             ( new File( plugins_file_name )
184                             , null
185                             ,  mbox
186                               .getStringManager()
187                               .getString( Plugin.class, "message.plugin_class" )
188                             , e.getMessage()
189                             )
190                       );
191         }
192     }
193 
194     /***
195      * Prints message describing tool usage.
196      */
197     protected final void usage()
198     {
199         mbox.info(  mbox
200                    .getStringManager()
201                    .getString( com.unitesk.atp.tool.Tool.class
202                              , "usage"
203                              , this
204                              )
205                  );
206     }
207 
208     /***
209      * Finds file or resource.
210      * If there is the specified file in working directory - returns input stream for it.
211      * Otherwise treats parameter as name of resource and returns
212      * input stream for resource or null if the resource could not be found.
213      * The following order of class loaders is used to find the resource:
214      * <OL>
215      * <LI>Current thread's class loader.</LI>
216      * <LI>Class loader of this class.</LI>
217      * <LI>System class loader</LI>
218      * </OL>
219      *
220      * @param  name    The name of file.
221      * @return         input stream for reading file or resource.
222      */
223     public static InputStream findFile( String name )
224     {
225         try
226         {
227             return new FileInputStream( name );
228         }
229         catch( FileNotFoundException e )
230         {
231             ClassLoader contextCL =  Thread
232                                     .currentThread()
233                                    .getContextClassLoader();
234             InputStream res;
235             if( contextCL != null )
236             {
237                 res = contextCL.getResourceAsStream( name );
238                 if( res != null ) return res;
239             }
240             res = Tool.class.getClassLoader().getResourceAsStream( name );
241             if( res != null ) return res;
242             return ClassLoader.getSystemClassLoader().getResourceAsStream( name );
243         }
244     }
245 }
246