File: NiCommunication.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 (212 lines) | stat: -rw-r--r-- 8,494 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package com.sap.dbtech.rte.comm;

import java.io.IOException;
import java.lang.reflect.Constructor;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Properties;
import java.util.Vector;

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

/*

 @author D031096

 ========== 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


 */
public class NiCommunication extends BasicSocketComm {
    private final static String NiHandle_Class_C     = "com.sap.ni.NiHandle";
    private final static String NiRoute_Class_C      = "com.sap.ni.NiRoute";
    private final static String NiRouteEntry_Class_C = "com.sap.ni.NiRouteEntry";
    private final static String Ni_Class_C           = "com.sap.ni.NI";
    
    private static Class niRoute; 
    private static Class niHandle; 
    private static Class niRouteEntry; 
    private static Class ni; 

    {
        try {
            niRoute       = Class.forName(NiRoute_Class_C);
            niHandle      = Class.forName(NiHandle_Class_C);
            niRouteEntry  = Class.forName(NiRouteEntry_Class_C);
            ni            = Class.forName(Ni_Class_C);
        } catch (ClassNotFoundException ex) {
            throw new RTEException(
                    MessageTranslator.translate(MessageKey.ERROR_LOAD_NILIBRARY, ex.toString()),
                    -709);
        }
    }

    public final static JdbcCommFactory factory = new JdbcCommFactory() {

        public JdbcCommunication open(String host, String dbname,
                Properties properties) throws RTEException {
            NiCommunication sc = new NiCommunication(host, properties);
            sc.connectDB(dbname);
            return sc;

        }

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

    };

    /**
     * @param hostPort
     * @param properties
     * @throws RTEException
     */
    
    public NiCommunication(String hostPort, Properties properties)
            throws RTEException {
        super(hostPort, properties);
        try {
            Constructor cst = NiCommunication.niRoute.getConstructor(new Class[]{String.class});
            Object niR = cst.newInstance(new Object[]{hostPort}); 
    	    /*patch port for last entry*/
            Vector routeEntries = (Vector)NiCommunication.niRoute.getField("route_entries").get(niR);  
            Object lastEntry = routeEntries.lastElement();
            String  curPort =  (String) lastEntry.getClass().getMethod("getPort",new Class[]{}).invoke(lastEntry,new Object[]{});
    	    if (curPort==null || curPort.equals("") || curPort.equals("3299")){
    	      this.host += "/S/"+this.getDefaultPort();  
    	    }
        } catch (Exception ioexc) {
            throw new RTEException(
                    MessageTranslator
                            .translate(
                                    MessageKey.ERROR_HOST_NICONNECT,
                                    this.host,
                                    ioexc.toString(),
                                    new Integer(
                                            RteC.CommunicationErrorCodeMap_C[RteC.SQLSTART_REQUIRED_C])),
                    RteC.CommunicationErrorCodeMap_C[RteC.SQLSTART_REQUIRED_C]);
        }
        this.openSocket();
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.sap.dbtech.rte.comm.BasicSocketComm#getNewCommunication()
     */
    protected BasicSocketComm getNewCommunication() throws RTEException {
        return new SocketComm(this.host, null);
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.sap.dbtech.rte.comm.BasicSocketComm#openSocket()
     */
    protected void openSocket() throws RTEException {
        try {
            Object talkmode = NiCommunication.ni.getField("NI_TALK_NATIVE").get(null);
            Constructor cst = NiCommunication.niHandle.getConstructor(new Class[]{String.class, Integer.TYPE, Byte.TYPE});
            Object niHdl = cst.newInstance(new Object[]{this.host, new Integer(0), talkmode});
            this.socket = (java.net.Socket) NiCommunication.niHandle.getMethod("getSocket",new Class[]{}).invoke(niHdl,new Object[]{});  

            //            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_HOST_NICONNECT,
                                    this.host,
                                    uhexc.toString(),
                                    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_NICONNECT,
                                    this.host,
                                    ioexc.toString(),
                                    new Integer(
                                            RteC.CommunicationErrorCodeMap_C[RteC.SQLSTART_REQUIRED_C])),
                    RteC.CommunicationErrorCodeMap_C[RteC.SQLSTART_REQUIRED_C]);

        } catch (Exception ioexc) {
            throw new RTEException(
                    MessageTranslator
                            .translate(
                                    MessageKey.ERROR_HOST_NICONNECT,
                                    this.host,
                                    ioexc.toString(),
                                    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
        }

    }

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

    /*
     * (non-Javadoc)
     * 
     * @see com.sap.dbtech.rte.comm.BasicSocketComm#socketMustClosedAfterInfoRequest()
     */
    protected boolean socketMustClosedAfterInfoRequest() {
        return false;
    }

}