KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > driver > text > TextConnection


1 /*
2  * Copyright 2006-2007 The Scriptella Project Team.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package scriptella.driver.text;
17
18 import scriptella.spi.ConnectionParameters;
19 import scriptella.spi.ParametersCallback;
20 import scriptella.spi.ProviderException;
21 import scriptella.spi.QueryCallback;
22 import scriptella.spi.Resource;
23 import scriptella.util.IOUtils;
24
25 import java.io.IOException JavaDoc;
26 import java.io.Reader JavaDoc;
27
28 /**
29  * Represents a connection to a Text file.
30  * <p>For configuration details and examples see <a HREF="package-summary.html">overview page</a>.
31  *
32  * @author Fyodor Kupolov
33  * @version 1.0
34  */

35 public class TextConnection extends AbstractTextConnection {
36     private TextScriptExecutor out;
37
38     /**
39      * For testing purposes only.
40      */

41     protected TextConnection() {
42     }
43
44     public TextConnection(ConnectionParameters parameters) {
45         super(Driver.DIALECT, parameters);
46     }
47
48     public void executeScript(final Resource scriptContent, final ParametersCallback parametersCallback) throws ProviderException {
49         initOut();
50         Reader JavaDoc reader = null;
51         try {
52             reader = scriptContent.open();
53             out.execute(reader, parametersCallback, counter);
54         } catch (IOException JavaDoc e) {
55             throw new TextProviderException("Failed to produce a text file", e);
56         } finally {
57             IOUtils.closeSilently(reader);
58         }
59     }
60
61     /**
62      * Lazily initializes script writer.
63      */

64     protected void initOut() {
65         if (out == null) {
66             try {
67                 out = new TextScriptExecutor(IOUtils.getWriter(IOUtils.getOutputStream(url), encoding), trim, eol);
68             } catch (IOException JavaDoc e) {
69                 throw new TextProviderException("Unable to open file " + url + " for writing", e);
70             }
71         }
72     }
73
74     public void executeQuery(Resource queryContent, ParametersCallback parametersCallback, QueryCallback queryCallback) throws ProviderException {
75         if (out != null) {
76             throw new TextProviderException("Cannot query and update a Text file simultaneously");
77         }
78
79         Reader JavaDoc in;
80         try {
81             in = IOUtils.getReader(url.openStream(), encoding);
82         } catch (IOException JavaDoc e) {
83             throw new TextProviderException("Cannot open a text file for reading", e);
84         }
85         Reader JavaDoc q;
86         try {
87             q = queryContent.open();
88         } catch (IOException JavaDoc e) {
89             throw new TextProviderException("Cannot read a text query", e);
90         }
91
92         TextQueryExecutor tq = null;
93         try {
94             tq = new TextQueryExecutor(q, trim, in , parametersCallback);
95             tq.execute(queryCallback, counter);
96         } finally {
97             if (tq!=null) {
98                 IOUtils.closeSilently(tq);
99             } else {
100                 IOUtils.closeSilently(in);
101             }
102         }
103     }
104
105     public void close() throws ProviderException {
106         IOUtils.closeSilently(out);
107         out = null;
108     }
109 }
110
Popular Tags