File: SocketComm.java

package info (click to toggle)
libsapdbc-java 5567-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,840 kB
  • ctags: 6,242
  • sloc: java: 49,887; makefile: 83
file content (149 lines) | stat: -rw-r--r-- 5,145 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*


 ========== licence begin GPL
    Copyright (C) 2002-2003 SAP AG

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
    ========== licence end


 */

package com.sap.dbtech.rte.comm;

import java.io.IOException;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Properties;

import com.sap.dbtech.util.MessageKey;
import com.sap.dbtech.util.MessageTranslator;

/**
 * Low-level socket communication.
 * 
 * as specified by the SAP DB runtime environment.
 *  
 */
public class SocketComm extends BasicSocketComm {
    public final static JdbcCommFactory factory = new JdbcCommFactory() {
        public JdbcCommunication open(String host, String dbname,
                Properties properties) throws RTEException {
            SocketComm sc = new SocketComm(host, properties);
            sc.connectDB(dbname);
            return sc;

        }

        public JdbcCommunication xopen(String host, String db, String dbroot,
                String pgm, Properties properties) throws RTEException {
            SocketComm sc = new SocketComm(host, properties);
            sc.connectAdmin(db, dbroot, pgm);
            return sc;
        }

    };

    // instance variables

    /**
     * creates a new socket connection to <i>host </i>.
     * 
     * @exception RTEException
     */
    protected SocketComm(String hostPort, Properties properties)
            throws RTEException {
        super(hostPort, properties);
        this.openSocket();
    }

    /**
     * opens a socket connection.
     * 
     * This converts any java.net specific exceptions to RTEException.
     * 
     * @param host
     *            java.lang.String
     * @exception RTEException
     */
    protected void openSocket() throws RTEException {
        try {
            this.socket = new Socket(this.host, this.lookupPort());

            try {
                this.socket.setSoTimeout(this.socketTimeOut);
                this.socket.setTcpNoDelay(true);
                //this.socket.setReceiveBufferSize(36864);
                this.socket.setSendBufferSize(36864);
            } catch (SocketException socketEx) {
                // ignore, as it is harmless
            }
            this.instream = this.socket.getInputStream();
            this.outstream = this.socket.getOutputStream();
        } catch (UnknownHostException uhexc) {
            throw new RTEException(
                    MessageTranslator
                            .translate(
                                    MessageKey.ERROR_UNKNOWN_HOST,
                                    this.host,
                                    uhexc.getMessage(),
                                    new Integer(
                                            RteC.CommunicationErrorCodeMap_C[RteC.SQLSERVER_OR_DB_UNKNOWN_C])),
                    RteC.CommunicationErrorCodeMap_C[RteC.SQLSERVER_OR_DB_UNKNOWN_C]);
        } catch (IOException ioexc) {
            throw new RTEException(
                    MessageTranslator
                            .translate(
                                    MessageKey.ERROR_HOST_CONNECT,
                                    this.host,
                                    ioexc.getMessage(),
                                    new Integer(
                                            RteC.CommunicationErrorCodeMap_C[RteC.SQLSTART_REQUIRED_C])),
                    RteC.CommunicationErrorCodeMap_C[RteC.SQLSTART_REQUIRED_C]);

        }
        try {
            // !!! DO NOT LINGER !!!
            // Modern OS linger in the background, if necessary.
            // Lingering causes the whole Java application to linger for that
            // time, if the
            // x_server hasn't read all data. And in case he hasn't our last
            // command was
            // a COMMIT/ROLLBACK WORK RELEASE, so no harm is done to the
            // database. The
            // only thing could be some nasty entry in some x_server log ...
            this.socket.setSoLinger(true, 15);
        } catch (SocketException soExc) {
            // ignore
        }

    }

    protected BasicSocketComm getNewCommunication() throws RTEException {
        return new SocketComm(this.host + ":" + this.port, null);
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.sap.dbtech.rte.comm.BasicSocketComm#getDefaultPort()
     */
    protected int getDefaultPort() {
        return RteC.defaultPort_C;
    }
 
}