KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.drda.Session
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.impl.drda;
23
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.OutputStream JavaDoc;
27 import java.net.Socket JavaDoc;
28 import java.util.Enumeration JavaDoc;
29 import java.util.Hashtable JavaDoc;
30 import org.apache.derby.iapi.tools.i18n.LocalizedResource;
31 import java.sql.SQLException JavaDoc;
32
33 /**
34     Session stores information about the current session
35     It is used so that a DRDAConnThread can work on any session.
36 */

37 class Session
38 {
39
40     // session states
41
protected static final int INIT = 1; // before exchange of server attributes
42
protected static final int ATTEXC = 2; // after first exchange of server attributes
43
protected static final int SECACC = 3; // after ACCSEC (Security Manager Accessed)
44
protected static final int CHKSEC = 4; // after SECCHK (Checked Security)
45
protected static final int CLOSED = 5; // session has ended
46

47     // session types
48
protected static final int DRDA_SESSION = 1;
49     protected static final int CMD_SESSION = 2;
50     
51     // trace name prefix and suffix
52
private static final String JavaDoc TRACENAME_PREFIX = "Server";
53     private static final String JavaDoc TRACENAME_SUFFIX = ".trace";
54
55     // session information
56
protected Socket JavaDoc clientSocket; // session socket
57
protected int connNum; // connection number
58
protected InputStream JavaDoc sessionInput; // session input stream
59
protected OutputStream JavaDoc sessionOutput; // session output stream
60
protected String JavaDoc traceFileName; // trace file name for session
61
protected boolean traceOn; // whether trace is currently on for the session
62
protected int state; // the current state of the session
63
protected int sessionType; // type of session - DRDA or NetworkServerControl command
64
protected String JavaDoc drdaID; // DRDA ID of the session
65
protected DssTrace dssTrace; // trace object associated with the session
66
protected AppRequester appRequester; // Application requester for this session
67
protected Database database; // current database
68
protected int qryinsid; // unique identifier for each query
69
protected LocalizedResource langUtil; // localization information for command session
70
// client
71

72     private Hashtable JavaDoc dbtable; // Table of databases accessed in this session
73

74     // constructor
75
/**
76      * Session constructor
77      *
78      * @param connNum connection number
79      * @param clientSocket communications socket for this session
80      * @param traceDirectory location for trace files
81      * @param traceOn whether to start tracing this connection
82      *
83      * @exception throws IOException
84      */

85     Session (int connNum, Socket JavaDoc clientSocket, String JavaDoc traceDirectory,
86             boolean traceOn) throws IOException JavaDoc
87     {
88         this.connNum = connNum;
89         this.clientSocket = clientSocket;
90         this.traceOn = traceOn;
91         if (traceOn)
92             dssTrace = new DssTrace();
93         dbtable = new Hashtable JavaDoc();
94         initialize(traceDirectory);
95     }
96
97     /**
98      * Close session - close connection sockets and set state to closed
99      *
100      */

101     protected void close() throws SQLException JavaDoc
102     {
103         
104         try {
105             sessionInput.close();
106             sessionOutput.close();
107             clientSocket.close();
108             if (dbtable != null)
109                 for (Enumeration JavaDoc e = dbtable.elements() ; e.hasMoreElements() ;)
110                 {
111                     ((Database) e.nextElement()).close();
112                 }
113             
114         }catch (IOException JavaDoc e) {} // ignore IOException when we are shutting down
115
finally {
116             state = CLOSED;
117             dbtable = null;
118             database = null;
119         }
120     }
121
122     /**
123      * initialize a server trace for the DRDA protocol
124      *
125      * @param traceDirectory - directory for trace file
126      */

127     protected void initTrace(String JavaDoc traceDirectory)
128     {
129         if (traceDirectory != null)
130             traceFileName = traceDirectory + "/" + TRACENAME_PREFIX+
131                 connNum+ TRACENAME_SUFFIX;
132         else
133             traceFileName = TRACENAME_PREFIX +connNum+ TRACENAME_SUFFIX;
134         traceOn = true;
135         if (dssTrace == null)
136             dssTrace = new DssTrace();
137         dssTrace.startComBufferTrace (traceFileName);
138     }
139
140     /**
141      * Set tracing on
142      *
143      * @param traceDirectory directory for trace files
144      */

145     protected void setTraceOn(String JavaDoc traceDirectory)
146     {
147         if (traceOn)
148             return;
149         initTrace(traceDirectory);
150     }
151     /**
152      * Get whether tracing is on
153      *
154      * @return true if tracing is on false otherwise
155      */

156     protected boolean isTraceOn()
157     {
158         if (traceOn)
159             return true;
160         else
161             return false;
162     }
163
164     /**
165      * Get connection number
166      *
167      * @return connection number
168      */

169     protected int getConnNum()
170     {
171         return connNum;
172     }
173
174     /**
175      * Set tracing off
176      *
177      */

178     protected void setTraceOff()
179     {
180         if (! traceOn)
181             return;
182         traceOn = false;
183         if (traceFileName != null)
184             dssTrace.stopComBufferTrace();
185     }
186     /**
187      * Add database to session table
188      */

189     protected void addDatabase(Database d)
190     {
191         dbtable.put(d.dbName, d);
192     }
193
194     /**
195      * Get database
196      */

197     protected Database getDatabase(String JavaDoc dbName)
198     {
199         return (Database)dbtable.get(dbName);
200     }
201
202     /**
203      * Get requried security checkpoint.
204      * Used to verify EXCSAT/ACCSEC/SECCHK order.
205      *
206      * @return next required Security checkpoint or -1 if
207      * neither ACCSEC or SECCHK are required at this time.
208      *
209      */

210     protected int getRequiredSecurityCodepoint()
211     {
212         switch (state)
213         {
214             case ATTEXC:
215                 // On initial exchange of attributes we require ACCSEC
216
// to access security manager
217
return CodePoint.ACCSEC;
218             case SECACC:
219                 // After security manager has been accessed successfully we
220
// require SECCHK to check security
221
return CodePoint.SECCHK;
222             default:
223                 return -1;
224         }
225     }
226
227     /**
228      * Check if a security codepoint is required
229      *
230      * @return true if ACCSEC or SECCHK are required at this time.
231      */

232     protected boolean requiresSecurityCodepoint()
233     {
234         return (getRequiredSecurityCodepoint() != -1);
235     }
236
237     /**
238      * Set Session state
239      *
240      */

241     protected void setState(int s)
242     {
243         state = s;
244     }
245     
246     /**
247      * Get session into initial state
248      *
249      * @param traceDirectory - directory for trace files
250      */

251     private void initialize(String JavaDoc traceDirectory)
252         throws IOException JavaDoc
253     {
254         sessionInput = clientSocket.getInputStream();
255         sessionOutput = clientSocket.getOutputStream();
256         if (traceOn)
257             initTrace(traceDirectory);
258         state = INIT;
259     }
260
261     protected String JavaDoc buildRuntimeInfo(String JavaDoc indent, LocalizedResource localLangUtil)
262     {
263         String JavaDoc s = "";
264         s += indent + localLangUtil.getTextMessage("DRDA_RuntimeInfoSessionNumber.I")
265             + connNum + "\n";
266         if (database == null)
267             return s;
268         s += database.buildRuntimeInfo(indent,localLangUtil);
269         s += "\n";
270         return s;
271     }
272 }
273
Popular Tags