001: /*
002: License $Id: JoRemoteSessionContextWrapper.java,v 1.4 2003/09/13 04:59:58 hendriks73 Exp $
003:
004: Copyright (c) 2001-2005 tagtraum industries.
005:
006: LGPL
007: ====
008:
009: jo! is free software; you can redistribute it and/or
010: modify it under the terms of the GNU Lesser General Public
011: License as published by the Free Software Foundation; either
012: version 2.1 of the License, or (at your option) any later version.
013:
014: jo! is distributed in the hope that it will be useful,
015: but WITHOUT ANY WARRANTY; without even the implied warranty of
016: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: Lesser General Public License for more details.
018:
019: You should have received a copy of the GNU Lesser General Public
020: License along with this library; if not, write to the Free Software
021: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
022:
023: For LGPL see <http://www.fsf.org/copyleft/lesser.txt>
024:
025:
026: Sun license
027: ===========
028:
029: This release contains software by Sun Microsystems. Therefore
030: the following conditions have to be met, too. They apply to the
031: files
032:
033: - lib/mail.jar
034: - lib/activation.jar
035: - lib/jsse.jar
036: - lib/jcert.jar
037: - lib/jaxp.jar
038: - lib/crimson.jar
039: - lib/servlet.jar
040: - lib/jnet.jar
041: - lib/jaas.jar
042: - lib/jaasmod.jar
043:
044: contained in this release.
045:
046: a. Licensee may not modify the Java Platform
047: Interface (JPI, identified as classes contained within the javax
048: package or any subpackages of the javax package), by creating additional
049: classes within the JPI or otherwise causing the addition to or modification
050: of the classes in the JPI. In the event that Licensee creates any
051: Java-related API and distribute such API to others for applet or
052: application development, you must promptly publish broadly, an accurate
053: specification for such API for free use by all developers of Java-based
054: software.
055:
056: b. Software is confidential copyrighted information of Sun and
057: title to all copies is retained by Sun and/or its licensors. Licensee
058: shall not modify, decompile, disassemble, decrypt, extract, or otherwise
059: reverse engineer Software. Software may not be leased, assigned, or
060: sublicensed, in whole or in part. Software is not designed or intended
061: for use in on-line control of aircraft, air traffic, aircraft navigation
062: or aircraft communications; or in the design, construction, operation or
063: maintenance of any nuclear facility. Licensee warrants that it will not
064: use or redistribute the Software for such purposes.
065:
066: c. Software is provided "AS IS," without a warranty
067: of any kind. ALL EXPRESS OR IMPLIED REPRESENTATIONS AND WARRANTIES,
068: INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
069: PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED.
070:
071: d. This License is effective until terminated. Licensee may
072: terminate this License at any time by destroying all copies of Software.
073: This License will terminate immediately without notice from Sun if Licensee
074: fails to comply with any provision of this License. Upon such termination,
075: Licensee must destroy all copies of Software.
076:
077: e. Software, including technical data, is subject to U.S.
078: export control laws, including the U.S. Export Administration Act and its
079: associated regulations, and may be subject to export or import regulations
080: in other countries. Licensee agrees to comply strictly with all such
081: regulations and acknowledges that it has the responsibility to obtain
082: licenses to export, re-export, or import Software. Software may not be
083: downloaded, or otherwise exported or re-exported (i) into, or to a national
084: or resident of, Cuba, Iraq, Iran, North Korea, Libya, Sudan, Syria or any
085: country to which the U.S. has embargoed goods; or (ii) to anyone on the
086: U.S. Treasury Department's list of Specially Designated Nations or the U.S.
087: Commerce Department's Table of Denial Orders.
088:
089:
090: Feedback
091: ========
092:
093: We encourage your feedback and suggestions and want to use your feedback to
094: improve the Software. Send all such feedback to:
095: <feedback@tagtraum.com>
096:
097: For more information on tagtraum industries and jo!
098: please see <http://www.tagtraum.com/>.
099:
100:
101: */
102: package com.tagtraum.jobalancer;
103:
104: import com.tagtraum.framework.log.C_Log;
105: import com.tagtraum.framework.log.Log;
106: import com.tagtraum.framework.util.FactoryException;
107: import com.tagtraum.jo.*;
108:
109: import javax.servlet.http.HttpSession;
110: import java.rmi.Naming;
111: import java.rmi.RemoteException;
112: import java.util.Enumeration;
113:
114: /**
115: * Wrapper around the {@link I_JoRemoteSessionContext} to
116: * hide RemoteException signatures.
117: *
118: * @author Hendrik Schreiber
119: * @version 1.1beta1 $Id: JoRemoteSessionContextWrapper.java,v 1.4 2003/09/13 04:59:58 hendriks73 Exp $
120: */
121: public class JoRemoteSessionContextWrapper implements
122: I_JoSessionContext, C_Jo, C_JoBalancer {
123:
124: /**
125: * Source-Version
126: */
127: public static String vcid = "$Id: JoRemoteSessionContextWrapper.java,v 1.4 2003/09/13 04:59:58 hendriks73 Exp $";
128:
129: /**
130: *
131: */
132: protected I_JoHost myHost;
133:
134: /**
135: *
136: */
137: protected I_JoServletContextPeer myPeer;
138:
139: /**
140: *
141: */
142: protected I_JoRemoteSessionContext myRemoteSessionContext;
143:
144: /**
145: *
146: */
147: protected String myIdentifier;
148:
149: /**
150: * Constructor declaration
151: *
152: *
153: * @see
154: */
155: public JoRemoteSessionContextWrapper() {
156: }
157:
158: /**
159: * Initialisiert den Kontext.
160: */
161: public void init(I_JoServletContextPeer aPeer) {
162: myPeer = aPeer;
163: myHost = (I_JoHost) aPeer.getHost();
164: myIdentifier = myHost.getService().getServerInfo() + " {";
165:
166: Enumeration en = myHost.getService().listenerNames();
167:
168: while (en.hasMoreElements()) {
169: myIdentifier += " "
170: + myHost.getService().getListener(
171: (String) en.nextElement()).toString();
172: }
173:
174: myIdentifier += "} " + myHost.getName() + "."
175: + myPeer.getName();
176:
177: String theURL = (String) myPeer
178: .getAttribute(C_RemoteSessionContextURL);
179:
180: if (theURL == null) {
181: theURL = C_Default_RemoteSessionContextURL;
182: // this looks nasty (rik)
183:
184: }
185:
186: try {
187: // System.setSecurityManager(new RMISecurityManager());
188: myRemoteSessionContext = (I_JoRemoteSessionContext) Naming
189: .lookup(theURL);
190:
191: if (Log.getLog(myHost.getName()).isLog(C_Log.MODULE)) {
192: Log
193: .getLog(myHost.getName())
194: .log(
195: "Initialization of RemoteSessionContext successfull.",
196: C_Log.MODULE);
197: }
198: } catch (java.rmi.ConnectException ce) {
199: if (Log.getLog(myHost.getName()).isLog(C_Log.ERROR)) {
200: Log.getLog(myHost.getName()).log(
201: "Initialization of SessionContext failed. Couldn't connect to '"
202: + theURL + "'.", C_Log.ERROR);
203: Log
204: .getLog(myHost.getName())
205: .log(
206: "Please check whether the RemoteSessionContext is started.",
207: C_Log.ERROR);
208: }
209:
210: throw new JoBalancerIOException(ce);
211: } catch (Exception e) {
212: if (Log.getLog(myHost.getName()).isLog(C_Log.ERROR)) {
213: Log.getLog(myHost.getName()).log(
214: "Initialization of SessionContext failed: "
215: + e.toString(), C_Log.ERROR);
216: }
217:
218: throw new JoBalancerIOException(e);
219: }
220:
221: try {
222: myRemoteSessionContext.register(myIdentifier);
223: } catch (RemoteException re) {
224: if (Log.getLog(myHost.getName()).isLog(C_Log.ERROR)) {
225: Log.getLog(myHost.getName()).log(
226: "Failed to register at RemoteSessionContext.",
227: C_Log.ERROR);
228: Log.getLog(myHost.getName()).log(re, C_Log.ERROR);
229: }
230:
231: throw new JoBalancerIOException(
232: "Failed to register at RemoteSessionContext.", re);
233: }
234: }
235:
236: /**
237: * Method.
238: *
239: *
240: * @see
241: */
242: public void run() {
243: } // should we remove Runnable from I_JoSessionContext...? (rik)
244:
245: /**
246: * Returns a unique new sessionid.
247: *
248: * @return new session ID
249: */
250: public String getNewId() {
251: try {
252: return myRemoteSessionContext.getNewId();
253: } catch (RemoteException re) {
254: if (re.detail instanceof RuntimeException) {
255: throw (RuntimeException) re.detail;
256: }
257:
258: if (Log.getLog(myHost.getName()).isLog(C_Log.ERROR)) {
259: Log
260: .getLog(myHost.getName())
261: .log(
262: "Failed to obtain new session ID from RemoteSessionContext.",
263: C_Log.ERROR);
264: Log.getLog(myHost.getName()).log(re, C_Log.ERROR);
265: }
266:
267: throw new JoBalancerIOException(
268: "Failed to obtain new session ID from RemoteSessionContext.",
269: re);
270: }
271: }
272:
273: /**
274: * Unregisters this client from the RemoteSessionContext.
275: */
276: public void destroy() {
277: try {
278: myRemoteSessionContext.unregister(myIdentifier);
279: } catch (RemoteException re) {
280: if (re.detail instanceof RuntimeException) {
281: throw (RuntimeException) re.detail;
282: }
283:
284: if (Log.getLog(myHost.getName()).isLog(C_Log.ERROR)) {
285: Log
286: .getLog(myHost.getName())
287: .log(
288: "Failed to unregister from RemoteSessionContext.",
289: C_Log.ERROR);
290: Log.getLog(myHost.getName()).log(re, C_Log.ERROR);
291: }
292:
293: throw new JoBalancerIOException(
294: "Failed to unregister from RemoteSessionContext.",
295: re);
296: }
297: }
298:
299: /**
300: * Returns the session with the specified id.
301: *
302: * @param sessionID the id
303: * @return the sesseion or <code>null</code>
304: */
305: public HttpSession getSession(String sessionId) {
306: try {
307: I_JoRemoteSession rs = myRemoteSessionContext
308: .getSession(sessionId);
309:
310: if (rs == null) {
311: return null;
312: }
313:
314: return new JoRemoteSessionWrapper(rs, myPeer); // this might mean that we don"t have session == session in consecutive calls. Do we care? (rik)
315: } catch (RemoteException re) {
316: if (re.detail instanceof RuntimeException) {
317: throw (RuntimeException) re.detail;
318: }
319:
320: if (Log.getLog(myHost.getName()).isLog(C_Log.ERROR)) {
321: Log
322: .getLog(myHost.getName())
323: .log(
324: "Failed to get session from RemoteSessionContext.",
325: C_Log.ERROR);
326: Log.getLog(myHost.getName()).log(re, C_Log.ERROR);
327: }
328:
329: throw new JoBalancerIOException(
330: "Failed to get session from RemoteSessionContext.",
331: re);
332: }
333: }
334:
335: /**
336: * Creates a new session.
337: *
338: * @return Session or <code>null</code>
339: */
340: public I_JoSession newSession() throws FactoryException {
341: try {
342: return new JoRemoteSessionWrapper(myRemoteSessionContext
343: .newSession(), myPeer);
344: } catch (RemoteException re) {
345: if (re.detail instanceof RuntimeException) {
346: throw (RuntimeException) re.detail;
347: }
348:
349: if (Log.getLog(myHost.getName()).isLog(C_Log.ERROR)) {
350: Log
351: .getLog(myHost.getName())
352: .log(
353: "Failed to obtain a new session from RemoteSessionContext.",
354: C_Log.ERROR);
355: Log.getLog(myHost.getName()).log(re, C_Log.ERROR);
356: }
357:
358: throw new JoBalancerIOException(
359: "Failed to obtain a new session from RemoteSessionContext.",
360: re);
361: }
362: }
363:
364: /**
365: * Adds a session to the RemoteSessionContext
366: *
367: * @param aSession a session
368: */
369: public void addSession(HttpSession aSession) {
370: try {
371: myRemoteSessionContext
372: .addSession(((JoRemoteSessionWrapper) aSession)
373: .getRemoteSession());
374: } catch (RemoteException re) {
375: if (re.detail instanceof RuntimeException) {
376: throw (RuntimeException) re.detail;
377: }
378:
379: if (Log.getLog(myHost.getName()).isLog(C_Log.ERROR)) {
380: Log
381: .getLog(myHost.getName())
382: .log(
383: "Failed to add session to RemoteSessionContext.",
384: C_Log.ERROR);
385: Log.getLog(myHost.getName()).log(re, C_Log.ERROR);
386: }
387:
388: throw new JoBalancerIOException(
389: "Failed to add session to RemoteSessionContext.",
390: re);
391: }
392: }
393:
394: /**
395: * Removes a session.
396: *
397: * @param anID a sessionID
398: */
399: public void removeSession(String anID) {
400: try {
401: myRemoteSessionContext.removeSession(anID);
402: } catch (RemoteException re) {
403: if (re.detail instanceof RuntimeException) {
404: throw (RuntimeException) re.detail;
405: }
406:
407: if (Log.getLog(myHost.getName()).isLog(C_Log.ERROR)) {
408: Log
409: .getLog(myHost.getName())
410: .log(
411: "Failed to remove a session from RemoteSessionContext.",
412: C_Log.ERROR);
413: Log.getLog(myHost.getName()).log(re, C_Log.ERROR);
414: }
415:
416: throw new JoBalancerIOException(
417: "Failed to remove a session from RemoteSessionContext.",
418: re);
419: }
420: }
421:
422: /**
423: * Invalidates all sessions.
424: */
425: public void invalidateAllSessions() {
426: try {
427: myRemoteSessionContext.invalidateAllSessions();
428: } catch (RemoteException re) {
429: if (re.detail instanceof RuntimeException) {
430: throw (RuntimeException) re.detail;
431: }
432:
433: if (Log.getLog(myHost.getName()).isLog(C_Log.ERROR)) {
434: Log.getLog(myHost.getName()).log(
435: "Failed to invalidate all sessions.",
436: C_Log.ERROR);
437: Log.getLog(myHost.getName()).log(re, C_Log.ERROR);
438: }
439:
440: throw new JoBalancerIOException(
441: "Failed to invalidate all sessions.", re);
442: }
443: }
444:
445: /**
446: * Returns the MaxInactiveInterval all sessions are initialized with.
447: *
448: * @return MaxInactiveInterval in seconds
449: */
450: public int getDefaultMaxInactiveInterval() {
451: try {
452: return myRemoteSessionContext
453: .getDefaultMaxInactiveInterval();
454: } catch (RemoteException re) {
455: if (re.detail instanceof RuntimeException) {
456: throw (RuntimeException) re.detail;
457: }
458:
459: if (Log.getLog(myHost.getName()).isLog(C_Log.ERROR)) {
460: Log.getLog(myHost.getName()).log(re, C_Log.ERROR);
461: }
462:
463: throw new JoBalancerIOException(re);
464: }
465: }
466:
467: /**
468: * Sets the MaxInactiveInterval, all sessions
469: * are initialized with
470: *
471: * @param interval MaxInactiveInterval in seconds
472: */
473: public void setDefaultMaxInactiveInterval(int interval) {
474: try {
475: myRemoteSessionContext
476: .setDefaultMaxInactiveInterval(interval);
477: } catch (RemoteException re) {
478: if (re.detail instanceof RuntimeException) {
479: throw (RuntimeException) re.detail;
480: }
481:
482: if (Log.getLog(myHost.getName()).isLog(C_Log.ERROR)) {
483: Log.getLog(myHost.getName()).log(re, C_Log.ERROR);
484: }
485:
486: throw new JoBalancerIOException(re);
487: }
488: }
489:
490: /**
491: * Empty .
492: */
493: public void sync(I_JoSession aSession) {
494: }
495:
496: /**
497: * Returns the peer.
498: *
499: * @return the peer
500: */
501: public I_JoServletContextPeer getServletContextPeer() {
502: return myPeer;
503: }
504:
505: }
|