1 15 package org.apache.tapestry.vlib; 16 17 import javax.naming.Context ; 18 import javax.naming.InitialContext ; 19 import javax.naming.NamingException ; 20 import javax.rmi.PortableRemoteObject ; 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 37 38 public class Global 39 { 40 private static final Log LOG = LogFactory.getLog(Global.class); 41 42 45 private IBookQueryHome _bookQueryHome; 46 private IOperationsHome _operationsHome; 47 48 private Context _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 findNamedObject(String name, Class expectedClass) 69 { 70 Object result = null; 71 72 int i = 0; 73 while (true) 74 { 75 try 76 { 77 Object raw = getRootNamingContext().lookup(name); 78 79 if (raw == null) 80 { 81 String 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 ex) 104 { 105 throw new ApplicationRuntimeException( 106 "Object " + name + " is not type " + expectedClass.getName() + ".", 107 ex); 108 } 109 catch (NamingException ex) 110 { 111 String 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 getRootNamingContext() 127 { 128 if (_rootNamingContext == null) 129 { 130 int i = 0; 131 while (true) 132 { 133 try 134 { 135 _rootNamingContext = new InitialContext (); 136 137 break; 138 } 139 catch (NamingException ex) 140 { 141 String 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 162 163 public synchronized void clear() 164 { 165 _rootNamingContext = null; 166 _bookQueryHome = null; 167 _operationsHome = null; 168 } 169 } 170 | Popular Tags |