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.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 <tool>.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 * <tool>.plugins.table. Default: <tool>.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