KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > client > net > NetConnectionRequest


1 /*
2
3    Derby - Class org.apache.derby.client.net.NetConnectionRequest
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20 */

21
22 package org.apache.derby.client.net;
23
24
25 import javax.transaction.xa.Xid JavaDoc;
26
27 import org.apache.derby.client.am.SqlException;
28 import org.apache.derby.client.am.ClientMessageId;
29 import org.apache.derby.client.am.Utils;
30 import org.apache.derby.shared.common.reference.SQLState;
31
32 public class NetConnectionRequest extends Request implements ConnectionRequestInterface {
33     NetConnectionRequest(NetAgent netAgent, CcsidManager ccsidManager, int bufferSize) {
34         super(netAgent, ccsidManager, bufferSize);
35     }
36     //----------------------------- entry points ---------------------------------
37

38     void writeExchangeServerAttributes(String JavaDoc externalName,
39                                        int targetAgent,
40                                        int targetSqlam,
41                                        int targetRdb,
42                                        int targetSecmgr,
43                                        int targetCmntcpip,
44                                        int targetCmnappc,
45                                        int targetXamgr,
46                                        int targetSyncptmgr,
47                                        int targetRsyncmgr) throws SqlException {
48         // send the exchange server attributes command to the server.
49
// no other commands will be chained to the excsat because
50
// the manager levels are needed before anything else is attempted.
51
buildEXCSAT(externalName,
52                 targetAgent,
53                 targetSqlam,
54                 targetRdb,
55                 targetSecmgr,
56                 targetCmntcpip,
57                 targetCmnappc,
58                 targetXamgr,
59                 targetSyncptmgr,
60                 targetRsyncmgr);
61
62     }
63
64     void writeDummyExchangeServerAttributes() throws SqlException {
65         // send the exchange server attributes command to the server,
66
// without any parameters
67
buildDummyEXCSAT();
68     }
69
70     void writeAccessSecurity(int securityMechanism,
71                              String JavaDoc databaseName,
72                              byte[] publicKey) throws SqlException {
73         buildACCSEC(securityMechanism, databaseName, publicKey);
74     }
75
76     void writeSecurityCheck(int securityMechanism,
77                             String JavaDoc databaseName,
78                             String JavaDoc userid,
79                             String JavaDoc password,
80                             byte[] encryptedUserid,
81                             byte[] encryptedPassword) throws SqlException {
82         buildSECCHK(securityMechanism,
83                 databaseName,
84                 userid,
85                 password,
86                 encryptedUserid,
87                 encryptedPassword);
88     }
89
90     void writeAccessDatabase(String JavaDoc rdbnam,
91                              boolean readOnly,
92                              byte[] correlationToken,
93                              byte[] productData,
94                              Typdef typdef) throws SqlException {
95         buildACCRDB(rdbnam,
96                 readOnly,
97                 correlationToken,
98                 productData,
99                 typdef);
100     }
101
102
103     public void writeCommitSubstitute(NetConnection connection) throws SqlException {
104         buildDummyEXCSAT();
105     }
106
107     public void writeLocalCommit(NetConnection connection) throws SqlException {
108         buildRDBCMM();
109     }
110
111     public void writeLocalRollback(NetConnection connection) throws SqlException {
112         buildRDBRLLBCK();
113     }
114
115     public void writeLocalXAStart(NetConnection connection) throws SqlException {
116     }
117
118
119     //Build the SYNNCTL commit command
120
public void writeLocalXACommit(NetConnection conn) throws SqlException {
121     }
122
123     //Build the SYNNCTL rollback command
124
public void writeLocalXARollback(NetConnection conn) throws SqlException {
125     }
126
127     public void writeXaStartUnitOfWork(NetConnection conn) throws SqlException {
128     }
129
130     public void writeXaEndUnitOfWork(NetConnection conn) throws SqlException {
131     }
132
133     protected void writeXaPrepare(NetConnection conn) throws SqlException {
134     }
135
136     protected void writeXaCommit(NetConnection conn, Xid JavaDoc xid) throws SqlException {
137     }
138
139     protected void writeXaRollback(NetConnection conn, Xid JavaDoc xid) throws SqlException {
140     }
141
142     protected void writeXaRecover(NetConnection conn, int flag) throws SqlException {
143     }
144
145     protected void writeXaForget(NetConnection conn, Xid JavaDoc xid) throws SqlException {
146     }
147
148     public void writeSYNCType(int codepoint, int syncType) {
149         writeScalar1Byte(codepoint, syncType);
150     }
151
152     public void writeForget(int codepoint, int value) {
153     }
154
155     public void writeReleaseConversation(int codepoint, int value) {
156     }
157
158     void writeNullXID(int codepoint) {
159     }
160
161     void writeXID(int codepoint, Xid JavaDoc xid) throws SqlException {
162     }
163
164
165     void writeXAFlags(int codepoint, int xaFlags) {
166     }
167
168
169     //----------------------helper methods----------------------------------------
170
// These methods are "private protected", which is not a recognized java privilege,
171
// but means that these methods are private to this class and to subclasses,
172
// and should not be used as package-wide friendly methods.
173

174     // RDB Commit Unit of Work (RDBCMM) Command commits all work performed
175
// for the current unit of work.
176
//
177
// The Relational Database Name (RDBNAM) is an optional parameter
178
// which will not be sent by this command to reduce size, building,
179
// and parsing.
180
void buildRDBCMM() throws SqlException {
181         createCommand();
182         writeLengthCodePoint(0x04, CodePoint.RDBCMM);
183     }
184
185     // RDB Rollback Unit of Work(RDBRLLBCK) Command rolls back
186
// all work performed for the current unit of work.
187
//
188
// The Relational Database Name (RDBNAM) is an optional parameter
189
// which will not be sent by this command to reduce size, building,
190
// and parsing.
191
void buildRDBRLLBCK() throws SqlException {
192         createCommand();
193         writeLengthCodePoint(0x04, CodePoint.RDBRLLBCK);
194     }
195
196     // build the Exchange Server Attributes Command.
197
// This command sends the following information to the server.
198
// - this driver's server class name
199
// - this driver's level of each of the manager's it supports
200
// - this driver's product release level
201
// - this driver's external name
202
// - this driver's server name
203
void buildEXCSAT(String JavaDoc externalName,
204                      int targetAgent,
205                      int targetSqlam,
206                      int targetRdb,
207                      int targetSecmgr,
208                      int targetCmntcpip,
209                      int targetCmnappc,
210                      int targetXamgr,
211                      int targetSyncptmgr,
212                      int targetRsyncmgr) throws SqlException {
213         createCommand();
214
215         // begin excsat collection by placing the 4 byte llcp in the buffer.
216
// the length of this command will be computed later and "filled in"
217
// with the call to request.updateLengthBytes().
218
markLengthBytes(CodePoint.EXCSAT);
219
220         // place the external name for the client into the buffer.
221
// the external name was previously calculated before the call to this method.
222
buildEXTNAM(externalName);
223
224         // place the server name for the client into the buffer.
225
buildSRVNAM("Derby");
226
227         // place the server release level for the client into the buffer.
228
// this is a hard coded value for the driver.
229
buildSRVRLSLV();
230
231         // the managers supported by this driver and their levels will
232
// be sent to the server. the variables which store these values
233
// were initialized during object constrcution to the highest values
234
// supported by the driver.
235

236         // for the case of the manager levels object, there is no
237
// need to have the length of the ddm object dynamically calculated
238
// because this method knows exactly how many will be sent and can set
239
// this now.
240
// each manager level class and level are 4 bytes long and
241
// right now 5 are being sent for a total of 20 bytes or 0x14 bytes.
242
// writeScalarHeader will be called to insert the llcp.
243
buildMGRLVLLS(targetAgent,
244                 targetSqlam,
245                 targetRdb,
246                 targetSecmgr,
247                 targetXamgr,
248                 targetSyncptmgr,
249                 targetRsyncmgr);
250
251
252         // place the server class name into the buffer.
253
// this value is hard coded for the driver.
254
buildSRVCLSNM();
255
256         // the excsat command is complete so the updateLengthBytes method
257
// is called to dynamically compute the length for this command and insert
258
// it into the buffer
259
updateLengthBytes();
260     }
261
262     void buildDummyEXCSAT() throws SqlException {
263         createCommand();
264
265         // begin excsat collection by placing the 4 byte llcp in the buffer.
266
// the length of this command will be computed later and "filled in"
267
// with the call to request.updateLengthBytes().
268
markLengthBytes(CodePoint.EXCSAT);
269
270         // the excsat command is complete so the updateLengthBytes method
271
// is called to dynamically compute the length for this command and insert
272
// it into the buffer
273
updateLengthBytes();
274     }
275
276     void buildACCSEC(int secmec,
277                      String JavaDoc rdbnam,
278                      byte[] sectkn) throws SqlException {
279         createCommand();
280
281         // place the llcp for the ACCSEC in the buffer. save the length bytes for
282
// later update
283
markLengthBytes(CodePoint.ACCSEC);
284
285         // the security mechanism is a required instance variable. it will
286
// always be sent.
287
buildSECMEC(secmec);
288
289         // the rdbnam will be built and sent. different sqlam levels support
290
// different lengths. at this point the length has been checked against
291
// the maximum allowable length. so write the bytes and padd up to the
292
// minimum length if needed.
293
buildRDBNAM(rdbnam);
294
295         if (sectkn != null) {
296             buildSECTKN(sectkn);
297         }
298
299         // the accsec command is complete so notify the the request object to
300
// update the ddm length and the dss header length.
301
updateLengthBytes();
302     }
303
304     void buildSECCHK(int secmec,
305                      String JavaDoc rdbnam,
306                      String JavaDoc user,
307                      String JavaDoc password,
308                      byte[] sectkn,
309                      byte[] sectkn2) throws SqlException {
310         createCommand();
311         markLengthBytes(CodePoint.SECCHK);
312
313         // always send the negotiated security mechanism for the connection.
314
buildSECMEC(secmec);
315
316         // the rdbnam will be built and sent. different sqlam levels support
317
// different lengths. at this point the length has been checked against
318
// the maximum allowable length. so write the bytes and padd up to the
319
// minimum length if needed.
320
buildRDBNAM(rdbnam);
321         if (user != null) {
322             buildUSRID(user);
323         }
324         if (password != null) {
325             buildPASSWORD(password);
326         }
327         if (sectkn != null) {
328             buildSECTKN(sectkn);
329         }
330         if (sectkn2 != null) {
331             buildSECTKN(sectkn2);
332         }
333         updateLengthBytes();
334
335     }
336
337     // The Access RDB (ACCRDB) command makes a named relational database (RDB)
338
// available to a requester by creating an instance of an SQL application
339
// manager. The access RDB command then binds the created instance to the target
340
// agent and to the RDB. The RDB remains available (accessed) until
341
// the communications conversation is terminate.
342
void buildACCRDB(String JavaDoc rdbnam,
343                      boolean readOnly,
344                      byte[] crrtkn,
345                      byte[] prddta,
346                      Typdef typdef) throws SqlException {
347         createCommand();
348
349         markLengthBytes(CodePoint.ACCRDB);
350
351         // the relational database name specifies the name of the rdb to
352
// be accessed. this can be different sizes depending on the level of
353
// support. the size will have ben previously checked so at this point just
354
// write the data and pad with the correct number of bytes as needed.
355
// this instance variable is always required.
356
buildRDBNAM(rdbnam);
357
358         // the rdb access manager class specifies an instance of the SQLAM
359
// that accesses the RDB. the sqlam manager class codepoint
360
// is always used/required for this. this instance variable
361
// is always required.
362
buildRDBACCCL();
363
364         // product specific identifier specifies the product release level
365
// of this driver. see the hard coded value in the NetConfiguration class.
366
// this instance variable is always required.
367
buildPRDID();
368
369         // product specific data. this is an optional parameter which carries
370
// product specific information. although it is optional, it will be
371
// sent to the server. use the first byte to determine the number
372
// of the prddta bytes to write to the buffer. note: this length
373
// doesn't include itself so increment by it by 1 to get the actual
374
// length of this data.
375
buildPRDDTA(prddta);
376
377
378         // the typdefnam parameter specifies the name of the data type to data representation
379
// mappings used when this driver sends command data objects.
380
buildTYPDEFNAM(typdef.getTypdefnam());
381
382         if (crrtkn == null) {
383             netAgent_.netConnection_.constructCrrtkn();
384         }
385
386         buildCRRTKN(netAgent_.netConnection_.crrtkn_);
387
388         // This specifies the single-byte, double-byte
389
// and mixed-byte CCSIDs of the Scalar Data Arrays (SDAs) in the identified
390
// data type to the data representation mapping definitions. This can
391
// contain 3 CCSIDs. The driver will only send the ones which were set.
392
buildTYPDEFOVR(typdef.isCcsidSbcSet(),
393                 typdef.getCcsidSbc(),
394                 typdef.isCcsidDbcSet(),
395                 typdef.getCcsidDbc(),
396                 typdef.isCcsidMbcSet(),
397                 typdef.getCcsidMbc());
398
399         // RDB allow update is an optional parameter which indicates
400
// whether the RDB allows the requester to perform update operations
401
// in the RDB. If update operations are not allowed, this connection
402
// is limited to read-only access of the RDB resources.
403
buildRDBALWUPD(readOnly);
404
405
406
407         // the Statement Decimal Delimiter (STTDECDEL),
408
// Statement String Delimiter (STTSTRDEL),
409
// and Target Default Value Return (TRGDFTRT) are all optional
410
// instance variables which will not be sent to the server.
411

412         // the command and the dss are complete so make the call to notify
413
// the request object.
414
updateLengthBytes();
415     }
416
417
418     void buildSYNCCTLMigrate() throws SqlException {
419     }
420
421     void buildSYNCCTLCommit(int xaFlags, Xid JavaDoc xid) throws SqlException {
422     }
423
424     void buildSYNCCTLRollback(int xaFlags) throws SqlException {
425     }
426
427
428     // The External Name is the name of the job, task, or process on a
429
// system for which a DDM server is active.
430
private void buildEXTNAM(String JavaDoc extnam) throws SqlException {
431         int extnamTruncateLength = Utils.min(extnam.length(),
432                 NetConfiguration.EXTNAM_MAXSIZE);
433
434         writeScalarString(CodePoint.EXTNAM,
435                 extnam.substring(0, extnamTruncateLength));
436     }
437
438     // Server Name is the name of the DDM server.
439
private void buildSRVNAM(String JavaDoc srvnam) throws SqlException {
440         int srvnamTruncateLength = Utils.min(srvnam.length(),
441                 NetConfiguration.SRVNAM_MAXSIZE);
442         writeScalarString(CodePoint.SRVNAM,
443                 srvnam.substring(0, srvnamTruncateLength));
444     }
445
446     // Server Product Release Level String specifies the product
447
// release level of a DDM server.
448
private void buildSRVRLSLV() throws SqlException {
449         // Hard-coded to ClientDNC 1.0 for dnc 1.0.
450
writeScalarString(CodePoint.SRVRLSLV, NetConfiguration.SRVRLSLV);
451     }
452
453     private void buildSRVCLSNM() throws SqlException {
454         // Server class name is hard-coded to QDERBY/JVM for dnc.
455
writeScalarString(CodePoint.SRVCLSNM, NetConfiguration.SRVCLSNM_JVM);
456     }
457
458     // Precondition: valid secmec is assumed.
459
private void buildSECMEC(int secmec) throws SqlException {
460         writeScalar2Bytes(CodePoint.SECMEC, secmec);
461     }
462
463     // Relational Database Name specifies the name of a relational database
464
// of the server.
465
// if length of RDB name <= 18 characters, there is not change to the format
466
// of the RDB name. The length of the RDBNAM remains fixed at 18 which includes
467
// any right bland padding if necessary.
468
// if length of the RDB name is > 18 characters, the length of the RDB name is
469
// identical to the length of the RDB name. No right blank padding is required.
470
private void buildRDBNAM(String JavaDoc rdbnam) throws SqlException {
471         // since this gets built more than once on the connect flow,
472
// see if we can optimize
473

474         int rdbnamLength = rdbnam.length();
475         if (rdbnamLength <= NetConfiguration.PKG_IDENTIFIER_FIXED_LEN) {
476             writeScalarPaddedString(CodePoint.RDBNAM,
477                     rdbnam,
478                     NetConfiguration.PKG_IDENTIFIER_FIXED_LEN); // minimum length of RDBNAM
479
} else {
480             if (rdbnamLength <= NetConfiguration.PKG_IDENTIFIER_MAX_LEN) {
481                 writeScalarString(CodePoint.RDBNAM, rdbnam);
482             } else {
483                 throw new SqlException(netAgent_.logWriter_,
484                     new ClientMessageId(SQLState.NET_DBNAME_TOO_LONG), rdbnam);
485             }
486             //"at SQLAM level " + netAgent_.targetSqlam_);
487
}
488     }
489
490     private void buildSECTKN(byte[] sectkn) throws SqlException {
491         if (sectkn.length > NetConfiguration.SECTKN_MAXSIZE) {
492             throw new SqlException(netAgent_.logWriter_,
493                 new ClientMessageId(SQLState.NET_SECTKN_TOO_LONG));
494         }
495         writeScalarBytes(CodePoint.SECTKN, sectkn);
496     }
497
498     private void buildUSRID(String JavaDoc usrid) throws SqlException {
499         int usridLength = usrid.length();
500         if ((usridLength == 0) || (usridLength > NetConfiguration.USRID_MAXSIZE)) {
501             throw new SqlException(netAgent_.logWriter_,
502                 new ClientMessageId(SQLState.NET_USERID_TOO_LONG));
503         }
504
505         writeScalarString(CodePoint.USRID, usrid);
506     }
507
508     private void buildPASSWORD(String JavaDoc password) throws SqlException {
509         int passwordLength = password.length();
510         if ((passwordLength == 0) || (passwordLength > NetConfiguration.PASSWORD_MAXSIZE)) {
511             throw new SqlException(netAgent_.logWriter_,
512                 new ClientMessageId(SQLState.NET_PASSWORD_TOO_LONG));
513         }
514         if (netAgent_.logWriter_ != null) {
515             // remember the position of password in order to
516
// mask it out in trace (see Request.sendBytes()).
517
passwordIncluded_ = true;
518             passwordStart_ = offset_ + 4;
519             passwordLength_ = passwordLength;
520         }
521         writeScalarString(CodePoint.PASSWORD, password);
522     }
523
524     private void buildRDBACCCL() throws SqlException {
525         writeScalar2Bytes(CodePoint.RDBACCCL, CodePoint.SQLAM);
526     }
527
528
529     private void buildPRDID() throws SqlException {
530         writeScalarString(CodePoint.PRDID, NetConfiguration.PRDID); // product id is hard-coded to DNC01000 for dnc 1.0.
531
}
532
533     private void buildPRDDTA(byte[] prddta) throws SqlException {
534         int prddtaLength = (prddta[NetConfiguration.PRDDTA_LEN_BYTE] & 0xff) + 1;
535         writeScalarBytes(CodePoint.PRDDTA, prddta, 0, prddtaLength);
536     }
537
538     private void buildTYPDEFNAM(String JavaDoc typdefnam) throws SqlException {
539         writeScalarString(CodePoint.TYPDEFNAM, typdefnam);
540     }
541
542     void buildTYPDEFOVR(boolean sendCcsidSbc,
543                         int ccsidSbc,
544                         boolean sendCcsidDbc,
545                         int ccsidDbc,
546                         boolean sendCcsidMbc,
547                         int ccsidMbc) throws SqlException {
548         markLengthBytes(CodePoint.TYPDEFOVR);
549         // write the single-byte ccsid used by this driver.
550
if (sendCcsidSbc) {
551             writeScalar2Bytes(CodePoint.CCSIDSBC, ccsidSbc);
552         }
553
554         // write the double-byte ccsid used by this driver.
555
if (sendCcsidDbc) {
556             writeScalar2Bytes(CodePoint.CCSIDDBC, ccsidDbc);
557         }
558
559         // write the mixed-byte ccsid used by this driver
560
if (sendCcsidMbc) {
561             writeScalar2Bytes(CodePoint.CCSIDMBC, ccsidMbc);
562         }
563
564         updateLengthBytes();
565
566     }
567
568     private void buildMGRLVLLS(int agent,
569                                int sqlam,
570                                int rdb,
571                                int secmgr,
572                                int xamgr,
573                                int syncptmgr,
574                                int rsyncmgr) throws SqlException {
575         markLengthBytes(CodePoint.MGRLVLLS);
576
577         // place the managers and their levels in the buffer
578
writeCodePoint4Bytes(CodePoint.AGENT, agent);
579         writeCodePoint4Bytes(CodePoint.SQLAM, sqlam);
580         writeCodePoint4Bytes(CodePoint.RDB, rdb);
581         writeCodePoint4Bytes(CodePoint.SECMGR, secmgr);
582
583         if (netAgent_.netConnection_.isXAConnection()) {
584             if (xamgr != NetConfiguration.MGRLVL_NA) {
585                 writeCodePoint4Bytes(CodePoint.XAMGR, xamgr);
586             }
587             if (syncptmgr != NetConfiguration.MGRLVL_NA) {
588                 writeCodePoint4Bytes(CodePoint.SYNCPTMGR, syncptmgr);
589             }
590             if (rsyncmgr != NetConfiguration.MGRLVL_NA) {
591                 writeCodePoint4Bytes(CodePoint.RSYNCMGR, rsyncmgr);
592             }
593         }
594         updateLengthBytes();
595     }
596
597     private void buildCRRTKN(byte[] crrtkn) throws SqlException {
598         writeScalarBytes(CodePoint.CRRTKN, crrtkn);
599     }
600
601     private void buildRDBALWUPD(boolean readOnly) throws SqlException {
602         if (readOnly) {
603             writeScalar1Byte(CodePoint.RDBALWUPD, CodePoint.FALSE);
604         }
605     }
606
607 }
608
609
610
611
Popular Tags