Add ee10 env scope for JNDI

This commit is contained in:
Jan Bartel 2022-07-13 12:55:19 +10:00
parent 927e94f673
commit 1e30b3ace5
13 changed files with 45 additions and 14 deletions

View File

@ -28,6 +28,7 @@ import org.eclipse.jetty.ee10.annotations.AnnotationIntrospector.AbstractIntrosp
import org.eclipse.jetty.ee10.plus.annotation.Injection;
import org.eclipse.jetty.ee10.plus.annotation.InjectionCollection;
import org.eclipse.jetty.ee10.plus.jndi.NamingEntryUtil;
import org.eclipse.jetty.ee10.servlet.ServletContextHandler;
import org.eclipse.jetty.ee10.webapp.MetaData;
import org.eclipse.jetty.ee10.webapp.WebAppContext;
import org.slf4j.Logger;
@ -152,9 +153,18 @@ public class ResourceAnnotationHandler extends AbstractIntrospectableAnnotationH
//No injection has been specified, add it
try
{
//try webapp scope first
boolean bound = NamingEntryUtil.bindToENC(_context, name, mappedName);
//try environment scope next
if (!bound)
bound = NamingEntryUtil.bindToENC(ServletContextHandler.__environment.getName(), name, mappedName);
//try Server scope next
if (!bound)
bound = NamingEntryUtil.bindToENC(_context.getServer(), name, mappedName);
//try jvm scope next
if (!bound)
bound = NamingEntryUtil.bindToENC(null, name, mappedName);
if (!bound)
@ -298,14 +308,18 @@ public class ResourceAnnotationHandler extends AbstractIntrospectableAnnotationH
try
{
//try binding name to environment
//try the webapp's environment first
//try the webapp's scope first
boolean bound = NamingEntryUtil.bindToENC(_context, name, mappedName);
//try the server's environment
//try the environment's scope
if (!bound)
bound = NamingEntryUtil.bindToENC(ServletContextHandler.__environment.getName(), name, mappedName);
//try the server's scope
if (!bound)
bound = NamingEntryUtil.bindToENC(_context.getServer(), name, mappedName);
//try the jvm's environment
//try the jvm's scope
if (!bound)
bound = NamingEntryUtil.bindToENC(null, name, mappedName);

View File

@ -46,7 +46,7 @@ public class ServerWithAnnotations
webapp.addConfiguration(new EnvConfiguration(), new PlusConfiguration(), new AnnotationConfiguration());
webapp.setContextPath("/");
Path warFile = JettyDemos.find("demo-spec/demo-spec-webapp/target/demo-spec-webapp-@VER@.war");
Path warFile = JettyDemos.find("ee10-demo-spec/ee10-demo-spec-webapp/target/demo-spec-webapp-@VER@.war");
webapp.setWar(warFile.toString());
webapp.setAttribute(
"org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern",
@ -55,7 +55,7 @@ public class ServerWithAnnotations
// Register new transaction manager in JNDI
// At runtime, the webapp accesses this as java:comp/UserTransaction
new Transaction(new org.example.MockUserTransaction());
new Transaction("ee10", new org.example.MockUserTransaction());
// Define an env entry with webapp scope.
// THIS ENTRY IS OVERRIDDEN BY THE ENTRY IN jetty-env.xml

View File

@ -36,7 +36,7 @@ public class ServerWithJNDI
// Create a WebApp
WebAppContext webapp = new WebAppContext();
webapp.setContextPath("/");
Path testJndiWar = JettyDemos.find("demo-jndi-webapp/target/demo-jndi-webapp-@VER@.war");
Path testJndiWar = JettyDemos.find("ee10-demo-jndi-webapp/target/ee10-demo-jndi-webapp-@VER@.war");
webapp.setWarResource(Resource.newResource(testJndiWar));
server.setHandler(webapp);
@ -45,7 +45,7 @@ public class ServerWithJNDI
// Register new transaction manager in JNDI
// At runtime, the webapp accesses this as java:comp/UserTransaction
new org.eclipse.jetty.ee10.plus.jndi.Transaction(
new org.eclipse.jetty.ee10.plus.jndi.Transaction("ee10",
new org.example.MockUserTransaction());
// Define an env entry with Server scope.

View File

@ -6,6 +6,7 @@
<Configure id='wac' class="org.eclipse.jetty.ee10.webapp.WebAppContext">
<New id="tx" class="org.eclipse.jetty.ee10.plus.jndi.Transaction">
<Arg><Property name="environment" default="ee10"/></Arg>
<Arg>
<New class="org.example.MockUserTransaction" />
</Arg>

View File

@ -7,6 +7,7 @@
<Configure id='wac' class="org.eclipse.jetty.ee10.webapp.WebAppContext">
<New id="tx" class="org.eclipse.jetty.ee10.plus.jndi.Transaction">
<Arg>ee10</Arg>
<Arg>
<New class="org.example.MockUserTransaction"/>
</Arg>

View File

@ -7,6 +7,7 @@
<Configure id='wac' class="org.eclipse.jetty.ee10.webapp.WebAppContext">
<New id="tx" class="org.eclipse.jetty.plus.jndi.Transaction">
<Arg>ee10</Arg>
<Arg>
<New class="org.example.MockUserTransaction"/>
</Arg>

View File

@ -8,6 +8,7 @@
<Set name="configurationDiscovered">true</Set>
<New id="tx" class="org.eclipse.jetty.ee10.plus.jndi.Transaction">
<Arg><Property name="environment" default="ee10"/></Arg>
<Arg>
<New class="org.example.MockUserTransaction" />
</Arg>

View File

@ -9,6 +9,7 @@
<Configure id='wac' class="org.eclipse.jetty.ee10.webapp.WebAppContext">
<New id="tx" class="org.eclipse.jetty.ee10.plus.jndi.Transaction">
<Arg>ee10</Arg>
<Arg>
<New class="org.example.MockUserTransaction"/>
</Arg>

View File

@ -10,6 +10,7 @@
<!-- Configure the tx mgr (only needed for mvn jetty:run -->
<New id="tx" class="org.eclipse.jetty.ee10.plus.jndi.Transaction">
<Arg>ee10</Arg>
<Arg>
<New class="org.example.MockUserTransaction"/>
</Arg>

View File

@ -23,6 +23,7 @@
<New id="tx" class="org.eclipse.jetty.ee10.plus.jndi.Transaction">
<Arg>ee10</Arg>
<Arg>
<New class="org.example.MockUserTransaction"/>
</Arg>

View File

@ -34,10 +34,14 @@ public class Transaction extends NamingEntry
private static final Logger LOG = LoggerFactory.getLogger(Transaction.class);
public static final String USER_TRANSACTION = "UserTransaction";
public static void bindToENC()
/**
* @param scope the scope level, usually an environment eg ee9 or ee10
* @throws NamingException
*/
public static void bindTransactionToENC(String scope)
throws NamingException
{
Transaction txEntry = (Transaction)NamingEntryUtil.lookupNamingEntry(null, Transaction.USER_TRANSACTION);
Transaction txEntry = (Transaction)NamingEntryUtil.lookupNamingEntry(scope, Transaction.USER_TRANSACTION);
if (txEntry != null)
{
@ -49,10 +53,15 @@ public class Transaction extends NamingEntry
}
}
public Transaction(UserTransaction userTransaction)
/**
* @param scope the scope, usually an environment like ee9, ee10
* @param userTransaction
* @throws NamingException
*/
public Transaction(String scope, UserTransaction userTransaction)
throws NamingException
{
super(USER_TRANSACTION);
super(scope, USER_TRANSACTION);
save(userTransaction);
}

View File

@ -20,6 +20,7 @@ import javax.naming.NameNotFoundException;
import org.eclipse.jetty.ee10.plus.annotation.InjectionCollection;
import org.eclipse.jetty.ee10.plus.annotation.LifeCycleCallbackCollection;
import org.eclipse.jetty.ee10.plus.jndi.Transaction;
import org.eclipse.jetty.ee10.servlet.ServletContextHandler;
import org.eclipse.jetty.ee10.webapp.AbstractConfiguration;
import org.eclipse.jetty.ee10.webapp.FragmentConfiguration;
import org.eclipse.jetty.ee10.webapp.JettyWebXmlConfiguration;
@ -89,7 +90,7 @@ public class PlusConfiguration extends AbstractConfiguration
{
try
{
Transaction.bindToENC();
Transaction.bindTransactionToENC(ServletContextHandler.__environment.getName());
}
catch (NameNotFoundException e)
{

View File

@ -117,7 +117,7 @@ import org.slf4j.LoggerFactory;
public class ServletContextHandler extends ContextHandler implements Graceful
{
private static final Logger LOG = LoggerFactory.getLogger(ServletContextHandler.class);
protected static final Environment __environment = Environment.ensure("ee10");
public static final Environment __environment = Environment.ensure("ee10");
public static final Class<?>[] SERVLET_LISTENER_TYPES =
{
ServletContextListener.class,