-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[WFLY-22003] Replace JPA initialization with JDBC CDI bean #1197
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Riovo
wants to merge
1
commit into
wildfly:main
Choose a base branch
from
Riovo:WFLY-22003
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletions
145
...security/src/main/java/org/jboss/as/quickstarts/servlet_security/DatabaseInitializer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| /* | ||
| * Copyright The WildFly Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| package org.jboss.as.quickstarts.servlet_security; | ||
|
|
||
| import java.sql.Connection; | ||
| import java.sql.PreparedStatement; | ||
| import java.sql.ResultSet; | ||
| import java.sql.SQLException; | ||
| import java.sql.Statement; | ||
| import java.util.logging.Level; | ||
| import java.util.logging.Logger; | ||
|
|
||
| import javax.sql.DataSource; | ||
|
|
||
| import jakarta.annotation.Resource; | ||
| import jakarta.enterprise.context.ApplicationScoped; | ||
| import jakarta.enterprise.context.Initialized; | ||
| import jakarta.enterprise.event.Observes; | ||
|
|
||
| /** | ||
| * Initializes the database schema and data for Elytron JDBC realm authentication. | ||
| * This CDI bean runs at application startup to create tables and populate | ||
| * test user credentials if they don't already exist. | ||
| * | ||
| * @author Mohammed Abourass mouhammedmax@hotmail.com | ||
| */ | ||
| @ApplicationScoped | ||
| public class DatabaseInitializer { | ||
|
|
||
| private static final Logger LOGGER = Logger.getLogger(DatabaseInitializer.class.getName()); | ||
|
|
||
| @Resource(lookup = "java:jboss/datasources/ServletSecurityDS") | ||
| private DataSource dataSource; | ||
|
|
||
| public void initializeDatabase(@Observes @Initialized(ApplicationScoped.class) Object init) { | ||
| LOGGER.info("Initializing database schema for servlet-security..."); | ||
|
|
||
| try { | ||
| createTables(); | ||
| insertTestData(); | ||
| LOGGER.info("Database initialization completed successfully."); | ||
| } catch (SQLException e) { | ||
| LOGGER.log(Level.SEVERE, "Failed to initialize database", e); | ||
| throw new RuntimeException("Database initialization failed", e); | ||
| } | ||
| } | ||
|
|
||
| private void createTables() throws SQLException { | ||
| try (Connection conn = dataSource.getConnection(); | ||
| Statement stmt = conn.createStatement()) { | ||
|
|
||
| // Create USERS table if not exists | ||
| stmt.executeUpdate( | ||
| "CREATE TABLE IF NOT EXISTS USERS (" + | ||
| "ID INT, " + | ||
| "USERNAME VARCHAR(20), " + | ||
| "PASSWORD VARCHAR(20))" | ||
| ); | ||
|
|
||
| // Create ROLES table if not exists | ||
| stmt.executeUpdate( | ||
| "CREATE TABLE IF NOT EXISTS ROLES (" + | ||
| "ID INT, " + | ||
| "NAME VARCHAR(20))" | ||
| ); | ||
|
|
||
| // Create USERS_ROLES junction table if not exists | ||
| stmt.executeUpdate( | ||
| "CREATE TABLE IF NOT EXISTS USERS_ROLES (" + | ||
| "USER_ID INT, " + | ||
| "ROLE_ID INT)" | ||
| ); | ||
|
|
||
| LOGGER.info("Database tables created or verified"); | ||
| } | ||
| } | ||
|
|
||
| private void insertTestData() throws SQLException { | ||
| try (Connection conn = dataSource.getConnection()) { | ||
|
|
||
| // Check if data already exists (avoid duplicates on redeployment) | ||
| if (userExists(conn, "quickstartUser")) { | ||
| LOGGER.info("Test data already exists, skipping insertion"); | ||
| return; | ||
| } | ||
|
|
||
| // Insert users | ||
| try (PreparedStatement insertStmt = conn.prepareStatement( | ||
| "INSERT INTO USERS (ID, USERNAME, PASSWORD) VALUES (?, ?, ?)")) { | ||
|
|
||
| insertStmt.setInt(1, 1); | ||
| insertStmt.setString(2, "quickstartUser"); | ||
| insertStmt.setString(3, "quickstartPwd1!"); | ||
| insertStmt.executeUpdate(); | ||
|
|
||
| insertStmt.setInt(1, 2); | ||
| insertStmt.setString(2, "guest"); | ||
| insertStmt.setString(3, "guestPwd1!"); | ||
| insertStmt.executeUpdate(); | ||
| } | ||
|
|
||
| // Insert roles | ||
| try (PreparedStatement insertStmt = conn.prepareStatement( | ||
| "INSERT INTO ROLES (ID, NAME) VALUES (?, ?)")) { | ||
|
|
||
| insertStmt.setInt(1, 1); | ||
| insertStmt.setString(2, "quickstarts"); | ||
| insertStmt.executeUpdate(); | ||
|
|
||
| insertStmt.setInt(1, 2); | ||
| insertStmt.setString(2, "guest"); | ||
| insertStmt.executeUpdate(); | ||
| } | ||
|
|
||
| // Insert user-role mappings | ||
| try (PreparedStatement insertStmt = conn.prepareStatement( | ||
| "INSERT INTO USERS_ROLES (USER_ID, ROLE_ID) VALUES (?, ?)")) { | ||
|
|
||
| insertStmt.setInt(1, 1); | ||
| insertStmt.setInt(2, 1); | ||
| insertStmt.executeUpdate(); | ||
|
|
||
| insertStmt.setInt(1, 2); | ||
| insertStmt.setInt(2, 2); | ||
| insertStmt.executeUpdate(); | ||
| } | ||
|
|
||
| LOGGER.info("Test data inserted successfully"); | ||
| } | ||
| } | ||
|
|
||
| private boolean userExists(Connection conn, String username) throws SQLException { | ||
| try (PreparedStatement stmt = conn.prepareStatement("SELECT COUNT(*) FROM USERS WHERE USERNAME = ?")) { | ||
| stmt.setString(1, username); | ||
| try (ResultSet rs = stmt.executeQuery()) { | ||
| if (rs.next()) { | ||
| return rs.getInt(1) > 0; | ||
| } | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
| } | ||
33 changes: 0 additions & 33 deletions
33
servlet-security/src/main/java/org/jboss/as/quickstarts/servlet_security/DummyEntity.java
This file was deleted.
Oops, something went wrong.
35 changes: 0 additions & 35 deletions
35
servlet-security/src/main/resources/META-INF/persistence.xml
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.