KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > cli > framework > InputsAndOutputs


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
24 package com.sun.enterprise.cli.framework;
25
26 import java.io.OutputStream JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.io.FileOutputStream JavaDoc;
29 import java.io.FileInputStream JavaDoc;
30 import java.io.IOException JavaDoc;
31
32 /**
33     A class which stores the inputs and outputs for the asadmin-specific
34     information.
35  */

36 public class InputsAndOutputs
37 {
38
39     
40     private static InputsAndOutputs sIO = null;
41     private IUserInput userInput = new UserInput( System.in );
42     private IUserOutput userOutputImpl = new UserOutputImpl( System.out, false );
43     private IErrorOutput errorOutputImpl = new ErrorOutputImpl( System.err, false );
44
45     /**
46         Default constructor.
47     */

48     private InputsAndOutputs()
49     {
50     }
51
52
53     /**
54        Singleton method that sets the instance of InputsAndOutputs
55     */

56     public static void setInstance(InputsAndOutputs iao)
57     {
58         if (iao == null) {
59             if (sIO!=null) {
60                 try {
61                     sIO.userOutputImpl.close();
62                     sIO.userInput.close();
63                 }
64                 catch (Exception JavaDoc e){
65                     e.printStackTrace();
66                 }
67             }
68             sIO = new InputsAndOutputs();
69             return;
70         }
71         if (iao != null && sIO != iao)
72             sIO = iao;
73     }
74
75
76     /**
77        Singleton method that returns an instance of InputsAndOutputs
78     */

79     public static InputsAndOutputs getInstance()
80     {
81     if (sIO == null)
82     {
83         sIO = new InputsAndOutputs();
84     }
85     return sIO;
86     }
87
88
89     /**
90     Returns the userOutputImpl class object.
91     */

92     public IUserOutput getUserOutput()
93     {
94     return this.userOutputImpl;
95     }
96
97
98     /**
99     Creates and sets the userOutputImpl class with the given OutputStream
100     object.
101
102     @param os is the OutputStream object to be set.
103     */

104     public void setUserOutput( OutputStream JavaDoc os )
105     {
106     this.userOutputImpl.close();
107     this.userOutputImpl = new UserOutputImpl( os, false );
108     }
109
110
111     /**
112      Set the output file name and force new output to this file
113      immediately.
114
115      Any existing output will be flushed and closed.
116      @param fileName is file in the file system to be set.
117      @throws IOException
118     */

119     public void setUserOutputFile( String JavaDoc fileName ) throws IOException JavaDoc
120     {
121         FileOutputStream JavaDoc userOutputFile = new FileOutputStream JavaDoc( fileName );
122         this.userOutputImpl.close();
123         this.userOutputImpl = new UserOutputImpl( userOutputFile, true );
124     }
125
126
127     /**
128     Returns the userInput object that is set in InputsAndOutputs.
129     */

130     public IUserInput getUserInput()
131     {
132         return this.userInput;
133     }
134
135
136     /**
137     Sets the InputStream object in the InputsAndOutputs.
138     @param userInput is the UserInput object to be set.
139     @throws IOException
140     */

141     public void setUserInput( InputStream JavaDoc is ) throws IOException JavaDoc
142     {
143         userInput.close();
144         this.userInput = new UserInput(is);
145     }
146
147
148     /**
149      * Sets the InputStream object in the InputsAndOutputs with specified
150      * character set.
151      * @param userInput is the UserInput object to be set.
152      * @throws IOException
153      */

154     public void setUserInput( InputStream JavaDoc is, String JavaDoc sEncoding )
155     throws IOException JavaDoc
156     {
157         userInput.close();
158         this.userInput = new UserInput(is);
159         this.userInput.setEncoding(sEncoding);
160     }
161
162
163     /**
164     Sets the userInput object in the InputsAndOutputs.
165     @param userInput is the UserInput object to be set.
166     @throws IOException
167     */

168     public void setUserInput( IUserInput userInput )
169         throws IOException JavaDoc
170     {
171         this.userInput.close();
172         this.userInput = userInput;
173     }
174
175
176     /**
177      Set the input to a file name
178
179      @param fileName is file in the file system to be set.
180      @throws IOException
181     */

182     public void setUserInputFile( String JavaDoc fileName ) throws IOException JavaDoc
183     {
184         final FileInputStream JavaDoc userInputFile = new FileInputStream JavaDoc( fileName );
185         setUserInput(userInputFile);
186     }
187
188
189     /**
190      Set the input to a file name with specified encoding
191
192      @param fileName is file in the file system to be set.
193      @throws IOException
194     */

195     public void setUserInputFile( String JavaDoc fileName, String JavaDoc sEncoding )
196         throws IOException JavaDoc
197     {
198         final FileInputStream JavaDoc userInputFile = new FileInputStream JavaDoc( fileName );
199         setUserInput(userInputFile, sEncoding);
200     }
201
202
203     /**
204     Returns the errorOutputImpl class object.
205     */

206     public IErrorOutput getErrorOutput()
207     {
208         return this.errorOutputImpl;
209     }
210
211
212     /**
213     Creates and sets the errorOutputImpl class with the given OutputStream
214     object.
215
216     @param os is the OutputStream object to be set.
217     */

218     public void setErrorOutput( OutputStream JavaDoc os )
219     {
220         this.errorOutputImpl.close();
221         this.errorOutputImpl = new ErrorOutputImpl( os, false );
222     }
223
224
225     /**
226      Set the output file name and force new output to this file
227      immediately.
228
229      Any existing output will be flushed and closed.
230      @param fileName is file in the file system to be set.
231      @throws IOException
232     */

233     public void setErrorOutputFile( String JavaDoc fileName ) throws IOException JavaDoc
234     {
235         FileOutputStream JavaDoc errorOutputFile = new FileOutputStream JavaDoc( fileName );
236         this.errorOutputImpl.close();
237         this.errorOutputImpl = new ErrorOutputImpl( errorOutputFile, true );
238     }
239
240 }
241
Popular Tags