001/*
002 * HA-JDBC: High-Availability JDBC
003 * Copyright (C) 2013  Paul Ferraro
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Lesser General Public License as published by
007 * the Free Software Foundation, either version 3 of the License, or
008 * (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU Lesser General Public License for more details.
014 *
015 * You should have received a copy of the GNU Lesser General Public License
016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017 */
018package net.sf.hajdbc.sql.io;
019
020import java.io.IOException;
021import java.io.Writer;
022
023import net.sf.hajdbc.Database;
024import net.sf.hajdbc.invocation.InvocationStrategies;
025import net.sf.hajdbc.invocation.Invoker;
026
027/**
028 * Static proxy for writers.
029 * @author Paul Ferraro
030 * @param <Z> connection source
031 * @param <D> database
032 * @param <P> parent type
033 */
034public class WriterProxy<Z, D extends Database<Z>, P> extends Writer
035{
036        private final WriterProxyFactory<Z, D, P> factory;
037        
038        public WriterProxy(WriterProxyFactory<Z, D, P> factory)
039        {
040                this.factory = factory;
041        }
042        
043        @Override
044        public void write(final int c) throws IOException
045        {
046                WriterInvoker<Z, D, Void> invoker = new WriterInvoker<Z, D, Void>()
047                {
048                        @Override
049                        public Void invoke(D database, Writer writer) throws IOException
050                        {
051                                writer.write(c);
052                                return null;
053                        }
054                };
055                
056                InvocationStrategies.INVOKE_ON_EXISTING.invoke(this.factory, invoker);
057                
058                this.factory.record(invoker);
059        }
060
061        @Override
062        public void write(final char[] cbuf) throws IOException
063        {
064                WriterInvoker<Z, D, Void> invoker = new WriterInvoker<Z, D, Void>()
065                {
066                        @Override
067                        public Void invoke(D database, Writer writer) throws IOException
068                        {
069                                writer.write(cbuf);
070                                return null;
071                        }
072                };
073                
074                InvocationStrategies.INVOKE_ON_EXISTING.invoke(this.factory, invoker);
075                
076                this.factory.record(invoker);
077        }
078
079        @Override
080        public void write(final String str) throws IOException
081        {
082                WriterInvoker<Z, D, Void> invoker = new WriterInvoker<Z, D, Void>()
083                {
084                        @Override
085                        public Void invoke(D database, Writer writer) throws IOException
086                        {
087                                writer.write(str);
088                                return null;
089                        }
090                };
091                
092                InvocationStrategies.INVOKE_ON_EXISTING.invoke(this.factory, invoker);
093                
094                this.factory.record(invoker);
095        }
096
097        @Override
098        public void write(final String str, final int off, final int len) throws IOException
099        {
100                WriterInvoker<Z, D, Void> invoker = new WriterInvoker<Z, D, Void>()
101                {
102                        @Override
103                        public Void invoke(D database, Writer writer) throws IOException
104                        {
105                                writer.write(str, off, len);
106                                return null;
107                        }
108                };
109                
110                InvocationStrategies.INVOKE_ON_EXISTING.invoke(this.factory, invoker);
111                
112                this.factory.record(invoker);
113        }
114
115        @Override
116        public Writer append(final CharSequence csq) throws IOException
117        {
118                WriterInvoker<Z, D, Writer> invoker = new WriterInvoker<Z, D, Writer>()
119                {
120                        @Override
121                        public Writer invoke(D database, Writer writer) throws IOException
122                        {
123                                return writer.append(csq);
124                        }
125                };
126                
127                InvocationStrategies.INVOKE_ON_EXISTING.invoke(this.factory, invoker);
128                
129                this.factory.record(invoker);
130                
131                return this;
132        }
133
134        @Override
135        public Writer append(final CharSequence csq, final int start, final int end) throws IOException
136        {
137                WriterInvoker<Z, D, Writer> invoker = new WriterInvoker<Z, D, Writer>()
138                {
139                        @Override
140                        public Writer invoke(D database, Writer writer) throws IOException
141                        {
142                                return writer.append(csq, start, end);
143                        }
144                };
145                
146                InvocationStrategies.INVOKE_ON_EXISTING.invoke(this.factory, invoker);
147                
148                this.factory.record(invoker);
149                
150                return this;
151        }
152
153        @Override
154        public Writer append(final char c) throws IOException
155        {
156                WriterInvoker<Z, D, Writer> invoker = new WriterInvoker<Z, D, Writer>()
157                {
158                        @Override
159                        public Writer invoke(D database, Writer writer) throws IOException
160                        {
161                                return writer.append(c);
162                        }
163                };
164                
165                InvocationStrategies.INVOKE_ON_EXISTING.invoke(this.factory, invoker);
166                
167                this.factory.record(invoker);
168                
169                return this;
170        }
171
172        @Override
173        public void write(final char[] cbuf, final int off, final int len) throws IOException
174        {
175                WriterInvoker<Z, D, Void> invoker = new WriterInvoker<Z, D, Void>()
176                {
177                        @Override
178                        public Void invoke(D database, Writer writer) throws IOException
179                        {
180                                writer.write(cbuf, off, len);
181                                return null;
182                        }
183                };
184                
185                InvocationStrategies.INVOKE_ON_EXISTING.invoke(this.factory, invoker);
186                
187                this.factory.record(invoker);
188        }
189
190        @Override
191        public void flush() throws IOException
192        {
193                WriterInvoker<Z, D, Void> invoker = new WriterInvoker<Z, D, Void>()
194                {
195                        @Override
196                        public Void invoke(D database, Writer writer) throws IOException
197                        {
198                                writer.flush();
199                                return null;
200                        }
201                };
202                
203                InvocationStrategies.INVOKE_ON_EXISTING.invoke(this.factory, invoker);
204                
205                this.factory.record(invoker);
206        }
207
208        @Override
209        public void close() throws IOException
210        {
211                WriterInvoker<Z, D, Void> invoker = new WriterInvoker<Z, D, Void>()
212                {
213                        @Override
214                        public Void invoke(D database, Writer writer) throws IOException
215                        {
216                                writer.close();
217                                return null;
218                        }
219                };
220                
221                InvocationStrategies.INVOKE_ON_EXISTING.invoke(this.factory, invoker);
222                
223                this.factory.remove();
224        }
225
226        private interface WriterInvoker<Z, D extends Database<Z>, R> extends Invoker<Z, D, Writer, R, IOException>
227        {
228        }
229}