JNDI support for Eclipse Jetty web server including dependency injection, lifecycle management, JNDI resource binding, and database-backed security services
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
JNDI support for Eclipse Jetty web server providing comprehensive enterprise Java features including dependency injection, lifecycle management, JNDI resource binding, and database-backed security services.
<groupId>org.eclipse.jetty</groupId><artifactId>jetty-plus</artifactId><version>12.0.21</version>// Dependency injection and lifecycle management
import org.eclipse.jetty.plus.annotation.Injection;
import org.eclipse.jetty.plus.annotation.InjectionCollection;
import org.eclipse.jetty.plus.annotation.LifeCycleCallback;
import org.eclipse.jetty.plus.annotation.LifeCycleCallbackCollection;
import org.eclipse.jetty.plus.annotation.PostConstructCallback;
import org.eclipse.jetty.plus.annotation.PreDestroyCallback;
// JNDI resource binding
import org.eclipse.jetty.plus.jndi.NamingEntry;
import org.eclipse.jetty.plus.jndi.Resource;
import org.eclipse.jetty.plus.jndi.EnvEntry;
import org.eclipse.jetty.plus.jndi.Link;
import org.eclipse.jetty.plus.jndi.Transaction;
import org.eclipse.jetty.plus.jndi.DataSourceCloser;
import org.eclipse.jetty.plus.jndi.NamingEntryUtil;
// Database security
import org.eclipse.jetty.plus.security.DataSourceLoginService;import org.eclipse.jetty.plus.jndi.Resource;
import org.eclipse.jetty.plus.annotation.InjectionCollection;
import org.eclipse.jetty.plus.security.DataSourceLoginService;
import javax.sql.DataSource;
// 1. JNDI Resource Binding
// Bind a DataSource to JNDI for application use
DataSource dataSource = // ... your datasource configuration
Resource dsResource = new Resource("jdbc/MyDB", dataSource);
// 2. Dependency Injection Setup
// Configure injection collection for webapp
InjectionCollection injections = new InjectionCollection();
// Add to webapp context as attribute
context.setAttribute(InjectionCollection.INJECTION_COLLECTION, injections);
// 3. Database-backed Authentication
// Configure login service with database
DataSourceLoginService loginService = new DataSourceLoginService("MyRealm");
loginService.setJndiName("jdbc/MyDB");
loginService.setUserTableName("users");
loginService.setUserTableUserField("username");
loginService.setUserTablePasswordField("password");
loginService.setRoleTableName("roles");
server.addBean(loginService);The jetty-plus module is organized into three main functional areas:
This modular design allows applications to use only the features they need while maintaining full JEE compatibility for enterprise Java applications.
Complete annotation-based dependency injection system with lifecycle callback support. Handles @Resource, @PostConstruct, and @PreDestroy annotations for enterprise Java applications.
// Core injection functionality
class Injection {
Injection(Class<?> clazz, Field field, Class<?> resourceType, String jndiName, String mappingName);
Injection(Class<?> clazz, Method method, Class<?> arg, Class<?> resourceType, String jndiName, String mappingName);
void inject(Object injectable);
Object lookupInjectedValue() throws NamingException;
}
class InjectionCollection {
void add(Injection injection);
Set<Injection> getInjections(String className);
void inject(Object injectable);
}
// Lifecycle management
class LifeCycleCallbackCollection {
void add(LifeCycleCallback callback);
void callPostConstructCallback(Object o) throws Exception;
void callPreDestroyCallback(Object o) throws Exception;
}Dependency Injection and Lifecycle
Comprehensive JNDI naming and resource binding capabilities. Supports environment entries, resources, links, and transactions with full webapp context integration.
// Core JNDI functionality
abstract class NamingEntry {
void bindToENC(String localName) throws NamingException;
void unbindENC();
String getJndiName();
}
class Resource extends NamingEntry {
Resource(Object scope, String jndiName, Object objToBind) throws NamingException;
Resource(String jndiName, Object objToBind) throws NamingException;
}
class NamingEntryUtil {
static boolean bindToENC(Object scope, String asName, String mappedName) throws NamingException;
static Object lookup(Object scope, String jndiName) throws NamingException;
static Context getContextForScope(Object scope) throws NamingException;
}Database-backed authentication and authorization using JNDI DataSources. Configurable user/role schema with full Jetty security integration.
class DataSourceLoginService extends AbstractLoginService {
DataSourceLoginService();
DataSourceLoginService(String name);
// Configuration
void setJndiName(String jndi);
void setUserTableName(String name);
void setUserTableUserField(String tableUserField);
void setUserTablePasswordField(String tablePasswordField);
void setRoleTableName(String tableName);
// Core authentication
UserPrincipal loadUserInfo(String username);
List<RolePrincipal> loadRoleInfo(UserPrincipal user);
}Required Dependencies:
org.eclipse.jetty:jetty-security - Core Jetty security frameworkorg.eclipse.jetty:jetty-util - Jetty utilitiesorg.slf4j:slf4j-api - Logging frameworkModule Requirements:
java.naming - Java Naming and Directory Interfacejava.sql - SQL database connectivity (for DataSourceLoginService)// Configure JNDI and injection for a webapp
WebAppContext webapp = new WebAppContext();
// Set up injection collection
InjectionCollection injections = new InjectionCollection();
webapp.setAttribute(InjectionCollection.INJECTION_COLLECTION, injections);
// Set up lifecycle callbacks
LifeCycleCallbackCollection callbacks = new LifeCycleCallbackCollection();
webapp.setAttribute(LifeCycleCallbackCollection.LIFECYCLE_CALLBACK_COLLECTION, callbacks);
// Bind JNDI resources
Resource dataSource = new Resource(webapp, "jdbc/MyDB", myDataSource);
EnvEntry maxConnections = new EnvEntry(webapp, "maxConnections", 100);// Complete enterprise setup combining all features
Server server = new Server();
// Database login service
DataSourceLoginService loginService = new DataSourceLoginService("MyRealm");
loginService.setJndiName("jdbc/SecurityDB");
loginService.setCreateTables(false);
server.addBean(loginService);
// Security handler with database authentication
SecurityHandler security = new SecurityHandler();
security.setLoginService(loginService);
// Webapp with JNDI and injection support
WebAppContext webapp = new WebAppContext();
webapp.setSecurityHandler(security);
// Configure JNDI resources
Resource securityDB = new Resource("jdbc/SecurityDB", createSecurityDataSource());
Resource appDB = new Resource("jdbc/AppDB", createApplicationDataSource());
// Transaction support
Transaction.bindTransactionToENC(webapp.toString());