KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > services > stream > SingleStream


1 /*
2
3    Derby - Class org.apache.derby.impl.services.stream.SingleStream
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.services.stream;
23
24 import org.apache.derby.iapi.services.stream.InfoStreams;
25 import org.apache.derby.iapi.services.stream.HeaderPrintWriter;
26 import org.apache.derby.iapi.services.stream.PrintWriterGetHeader;
27
28 import org.apache.derby.iapi.services.sanity.SanityManager;
29 import org.apache.derby.iapi.services.monitor.ModuleControl;
30 import org.apache.derby.iapi.services.monitor.ModuleSupportable;
31 import org.apache.derby.iapi.services.monitor.Monitor;
32
33 import org.apache.derby.iapi.reference.Property;
34 import org.apache.derby.iapi.services.property.PropertyUtil;
35
36 import java.io.BufferedOutputStream JavaDoc;
37 import java.io.FileOutputStream JavaDoc;
38 import java.io.IOException JavaDoc;
39 import java.io.OutputStream JavaDoc;
40 import java.io.File JavaDoc;
41 import java.io.Writer JavaDoc;
42
43 import java.util.Properties JavaDoc;
44
45 import java.lang.reflect.Method JavaDoc;
46 import java.lang.reflect.Field JavaDoc;
47 import java.lang.reflect.Modifier JavaDoc;
48 import java.lang.reflect.Member JavaDoc;
49 import java.lang.reflect.InvocationTargetException JavaDoc;
50
51 /**
52  *
53  * The Basic Services provide InfoStreams for reporting
54  * information. Two streams are provided: trace and error.
55  * It is configurable where these streams are directed.
56  * <p>
57  * Errors will be printed to the error stream in addition
58  * to being sent to the client.
59  * <p>
60  * By default both streams are sent to an error log
61  * for the system. When creating a message for a stream,
62  * you can create an initial entry with header information
63  * and then append to it as many times as desired.
64  * <p>
65  * Note: if character encodings are needed, the use of
66  * java.io.*OutputStream's should be replaced with
67  * java.io.*Writer's (assuming the Writer interface
68  * remains stable in JDK1.1)
69  *
70  * @author ames
71  */

72 public final class SingleStream
73 implements InfoStreams, ModuleControl, java.security.PrivilegedAction JavaDoc
74 {
75
76     /*
77     ** Instance fields
78     */

79     private HeaderPrintWriter theStream;
80
81
82     /**
83           The no-arg public constructor for ModuleControl's use.
84      */

85     public SingleStream() {
86     }
87
88     /**
89      * @see org.apache.derby.iapi.services.monitor.ModuleControl#boot
90      */

91     public void boot(boolean create, Properties JavaDoc properties) {
92         theStream = makeStream();
93     }
94
95
96     /**
97      * @see org.apache.derby.iapi.services.monitor.ModuleControl#stop
98      */

99     public void stop() {
100         ((BasicHeaderPrintWriter) theStream).complete();
101     }
102
103     /*
104      * InfoStreams interface
105      */

106
107     /**
108      * @see org.apache.derby.iapi.services.stream.InfoStreams#stream
109      */

110     public HeaderPrintWriter stream() {
111         return theStream;
112     }
113
114     //
115
// class interface
116
//
117

118     /**
119         Make the stream; note that service properties override
120         application and system properties.
121
122      */

123     private HeaderPrintWriter makeStream() {
124
125         // get the header
126
PrintWriterGetHeader header = makeHeader();
127         HeaderPrintWriter hpw = makeHPW(header);
128
129         // If hpw == null then no properties were specified for the stream
130
// so use/create the default stream.
131
if (hpw == null)
132             hpw = createDefaultStream(header);
133         return hpw;
134     }
135
136     /**
137         Return a new header object.
138     */

139     private PrintWriterGetHeader makeHeader() {
140
141         return new BasicGetLogHeader(true, true, (String JavaDoc) null);
142     }
143
144     /**
145         create a HeaderPrintWriter based on the header.
146         Will still need to determine the target type.
147      */

148     private HeaderPrintWriter makeHPW(PrintWriterGetHeader header) {
149
150         // the type of target is based on which property is used
151
// to set it. choices are file, method, field, stream
152

153         String JavaDoc target = PropertyUtil.
154                    getSystemProperty(Property.ERRORLOG_FILE_PROPERTY);
155         if (target!=null)
156             return makeFileHPW(target, header);
157
158         target = PropertyUtil.
159                    getSystemProperty(Property.ERRORLOG_METHOD_PROPERTY);
160         if (target!=null)
161             return makeMethodHPW(target, header);
162
163         target = PropertyUtil.
164                    getSystemProperty(Property.ERRORLOG_FIELD_PROPERTY);
165         if (target!=null)
166             return makeFieldHPW(target, header);
167
168         return null;
169     }
170
171     /**
172         Make a header print writer out of a file name. If it is a relative
173         path name then it is taken as relative to derby.system.home if that is set,
174         otherwise relative to the current directory. If the path name is absolute
175         then it is taken as absolute.
176     */

177     private HeaderPrintWriter PBmakeFileHPW(String JavaDoc fileName,
178                                             PrintWriterGetHeader header) {
179
180         boolean appendInfoLog = PropertyUtil.getSystemBoolean(Property.LOG_FILE_APPEND);
181
182         File JavaDoc streamFile = new File JavaDoc(fileName);
183
184         // See if this needs to be made relative to something ...
185
if (!streamFile.isAbsolute()) {
186             Object JavaDoc monitorEnv = Monitor.getMonitor().getEnvironment();
187             if (monitorEnv instanceof File JavaDoc)
188                 streamFile = new File JavaDoc((File JavaDoc) monitorEnv, fileName);
189         }
190
191         FileOutputStream JavaDoc fos;
192
193         try {
194
195             if (streamFile.exists() && appendInfoLog)
196                 fos = new FileOutputStream JavaDoc(streamFile.getPath(), true);
197             else
198                 fos = new FileOutputStream JavaDoc(streamFile);
199         } catch (IOException JavaDoc ioe) {
200             return useDefaultStream(header, ioe);
201         } catch (SecurityException JavaDoc se) {
202             return useDefaultStream(header, se);
203         }
204
205         return new BasicHeaderPrintWriter(new BufferedOutputStream JavaDoc(fos), header,
206             true, streamFile.getPath());
207     }
208
209     private HeaderPrintWriter makeMethodHPW(String JavaDoc methodInvocation,
210                                             PrintWriterGetHeader header) {
211
212         int lastDot = methodInvocation.lastIndexOf('.');
213         String JavaDoc className = methodInvocation.substring(0, lastDot);
214         String JavaDoc methodName = methodInvocation.substring(lastDot+1);
215
216         Throwable JavaDoc t;
217         try {
218             Class JavaDoc theClass = Class.forName(className);
219
220             try {
221                 Method JavaDoc theMethod = theClass.getMethod(methodName, new Class JavaDoc[0]);
222
223                 if (!Modifier.isStatic(theMethod.getModifiers())) {
224                     HeaderPrintWriter hpw = useDefaultStream(header);
225                     hpw.printlnWithHeader(theMethod.toString() + " is not static");
226                     return hpw;
227                 }
228
229                 try {
230                     return makeValueHPW(theMethod, theMethod.invoke((Object JavaDoc) null,
231                         new Object JavaDoc[0]), header, methodInvocation);
232                 } catch (IllegalAccessException JavaDoc iae) {
233                     t = iae;
234                 } catch (IllegalArgumentException JavaDoc iarge) {
235                     t = iarge;
236                 } catch (InvocationTargetException JavaDoc ite) {
237                     t = ite.getTargetException();
238                 }
239
240             } catch (NoSuchMethodException JavaDoc nsme) {
241                 t = nsme;
242             }
243         } catch (ClassNotFoundException JavaDoc cnfe) {
244             t = cnfe;
245         } catch (SecurityException JavaDoc se) {
246             t = se;
247             
248         }
249         return useDefaultStream(header, t);
250
251     }
252
253
254     private HeaderPrintWriter makeFieldHPW(String JavaDoc fieldAccess,
255                                             PrintWriterGetHeader header) {
256
257         int lastDot = fieldAccess.lastIndexOf('.');
258         String JavaDoc className = fieldAccess.substring(0, lastDot);
259         String JavaDoc fieldName = fieldAccess.substring(lastDot+1,
260                               fieldAccess.length());
261
262         Throwable JavaDoc t;
263         try {
264             Class JavaDoc theClass = Class.forName(className);
265
266             try {
267                 Field JavaDoc theField = theClass.getField(fieldName);
268         
269                 if (!Modifier.isStatic(theField.getModifiers())) {
270                     HeaderPrintWriter hpw = useDefaultStream(header);
271                     hpw.printlnWithHeader(theField.toString() + " is not static");
272                     return hpw;
273                 }
274
275                 try {
276                     return makeValueHPW(theField, theField.get((Object JavaDoc) null),
277                         header, fieldAccess);
278                 } catch (IllegalAccessException JavaDoc iae) {
279                     t = iae;
280                 } catch (IllegalArgumentException JavaDoc iarge) {
281                     t = iarge;
282                 }
283
284             } catch (NoSuchFieldException JavaDoc nsfe) {
285                 t = nsfe;
286             }
287         } catch (ClassNotFoundException JavaDoc cnfe) {
288             t = cnfe;
289         } catch (SecurityException JavaDoc se) {
290             t = se;
291         }
292         return useDefaultStream(header, t);
293
294         /*
295             If we decide it is a bad idea to use reflect and need
296             an alternate implementation, we can hard-wire those
297             fields that we desire to give configurations access to,
298             like so:
299
300         if ("java.lang.System.out".equals(fieldAccess))
301             os = System.out;
302         else if ("java.lang.System.err".equals(fieldAccess))
303             os = System.err;
304         */

305     }
306
307     private HeaderPrintWriter makeValueHPW(Member JavaDoc whereFrom, Object JavaDoc value,
308         PrintWriterGetHeader header, String JavaDoc name) {
309
310         if (value instanceof OutputStream JavaDoc)
311              return new BasicHeaderPrintWriter((OutputStream JavaDoc) value, header, false, name);
312         else if (value instanceof Writer)
313              return new BasicHeaderPrintWriter((Writer) value, header, false, name);
314         
315         HeaderPrintWriter hpw = useDefaultStream(header);
316
317         if (value == null)
318             hpw.printlnWithHeader(whereFrom.toString() + "=null");
319         else
320             hpw.printlnWithHeader(whereFrom.toString() + " instanceof " + value.getClass().getName());
321
322         return hpw;
323     }
324  
325
326     /**
327         Used when no configuration information exists for a stream.
328     */

329     private HeaderPrintWriter createDefaultStream(PrintWriterGetHeader header) {
330         return makeFileHPW("derby.log", header);
331     }
332
333     /**
334         Used when creating a stream creates an error.
335     */

336     private HeaderPrintWriter useDefaultStream(PrintWriterGetHeader header) {
337
338         return new BasicHeaderPrintWriter(System.err, header, false, "System.err");
339     }
340
341     private HeaderPrintWriter useDefaultStream(PrintWriterGetHeader header, Throwable JavaDoc t) {
342
343         HeaderPrintWriter hpw = useDefaultStream(header);
344         hpw.printlnWithHeader(t.toString());
345         return hpw;
346     }
347
348     /*
349     ** Priv block code, moved out of the old Java2 version.
350     */

351
352     private String JavaDoc PBfileName;
353     private PrintWriterGetHeader PBheader;
354
355     private HeaderPrintWriter makeFileHPW(String JavaDoc fileName, PrintWriterGetHeader header)
356     {
357         this.PBfileName = fileName;
358         this.PBheader = header;
359         return (HeaderPrintWriter) java.security.AccessController.doPrivileged(this);
360     }
361
362
363     public final Object JavaDoc run()
364     {
365         // SECURITY PERMISSION - OP4, OP5
366
return PBmakeFileHPW(PBfileName, PBheader);
367     }
368 }
369
370
Popular Tags