|
19 | 19 | */ |
20 | 20 | package org.zaproxy.addon.authhelper.internal.db; |
21 | 21 |
|
| 22 | +import java.lang.reflect.Method; |
22 | 23 | import java.sql.Connection; |
23 | 24 | import java.sql.SQLException; |
24 | 25 | import java.util.Properties; |
25 | 26 | import javax.jdo.Constants; |
26 | 27 | import javax.jdo.JDOHelper; |
27 | 28 | import javax.jdo.PersistenceManagerFactory; |
| 29 | +import org.apache.logging.log4j.LogManager; |
| 30 | +import org.apache.logging.log4j.Logger; |
28 | 31 | import org.datanucleus.PropertyNames; |
29 | 32 | import org.flywaydb.core.Flyway; |
| 33 | +import org.parosproxy.paros.db.Database; |
30 | 34 | import org.parosproxy.paros.db.DatabaseException; |
| 35 | +import org.parosproxy.paros.db.DatabaseListener; |
31 | 36 | import org.parosproxy.paros.db.DatabaseServer; |
32 | | -import org.parosproxy.paros.db.DatabaseUnsupportedException; |
33 | | -import org.parosproxy.paros.db.paros.ParosAbstractTable; |
| 37 | +import org.parosproxy.paros.db.paros.ParosDatabaseServer; |
34 | 38 |
|
35 | | -public class TableJdo extends ParosAbstractTable { |
| 39 | +public class TableJdo implements DatabaseListener { |
36 | 40 |
|
37 | | - private static final String USER = "sa"; |
| 41 | + private static final Logger LOGGER = LogManager.getLogger(TableJdo.class); |
| 42 | + |
| 43 | + private static Method getUrlMethod; |
| 44 | + private static Method getUserMethod; |
| 45 | + private static Method getPasswordMethod; |
38 | 46 |
|
39 | 47 | private static PersistenceManagerFactory pmf; |
40 | 48 |
|
41 | | - @Override |
42 | | - public void databaseOpen(DatabaseServer server) |
43 | | - throws DatabaseException, DatabaseUnsupportedException { |
44 | | - if (pmf != null) { |
45 | | - pmf.close(); |
46 | | - pmf = null; |
| 49 | + private final Database db; |
| 50 | + |
| 51 | + static { |
| 52 | + try { |
| 53 | + Class<?> dbServerClass = Class.forName("org.parosproxy.paros.db.DatabaseServer"); |
| 54 | + getUrlMethod = dbServerClass.getMethod("getUrl"); |
| 55 | + getUserMethod = dbServerClass.getMethod("getUser"); |
| 56 | + getPasswordMethod = dbServerClass.getMethod("getPassword"); |
| 57 | + |
| 58 | + } catch (Exception e) { |
| 59 | + LOGGER.debug("An error occurred while getting the methods:", e); |
47 | 60 | } |
| 61 | + } |
| 62 | + |
| 63 | + public TableJdo(Database db) throws DatabaseException { |
| 64 | + this.db = db; |
48 | 65 |
|
49 | | - super.databaseOpen(server); |
| 66 | + db.addDatabaseListener(this); |
| 67 | + databaseOpen(db.getDatabaseServer()); |
50 | 68 | } |
51 | 69 |
|
52 | 70 | @Override |
53 | | - protected void reconnect(Connection conn) throws DatabaseException { |
54 | | - try { |
55 | | - String dbUrl = conn.getMetaData().getURL(); |
56 | | - ClassLoader classLoader = this.getClass().getClassLoader(); |
57 | | - Flyway.configure(classLoader) |
58 | | - .table("AUTHHELPER_FLYWAY_SCHEMA_HISTORY") |
59 | | - .baselineOnMigrate(true) |
60 | | - .baselineVersion("0") |
61 | | - .dataSource(dbUrl, USER, "") |
62 | | - .load() |
63 | | - .migrate(); |
| 71 | + public void databaseOpen(DatabaseServer db) throws DatabaseException { |
| 72 | + if (getUrlMethod == null) { |
| 73 | + closing(db); |
| 74 | + } |
| 75 | + |
| 76 | + String dbUrl = getUrl(db); |
| 77 | + String user = getUser(db); |
| 78 | + String password = getPassword(db); |
| 79 | + ClassLoader classLoader = this.getClass().getClassLoader(); |
| 80 | + Flyway.configure(classLoader) |
| 81 | + .table("AUTHHELPER_FLYWAY_SCHEMA_HISTORY") |
| 82 | + .baselineOnMigrate(true) |
| 83 | + .baselineVersion("0") |
| 84 | + .dataSource(dbUrl, user, password) |
| 85 | + .load() |
| 86 | + .migrate(); |
64 | 87 |
|
65 | | - Properties jdoProperties = new Properties(); |
66 | | - jdoProperties.setProperty(Constants.PROPERTY_CONNECTION_URL, dbUrl); |
67 | | - jdoProperties.setProperty(Constants.PROPERTY_CONNECTION_USER_NAME, USER); |
| 88 | + Properties jdoProperties = new Properties(); |
| 89 | + jdoProperties.setProperty(Constants.PROPERTY_CONNECTION_URL, dbUrl); |
| 90 | + jdoProperties.setProperty(Constants.PROPERTY_CONNECTION_USER_NAME, user); |
| 91 | + jdoProperties.setProperty(Constants.PROPERTY_CONNECTION_PASSWORD, password); |
68 | 92 |
|
69 | | - jdoProperties.put(PropertyNames.PROPERTY_CLASSLOADER_PRIMARY, classLoader); |
| 93 | + jdoProperties.put(PropertyNames.PROPERTY_CLASSLOADER_PRIMARY, classLoader); |
| 94 | + |
| 95 | + pmf = JDOHelper.getPersistenceManagerFactory(jdoProperties, "authhelper", classLoader); |
| 96 | + } |
70 | 97 |
|
71 | | - pmf = JDOHelper.getPersistenceManagerFactory(jdoProperties, "authhelper", classLoader); |
| 98 | + private static String getUrl(DatabaseServer db) throws DatabaseException { |
| 99 | + try { |
| 100 | + if (getUrlMethod != null) { |
| 101 | + return (String) getUrlMethod.invoke(db); |
| 102 | + } |
| 103 | + } catch (Exception e) { |
| 104 | + LOGGER.warn("An error occurred while getting the URL:", e); |
| 105 | + } |
72 | 106 |
|
| 107 | + try (Connection connection = getConnection(db)) { |
| 108 | + return connection.getMetaData().getURL(); |
73 | 109 | } catch (SQLException e) { |
74 | 110 | throw new DatabaseException(e); |
75 | 111 | } |
76 | 112 | } |
77 | 113 |
|
| 114 | + private static Connection getConnection(DatabaseServer db) throws SQLException { |
| 115 | + if (db instanceof ParosDatabaseServer pds) { |
| 116 | + return pds.getNewConnection(); |
| 117 | + } |
| 118 | + if (db instanceof ParosDatabaseServer pds) { |
| 119 | + return pds.getNewConnection(); |
| 120 | + } |
| 121 | + throw new SQLException("Unknown DB implementation"); |
| 122 | + } |
| 123 | + |
| 124 | + private static String getUser(DatabaseServer db) { |
| 125 | + try { |
| 126 | + if (getUserMethod != null) { |
| 127 | + return (String) getUserMethod.invoke(db); |
| 128 | + } |
| 129 | + } catch (Exception e) { |
| 130 | + LOGGER.warn("An error occurred while getting the user:", e); |
| 131 | + } |
| 132 | + |
| 133 | + return "sa"; |
| 134 | + } |
| 135 | + |
| 136 | + private static String getPassword(DatabaseServer db) { |
| 137 | + try { |
| 138 | + if (getPasswordMethod != null) { |
| 139 | + return (String) getPasswordMethod.invoke(db); |
| 140 | + } |
| 141 | + } catch (Exception e) { |
| 142 | + LOGGER.warn("An error occurred while getting the password:", e); |
| 143 | + } |
| 144 | + |
| 145 | + return ""; |
| 146 | + } |
| 147 | + |
| 148 | + // @Override |
| 149 | + public void closing(DatabaseServer db) { |
| 150 | + if (pmf != null) { |
| 151 | + pmf.close(); |
| 152 | + pmf = null; |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + public void unload() { |
| 157 | + db.removeDatabaseListener(this); |
| 158 | + } |
| 159 | + |
78 | 160 | public static PersistenceManagerFactory getPmf() { |
79 | 161 | return pmf; |
80 | 162 | } |
|
0 commit comments