EMMA Coverage Report (generated Wed Jun 28 22:15:27 PDT 2006)
[all classes][org.apache.derby.client.net]

COVERAGE SUMMARY FOR SOURCE FILE [NetAgent.java]

nameclass, %method, %block, %line, %
NetAgent.java100% (1/1)78%  (21/27)58%  (458/795)68%  (127.8/188)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class NetAgent100% (1/1)78%  (21/27)58%  (458/795)68%  (127.8/188)
NetAgent (NetConnection, LogWriter): void 0%   (0/1)0%   (0/18)0%   (0/6)
NetAgent (NetConnection, LogWriter, int, String, int): void 100% (1/1)85%  (300/352)85%  (63/74)
beginReadChain (Statement): void 100% (1/1)100% (6/6)100% (3/3)
beginReadChainOutsideUOW (): void 100% (1/1)100% (5/5)100% (3/3)
beginWriteChain (Statement): void 100% (1/1)100% (9/9)100% (4/4)
beginWriteChainOutsideUOW (): void 100% (1/1)100% (8/8)100% (4/4)
checkForChainBreakingException_ (): void 100% (1/1)100% (11/11)100% (5/5)
clearSvrcod (): void 100% (1/1)100% (4/4)100% (2/2)
close_ (): void 100% (1/1)24%  (35/144)42%  (10/24)
convertToStringTcpIpAddress (int): String 0%   (0/1)0%   (0/49)0%   (0/9)
endReadChain (): void 100% (1/1)100% (3/3)100% (2/2)
endWriteChain (): void 100% (1/1)100% (3/3)100% (2/2)
flush_ (): void 100% (1/1)100% (6/6)100% (3/3)
getInputStream (): InputStream 100% (1/1)100% (3/3)100% (1/1)
getOutputStream (): OutputStream 100% (1/1)100% (3/3)100% (1/1)
getPort (): int 100% (1/1)100% (3/3)100% (1/1)
getSvrcod (): int 100% (1/1)100% (3/3)100% (1/1)
markChainBreakingException_ (): void 100% (1/1)100% (4/4)100% (2/2)
newLogWriter_ (PrintWriter, int): LogWriter 0%   (0/1)0%   (0/6)0%   (0/1)
readDeferredResetConnection (): void 100% (1/1)50%  (12/24)60%  (6/10)
resetAgent_ (LogWriter, int, String, int): void 100% (1/1)38%  (15/39)50%  (5/10)
sendRequest (): void 100% (1/1)47%  (7/15)60%  (3/5)
setInputStream (InputStream): void 0%   (0/1)0%   (0/4)0%   (0/2)
setOutputStream (OutputStream): void 0%   (0/1)0%   (0/4)0%   (0/2)
setSvrcod (int): void 100% (1/1)100% (8/8)100% (3/3)
throwCommunicationsFailure (String, String, String, String): void 0%   (0/1)0%   (0/39)0%   (0/2)
writeDeferredResetConnection (): void 100% (1/1)45%  (10/22)56%  (5/9)

1/*
2 
3   Derby - Class org.apache.derby.client.net.NetAgent
4 
5   Copyright (c) 2001, 2005 The Apache Software Foundation or its licensors, where applicable.
6 
7   Licensed under the Apache License, Version 2.0 (the "License");
8   you may not use this file except in compliance with the License.
9   You may obtain a copy of the License at
10 
11      http://www.apache.org/licenses/LICENSE-2.0
12 
13   Unless required by applicable law or agreed to in writing, software
14   distributed under the License is distributed on an "AS IS" BASIS,
15   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   See the License for the specific language governing permissions and
17   limitations under the License.
18 
19*/
20 
21package org.apache.derby.client.net;
22 
23import java.net.SocketException;
24 
25import org.apache.derby.client.am.Agent;
26import org.apache.derby.client.am.DisconnectException;
27import org.apache.derby.client.am.SqlException;
28import org.apache.derby.client.am.ClientMessageId;
29import org.apache.derby.client.am.Utils;
30import org.apache.derby.shared.common.sanity.SanityManager;
31 
32import org.apache.derby.shared.common.reference.SQLState;
33import org.apache.derby.shared.common.reference.MessageId;
34import org.apache.derby.shared.common.i18n.MessageUtil;
35 
36public class NetAgent extends Agent {
37    //---------------------navigational members-----------------------------------
38 
39    // All these request objects point to the same physical request object.
40    public ConnectionRequestInterface connectionRequest_;
41    public NetConnectionRequest packageRequest_;
42    public StatementRequestInterface statementRequest_;
43    public ResultSetRequestInterface resultSetRequest_;
44 
45    // All these reply objects point to the same physical reply object.
46    public ConnectionReply connectionReply_;
47    public ConnectionReply packageReply_;
48    public StatementReply statementReply_;
49    public ResultSetReply resultSetReply_;
50 
51    //---------------------navigational cheat-links-------------------------------
52    // Cheat-links are for convenience only, and are not part of the conceptual model.
53    // Warning:
54    //   Cheat-links should only be defined for invariant state data.
55    //   That is, the state data is set by the constructor and never changes.
56 
57    // Alias for (NetConnection) super.connection
58    NetConnection netConnection_;
59 
60    // Alias for (Request) super.*Request, all in one
61    // In the case of the NET implementation, these all point to the same physical request object.
62    protected Request request_;
63    public NetConnectionRequest netConnectionRequest_;
64    public NetPackageRequest netPackageRequest_;
65    public NetStatementRequest netStatementRequest_;
66    public NetResultSetRequest netResultSetRequest_;
67 
68    // Alias for (Reply) super.*Reply, all in one.
69    // In the case of the NET implementation, these all point to the same physical reply object.
70    protected Reply reply_;
71    public NetConnectionReply netConnectionReply_;
72    public NetPackageReply netPackageReply_;
73    public NetStatementReply netStatementReply_;
74    public NetResultSetReply netResultSetReply_;
75 
76    //-----------------------------state------------------------------------------
77 
78    java.net.Socket socket_;
79    java.io.InputStream rawSocketInputStream_;
80    java.io.OutputStream rawSocketOutputStream_;
81 
82    String server_;
83    int port_;
84    public CcsidManager sourceCcsidManager_;
85    public CcsidManager targetCcsidManager_;
86    public Typdef typdef_;
87    public Typdef targetTypdef_;
88    public Typdef originalTargetTypdef_; // added to support typdef overrides
89 
90    protected int svrcod_;
91 
92    public int orignalTargetSqlam_ = NetConfiguration.MGRLVL_7;
93    public int targetSqlam_ = orignalTargetSqlam_;
94 
95    public SqlException exceptionOpeningSocket_ = null;
96 
97    //---------------------constructors/finalizer---------------------------------
98    public NetAgent(NetConnection netConnection,
99                    org.apache.derby.client.am.LogWriter logWriter) throws SqlException {
100        super(netConnection, logWriter);
101        this.netConnection_ = netConnection;
102    }
103 
104    NetAgent(NetConnection netConnection,
105             org.apache.derby.client.am.LogWriter netLogWriter,
106             int loginTimeout,
107             String server,
108             int port) throws SqlException {
109        super(netConnection, netLogWriter);
110 
111        server_ = server;
112        port_ = port;
113        netConnection_ = netConnection;
114        if (server_ == null) {
115            throw new DisconnectException(this, 
116                new ClientMessageId(SQLState.CONNECT_REQUIRED_PROPERTY_NOT_SET),
117                "serverName");
118        }
119 
120        try {
121            socket_ = (java.net.Socket) java.security.AccessController.doPrivileged(new OpenSocketAction(server, port));
122        } catch (java.security.PrivilegedActionException e) {
123            throw new DisconnectException(this,
124                new ClientMessageId(SQLState.CONNECT_UNABLE_TO_CONNECT_TO_SERVER),
125                new Object[] { e.getException().getClass().getName(), server, 
126                    Integer.toString(port), e.getException().getMessage() },
127                e.getException());
128        }
129 
130        // Set TCP/IP Socket Properties
131        try {
132            if (exceptionOpeningSocket_ == null) {
133                socket_.setTcpNoDelay(true); // disables nagles algorithm
134                socket_.setKeepAlive(true); // PROTOCOL Manual: TCP/IP connection allocation rule #2
135                socket_.setSoTimeout(loginTimeout * 1000);
136            }
137        } catch (java.net.SocketException e) {
138            try {
139                socket_.close();
140            } catch (java.io.IOException doNothing) {
141            }
142            exceptionOpeningSocket_ = new DisconnectException(this,
143                new ClientMessageId(SQLState.CONNECT_SOCKET_EXCEPTION),
144                e.getMessage(), e);
145        }
146 
147        try {
148            if (exceptionOpeningSocket_ == null) {
149                rawSocketOutputStream_ = socket_.getOutputStream();
150                rawSocketInputStream_ = socket_.getInputStream();
151            }
152        } catch (java.io.IOException e) {
153            try {
154                socket_.close();
155            } catch (java.io.IOException doNothing) {
156            }
157            exceptionOpeningSocket_ = new DisconnectException(this, 
158                new ClientMessageId(SQLState.CONNECT_UNABLE_TO_OPEN_SOCKET_STREAM),
159                e.getMessage(), e);
160        }
161 
162        sourceCcsidManager_ = new EbcdicCcsidManager(); // delete these
163        targetCcsidManager_ = sourceCcsidManager_; // delete these
164 
165        if (netConnection_.isXAConnection()) {
166            NetXAConnectionReply netXAConnectionReply_ = new NetXAConnectionReply(this, netConnection_.commBufferSize_);
167            netResultSetReply_ = (NetResultSetReply) netXAConnectionReply_;
168            netStatementReply_ = (NetStatementReply) netResultSetReply_;
169            netPackageReply_ = (NetPackageReply) netStatementReply_;
170            netConnectionReply_ = (NetConnectionReply) netPackageReply_;
171            reply_ = (Reply) netConnectionReply_;
172 
173            resultSetReply_ = new ResultSetReply(this,
174                    netResultSetReply_,
175                    netStatementReply_,
176                    netConnectionReply_);
177            statementReply_ = (StatementReply) resultSetReply_;
178            packageReply_ = (ConnectionReply) statementReply_;
179            connectionReply_ = (ConnectionReply) packageReply_;
180            NetXAConnectionRequest netXAConnectionRequest_ = new NetXAConnectionRequest(this, sourceCcsidManager_, netConnection_.commBufferSize_);
181            netResultSetRequest_ = (NetResultSetRequest) netXAConnectionRequest_;
182            netStatementRequest_ = (NetStatementRequest) netResultSetRequest_;
183            netPackageRequest_ = (NetPackageRequest) netStatementRequest_;
184            netConnectionRequest_ = (NetConnectionRequest) netPackageRequest_;
185            request_ = (Request) netConnectionRequest_;
186 
187            resultSetRequest_ = (ResultSetRequestInterface) netResultSetRequest_;
188            statementRequest_ = (StatementRequestInterface) netStatementRequest_;
189            packageRequest_ = (NetConnectionRequest) netPackageRequest_;
190            connectionRequest_ = (ConnectionRequestInterface) netConnectionRequest_;
191        } else {
192            netResultSetReply_ = new NetResultSetReply(this, netConnection_.commBufferSize_);
193            netStatementReply_ = (NetStatementReply) netResultSetReply_;
194            netPackageReply_ = (NetPackageReply) netStatementReply_;
195            netConnectionReply_ = (NetConnectionReply) netPackageReply_;
196            reply_ = (Reply) netConnectionReply_;
197 
198            resultSetReply_ = new ResultSetReply(this,
199                    netResultSetReply_,
200                    netStatementReply_,
201                    netConnectionReply_);
202            statementReply_ = (StatementReply) resultSetReply_;
203            packageReply_ = (ConnectionReply) statementReply_;
204            connectionReply_ = (ConnectionReply) packageReply_;
205            netResultSetRequest_ = new NetResultSetRequest(this, sourceCcsidManager_, netConnection_.commBufferSize_);
206            netStatementRequest_ = (NetStatementRequest) netResultSetRequest_;
207            netPackageRequest_ = (NetPackageRequest) netStatementRequest_;
208            netConnectionRequest_ = (NetConnectionRequest) netPackageRequest_;
209            request_ = (Request) netConnectionRequest_;
210 
211            resultSetRequest_ = (ResultSetRequestInterface) netResultSetRequest_;
212            statementRequest_ = (StatementRequestInterface) netStatementRequest_;
213            packageRequest_ = (NetConnectionRequest) netPackageRequest_;
214            connectionRequest_ = (ConnectionRequestInterface) netConnectionRequest_;
215        }
216    }
217 
218    protected void resetAgent_(org.apache.derby.client.am.LogWriter netLogWriter,
219                               //CcsidManager sourceCcsidManager,
220                               //CcsidManager targetCcsidManager,
221                               int loginTimeout,
222                               String server,
223                               int port) throws SqlException {
224 
225        // most properties will remain unchanged on connect reset.
226        targetTypdef_ = originalTargetTypdef_;
227        svrcod_ = 0;
228 
229        // Set TCP/IP Socket Properties
230        try {
231            socket_.setSoTimeout(loginTimeout * 1000);
232        } catch (java.net.SocketException e) {
233            try {
234                socket_.close();
235            } catch (java.io.IOException doNothing) {
236            }
237            throw new SqlException(logWriter_, 
238                new ClientMessageId(SQLState.SOCKET_EXCEPTION),
239                e.getMessage(), e);
240        }
241    }
242 
243 
244    void setSvrcod(int svrcod) {
245        if (svrcod > svrcod_) {
246            svrcod_ = svrcod;
247        }
248    }
249 
250    void clearSvrcod() {
251        svrcod_ = CodePoint.SVRCOD_INFO;
252    }
253 
254    int getSvrcod() {
255        return svrcod_;
256    }
257 
258    public void flush_() throws DisconnectException {
259        sendRequest();
260        reply_.initialize();
261    }
262 
263    // Close socket and its streams.
264    public void close_() throws SqlException {
265        // can we just close the socket here, do we need to close streams individually
266        SqlException accumulatedExceptions = null;
267        if (rawSocketInputStream_ != null) {
268            try {
269                rawSocketInputStream_.close();
270            } catch (java.io.IOException e) {
271                // note when {6} = 0 it indicates the socket was closed.
272                // this should be ok since we are going to go an close the socket
273                // immediately following this call.
274                // changing {4} to e.getMessage() may require pub changes
275                accumulatedExceptions = new SqlException(logWriter_,
276                    new ClientMessageId(SQLState.COMMUNICATION_ERROR),
277                    e.getMessage(), e);
278            } finally {
279                rawSocketInputStream_ = null;
280            }
281        }
282 
283        if (rawSocketOutputStream_ != null) {
284            try {
285                rawSocketOutputStream_.close();
286            } catch (java.io.IOException e) {
287                // note when {6} = 0 it indicates the socket was closed.
288                // this should be ok since we are going to go an close the socket
289                // immediately following this call.
290                // changing {4} to e.getMessage() may require pub changes
291                SqlException latestException = new SqlException(logWriter_,
292                    new ClientMessageId(SQLState.COMMUNICATION_ERROR),
293                    e.getMessage(), e);
294                accumulatedExceptions = Utils.accumulateSQLException(latestException, accumulatedExceptions);
295            } finally {
296                rawSocketOutputStream_ = null;
297            }
298        }
299 
300        if (socket_ != null) {
301            try {
302                socket_.close();
303            } catch (java.io.IOException e) {
304                // again {6} = 0, indicates the socket was closed.
305                // maybe set {4} to e.getMessage().
306                // do this for now and but may need to modify or
307                // add this to the message pubs.
308                SqlException latestException = new SqlException(logWriter_,
309                    new ClientMessageId(SQLState.COMMUNICATION_ERROR),
310                        e.getMessage(), e);
311                accumulatedExceptions = Utils.accumulateSQLException(latestException, accumulatedExceptions);
312            } finally {
313                socket_ = null;
314            }
315        }
316 
317        if (accumulatedExceptions != null) {
318            throw accumulatedExceptions;
319        }
320    }
321 
322    /**
323     * Specifies the maximum blocking time that should be used when sending
324     * and receiving messages. The timeout is implemented by using the the 
325     * underlying socket implementation's timeout support. 
326     * 
327     * Note that the support for timeout on sockets is dependent on the OS 
328     * implementation. For the same reason we ignore any exceptions thrown
329     * by the call to the socket layer.
330     * 
331     * @param timeout The timeout value in seconds. A value of 0 corresponds to 
332     * infinite timeout.
333     */
334    protected void setTimeout(int timeout) {
335        try {
336            // Sets a timeout on the socket
337            socket_.setSoTimeout(timeout * 1000); // convert to milliseconds
338        } catch (SocketException se) {
339            // Silently ignore any exceptions from the socket layer
340            if (SanityManager.DEBUG) {
341                System.out.println("NetAgent.setTimeout: ignoring exception: " + 
342                                   se);
343            }
344        }
345    }
346 
347    /**
348     * Returns the current timeout value that is set on the socket.
349     * 
350     * Note that the support for timeout on sockets is dependent on the OS 
351     * implementation. For the same reason we ignore any exceptions thrown
352     * by the call to the socket layer.
353     * 
354     * @return The timeout value in seconds. A value of 0 corresponds to
355     * that no timeout is specified on the socket.
356     */
357    protected int getTimeout() {
358        int timeout = 0; // 0 is default timeout for sockets
359 
360        // Read the timeout currently set on the socket
361        try {
362            timeout = socket_.getSoTimeout();
363        } catch (SocketException se) {
364            // Silently ignore any exceptions from the socket layer
365            if (SanityManager.DEBUG) {
366                System.out.println("NetAgent.getTimeout: ignoring exception: " + 
367                                   se);
368            }
369        }
370 
371        // Convert from milliseconds to seconds (note that this truncates
372        // the results towards zero but that should not be a problem).
373        timeout = timeout / 1000;
374        return timeout;
375    }
376 
377    protected void sendRequest() throws DisconnectException {
378        try {
379            request_.flush(rawSocketOutputStream_);
380        } catch (java.io.IOException e) {
381            throwCommunicationsFailure(e);
382        }
383    }
384 
385    public java.io.InputStream getInputStream() {
386        return rawSocketInputStream_;
387    }
388 
389    public java.io.OutputStream getOutputStream() {
390        return rawSocketOutputStream_;
391    }
392 
393    void setInputStream(java.io.InputStream inputStream) {
394        rawSocketInputStream_ = inputStream;
395    }
396 
397    void setOutputStream(java.io.OutputStream outputStream) {
398        rawSocketOutputStream_ = outputStream;
399    }
400 
401    public void throwCommunicationsFailure(Throwable cause) 
402        throws org.apache.derby.client.am.DisconnectException {
403        //org.apache.derby.client.am.DisconnectException
404        //accumulateReadExceptionAndDisconnect
405        // note when {6} = 0 it indicates the socket was closed.
406        // need to still validate any token values against message publications.
407        accumulateChainBreakingReadExceptionAndThrow(
408            new org.apache.derby.client.am.DisconnectException(this,
409                new ClientMessageId(SQLState.COMMUNICATION_ERROR),
410                cause.getMessage(), cause));
411    }
412        
413    // ----------------------- call-down methods ---------------------------------
414 
415    public org.apache.derby.client.am.LogWriter newLogWriter_(java.io.PrintWriter printWriter,
416                                                              int traceLevel) {
417        return new NetLogWriter(printWriter, traceLevel);
418    }
419 
420    protected void markChainBreakingException_() {
421        setSvrcod(CodePoint.SVRCOD_ERROR);
422    }
423 
424    public void checkForChainBreakingException_() throws SqlException {
425        int svrcod = getSvrcod();
426        clearSvrcod();
427        if (svrcod > CodePoint.SVRCOD_WARNING) // Not for SQL warning, if svrcod > WARNING, then its a chain breaker
428        {
429            super.checkForExceptions(); // throws the accumulated exceptions, we'll always have at least one.
430        }
431    }
432 
433    private void writeDeferredResetConnection() throws SqlException {
434        if (!netConnection_.resetConnectionAtFirstSql_) {
435            return;
436        }
437        try {
438            netConnection_.writeDeferredReset();
439        } catch (SqlException sqle) {
440            DisconnectException de = new DisconnectException(this, 
441                new ClientMessageId(SQLState.CONNECTION_FAILED_ON_DEFERRED_RESET));
442            de.setNextException(sqle);
443            throw de;
444        }
445    }
446 
447    public void beginWriteChainOutsideUOW() throws SqlException {
448        request_.initialize();
449        writeDeferredResetConnection();
450        super.beginWriteChainOutsideUOW();
451    }
452 
453    public void beginWriteChain(org.apache.derby.client.am.Statement statement) throws SqlException {
454        request_.initialize();
455        writeDeferredResetConnection();
456        super.beginWriteChain(statement);
457    }
458 
459    protected void endWriteChain() {
460        super.endWriteChain();
461    }
462 
463    private void readDeferredResetConnection() throws SqlException {
464        if (!netConnection_.resetConnectionAtFirstSql_) {
465            return;
466        }
467        try {
468            netConnection_.readDeferredReset();
469            checkForExceptions();
470        } catch (SqlException sqle) {
471            DisconnectException de = new DisconnectException(this, 
472                new ClientMessageId(SQLState.CONNECTION_FAILED_ON_DEFERRED_RESET));
473            de.setNextException(sqle);
474            throw de;
475        }
476    }
477 
478    protected void beginReadChain(org.apache.derby.client.am.Statement statement) throws SqlException {
479        readDeferredResetConnection();
480        super.beginReadChain(statement);
481    }
482 
483    protected void beginReadChainOutsideUOW() throws SqlException {
484        readDeferredResetConnection();
485        super.beginReadChainOutsideUOW();
486    }
487 
488    public void endReadChain() throws SqlException {
489        super.endReadChain();
490    }
491 
492 
493    public String convertToStringTcpIpAddress(int tcpIpAddress) {
494        StringBuffer ipAddrBytes = new StringBuffer();
495        ipAddrBytes.append((tcpIpAddress >> 24) & 0xff);
496        ipAddrBytes.append(".");
497        ipAddrBytes.append((tcpIpAddress >> 16) & 0xff);
498        ipAddrBytes.append(".");
499        ipAddrBytes.append((tcpIpAddress >> 8) & 0xff);
500        ipAddrBytes.append(".");
501        ipAddrBytes.append((tcpIpAddress) & 0xff);
502 
503        return ipAddrBytes.toString();
504    }
505 
506    protected int getPort() {
507        return port_;
508    }
509 
510}
511 
512 

[all classes][org.apache.derby.client.net]
EMMA 2.0.5312 (C) Vladimir Roubtsov