KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > vlib > Global


1 // Copyright 2004 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
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 package org.apache.tapestry.vlib;
16
17 import javax.naming.Context JavaDoc;
18 import javax.naming.InitialContext JavaDoc;
19 import javax.naming.NamingException JavaDoc;
20 import javax.rmi.PortableRemoteObject JavaDoc;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.apache.hivemind.ApplicationRuntimeException;
25 import org.apache.tapestry.vlib.ejb.IBookQueryHome;
26 import org.apache.tapestry.vlib.ejb.IOperationsHome;
27
28 /**
29  * The Global object is shared by all instances of the engine. It is used
30  * to centralize all JNDI lookups.
31  *
32  * @author Howard Lewis Ship
33  * @version $Id: Global.java,v 1.6 2004/04/30 15:17:22 hlship Exp $
34  * @since 3.0
35  *
36  **/

37
38 public class Global
39 {
40     private static final Log LOG = LogFactory.getLog(Global.class);
41
42     // Home interfaces are static, such that they are only
43
// looked up once (JNDI lookup is very expensive).
44

45     private IBookQueryHome _bookQueryHome;
46     private IOperationsHome _operationsHome;
47
48     private Context JavaDoc _rootNamingContext;
49
50     public synchronized IBookQueryHome getBookQueryHome()
51     {
52         if (_bookQueryHome == null)
53             _bookQueryHome =
54                 (IBookQueryHome) findNamedObject("vlib/BookQuery", IBookQueryHome.class);
55
56         return _bookQueryHome;
57     }
58
59     public synchronized IOperationsHome getOperationsHome()
60     {
61         if (_operationsHome == null)
62             _operationsHome =
63                 (IOperationsHome) findNamedObject("vlib/Operations", IOperationsHome.class);
64
65         return _operationsHome;
66     }
67
68     public synchronized Object JavaDoc findNamedObject(String JavaDoc name, Class JavaDoc expectedClass)
69     {
70         Object JavaDoc result = null;
71
72         int i = 0;
73         while (true)
74         {
75             try
76             {
77                 Object JavaDoc raw = getRootNamingContext().lookup(name);
78
79                 if (raw == null)
80                 {
81                     String JavaDoc message =
82                         "JNDI lookup of "
83                             + name
84                             + " yielded null; expected instance of "
85                             + expectedClass.getName()
86                             + ".";
87
88                     LOG.error(message);
89
90                     if (i++ == 0)
91                     {
92                         clear();
93                         continue;
94                     }
95
96                     throw new ApplicationRuntimeException(message);
97                 }
98
99                 result = PortableRemoteObject.narrow(raw, expectedClass);
100
101                 break;
102             }
103             catch (ClassCastException JavaDoc ex)
104             {
105                 throw new ApplicationRuntimeException(
106                     "Object " + name + " is not type " + expectedClass.getName() + ".",
107                     ex);
108             }
109             catch (NamingException JavaDoc ex)
110             {
111                 String JavaDoc message = "Unable to resolve object " + name + ".";
112
113                 if (i++ == 0)
114                 {
115                     LOG.error(message, ex);
116                     clear();
117                 }
118                 else
119                     throw new ApplicationRuntimeException(message, ex);
120             }
121         }
122
123         return result;
124     }
125
126     public synchronized Context JavaDoc getRootNamingContext()
127     {
128         if (_rootNamingContext == null)
129         {
130             int i = 0;
131             while (true)
132             {
133                 try
134                 {
135                     _rootNamingContext = new InitialContext JavaDoc();
136
137                     break;
138                 }
139                 catch (NamingException JavaDoc ex)
140                 {
141                     String JavaDoc message = "Unable to locate root naming context.";
142
143                     if (i++ == 0)
144                     {
145                         LOG.error(message, ex);
146                         clear();
147                     }
148                     else
149                         throw new ApplicationRuntimeException(message, ex);
150                 }
151             }
152         }
153
154         return _rootNamingContext;
155     }
156
157     /**
158      * Invoked after any kind of naming or remote exception. Cached
159      * naming contexts and interfaces are discarded.
160      *
161      **/

162
163     public synchronized void clear()
164     {
165         _rootNamingContext = null;
166         _bookQueryHome = null;
167         _operationsHome = null;
168     }
169 }
170
Popular Tags