KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mortbay > j2ee > session > SerializableContext


1 // ========================================================================
2
// $Id: SerializableContext.java,v 1.4 2004/05/09 20:30:47 gregwilkins Exp $
3
// Copyright 2002-2004 Mort Bay Consulting Pty. Ltd.
4
// ------------------------------------------------------------------------
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
// ========================================================================
15

16 package org.mortbay.j2ee.session;
17
18 import javax.naming.Context JavaDoc;
19 import javax.naming.InitialContext JavaDoc;
20
21 // utility for unambiguously shipping Contexts from node to node..
22

23 // Aarrgh ! - I thought this would work - but no luck - looking up a
24
// Context returns a different Object each time, and I cannot find a
25
// decent way to figure out whether they represent the same Context or
26
// not...
27

28 // it looks like this will need proprietary info - the J2EE API does
29
// not give us enough...
30

31 public class
32   SerializableContext
33   implements java.io.Serializable JavaDoc
34 {
35   protected void
36     log_warn(String JavaDoc message)
37     {
38       System.err.println("WARNING: "+message);
39     }
40
41   protected void
42     log_error(String JavaDoc message, Exception JavaDoc e)
43     {
44       System.err.println("ERROR: "+message);
45       e.printStackTrace(System.err);
46     }
47
48   protected
49     SerializableContext()
50     throws java.rmi.RemoteException JavaDoc
51     {
52     }
53
54   SerializableContext(Context JavaDoc context)
55     throws java.rmi.RemoteException JavaDoc
56     {
57
58       log_warn("distribution of Contexts is NYI - assuming java:comp/env");
59
60       // Context tmp=null;
61
// try
62
// {
63
// tmp=(Context)new javax.naming.InitialContext().lookup("java:comp/env");
64
// System.err.println(new javax.naming.InitialContext().lookup("java:comp/env")+"!="+new javax.naming.InitialContext().lookup("java:comp/env"));
65
// }
66
// catch (Exception e)
67
// {
68
// System.err.println("could not distribute Context"+context);
69
// e.printStackTrace(System.err);
70
// }
71
//
72
// if (context!=tmp)
73
// {
74
// System.err.println(context+"!="+tmp);
75
// throw new IllegalArgumentException("only java:comp/env Context may be distributed");
76
// }
77
}
78
79   Context JavaDoc
80     toContext()
81     throws java.rmi.RemoteException JavaDoc
82     {
83       try
84       {
85     // optimise - TODO
86
return (Context JavaDoc)new InitialContext JavaDoc().lookup("java:comp/env");
87       }
88       catch (Exception JavaDoc e)
89       {
90     log_error("could not lookup Context: "+"java:comp/env", e);
91     return null;
92       }
93     }
94 }
95
Popular Tags