Repository: tomee Updated Branches: refs/heads/master e0bbf0be6 -> 1ead00932
TOMEE-1962 deprecate LocalInitialContextFactory Project: http://git-wip-us.apache.org/repos/asf/tomee/repo Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/1ead0093 Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/1ead0093 Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/1ead0093 Branch: refs/heads/master Commit: 1ead00932d18728d6b66c923f76953ee4ac7914d Parents: e0bbf0b Author: rmannibucau <[email protected]> Authored: Thu Oct 20 18:27:57 2016 +0200 Committer: rmannibucau <[email protected]> Committed: Thu Oct 20 18:27:57 2016 +0200 ---------------------------------------------------------------------- .../core/LocalInitialContextFactory.java | 1 + .../core/OpenEJBInitialContextFactory.java | 76 ++++++++++++++++ .../client/LocalInitialContextFactory.java | 1 + .../core/OpenEJBInitialContextFactoryTest.java | 92 ++++++++++++++++++++ 4 files changed, 170 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tomee/blob/1ead0093/container/openejb-core/src/main/java/org/apache/openejb/core/LocalInitialContextFactory.java ---------------------------------------------------------------------- diff --git a/container/openejb-core/src/main/java/org/apache/openejb/core/LocalInitialContextFactory.java b/container/openejb-core/src/main/java/org/apache/openejb/core/LocalInitialContextFactory.java index ef318e0..de7961c 100644 --- a/container/openejb-core/src/main/java/org/apache/openejb/core/LocalInitialContextFactory.java +++ b/container/openejb-core/src/main/java/org/apache/openejb/core/LocalInitialContextFactory.java @@ -34,6 +34,7 @@ import java.util.concurrent.locks.ReentrantLock; * @version $Rev$ $Date$ */ @SuppressWarnings("UseOfObsoleteCollectionType") +@Deprecated // use org.apache.openejb.core.OpenEJBInitialContextFactory public class LocalInitialContextFactory implements InitialContextFactory { private static final ReentrantLock lock = new ReentrantLock(); http://git-wip-us.apache.org/repos/asf/tomee/blob/1ead0093/container/openejb-core/src/main/java/org/apache/openejb/core/OpenEJBInitialContextFactory.java ---------------------------------------------------------------------- diff --git a/container/openejb-core/src/main/java/org/apache/openejb/core/OpenEJBInitialContextFactory.java b/container/openejb-core/src/main/java/org/apache/openejb/core/OpenEJBInitialContextFactory.java new file mode 100644 index 0000000..0d91b41 --- /dev/null +++ b/container/openejb-core/src/main/java/org/apache/openejb/core/OpenEJBInitialContextFactory.java @@ -0,0 +1,76 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.openejb.core; + +import org.apache.openejb.core.ivm.naming.ContextWrapper; +import org.apache.openejb.loader.SystemInstance; +import org.apache.openejb.spi.ContainerSystem; + +import javax.naming.Context; +import javax.naming.NameNotFoundException; +import javax.naming.NamingException; +import javax.naming.spi.InitialContextFactory; +import java.util.Hashtable; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; + +public class OpenEJBInitialContextFactory implements InitialContextFactory { + @Override + public Context getInitialContext(final Hashtable env) throws NamingException { + // don't validate there env content, it is commonly built from System properties and therefore can inherit a bunch of things + return new LocalFallbackContextWrapper(SystemInstance.get().getComponent(ContainerSystem.class).getJNDIContext()); + } + + private static class LocalFallbackContextWrapper extends ContextWrapper { + private final ConcurrentMap<String, String> mapping = new ConcurrentHashMap<>(); + + private LocalFallbackContextWrapper(final Context jndiContext) { + super(jndiContext); + } + + @Override + public Object lookup(final String userName) throws NamingException { + String jndi = mapping.get(userName); + if (jndi == null) { + jndi = userName; + } + try { + return super.lookup(jndi); + } catch (final NameNotFoundException nnfe) { + if (!jndi.startsWith("java:") && !jndi.startsWith("openejb:")) { // try jndi lookup + try { + final String ejb = "java:openejb/local/" + jndi; + final Object lookup = super.lookup(ejb); + mapping.put(userName, ejb); + return lookup; + } catch (final NameNotFoundException nnfeIgnored) { // resource + try { + final String resource = "java:openejb/Resource/" + jndi; + final Object resourceInstance = super.lookup(resource); + mapping.put(userName, resource); + return resourceInstance; + } catch (final NameNotFoundException nnfeIgnoredAgain) { + throw nnfe; + } + } + } + throw nnfe; + } + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/tomee/blob/1ead0093/container/openejb-core/src/main/java/org/openejb/client/LocalInitialContextFactory.java ---------------------------------------------------------------------- diff --git a/container/openejb-core/src/main/java/org/openejb/client/LocalInitialContextFactory.java b/container/openejb-core/src/main/java/org/openejb/client/LocalInitialContextFactory.java index 01372e1..ed3adfc 100644 --- a/container/openejb-core/src/main/java/org/openejb/client/LocalInitialContextFactory.java +++ b/container/openejb-core/src/main/java/org/openejb/client/LocalInitialContextFactory.java @@ -21,5 +21,6 @@ package org.openejb.client; * @version $Revision: 1222119 $ $Date: 2011-12-22 10:23:14 +0100 (jeu. 22 déc. 2011) $ * @deprecated use org.apache.openejb.core.LocalInitialContextFactory */ +@Deprecated public class LocalInitialContextFactory extends org.apache.openejb.core.LocalInitialContextFactory { } http://git-wip-us.apache.org/repos/asf/tomee/blob/1ead0093/container/openejb-core/src/test/java/org/apache/openejb/core/OpenEJBInitialContextFactoryTest.java ---------------------------------------------------------------------- diff --git a/container/openejb-core/src/test/java/org/apache/openejb/core/OpenEJBInitialContextFactoryTest.java b/container/openejb-core/src/test/java/org/apache/openejb/core/OpenEJBInitialContextFactoryTest.java new file mode 100644 index 0000000..95be0e3 --- /dev/null +++ b/container/openejb-core/src/test/java/org/apache/openejb/core/OpenEJBInitialContextFactoryTest.java @@ -0,0 +1,92 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.openejb.core; + +import org.apache.openejb.junit.ApplicationComposer; +import org.apache.openejb.testing.Classes; +import org.apache.openejb.testing.ContainerProperties; +import org.apache.openejb.testng.PropertiesBuilder; +import org.junit.Test; +import org.junit.runner.RunWith; + +import javax.ejb.Singleton; +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.sql.DataSource; +import java.util.concurrent.Callable; +import java.util.concurrent.atomic.AtomicBoolean; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +@RunWith(ApplicationComposer.class) +@Classes(innerClassesAsBean = true) +@ContainerProperties(@ContainerProperties.Property(name = "db", value = "new://Resource?type=DataSource")) +public class OpenEJBInitialContextFactoryTest { + @Test + public void run() throws Exception { + final Callable<Boolean> innerTest = new Callable<Boolean>() { + @Override + public Boolean call() throws Exception { + final Context ctx = new InitialContext(new PropertiesBuilder() + .p(Context.INITIAL_CONTEXT_FACTORY, OpenEJBInitialContextFactory.class.getName()) + .build()); + + // ejbs + assertEquals("ejb", SomeEjb.class.cast(ctx.lookup("java:global/openejb/SomeEjb")).from()); + assertEquals("ejb", SomeEjb.class.cast(ctx.lookup("global/openejb/SomeEjb")).from()); + assertEquals("ejb", SomeEjb.class.cast(ctx.lookup("java:openejb/local/SomeEjbLocalBean")).from()); + assertEquals("ejb", SomeEjb.class.cast(ctx.lookup("openejb/local/SomeEjbLocalBean")).from()); + assertEquals("ejb", SomeEjb.class.cast(ctx.lookup("openejb:local/SomeEjbLocalBean")).from()); + assertEquals("ejb", SomeEjb.class.cast(ctx.lookup("SomeEjbLocalBean")).from()); + + // resources (hibernate use case for instance) + assertTrue(DataSource.class.isInstance(ctx.lookup("openejb:Resource/db"))); + assertTrue(DataSource.class.isInstance(ctx.lookup("java:openejb/Resource/db"))); + assertTrue(DataSource.class.isInstance(ctx.lookup("openejb/Resource/db"))); + assertTrue(DataSource.class.isInstance(ctx.lookup("db"))); + return true; + } + }; + + // in an unmanaged thread + final AtomicBoolean result = new AtomicBoolean(false); + final Thread outOfContext = new Thread(new Runnable() { + @Override + public void run() { + try { + result.set(innerTest.call()); + } catch (final Exception e) { + result.set(false); + } + } + }); + outOfContext.start(); + outOfContext.join(); + assertTrue(result.get()); + + // and in a managed thread + assertTrue(innerTest.call()); + } + + @Singleton + public static class SomeEjb { + public String from() { + return "ejb"; + } + } +}
