Source Code Cross Referenced for AbstractServlet.java in  » Database-DBMS » myoodb-2.2.1 » org » myoodb » core » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database DBMS » myoodb 2.2.1 » org.myoodb.core 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:///////////////////////////////////////////////////////////////////////////////
002://
003://   Copyright (C) 2003-@year@ by Thomas M. Hazel, MyOODB (www.myoodb.org)
004://
005://                          All Rights Reserved
006://
007://   This program is free software; you can redistribute it and/or modify
008://   it under the terms of the GNU General Public License and GNU Library
009://   General Public License as published by the Free Software Foundation;
010://   either version 2, or (at your option) any later version.
011://
012://   This program is distributed in the hope that it will be useful,
013://   but WITHOUT ANY WARRANTY; without even the implied warranty of
014://   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015://   GNU General Public License and GNU Library General Public License
016://   for more details.
017://
018://   You should have received a copy of the GNU General Public License
019://   and GNU Library General Public License along with this program; if
020://   not, write to the Free Software Foundation, 675 Mass Ave, Cambridge,
021://   MA 02139, USA.
022://
023:///////////////////////////////////////////////////////////////////////////////
024:package org.myoodb.core;
025:
026:import java.io.*;
027:import java.net.*;
028:import java.util.*;
029:
030:public class AbstractServlet extends AbstractConnection implements  java.lang.Runnable
031:{
032:    private static volatile int s_tunnelKeepAliveTime = @tunnelKeepAliveMs@;
033:    private static volatile String s_tunnelExtension = "@tunnelDefaultExtension@";
034:
035:    private Timer m_timer;
036:    private Thread m_keepAliveThread;
037:
038:    protected int m_timeout;
039:    protected int m_requestVal;
040:    protected long m_sessionIdentifier;
041:    protected volatile long m_lastSentTime;
042:
043:    protected URL m_url;
044:    protected URLConnection m_urlcon;
045:    protected ObjectInputStream m_in;
046:    protected ObjectOutputStream m_out;
047:
048:    public static void setTunnelExtension(String tunnelExtension)
049:    {
050:        s_tunnelExtension = tunnelExtension;
051:    }
052:
053:    public static String getTunnelExtension()
054:    {
055:        return s_tunnelExtension;
056:    }
057:
058:    public static void setTunnelKeepAliveTime(int time)
059:    {
060:        s_tunnelKeepAliveTime = time;
061:    }
062:
063:    public static int getTunnelKeepAliveTime()
064:    {
065:        return s_tunnelKeepAliveTime;
066:    }
067:
068:    public AbstractServlet(AbstractDatabase db, Long id, String host, int port, int timeout, boolean secure, boolean passThrough, String tunnelExtension) throws IOException
069:    {
070:        super (db, id);
071:
072:        m_timeout = timeout;
073:
074:        if (passThrough == false)
075:        {
076:            m_requestVal = TunnelManager.Protocol.USER_DATA;
077:        }
078:        else
079:        {
080:            m_requestVal = TunnelManager.Protocol.USER_DATA_TUNNEL;
081:        }
082:
083:        if (tunnelExtension == null)
084:        {
085:            tunnelExtension = getTunnelExtension();
086:        }
087:
088:        if (secure == true)
089:        {
090:            m_url = new URL("https://" + host + ":" + port + tunnelExtension);
091:        }
092:        else
093:        {
094:            m_url = new URL("http://" + host + ":" + port + tunnelExtension);
095:        }
096:
097:        init();
098:    }
099:
100:    private void start()
101:    {
102:        if (m_keepAliveThread == null)
103:        {
104:            m_keepAliveThread = new Thread(this );
105:            m_keepAliveThread.setDaemon(true);
106:            m_keepAliveThread.setName("MyOodb Servlet Thread: " + m_url);
107:            m_keepAliveThread.start();
108:        }
109:    }
110:
111:    protected void init() throws IOException
112:    {
113:        m_timer = null;
114:        m_urlcon = null;
115:        m_keepAliveThread = null;
116:        m_sessionIdentifier = -1;
117:
118:        start();
119:    }
120:
121:    public void run()
122:    {
123:        while (m_keepAliveThread != null)
124:        {
125:            ObjectInputStream in = null;
126:            ObjectOutputStream out = null;
127:
128:            try
129:            {
130:                Thread.sleep(getTunnelKeepAliveTime());
131:
132:                if (m_sessionIdentifier == -1)
133:                {
134:                    continue;
135:                }
136:                else if ((System.currentTimeMillis() - m_lastSentTime) < getTunnelKeepAliveTime())
137:                {
138:                    continue;
139:                }
140:
141:                URLConnection urlcon = m_url.openConnection();
142:
143:                urlcon.setDoInput(true);
144:                urlcon.setDoOutput(true);
145:                urlcon.setUseCaches(false);
146:                urlcon.setRequestProperty("Content-Type", "application/octet-stream");
147:
148:                out = getObjectOutputStream(urlcon.getOutputStream());
149:                try
150:                {
151:                    out.writeShort(TunnelManager.Protocol.KEEP_ALIVE);
152:                    out.writeLong(m_sessionIdentifier);
153:                    out.flush();
154:                }
155:                finally
156:                {
157:                    out.close();
158:                    out = null;
159:                }
160:
161:                in = getObjectInputStream(urlcon.getInputStream());
162:                in.close();
163:                in = null;
164:            }
165:            catch (Exception e1)
166:            {
167:                try
168:                {
169:                    if (out != null)
170:                    {
171:                        out.close();
172:                    }
173:                }
174:                catch (java.io.IOException e2)
175:                {
176:                    // nothing to do
177:                }
178:
179:                try
180:                {
181:                    if (in != null)
182:                    {
183:                        in.close();
184:                    }
185:                }
186:                catch (java.io.IOException e2)
187:                {
188:                    // nothing to do
189:                }
190:
191:                try
192:                {
193:                    Thread.sleep(1000);
194:                }
195:                catch (InterruptedException e2)
196:                {
197:                    // nothing to do
198:                }
199:            }
200:        }
201:    }
202:
203:    public void send(Object obj) throws IOException
204:    {
205:        m_urlcon = m_url.openConnection();
206:        m_urlcon.setDoInput(true);
207:        m_urlcon.setDoOutput(true);
208:        m_urlcon.setUseCaches(false);
209:        m_urlcon.setRequestProperty("Content-Type", "application/octet-stream");
210:
211:        if (m_timeout != -1)
212:        {
213:            m_urlcon.setConnectTimeout(m_timeout);
214:        }
215:
216:        m_out = getObjectOutputStream(m_urlcon.getOutputStream());
217:
218:        try
219:        {
220:            m_out.writeShort(m_requestVal);
221:            m_out.writeLong(m_sessionIdentifier);
222:            m_out.writeObject(obj);
223:            m_out.flush();
224:        }
225:        finally
226:        {
227:            m_lastSentTime = System.currentTimeMillis();
228:            m_out.close();
229:            m_out = null;
230:        }
231:    }
232:
233:    public Object receive(int timeout) throws IOException, ClassNotFoundException, org.myoodb.exception.TimeoutException
234:    {
235:        if (m_urlcon == null)
236:        {
237:            throw new IOException("connection has been closed");
238:        }
239:
240:        m_in = getObjectInputStream(m_urlcon.getInputStream());
241:
242:        try
243:        {
244:            if (timeout != -1)
245:            {
246:                m_timer = new Timer(m_in, timeout);
247:                m_timer.setDaemon(true);
248:                m_timer.setName("MyOodb Send Http Timeout Thread: " + m_url);
249:                m_timer.start();
250:            }
251:
252:            Object result = null;
253:
254:            try
255:            {
256:                m_sessionIdentifier = m_in.readLong();
257:                result = m_in.readObject();
258:            }
259:            catch (IOException e)
260:            {
261:                if ((timeout != -1) && (m_timer.hasExpired() == true))
262:                {
263:                    throw new org.myoodb.exception.TimeoutException("Receive timed out");
264:                }
265:                else
266:                {
267:                    throw e;
268:                }
269:            }
270:            finally
271:            {
272:                if (timeout != -1)
273:                {
274:                    m_timer.halt();
275:                    m_timer = null;
276:                }
277:            }
278:
279:            return result;
280:        }
281:        finally
282:        {
283:            m_in.close();
284:            m_in = null;
285:        }
286:    }
287:
288:    public void close() throws IOException
289:    {
290:        m_url = null;
291:        m_urlcon = null;
292:        m_keepAliveThread = null;
293:        m_sessionIdentifier = -1;
294:
295:        ObjectInputStream in = m_in;
296:        m_in = null;
297:
298:        ObjectOutputStream out = m_out;
299:        m_out = null;
300:
301:        if (out != null)
302:        {
303:            out.close();
304:        }
305:
306:        if (in != null)
307:        {
308:            in.close();
309:        }
310:    }
311:
312:    public boolean isConnected()
313:    {
314:        return (m_url != null);
315:    }
316:
317:    public URL getURL()
318:    {
319:        return m_url;
320:    }
321:}
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.