KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > appserv > management > util > misc > FileOutput


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.appserv.management.util.misc;
24
25 import java.io.File JavaDoc;
26 import java.io.FileOutputStream JavaDoc;
27 import java.io.PrintStream JavaDoc;
28 import java.io.FileNotFoundException JavaDoc;
29
30 /**
31     Directs output to a file. Lazy initialization; the file
32     is not actually opened until output is sent.
33  */

34 public final class FileOutput implements Output
35 {
36     private PrintStream JavaDoc mOut;
37     private final File JavaDoc mFile;
38     private final boolean mAppend;
39     
40         public
41     FileOutput( final File JavaDoc f )
42     {
43         this( f, false );
44     }
45     
46         public
47     FileOutput( final File JavaDoc f, boolean append )
48     {
49         mOut = null;
50         mFile = f;
51         mAppend = append;
52     }
53     
54         private void
55     lazyInit()
56     {
57         if ( mOut == null ) synchronized( this )
58         {
59             if ( mOut == null )
60             {
61                 try
62                 {
63                     mOut = new PrintStream JavaDoc( new FileOutputStream JavaDoc( mFile, mAppend) );
64                 }
65                 catch( Exception JavaDoc e )
66                 {
67                     // don't use System.out/err; possible infinite recursion
68
throw new RuntimeException JavaDoc( "Can't create file: " + mFile +
69                         ", exception = " + e );
70                 }
71             }
72         }
73     }
74     
75         public void
76     print( final Object JavaDoc o )
77     {
78         lazyInit();
79         mOut.print( o.toString() );
80     }
81     
82         public void
83     println( Object JavaDoc o )
84     {
85         lazyInit();
86         mOut.println( o.toString() );
87     }
88     
89         public void
90     printError( final Object JavaDoc o )
91     {
92         lazyInit();
93         println( "ERROR: " + o );
94     }
95     
96         public boolean
97     getDebug()
98     {
99         lazyInit();
100         return( false );
101     }
102     
103         public void
104     printDebug( final Object JavaDoc o )
105     {
106         lazyInit();
107         println( "DEBUG: " + o );
108     }
109     
110     
111         public void
112     close( )
113     {
114         if ( mOut != null )
115         {
116             try
117             {
118                 mOut.close();
119             }
120             finally
121             {
122                 mOut = null;
123             }
124         }
125     }
126 };
127
128
129
Popular Tags