| CODENOTIFIER | HelpYou are not signed inSign in |
Project: Jetty
Revision: 3601
Author: janb
Date: 04 Sep 2008 02:14:02
Changes:JETTY-708 allow 3 scopes for jndi resources: jvm,server and webapp
Files:| ... | ...@@ -38,71 +38,20 @@ | |
| 38 | 38 | public class EnvEntry extends NamingEntry |
| 39 | 39 | { |
| 40 | 40 | private boolean overrideWebXml; |
| 41 | ||
| 42 | ||
| 43 | /** | |
| 44 | * Bind a name and value to java:comp/env, taking into account | |
| 45 | * any overriding EnvEntrys in the environment, either local or global. | |
| 46 | * | |
| 47 | * @param name the name from web.xml env-entry | |
| 48 | * @param value the value from web.xml env-entry | |
| 49 | * @throws NamingException | |
| 50 | */ | |
| 51 | public static void bindToENC (String name, Object value) | |
| 52 | throws NamingException | |
| 53 | { | |
| 54 | if (name==null||name.trim().equals("")) | |
| 55 | throw new NamingException("No name for EnvEntry"); | |
| 41 | ||
| 56 | 42 | |
| 57 | //Is there an EnvEntry with the same name? If so, check its overrideWebXml setting. If true, | |
| 58 | //it's value should be used instead of the one supplied in web.xml - as the name matches, then in | |
| 59 | //fact the value is already bound, so there's nothing to do . If false, use the value | |
| 60 | //from web.xml. In the case where the EnvEntry has been scoped only to the webapp (ie it was | |
| 61 | //in jetty-env.xml). | |
| 62 | EnvEntry envEntry = (EnvEntry)NamingEntryUtil.lookupNamingEntry(name); | |
| 63 | if (envEntry!=null && envEntry.isOverrideWebXml()) | |
| 64 | envEntry.bindToENC(name); | |
| 65 | else | |
| 66 | { | |
| 67 | //No EnvEntry, or it wasnt set to override, so just bind the value from web.xml | |
| 68 | InitialContext ic = new InitialContext(); | |
| 69 | Context envCtx = (Context)ic.lookup("java:comp/env"); | |
| 70 | NamingUtil.bind(envCtx, name, value); | |
| 71 | } | |
| 72 | } | |
| 73 | ||
| 74 | public static List lookupGlobalEnvEntries () | |
| 43 | public EnvEntry (Object scope, String jndiName, Object objToBind, boolean overrideWebXml) | |
| 75 | 44 | throws NamingException |
| 76 | 45 | { |
| 77 | ArrayList list = new ArrayList(); | |
| 78 | lookupEnvEntries(list, new InitialContext()); | |
| 79 | return list; | |
| 46 | super (scope, jndiName, objToBind); | |
| 47 | this.overrideWebXml = overrideWebXml; | |
| 80 | 48 | } |
| 81 | 49 | |
| 82 | private static List lookupEnvEntries (List list, Context context) | |
| 50 | public EnvEntry (Object scope, String jndiName, Object objToBind) | |
| 83 | 51 | throws NamingException |
| 84 | 52 | { |
| 85 | try | |
| 86 | { | |
| 87 | NamingEnumeration nenum = context.listBindings(""); | |
| 88 | while (nenum.hasMoreElements()) | |
| 89 | { | |
| 90 | Binding binding = (Binding)nenum.next(); | |
| 91 | if (binding.getObject() instanceof Context) | |
| 92 | lookupEnvEntries (list, (Context)binding.getObject()); | |
| 93 | else if (EnvEntry.class.isInstance(binding.getObject())) | |
| 94 | list.add(binding.getObject()); | |
| 95 | } | |
| 96 | } | |
| 97 | catch (NameNotFoundException e) | |
| 98 | { | |
| 99 | Log.debug("No EnvEntries in context="+context); | |
| 100 | } | |
| 101 | ||
| 102 | return list; | |
| 53 | this (scope, jndiName, objToBind, false); | |
| 103 | 54 | } |
| 104 | ||
| 105 | ||
| 106 | 55 | |
| 107 | 56 | public EnvEntry (String jndiName, Object objToBind) |
| 108 | 57 | throws NamingException |
| ... | ...@@ -4,7 +4,7 @@ | |
| 4 | 4 | <!-- =============================================================== --> |
| 5 | 5 | <!-- Configure the test-jndi webapp --> |
| 6 | 6 | <!-- =============================================================== --> |
| 7 | <Configure class="org.mortbay.jetty.webapp.WebAppContext"> | |
| 7 | <Configure id='wac' class="org.mortbay.jetty.webapp.WebAppContext"> | |
| 8 | 8 | |
| 9 | 9 | <!-- Ensure Jetty Plus features are enabled for this webapp --> |
| 10 | 10 | <Set name="configurationClasses"> |
| ... | ...@@ -36,13 +36,17 @@ | |
| 36 | 36 | <Arg><SystemProperty name="jetty.home" default="."/>/contexts/test-jndi.d/WEB-INF/classes/jta.properties</Arg> |
| 37 | 37 | </Call> |
| 38 | 38 | |
| 39 | <!-- Define some env entries for java:comp/env --> | |
| 40 | <New id="woggle" class="org.mortbay.jetty.plus.naming.EnvEntry"> | |
| 39 | <!-- Define an env entry with Server scope for java:comp/env --> | |
| 40 | <New id="woggle" class="org.mortbay.jetty.plus.naming.EnvEntry"> | |
| 41 | <Arg><Property name='server'/></Arg> | |
| 41 | 42 | <Arg>woggle</Arg> |
| 42 | <Arg type="java.lang.Integer">4000</Arg> | |
| 43 | <Arg type="java.lang.Integer">4000</Arg> | |
| 44 | <Arg type="boolean">false</Arg> | |
| 43 | 45 | </New> |
| 44 | 46 | |
| 47 | <!-- Define an env entry with webapp scope for java:comp/env --> | |
| 45 | 48 | <New id="wiggle" class="org.mortbay.jetty.plus.naming.EnvEntry"> |
| 49 | <Arg><Ref id='wac'/></Arg> | |
| 46 | 50 | <Arg>wiggle</Arg> |
| 47 | 51 | <Arg type="java.lang.Double">100</Arg> |
| 48 | 52 | <Arg type="boolean">true</Arg> |
| ... | ...@@ -50,6 +54,7 @@ | |
| 50 | 54 | |
| 51 | 55 | <!-- Mail Session setup --> |
| 52 | 56 | <New id="xxxmail" class="org.mortbay.jetty.plus.naming.Resource"> |
| 57 | <Arg><Ref id='wac'/></Arg> | |
| 53 | 58 | <Arg>mail/Session</Arg> |
| 54 | 59 | <Arg> |
| 55 | 60 | <New class="org.mortbay.naming.factories.MailSessionReference"> |
| ... | ...@@ -87,6 +92,7 @@ | |
| 87 | 92 | |
| 88 | 93 | |
| 89 | 94 | <New id="mydatasource" class="org.mortbay.jetty.plus.naming.Resource"> |
| 95 | <Arg><Ref id='wac'/></Arg> | |
| 90 | 96 | <Arg>jdbc/mydatasource</Arg> |
| 91 | 97 | <Arg> |
| 92 | 98 | @ATOMIKOS-DATASOURCE1@ |
| ... | ...@@ -97,6 +103,7 @@ | |
| 97 | 103 | @ATOMIKOS-DATASOURCE2-PRE@ |
| 98 | 104 | |
| 99 | 105 | <New id="mydatasource2" class="org.mortbay.jetty.plus.naming.Resource"> |
| 106 | <Arg><Ref id='wac'/></Arg> | |
| 100 | 107 | <Arg>jdbc/mydatasource2</Arg> |
| 101 | 108 | <Arg> |
| 102 | 109 | @ATOMIKOS-DATASOURCE2@ |
| ... | ...@@ -118,6 +125,7 @@ | |
| 118 | 125 | |
| 119 | 126 | |
| 120 | 127 | <New id="mydatasource" class="org.mortbay.jetty.plus.naming.Resource"> |
| 128 | <Arg><Ref id='wac'/></Arg> | |
| 121 | 129 | <Arg>jdbc/mydatasource</Arg> |
| 122 | 130 | <Arg> |
| 123 | 131 | @JOTM-DATASOURCE1@ |
| ... | ...@@ -128,6 +136,7 @@ | |
| 128 | 136 | @JOTM-DATASOURCE2-PRE@ |
| 129 | 137 | |
| 130 | 138 | <New id="mydatasource2" class="org.mortbay.jetty.plus.naming.Resource"> |
| 139 | <Arg><Ref id='wac'/></Arg> | |
| 131 | 140 | <Arg>jdbc/mydatasource2</Arg> |
| 132 | 141 | <Arg> |
| 133 | 142 | @JOTM-DATASOURCE2@ |
| ... | ...@@ -22,10 +22,12 @@ | |
| 22 | 22 | import javax.naming.NameNotFoundException; |
| 23 | 23 | |
| 24 | 24 | import org.mortbay.jetty.plus.naming.EnvEntry; |
| 25 | import org.mortbay.jetty.plus.naming.Link; | |
| 25 | 26 | import org.mortbay.jetty.plus.naming.NamingEntry; |
| 26 | 27 | import org.mortbay.jetty.plus.naming.NamingEntryUtil; |
| 27 | 28 | import org.mortbay.jetty.plus.naming.Transaction; |
| 28 | 29 | import org.mortbay.log.Log; |
| 30 | import org.mortbay.naming.NamingUtil; | |
| 29 | 31 | |
| 30 | 32 | |
| 31 | 33 | /** |
| ... | ...@@ -52,7 +54,32 @@ | |
| 52 | 54 | */ |
| 53 | 55 | public void bindEnvEntry(String name, Object value) throws Exception |
| 54 | 56 | { |
| 55 | EnvEntry.bindToENC(name, value); | |
| 57 | InitialContext ic = null; | |
| 58 | boolean bound = false; | |
| 59 | //check to see if we bound a value and an EnvEntry with this name already | |
| 60 | //when we processed the server and the webapp's naming environment | |
| 61 | //@see EnvConfiguration.bindEnvEntries() | |
| 62 | ic = new InitialContext(); | |
| 63 | try | |
| 64 | { | |
| 65 | NamingEntry ne = (NamingEntry)ic.lookup("java:comp/env/"+NamingEntryUtil.makeNamingEntryName(ic.getNameParser(""), name)); | |
| 66 | if (ne!=null && ne instanceof EnvEntry) | |
| 67 | { | |
| 68 | EnvEntry ee = (EnvEntry)ne; | |
| 69 | bound = ee.isOverrideWebXml(); | |
| 70 | } | |
| 71 | } | |
| 72 | catch (NameNotFoundException e) | |
| 73 | { | |
| 74 | bound = false; | |
| 75 | } | |
| 76 | ||
| 77 | if (!bound) | |
| 78 | { | |
| 79 | //either nothing was bound or the value from web.xml should override | |
| 80 | Context envCtx = (Context)ic.lookup("java:comp/env"); | |
| 81 | NamingUtil.bind(envCtx, name, value); | |
| 82 | } | |
| 56 | 83 | } |
| 57 | 84 | |
| 58 | 85 | /** |
| ... | ...@@ -68,20 +95,7 @@ | |
| 68 | 95 | public void bindResourceRef(String name, Class typeClass) |
| 69 | 96 | throws Exception |
| 70 | 97 | { |
| 71 | try | |
| 72 | { | |
| 73 | String mappedName = NamingEntryUtil.getMappedName (name); | |
| 74 | NamingEntryUtil.bindToENC(name, mappedName); | |
| 75 | } | |
| 76 | catch (NameNotFoundException e) | |
| 77 | { | |
| 78 | //There is no matching resource bound into the container's environment, try a default name. | |
| 79 | //The default name syntax is: the [res-type]/default | |
| 80 | //eg javax.sql.DataSource/default | |
| 81 | NamingEntry defaultNE = NamingEntryUtil.lookupNamingEntry(typeClass.getName()+"/default"); | |
| 82 | if (defaultNE!=null) | |
| 83 | defaultNE.bindToENC(name); | |
| 84 | } | |
| 98 | bindEntry(name, typeClass); | |
| 85 | 99 | } |
| 86 | 100 | |
| 87 | 101 | /** |
| ... | ...@@ -92,40 +106,14 @@ | |
| 92 | 106 | public void bindResourceEnvRef(String name, Class typeClass) |
| 93 | 107 | throws Exception |
| 94 | 108 | { |
| 95 | try | |
| 96 | { | |
| 97 | String mappedName = NamingEntryUtil.getMappedName (name); | |
| 98 | NamingEntryUtil.bindToENC(name, mappedName); | |
| 99 | } | |
| 100 | catch (NameNotFoundException e) | |
| 101 | { | |
| 102 | //There is no matching resource bound into the container's environment, try a default | |
| 103 | //The default name syntax is: the [res-type]/default | |
| 104 | //eg javax.sql.DataSource/default | |
| 105 | NamingEntry defaultNE = NamingEntryUtil.lookupNamingEntry(typeClass.getName()+"/default"); | |
| 106 | if (defaultNE!=null) | |
| 107 | defaultNE.bindToENC(name); | |
| 108 | } | |
| 109 | bindEntry(name, typeClass); | |
| 109 | 110 | } |
| 110 | 111 | |
| 111 | 112 | |
| 112 | 113 | public void bindMessageDestinationRef(String name, Class typeClass) |
| 113 | 114 | throws Exception |
| 114 | 115 | { |
| 115 | try | |
| 116 | { | |
| 117 | String mappedName = NamingEntryUtil.getMappedName (name); | |
| 118 | NamingEntryUtil.bindToENC(name, mappedName); | |
| 119 | } | |
| 120 | catch (NameNotFoundException e) | |
| 121 | { | |
| 122 | //There is no matching resource bound into the container's environment, try a default | |
| 123 | //The default name syntax is: the [res-type]/default | |
| 124 | //eg javax.sql.DataSource/default | |
| 125 | NamingEntry defaultNE = NamingEntryUtil.lookupNamingEntry(typeClass.getName()+"/default"); | |
| 126 | if (defaultNE!=null) | |
| 127 | defaultNE.bindToENC(name); | |
| 128 | } | |
| 116 | bindEntry(name, typeClass); | |
| 129 | 117 | } |
| 130 | 118 | |
| 131 | 119 | public void bindUserTransaction () |
| ... | ...@@ -204,4 +192,70 @@ | |
| 204 | 192 | //see org.mortbay.jetty.annotations.Configuration instead |
| 205 | 193 | } |
| 206 | 194 | |
| 195 | /** | |
| 196 | * Bind a resource with the given name from web.xml of the given type | |
| 197 | * with a jndi resource from either the server or the webapp's naming | |
| 198 | * environment. | |
| 199 | * | |
| 200 | * As the servlet spec does not cover the mapping of names in web.xml with | |
| 201 | * names from the execution environment, jetty uses the concept of a Link, which is | |
| 202 | * a subclass of the NamingEntry class. A Link defines a mapping of a name | |
| 203 | * from web.xml with a name from the execution environment (ie either the server or the | |
| 204 | * webapp's naming environment). | |
| 205 | * | |
| 206 | * @param name name of the resource from web.xml | |
| 207 | * @param typeClass | |
| 208 | * @throws Exception | |
| 209 | */ | |
| 210 | private void bindEntry (String name, Class typeClass) | |
| 211 | throws Exception | |
| 212 | { | |
| 213 | String nameInEnvironment = name; | |
| 214 | boolean bound = false; | |
| 215 | ||
| 216 | //check if the name in web.xml has been mapped to something else | |
| 217 | //check a context-specific naming environment first | |
| 218 | Object scope = getWebAppContext(); | |
| 219 | NamingEntry ne = NamingEntryUtil.lookupNamingEntry(scope, name); | |
| 220 | ||
| 221 | if (ne!=null && (ne instanceof Link)) | |
| 222 | { | |
| 223 | //if we found a mapping, get out name it is mapped to in the environment | |
| 224 | nameInEnvironment = (String)((Link)ne).getObjectToBind(); | |
| 225 | Link l = (Link)ne; | |
| 226 | } | |
| 227 | ||
| 228 | //try finding that mapped name in the webapp's environment first | |
| 229 | scope = getWebAppContext(); | |
| 230 | bound = NamingEntryUtil.bindToENC(scope, name, nameInEnvironment); | |
| 231 | ||
| 232 | if (bound) | |
| 233 | return; | |
| 234 | ||
| 235 | //try the server's environment | |
| 236 | scope = getWebAppContext().getServer(); | |
| 237 | bound = NamingEntryUtil.bindToENC(scope, name, nameInEnvironment); | |
| 238 | if (bound) | |
| 239 | return; | |
| 240 | ||
| 241 | //try the jvm environment | |
| 242 | bound = NamingEntryUtil.bindToENC(null, name, nameInEnvironment); | |
| 243 | if (bound) | |
| 244 | return; | |
| 245 | ||
| 246 | ||
| 247 | //There is no matching resource so try a default name. | |
| 248 | //The default name syntax is: the [res-type]/default | |
| 249 | //eg javax.sql.DataSource/default | |
| 250 | nameInEnvironment = typeClass.getName()+"/default"; | |
| 251 | //First try the server scope | |
| 252 | NamingEntry defaultNE = NamingEntryUtil.lookupNamingEntry(getWebAppContext().getServer(), nameInEnvironment); | |
| 253 | if (defaultNE==null) | |
| 254 | defaultNE = NamingEntryUtil.lookupNamingEntry(null, nameInEnvironment); | |
| 255 | ||
| 256 | if (defaultNE!=null) | |
| 257 | defaultNE.bindToENC(name); | |
| 258 | else | |
| 259 | throw new IllegalStateException("Nothing to bind for name "+nameInEnvironment); | |
| 260 | } | |
| 207 | 261 | } |
| ... | ...@@ -42,6 +42,7 @@ | |
| 42 | 42 | import org.mortbay.naming.NamingContext; |
| 43 | 43 | |
| 44 | 44 | |
| 45 | ||
| 45 | 46 | public class TestJNDI extends TestCase |
| 46 | 47 | { |
| 47 | 48 | |
| ... | ...@@ -83,207 +84,214 @@ | |
| 83 | 84 | } |
| 84 | 85 | |
| 85 | 86 | public void testIt () |
| 86 | throws Exception | |
| 87 | throws Exception | |
| 87 | 88 | { |
| 88 | //set up some classloaders | |
| 89 | Thread currentThread = Thread.currentThread(); | |
| 90 | ClassLoader currentLoader = currentThread.getContextClassLoader(); | |
| 91 | ClassLoader childLoader1 = new URLClassLoader(new URL[0], currentLoader); | |
| 92 | ClassLoader childLoader2 = new URLClassLoader(new URL[0], currentLoader); | |
| 93 | ||
| 94 | //set the current thread's classloader | |
| 95 | currentThread.setContextClassLoader(childLoader1); | |
| 96 | ||
| 97 | InitialContext initCtxA = new InitialContext(); | |
| 98 | initCtxA.bind ("blah", "123"); | |
| 99 | assertEquals ("123", initCtxA.lookup("blah")); | |
| 100 | ||
| 101 | ||
| 102 | ||
| 103 | ||
| 104 | InitialContext initCtx = new InitialContext(); | |
| 105 | Context sub0 = (Context)initCtx.lookup("java:"); | |
| 106 | ||
| 107 | if(Log.isDebugEnabled())Log.debug("------ Looked up java: --------------"); | |
| 108 | ||
| 109 | Name n = sub0.getNameParser("").parse("/red/green/"); | |
| 110 | ||
| 111 | ||
| 112 | if(Log.isDebugEnabled())Log.debug("get(0)="+n.get(0)); | |
| 113 | if(Log.isDebugEnabled())Log.debug("getPrefix(1)="+n.getPrefix(1)); | |
| 114 | n = n.getSuffix(1); | |
| 115 | if(Log.isDebugEnabled())Log.debug("getSuffix(1)="+n); | |
| 116 | if(Log.isDebugEnabled())Log.debug("get(0)="+n.get(0)); | |
| 117 | if(Log.isDebugEnabled())Log.debug("getPrefix(1)="+n.getPrefix(1)); | |
| 118 | n = n.getSuffix(1); | |
| 119 | if(Log.isDebugEnabled())Log.debug("getSuffix(1)="+n); | |
| 120 | if(Log.isDebugEnabled())Log.debug("get(0)="+n.get(0)); | |
| 121 | if(Log.isDebugEnabled())Log.debug("getPrefix(1)="+n.getPrefix(1)); | |
| 122 | n = n.getSuffix(1); | |
| 123 | if(Log.isDebugEnabled())Log.debug("getSuffix(1)="+n); | |
| 124 | ||
| 125 | n = sub0.getNameParser("").parse("pink/purple/"); | |
| 126 | if(Log.isDebugEnabled())Log.debug("get(0)="+n.get(0)); | |
| 127 | if(Log.isDebugEnabled())Log.debug("getPrefix(1)="+n.getPrefix(1)); | |
| 128 | n = n.getSuffix(1); | |
| 129 | if(Log.isDebugEnabled())Log.debug("getSuffix(1)="+n); | |
| 130 | if(Log.isDebugEnabled())Log.debug("get(0)="+n.get(0)); | |
| 131 | if(Log.isDebugEnabled())Log.debug("getPrefix(1)="+n.getPrefix(1)); | |
| 132 | ||
| 133 | NamingContext ncontext = (NamingContext)sub0; | |
| 134 | ||
| 135 | Name nn = ncontext.toCanonicalName(ncontext.getNameParser("").parse("/yellow/blue/")); | |
| 136 | Log.debug(nn.toString()); | |
| 137 | assertEquals (2, nn.size()); | |
| 138 | ||
| 139 | nn = ncontext.toCanonicalName(ncontext.getNameParser("").parse("/yellow/blue")); | |
| 140 | Log.debug(nn.toString()); | |
| 141 | assertEquals (2, nn.size()); | |
| 142 | ||
| 143 | nn = ncontext.toCanonicalName(ncontext.getNameParser("").parse("/")); | |
| 144 | if(Log.isDebugEnabled())Log.debug("/ parses as: "+nn+" with size="+nn.size()); | |
| 145 | Log.debug(nn.toString()); | |
| 146 | assertEquals (1, nn.size()); | |
| 147 | ||
| 148 | nn = ncontext.toCanonicalName(ncontext.getNameParser("").parse("")); | |
| 149 | Log.debug(nn.toString()); | |
| 150 | assertEquals (0, nn.size()); | |
| 151 | ||
| 152 | Context fee = ncontext.createSubcontext("fee"); | |
| 153 | fee.bind ("fi", "88"); | |
| 154 | assertEquals("88", initCtxA.lookup("java:/fee/fi")); | |
| 155 | assertEquals("88", initCtxA.lookup("java:/fee/fi/")); | |
| 156 | assertTrue (initCtxA.lookup("java:/fee/") instanceof javax.naming.Context); | |
| 157 | ||
| 158 | 89 | try |
| 159 | 90 | { |
| 160 | Context sub1 = sub0.createSubcontext ("comp"); | |
| 161 | fail("Comp should already be bound"); | |
| 162 | } | |
| 163 | catch (NameAlreadyBoundException e) | |
| 164 | { | |
| 165 | //expected exception | |
| 166 | } | |
| 91 | //set up some classloaders | |
| 92 | Thread currentThread = Thread.currentThread(); | |
| 93 | ClassLoader currentLoader = currentThread.getContextClassLoader(); | |
| 94 | ClassLoader childLoader1 = new URLClassLoader(new URL[0], currentLoader); | |
| 95 | ClassLoader childLoader2 = new URLClassLoader(new URL[0], currentLoader); | |
| 167 | 96 | |
| 168 | ||
| 97 | //set the current thread's classloader | |
| 98 | currentThread.setContextClassLoader(childLoader1); | |
| 169 | 99 | |
| 170 | //check bindings at comp | |
| 171 | Context sub1 = (Context)initCtx.lookup("java:comp"); | |
| 100 | InitialContext initCtxA = new InitialContext(); | |
| 101 | initCtxA.bind ("blah", "123"); | |
| 102 | assertEquals ("123", initCtxA.lookup("blah")); | |
| 172 | 103 | |
| 173 | Context sub2 = sub1.createSubcontext ("env"); | |
| 174 | 104 | |
| 175 | initCtx.bind ("java:comp/env/rubbish", "abc"); | |
| 176 | assertEquals ("abc", (String)initCtx.lookup("java:comp/env/rubbish")); | |
| 177 | ||
| 178 | ||
| 179 | ||
| 180 | //check binding LinkRefs | |
| 181 | LinkRef link = new LinkRef ("java:comp/env/rubbish"); | |
| 182 | initCtx.bind ("java:comp/env/poubelle", link); | |
| 183 | assertEquals ("abc", (String)initCtx.lookup("java:comp/env/poubelle")); | |
| 184 | ||
| 185 | //check binding References | |
| 186 | StringRefAddr addr = new StringRefAddr("blah", "myReferenceable"); | |
| 187 | Reference ref = new Reference (java.lang.String.class.getName(), | |
| 188 | addr, | |
| 189 | MyObjectFactory.class.getName(), | |
| 190 | (String)null); | |
| 191 | ||
| 192 | initCtx.bind ("java:comp/env/quatsch", ref); | |
| 193 | assertEquals (MyObjectFactory.myString, (String)initCtx.lookup("java:comp/env/quatsch")); | |
| 194 | ||
| 195 | //test binding something at java: | |
| 196 | Context sub3 = initCtx.createSubcontext("java:zero"); | |
| 197 | initCtx.bind ("java:zero/one", "ONE"); | |
| 198 | assertEquals ("ONE", initCtx.lookup("java:zero/one")); | |
| 199 | ||
| 200 | ||
| 201 | ||
| 202 | ||
| 203 | //change the current thread's classloader to check distinct naming | |
| 204 | currentThread.setContextClassLoader(childLoader2); | |
| 205 | 105 | |
| 206 | Context otherSub1 = (Context)initCtx.lookup("java:comp"); | |
| 207 | assertTrue (!(sub1 == otherSub1)); | |
| 208 | try | |
| 209 | { | |
| 210 | initCtx.lookup("java:comp/env/rubbish"); | |
| 211 | } | |
| 212 | catch (NameNotFoundException e) | |
| 213 | { | |
| 214 | //expected | |
| 215 | } | |
| 216 | 106 | |
| 217 | ||
| 218 | //put the thread's classloader back | |
| 219 | currentThread.setContextClassLoader(childLoader1); | |
| 220 | ||
| 221 | //test rebind with existing binding | |
| 222 | initCtx.rebind("java:comp/env/rubbish", "xyz"); | |
| 223 | assertEquals ("xyz", initCtx.lookup("java:comp/env/rubbish")); | |
| 224 | ||
| 225 | //test rebind with no existing binding | |
| 226 | initCtx.rebind ("java:comp/env/mullheim", "hij"); | |
| 227 | assertEquals ("hij", initCtx.lookup("java:comp/env/mullheim")); | |
| 228 | ||
| 229 | //test that the other bindings are already there | |
| 230 | assertEquals ("xyz", (String)initCtx.lookup("java:comp/env/poubelle")); | |
| 231 | ||
| 232 | //test java:/comp/env/stuff | |
| 233 | assertEquals ("xyz", (String)initCtx.lookup("java:/comp/env/poubelle/")); | |
| 234 | ||
| 235 | //test list Names | |
| 236 | NamingEnumeration nenum = initCtx.list ("java:comp/env"); | |
| 237 | HashMap results = new HashMap(); | |
| 238 | while (nenum.hasMore()) | |
| 239 | { | |
| 240 | NameClassPair ncp = (NameClassPair)nenum.next(); | |
| 241 | results.put (ncp.getName(), ncp.getClassName()); | |
| 242 | } | |
| 107 | InitialContext initCtx = new InitialContext(); | |
| 108 | Context sub0 = (Context)initCtx.lookup("java:"); | |
| 243 | 109 | |
| 244 | assertEquals (4, results.size()); | |
| 245 | ||
| 246 | assertEquals ("java.lang.String", (String)results.get("rubbish")); | |
| 247 | assertEquals ("javax.naming.LinkRef", (String)results.get("poubelle")); | |
| 248 | assertEquals ("java.lang.String", (String)results.get("mullheim")); | |
| 249 | assertEquals ("javax.naming.Reference", (String)results.get("quatsch")); | |
| 250 | ||
| 251 | //test list Bindings | |
| 252 | NamingEnumeration benum = initCtx.list("java:comp/env"); | |
| 253 | assertEquals (4, results.size()); | |
| 254 | ||
| 255 | //test NameInNamespace | |
| 256 | assertEquals ("comp/env", sub2.getNameInNamespace()); | |
| 257 | ||
| 258 | //test close does nothing | |
| 259 | Context closeCtx = (Context)initCtx.lookup("java:comp/env"); | |
| 260 | closeCtx.close(); | |
| 261 | ||
| 262 | ||
| 263 | //test what happens when you close an initial context | |
| 264 | InitialContext closeInit = new InitialContext(); | |
| 265 | closeInit.close(); | |
| 266 | ||
| 267 | ||
| 268 | ||
| 269 | //check locking the context | |
| 270 | Context ectx = (Context)initCtx.lookup("java:comp"); | |
| 271 | ectx.bind("crud", "xxx"); | |
| 272 | ectx.addToEnvironment("org.mortbay.jndi.immutable", "TRUE"); | |
| 273 | assertEquals ("xxx", (String)initCtx.lookup("java:comp/crud")); | |
| 274 | try | |
| 275 | { | |
| 276 | ectx.bind("crud2", "xxx2"); | |
| 110 | if(Log.isDebugEnabled())Log.debug("------ Looked up java: --------------"); | |
| 111 | ||
| 112 | Name n = sub0.getNameParser("").parse("/red/green/"); | |
| 113 | ||
| 114 | ||
| 115 | if(Log.isDebugEnabled())Log.debug("get(0)="+n.get(0)); | |
| 116 | if(Log.isDebugEnabled())Log.debug("getPrefix(1)="+n.getPrefix(1)); | |
| 117 | n = n.getSuffix(1); | |
| 118 | if(Log.isDebugEnabled())Log.debug("getSuffix(1)="+n); | |
| 119 | if(Log.isDebugEnabled())Log.debug("get(0)="+n.get(0)); | |
| 120 | if(Log.isDebugEnabled())Log.debug("getPrefix(1)="+n.getPrefix(1)); | |
| 121 | n = n.getSuffix(1); | |
| 122 | if(Log.isDebugEnabled())Log.debug("getSuffix(1)="+n); | |
| 123 | if(Log.isDebugEnabled())Log.debug("get(0)="+n.get(0)); | |
| 124 | if(Log.isDebugEnabled())Log.debug("getPrefix(1)="+n.getPrefix(1)); | |
| 125 | n = n.getSuffix(1); | |
| 126 | if(Log.isDebugEnabled())Log.debug("getSuffix(1)="+n); | |
| 127 | ||
| 128 | n = sub0.getNameParser("").parse("pink/purple/"); | |
| 129 | if(Log.isDebugEnabled())Log.debug("get(0)="+n.get(0)); | |
| 130 | if(Log.isDebugEnabled())Log.debug("getPrefix(1)="+n.getPrefix(1)); | |
| 131 | n = n.getSuffix(1); | |
| 132 | if(Log.isDebugEnabled())Log.debug("getSuffix(1)="+n); | |
| 133 | if(Log.isDebugEnabled())Log.debug("get(0)="+n.get(0)); | |
| 134 | if(Log.isDebugEnabled())Log.debug("getPrefix(1)="+n.getPrefix(1)); | |
| 135 | ||
| 136 | NamingContext ncontext = (NamingContext)sub0; | |
| 137 | ||
| 138 | Name nn = ncontext.toCanonicalName(ncontext.getNameParser("").parse("/yellow/blue/")); | |
| 139 | Log.debug(nn.toString()); | |
| 140 | assertEquals (2, nn.size()); | |
| 141 | ||
| 142 | nn = ncontext.toCanonicalName(ncontext.getNameParser("").parse("/yellow/blue")); | |
| 143 | Log.debug(nn.toString()); | |
| 144 | assertEquals (2, nn.size()); | |
| 145 | ||
| 146 | nn = ncontext.toCanonicalName(ncontext.getNameParser("").parse("/")); | |
| 147 | if(Log.isDebugEnabled())Log.debug("/ parses as: "+nn+" with size="+nn.size()); | |
| 148 | Log.debug(nn.toString()); | |
| 149 | assertEquals (1, nn.size()); | |
| 150 | ||
| 151 | nn = ncontext.toCanonicalName(ncontext.getNameParser("").parse("")); | |
| 152 | Log.debug(nn.toString()); | |
| 153 | assertEquals (0, nn.size()); | |
| 154 | ||
| 155 | Context fee = ncontext.createSubcontext("fee"); | |
| 156 | fee.bind ("fi", "88"); | |
| 157 | assertEquals("88", initCtxA.lookup("java:/fee/fi")); | |
| 158 | assertEquals("88", initCtxA.lookup("java:/fee/fi/")); | |
| 159 | assertTrue (initCtxA.lookup("java:/fee/") instanceof javax.naming.Context); | |
| 160 | ||
| 161 | try | |
| 162 | { | |
| 163 | Context sub1 = sub0.createSubcontext ("comp"); | |
| 164 | fail("Comp should already be bound"); | |
| 165 | } | |
| 166 | catch (NameAlreadyBoundException e) | |
| 167 | { | |
| 168 | //expected exception | |
| 169 | } | |
| 170 | ||
| 171 | ||
| 172 | ||
| 173 | //check bindings at comp | |
| 174 | Context sub1 = (Context)initCtx.lookup("java:comp"); | |
| 175 | ||
| 176 | Context sub2 = sub1.createSubcontext ("env"); | |
| 177 | ||
| 178 | initCtx.bind ("java:comp/env/rubbish", "abc"); | |
| 179 | assertEquals ("abc", (String)initCtx.lookup("java:comp/env/rubbish")); | |
| 180 | ||
| 181 | ||
| 182 | ||
| 183 | //check binding LinkRefs | |
| 184 | LinkRef link = new LinkRef ("java:comp/env/rubbish"); | |
| 185 | initCtx.bind ("java:comp/env/poubelle", link); | |
| 186 | assertEquals ("abc", (String)initCtx.lookup("java:comp/env/poubelle")); | |
| 187 | ||
| 188 | //check binding References | |
| 189 | StringRefAddr addr = new StringRefAddr("blah", "myReferenceable"); | |
| 190 | Reference ref = new Reference (java.lang.String.class.getName(), | |
| 191 | addr, | |
| 192 | MyObjectFactory.class.getName(), | |
| 193 | (String)null); | |
| 194 | ||
| 195 | initCtx.bind ("java:comp/env/quatsch", ref); | |
| 196 | assertEquals (MyObjectFactory.myString, (String)initCtx.lookup("java:comp/env/quatsch")); | |
| 197 | ||
| 198 | //test binding something at java: | |
| 199 | Context sub3 = initCtx.createSubcontext("java:zero"); | |
| 200 | initCtx.bind ("java:zero/one", "ONE"); | |
| 201 | assertEquals ("ONE", initCtx.lookup("java:zero/one")); | |
| 202 | ||
| 203 | ||
| 204 | ||
| 205 | ||
| 206 | //change the current thread's classloader to check distinct naming | |
| 207 | currentThread.setContextClassLoader(childLoader2); | |
| 208 | ||
| 209 | Context otherSub1 = (Context)initCtx.lookup("java:comp"); | |
| 210 | assertTrue (!(sub1 == otherSub1)); | |
| 211 | try | |
| 212 | { | |
| 213 | initCtx.lookup("java:comp/env/rubbish"); | |
| 214 | } | |
| 215 | catch (NameNotFoundException e) | |
| 216 | { | |
| 217 | //expected | |
| 218 | } | |
| 219 | ||
| 220 | ||
| 221 | //put the thread's classloader back | |
| 222 | currentThread.setContextClassLoader(childLoader1); | |
| 223 | ||
| 224 | //test rebind with existing binding | |
| 225 | initCtx.rebind("java:comp/env/rubbish", "xyz"); | |
| 226 | assertEquals ("xyz", initCtx.lookup("java:comp/env/rubbish")); | |
| 227 | ||
| 228 | //test rebind with no existing binding | |
| 229 | initCtx.rebind ("java:comp/env/mullheim", "hij"); | |
| 230 | assertEquals ("hij", initCtx.lookup("java:comp/env/mullheim")); | |
| 231 | ||
| 232 | //test that the other bindings are already there | |
| 233 | assertEquals ("xyz", (String)initCtx.lookup("java:comp/env/poubelle")); | |
| 234 | ||
| 235 | //test java:/comp/env/stuff | |
| 236 | assertEquals ("xyz", (String)initCtx.lookup("java:/comp/env/poubelle/")); | |
| 237 | ||
| 238 | //test list Names | |
| 239 | NamingEnumeration nenum = initCtx.list ("java:comp/env"); | |
| 240 | HashMap results = new HashMap(); | |
| 241 | while (nenum.hasMore()) | |
| 242 | { | |
| 243 | NameClassPair ncp = (NameClassPair)nenum.next(); | |
| 244 | results.put (ncp.getName(), ncp.getClassName()); | |
| 245 | } | |
| 246 | ||
| 247 | assertEquals (4, results.size()); | |
| 248 | ||
| 249 | assertEquals ("java.lang.String", (String)results.get("rubbish")); | |
| 250 | assertEquals ("javax.naming.LinkRef", (String)results.get("poubelle")); | |
| 251 | assertEquals ("java.lang.String", (String)results.get("mullheim")); | |
| 252 | assertEquals ("javax.naming.Reference", (String)results.get("quatsch")); | |
| 253 | ||
| 254 | //test list Bindings | |
| 255 | NamingEnumeration benum = initCtx.list("java:comp/env"); | |
| 256 | assertEquals (4, results.size()); | |
| 257 | ||
| 258 | //test NameInNamespace | |
| 259 | assertEquals ("comp/env", sub2.getNameInNamespace()); | |
| 260 | ||
| 261 | //test close does nothing | |
| 262 | Context closeCtx = (Context)initCtx.lookup("java:comp/env"); | |
| 263 | closeCtx.close(); | |
| 264 | ||
| 265 | ||
| 266 | //test what happens when you close an initial context | |
| 267 | InitialContext closeInit = new InitialContext(); | |
| 268 | closeInit.close(); | |
| 269 | ||
| 270 | ||
| 271 | ||
| 272 | //check locking the context | |
| 273 | Context ectx = (Context)initCtx.lookup("java:comp"); | |
| 274 | ectx.bind("crud", "xxx"); | |
| 275 | ectx.addToEnvironment("org.mortbay.jndi.immutable", "TRUE"); | |
| 276 | assertEquals ("xxx", (String)initCtx.lookup("java:comp/crud")); | |
| 277 | try | |
| 278 | { | |
| 279 | ectx.bind("crud2", "xxx2"); | |
| 280 | } | |
| 281 | catch (NamingException ne) | |
| 282 | { | |
| 283 | //expected failure to modify immutable context | |
| 284 | } | |
| 285 | ||
| 286 | //test what happens when you close an initial context that was used | |
| 287 | initCtx.close(); | |
| 277 | 288 | } |
| 278 | catch (NamingException ne) | |
| 289 | finally | |
| 279 | 290 | { |
| 280 | //expected failure to modify immutable context | |
| 291 | InitialContext ic = new InitialContext(); | |
| 292 | Context comp = (Context)ic.lookup("java:comp"); | |
| 293 | comp.destroySubcontext("env"); | |
| 281 | 294 | } |
| 282 | ||
| 283 | //test what happens when you close an initial context that was used | |
| 284 | initCtx.close(); | |
| 285 | ||
| 286 | 295 | } |
| 287 | 296 | |
| 288 | ||
| 289 | 297 | } |
| ... | ...@@ -6,7 +6,7 @@ | |
| 6 | 6 | <!-- Configure the test-annotations webapp --> |
| 7 | 7 | <!-- =============================================================== --> |
| 8 | 8 | |
| 9 | <Configure class="org.mortbay.jetty.webapp.WebAppContext"> | |
| 9 | <Configure id='wac' class="org.mortbay.jetty.webapp.WebAppContext"> | |
| 10 | 10 | |
| 11 | 11 | <!-- =========================================================== --> |
| 12 | 12 | <!-- Configurations for WebAppContexts --> |
| ... | ...@@ -54,6 +54,7 @@ | |
| 54 | 54 | |
| 55 | 55 | |
| 56 | 56 | <New id="maxAmount" class="org.mortbay.jetty.plus.naming.EnvEntry"> |
| 57 | <Arg><Ref id='wac'/></Arg> | |
| 57 | 58 | <Arg>maxAmount</Arg> |
| 58 | 59 | <Arg type="java.lang.Double">100</Arg> |
| 59 | 60 | <Arg type="boolean">true</Arg> |
| ... | ...@@ -81,6 +82,7 @@ | |
| 81 | 82 | |
| 82 | 83 | |
| 83 | 84 | <New id="mydatasource" class="org.mortbay.jetty.plus.naming.Resource"> |
| 85 | <Arg><Ref id='wac'/></Arg> | |
| 84 | 86 | <Arg>jdbc/mydatasource</Arg> |
| 85 | 87 | <Arg> |
| 86 | 88 |