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:}
|