KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > drda > DRDAProtocolException


1 /*
2
3    Derby - Class org.apache.derby.impl.drda.DRDAProtocolException
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 /*
23
24 /**
25     DRDAProtocolException is the root of all protocol exceptions that are
26     handled in a standard fashion by the DRDA AS.
27     If a protocol error message needs to send more than
28     SVRCOD, an ERRCD and CODPNT arg it should be subclassed
29
30     @author marsden
31 */

32
33 package org.apache.derby.impl.drda;
34 import java.util.Hashtable JavaDoc;
35 import org.apache.derby.iapi.services.sanity.SanityManager;
36
37 class DRDAProtocolException extends Exception JavaDoc
38 {
39     
40     /* Static values, used in constructor if there is no associated
41        Error Code or the codepoint argument.
42     */

43     
44     protected static final int NO_ASSOC_ERRCD = 0;
45     protected static final int NO_CODPNT_ARG = 0;
46
47     
48     private DRDAConnThread agent;
49
50     // request correlation id
51
private int correlationID;
52     
53     // correlation token
54
private byte[] crrtkn;
55
56     //Codepoint arg
57
private int codpntArg;
58
59     private DRDAProtocolExceptionInfo exceptionInfo;
60
61     // CodePoint of this error
62
private int errorCodePoint;
63     
64     // Severity Code
65
private int svrcod;
66     
67     // error code (e.g. SYNERRCD)
68
private int errcd;
69     
70     // messageid for logging errors.
71
private String JavaDoc messageid;
72
73     // database name
74
private String JavaDoc rdbnam;
75     
76     // database diagnostic information
77
private String JavaDoc srvdgn;
78
79     // message arguments
80
private Object JavaDoc [] messageArgs;
81     
82     // A verbose error message string, will be helpful
83
// when getMessage() is called on this Exception object
84
private String JavaDoc msg;
85     
86     private static Hashtable JavaDoc errorInfoTable;
87     
88     protected static String JavaDoc DRDA_Proto_CMDCHKRM= "DRDA_Proto_CMDCHKRM";
89     protected static String JavaDoc DRDA_Proto_CMDNSPRM= "DRDA_Proto_CMDNSPRM";
90     protected static String JavaDoc DRDA_Proto_DTAMCHRM= "DRDA_Proto_DTAMCHRM";
91
92     protected static String JavaDoc DRDA_Proto_OBJNSPRM = "DRDA_Proto_OBJNSPRM";
93     protected static String JavaDoc DRDA_Proto_PKGBNARM= "DRDA_Proto_PKGBNARM";
94     protected static String JavaDoc DRDA_Proto_PRCCNVRM= "DRDA_Proto_PRCCNVRM";
95     protected static String JavaDoc DRDA_Proto_PRMNSRM = "DRDA_Proto_PRMNSPRM";
96
97     protected static String JavaDoc DRDA_Proto_SYNTAXRM= "DRDA_Proto_SYNTAXRM";
98     protected static String JavaDoc DRDA_Proto_VALNSPRM= "DRDA_Proto_VALNSPRM";
99     protected static String JavaDoc DRDA_Proto_MGRLVLRM= "DRDA_Proto_MGRLVLRM";
100     protected static String JavaDoc DRDA_Proto_RDBNFNRM= "DRDA_Proto_RDBNFNRM";
101
102     protected static String JavaDoc DRDA_Disconnect= "DRDA_Disconnect";
103     protected static String JavaDoc DRDA_AgentError= "DRDA_AgentError";
104
105     static {
106     /* Create the errorInfoTable
107        The Hashtable is keyed on messageid and holds
108        DRDAProtocolExceptionInfo for each of our messages.
109     */

110     
111     errorInfoTable = new Hashtable JavaDoc();
112     
113     errorInfoTable.put(
114                DRDA_Proto_CMDCHKRM,
115                new DRDAProtocolExceptionInfo(
116                               CodePoint.CMDCHKRM,
117                               CodePoint.SVRCOD_ERROR,
118                               NO_ASSOC_ERRCD,
119                               false));
120
121     errorInfoTable.put(
122                DRDA_Proto_CMDNSPRM,
123                new DRDAProtocolExceptionInfo(
124                               CodePoint.CMDNSPRM,
125                               CodePoint.SVRCOD_ERROR,
126                               NO_ASSOC_ERRCD,
127                               true));
128     errorInfoTable.put(
129                DRDA_Proto_DTAMCHRM,
130                new DRDAProtocolExceptionInfo(
131                               CodePoint.DTAMCHRM,
132                               CodePoint.SVRCOD_ERROR,
133                               NO_ASSOC_ERRCD,
134                               false));
135     errorInfoTable.put(
136                DRDA_Proto_OBJNSPRM,
137                new DRDAProtocolExceptionInfo(
138                               CodePoint.OBJNSPRM,
139                               CodePoint.SVRCOD_ERROR,
140                               NO_ASSOC_ERRCD,
141                               true));
142         
143     errorInfoTable.put(
144                        DRDA_Proto_PKGBNARM,
145                        new DRDAProtocolExceptionInfo(
146                                CodePoint.PKGBNARM,
147                                CodePoint.SVRCOD_ERROR,
148                                NO_ASSOC_ERRCD,
149                                false));
150                
151     errorInfoTable.put(DRDA_Proto_PRCCNVRM,
152                new DRDAProtocolExceptionInfo(
153                              CodePoint.PRCCNVRM,
154                              CodePoint.SVRCOD_ERROR,
155                              CodePoint.PRCCNVCD,
156                              false));
157
158     errorInfoTable.put(DRDA_Proto_SYNTAXRM,
159                new DRDAProtocolExceptionInfo(
160                              CodePoint.SYNTAXRM,
161                              CodePoint.SVRCOD_ERROR,
162                              CodePoint.SYNERRCD,
163                              true));
164
165     errorInfoTable.put(DRDA_Proto_VALNSPRM,
166                new DRDAProtocolExceptionInfo(
167                              CodePoint.VALNSPRM,
168                              CodePoint.SVRCOD_ERROR,
169                              NO_ASSOC_ERRCD,
170                              true));
171
172     errorInfoTable.put(DRDA_Proto_MGRLVLRM,
173                new DRDAProtocolExceptionInfo(
174                              CodePoint.MGRLVLRM,
175                              CodePoint.SVRCOD_ERROR,
176                              NO_ASSOC_ERRCD,
177                              false));
178
179     errorInfoTable.put(DRDA_Proto_RDBNFNRM,
180                new DRDAProtocolExceptionInfo(
181                              CodePoint.RDBNFNRM,
182                              CodePoint.SVRCOD_ERROR,
183                              NO_ASSOC_ERRCD,
184                              false));
185
186                
187     errorInfoTable.put(DRDA_Disconnect,
188                new DRDAProtocolExceptionInfo(
189                              0,
190                              0,
191                              NO_ASSOC_ERRCD,
192                              false));
193
194     // Permanent Agent Error (AGNPRMRM) Reply Message indicates that the command
195
// requested could not be completed because of a permanent error
196
// condition detected at the target system.
197
errorInfoTable.put(DRDA_AgentError,
198                new DRDAProtocolExceptionInfo(
199                              CodePoint.AGNPRMRM,
200                              CodePoint.SVRCOD_PRMDMG,
201                              NO_ASSOC_ERRCD,
202                              false));
203
204     }
205         
206     
207     /** Create a new Protocol exception
208      *
209      * @param agent DRDAConnThread that threw this exception
210      *
211      * @param cpArg CODPNT value to pass to send
212      *
213      *
214      * @param msgid The messageid for this message. (needs to be
215      * integrated into logging mechanism)
216      *
217      * @param args Argments for the message in an Object[]
218      *
219      */

220     
221     DRDAProtocolException(String JavaDoc msgid,
222                                     DRDAConnThread agent,
223                                     int cpArg,
224                                     int errCdArg, Object JavaDoc []args)
225                         
226     {
227         
228         boolean agentError = false;
229
230         exceptionInfo =
231             (DRDAProtocolExceptionInfo) errorInfoTable.get(msgid);
232                 
233         if (agent != null)
234         {
235             this.correlationID = agent.getCorrelationID();
236             this.crrtkn = agent.getCrrtkn();
237         }
238
239         this.codpntArg= cpArg;
240         this.errorCodePoint = exceptionInfo.errorCodePoint;
241         this.errcd = errCdArg;
242         this.messageid = msgid;
243
244         if (msgid.equals(DRDA_AgentError))
245         {
246             this.svrcod = ((Integer JavaDoc)args[0]).intValue();
247             this.rdbnam = (String JavaDoc)args[1];
248             // retrieve the server diagnostic error message
249
String JavaDoc srvdgn = (String JavaDoc)args[2];
250             msg = "Execution failed because of Permanent Agent Error: SVRCOD = " +
251                 java.lang.Integer.toHexString(this.svrcod) +
252                 "; RDBNAM = "+ rdbnam +"; diagnostic msg = "+ srvdgn;
253             agentError = true;
254         }
255         else if (msgid.equals(DRDA_Proto_RDBNFNRM))
256         {
257             this.svrcod = exceptionInfo.svrcod;
258             this.rdbnam = (String JavaDoc)args[0];
259             msg = "Execution failed because of Distributed Protocol Error: "
260                 + messageid +
261                 "; RDBNAM = "+ rdbnam;
262         }
263         else
264         {
265             this.svrcod = exceptionInfo.svrcod;
266             msg = "Execution failed because of a Distributed Protocol Error: "
267                 + messageid +
268                 "; CODPNT arg = " + java.lang.Integer.toHexString(cpArg) +
269                 "; Error Code Value = " + java.lang.Integer.toHexString(errCdArg);
270         }
271         
272         
273         if (!agentError && args != null)
274         {
275             messageArgs = args;
276             for (int i = 0; i < args.length; i++)
277             {
278                 //args contain managers and manager levels display in hex
279
if (msgid.equals(DRDA_Proto_MGRLVLRM))
280                     msg += "," +
281                         java.lang.Integer.toHexString(((Integer JavaDoc)args[i]).intValue());
282                 else
283                     msg += "," + args[i];
284                 
285             }
286         }
287
288
289         // for now dump all errors except disconnects to console
290
// and log
291
if (!isDisconnectException())
292         {
293             DRDAConnThread.println2Log(agent.getDbName(),
294                                    agent.getSession().drdaID,
295                                    msg);
296             NetworkServerControlImpl s = agent.getServer();
297             s.consoleExceptionPrintTrace(this);
298         }
299     }
300     
301     // Constructor with no additional args
302
DRDAProtocolException(String JavaDoc msgid,
303                                     DRDAConnThread agent,
304                                     int cpArg,
305                                     int errCdArg)
306     {
307         this(msgid,agent, cpArg, errCdArg, (Object JavaDoc []) null);
308     }
309
310
311     protected static DRDAProtocolException newDisconnectException(DRDAConnThread
312                                                                   agent,Object JavaDoc[] args)
313     {
314         return new DRDAProtocolException(DRDA_Disconnect,
315                                          agent,
316                                          NO_CODPNT_ARG,
317                                          NO_ASSOC_ERRCD,
318                                          args);
319         
320     }
321     
322     protected static DRDAProtocolException newAgentError(DRDAConnThread agent,
323         int svrcod, String JavaDoc rdbnam, String JavaDoc srvdgn)
324     {
325         if ( SanityManager.DEBUG )
326             agent.trace("agentError in " + agent);
327         Object JavaDoc[] oa = {new Integer JavaDoc(svrcod), rdbnam, srvdgn};
328         return new DRDAProtocolException(DRDA_AgentError,
329                                         agent,
330                                         NO_CODPNT_ARG,
331                                         NO_ASSOC_ERRCD,
332                                         oa);
333     }
334     
335     protected final boolean isDisconnectException()
336     {
337         return (errorCodePoint == 0);
338     }
339     
340     /** write will write the Error information to the buffer.
341      * Most errors will write only the codepoint and svrcod
342      * Where appropriate the codepoint specific error code and
343      * codePoint of origin will be written
344      *
345      * @param writer The DDMWriter for the agent.
346      */

347     
348     protected void write(DDMWriter writer)
349     {
350         //Writing Protocol Error
351
writer.createDssReply();
352         writer.startDdm(errorCodePoint);
353         writer.writeScalar2Bytes(CodePoint.SVRCOD,svrcod);
354         if (exceptionInfo.sendsCodpntArg)
355             writer.writeScalar2Bytes(CodePoint.CODPNT,codpntArg);
356         if (exceptionInfo.errCdCodePoint != NO_ASSOC_ERRCD)
357             writer.writeScalar1Byte(exceptionInfo.errCdCodePoint,
358                                     errcd);
359         if (rdbnam != null && agent != null)
360         {
361             try {
362                 agent.writeRDBNAM(rdbnam);
363             } catch (DRDAProtocolException e) {} //ignore exceptions while processing
364
}
365         // for MGRLVLRM, need to write out the manager levels
366
if (errorCodePoint == CodePoint.MGRLVLRM)
367         {
368             writer.startDdm(CodePoint.MGRLVLLS);
369             for (int i = 0; i < messageArgs.length ; i += 2)
370             {
371                 writer.writeNetworkShort(((Integer JavaDoc)messageArgs[i]).intValue());
372                 writer.writeNetworkShort(((Integer JavaDoc)messageArgs[i+1]).intValue());
373             }
374             writer.endDdm();
375         }
376         writer.endDdmAndDss();
377     }
378     
379     /**
380      * Override getMessage()
381      * @return the server diagnostic error message for this exception
382      */

383     public String JavaDoc getMessage()
384     {
385         return msg;
386     }
387 }
388
389
390
391
392
393
394
Popular Tags