Repository navigation
[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
Merged
Merged
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
126 changes: 126 additions & 0 deletions
126
...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,126 @@ | ||
| /* | ||
| * 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.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. | ||
| * | ||
| * @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 (Connection conn = dataSource.getConnection()) { | ||
| conn.setAutoCommit(false); | ||
| try { | ||
| createTables(conn); | ||
| insertTestData(conn); | ||
| conn.commit(); | ||
| } catch (SQLException e) { | ||
| conn.rollback(); | ||
| throw e; | ||
| } | ||
| 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(Connection conn) throws SQLException { | ||
| try (Statement stmt = conn.createStatement()) { | ||
| stmt.executeUpdate("DROP TABLE IF EXISTS USERS_ROLES"); | ||
| stmt.executeUpdate("DROP TABLE IF EXISTS ROLES"); | ||
| stmt.executeUpdate("DROP TABLE IF EXISTS USERS"); | ||
|
|
||
| stmt.executeUpdate( | ||
| "CREATE TABLE USERS (" + | ||
| "ID INT, " + | ||
| "USERNAME VARCHAR(20), " + | ||
| "PASSWORD VARCHAR(20))" | ||
| ); | ||
|
|
||
| stmt.executeUpdate( | ||
| "CREATE TABLE ROLES (" + | ||
| "ID INT, " + | ||
| "NAME VARCHAR(20))" | ||
| ); | ||
|
|
||
| stmt.executeUpdate( | ||
| "CREATE TABLE USERS_ROLES (" + | ||
| "USER_ID INT, " + | ||
| "ROLE_ID INT)" | ||
| ); | ||
|
|
||
| LOGGER.info("Database tables created"); | ||
| } | ||
| } | ||
|
|
||
| private void insertTestData(Connection conn) throws SQLException { | ||
| 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(); | ||
| } | ||
|
|
||
| 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(); | ||
| } | ||
|
|
||
| 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"); | ||
| } | ||
| } | ||
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.