From dfa31bb25b3aaa009e331e96bfd26409c59fc21d Mon Sep 17 00:00:00 2001 From: Philip Helger Date: Tue, 30 Jul 2024 22:23:05 +0200 Subject: [PATCH] Some #267 post processing --- .../mgr/SMPSettingsManagerMongoDB.java | 39 +++++++++++-------- .../sql/mgr/SMPSettingsManagerJDBC.java | 14 +++---- .../phoss/smp/settings/SMPSettings.java | 18 ++++++++- .../smp/settings/SMPSettingsManagerXML.java | 26 ++++++------- .../phoss/smp/settings/SMPSettingsTest.java | 2 +- 5 files changed, 58 insertions(+), 41 deletions(-) diff --git a/phoss-smp-backend-mongodb/src/main/java/com/helger/phoss/smp/backend/mongodb/mgr/SMPSettingsManagerMongoDB.java b/phoss-smp-backend-mongodb/src/main/java/com/helger/phoss/smp/backend/mongodb/mgr/SMPSettingsManagerMongoDB.java index 1507d1c74..aa4456bd7 100644 --- a/phoss-smp-backend-mongodb/src/main/java/com/helger/phoss/smp/backend/mongodb/mgr/SMPSettingsManagerMongoDB.java +++ b/phoss-smp-backend-mongodb/src/main/java/com/helger/phoss/smp/backend/mongodb/mgr/SMPSettingsManagerMongoDB.java @@ -23,10 +23,12 @@ import org.bson.Document; +import com.helger.commons.ValueEnforcer; import com.helger.commons.annotation.ReturnsMutableCopy; import com.helger.commons.annotation.ReturnsMutableObject; import com.helger.commons.callback.CallbackList; import com.helger.commons.state.EChange; +import com.helger.commons.string.StringHelper; import com.helger.phoss.smp.settings.ISMPSettings; import com.helger.phoss.smp.settings.ISMPSettingsCallback; import com.helger.phoss.smp.settings.ISMPSettingsManager; @@ -51,7 +53,7 @@ public class SMPSettingsManagerMongoDB extends AbstractManagerMongoDB implements private static final String BSON_SML_ENABLED = "sml-enabled"; private static final String BSON_SML_INFO_ID = "smlinfo-id"; - private final SMPSettings m_aSettings = new SMPSettings (true); + private final SMPSettings m_aSMPSettings = SMPSettings.createInitializedFromConfiguration (); private final CallbackList m_aCallbacks = new CallbackList <> (); private final AtomicBoolean m_aInsertDocument = new AtomicBoolean (); @@ -76,6 +78,9 @@ public static Document toBson (@Nonnull final ISMPSettings aValue) public static void toDomain (@Nonnull final Document aDoc, @Nonnull final SMPSettings aTarget) { + ValueEnforcer.notNull (aDoc, "Doc"); + ValueEnforcer.notNull (aTarget, "Target"); + aTarget.setRESTWritableAPIDisabled (aDoc.getBoolean (BSON_SMP_REST_WRITABLE_API_DISABLED, aTarget.isRESTWritableAPIDisabled ())); aTarget.setDirectoryIntegrationEnabled (aDoc.getBoolean (BSON_DIRECTORY_INTEGRATION_ENABLED, @@ -84,12 +89,12 @@ public static void toDomain (@Nonnull final Document aDoc, @Nonnull final SMPSet aTarget.isDirectoryIntegrationRequired ())); aTarget.setDirectoryIntegrationAutoUpdate (aDoc.getBoolean (BSON_DIRECTORY_INTEGRATION_AUTO_UPDATE, aTarget.isDirectoryIntegrationAutoUpdate ())); + String sDirectoryHostName = aDoc.getString (BSON_DIRECTORY_HOSTNAME); - if (sDirectoryHostName == null) - { + if (StringHelper.hasNoText (sDirectoryHostName)) sDirectoryHostName = aTarget.getDirectoryHostName (); - } aTarget.setDirectoryHostName (sDirectoryHostName); + aTarget.setSMLEnabled (aDoc.getBoolean (BSON_SML_ENABLED, aTarget.isSMLEnabled ())); aTarget.setSMLRequired (aDoc.getBoolean (BSON_SML_REQUIRED, aTarget.isSMLRequired ())); aTarget.setSMLInfoID (aDoc.getString (BSON_SML_INFO_ID)); @@ -100,12 +105,12 @@ public SMPSettingsManagerMongoDB () super ("smp-settings"); final Document aDoc = getCollection ().find (new Document (BSON_ID, ID_SETTINGS)).first (); if (aDoc != null) - toDomain (aDoc, m_aSettings); + toDomain (aDoc, m_aSMPSettings); m_aInsertDocument.set (aDoc == null); } @Nonnull - @aTargeturnsMutableObject + @ReturnsMutableObject public final CallbackList callbacks () { return m_aCallbacks; @@ -114,7 +119,7 @@ public final CallbackList callbacks () @Nonnull public ISMPSettings getSettings () { - return m_aSettings; + return m_aSMPSettings; } @Nonnull @@ -128,18 +133,18 @@ public EChange updateSettings (final boolean bRESTWritableAPIDisabled, @Nullable final String sSMLInfoID) { EChange eChange = EChange.UNCHANGED; - eChange = eChange.or (m_aSettings.setRESTWritableAPIDisabled (bRESTWritableAPIDisabled)); - eChange = eChange.or (m_aSettings.setDirectoryIntegrationEnabled (bDirectoryIntegrationEnabled)); - eChange = eChange.or (m_aSettings.setDirectoryIntegrationRequired (bDirectoryIntegrationRequired)); - eChange = eChange.or (m_aSettings.setDirectoryIntegrationAutoUpdate (bDirectoryIntegrationAutoUpdate)); - eChange = eChange.or (m_aSettings.setDirectoryHostName (sDirectoryHostName)); - eChange = eChange.or (m_aSettings.setSMLEnabled (bSMLEnabled)); - eChange = eChange.or (m_aSettings.setSMLRequired (bSMLRequired)); - eChange = eChange.or (m_aSettings.setSMLInfoID (sSMLInfoID)); + eChange = eChange.or (m_aSMPSettings.setRESTWritableAPIDisabled (bRESTWritableAPIDisabled)); + eChange = eChange.or (m_aSMPSettings.setDirectoryIntegrationEnabled (bDirectoryIntegrationEnabled)); + eChange = eChange.or (m_aSMPSettings.setDirectoryIntegrationRequired (bDirectoryIntegrationRequired)); + eChange = eChange.or (m_aSMPSettings.setDirectoryIntegrationAutoUpdate (bDirectoryIntegrationAutoUpdate)); + eChange = eChange.or (m_aSMPSettings.setDirectoryHostName (sDirectoryHostName)); + eChange = eChange.or (m_aSMPSettings.setSMLEnabled (bSMLEnabled)); + eChange = eChange.or (m_aSMPSettings.setSMLRequired (bSMLRequired)); + eChange = eChange.or (m_aSMPSettings.setSMLInfoID (sSMLInfoID)); if (eChange.isChanged ()) { // Write to DB - final Document aDoc = toBson (m_aSettings); + final Document aDoc = toBson (m_aSMPSettings); if (m_aInsertDocument.getAndSet (false)) { if (!getCollection ().insertOne (aDoc).wasAcknowledged ()) @@ -152,7 +157,7 @@ public EChange updateSettings (final boolean bRESTWritableAPIDisabled, } // Invoke callbacks - m_aCallbacks.forEach (x -> x.onSMPSettingsChanged (m_aSettings)); + m_aCallbacks.forEach (x -> x.onSMPSettingsChanged (m_aSMPSettings)); } return eChange; diff --git a/phoss-smp-backend-sql/src/main/java/com/helger/phoss/smp/backend/sql/mgr/SMPSettingsManagerJDBC.java b/phoss-smp-backend-sql/src/main/java/com/helger/phoss/smp/backend/sql/mgr/SMPSettingsManagerJDBC.java index 529413ad1..76e3d70b9 100644 --- a/phoss-smp-backend-sql/src/main/java/com/helger/phoss/smp/backend/sql/mgr/SMPSettingsManagerJDBC.java +++ b/phoss-smp-backend-sql/src/main/java/com/helger/phoss/smp/backend/sql/mgr/SMPSettingsManagerJDBC.java @@ -167,7 +167,7 @@ public String getSettingsValue (@Nullable final String sKey) public static class SettingsSingleton extends AbstractRequestWebSingleton { private static final Logger LOGGER = LoggerFactory.getLogger (SMPSettingsManagerJDBC.SettingsSingleton.class); - private ISMPSettings m_aSettings; + private ISMPSettings m_aSMPSettings; @Deprecated @UsedViaReflection @@ -181,12 +181,12 @@ public static SettingsSingleton getInstance () } @Nonnull - private ISMPSettings _getSettingsFromDB (@Nonnull final SMPSettingsManagerJDBC aMgr) + private static ISMPSettings _createSettingsFromDB (@Nonnull final SMPSettingsManagerJDBC aMgr) { // Queries DB final ICommonsMap aValues = aMgr.getAllSettingsValuesFromDB (); - final SMPSettings ret = new SMPSettings (true); + final SMPSettings ret = SMPSettings.createInitializedFromConfiguration (); ret.setRESTWritableAPIDisabled (StringParser.parseBool (aValues.get (SMP_REST_WRITABLE_API_DISABLED), ret.isRESTWritableAPIDisabled ())); ret.setDirectoryIntegrationEnabled (StringParser.parseBool (aValues.get (DIRECTORY_INTEGRATION_ENABLED), @@ -196,10 +196,8 @@ private ISMPSettings _getSettingsFromDB (@Nonnull final SMPSettingsManagerJDBC a ret.setDirectoryIntegrationAutoUpdate (StringParser.parseBool (aValues.get (DIRECTORY_INTEGRATION_AUTO_UPDATE), ret.isDirectoryIntegrationAutoUpdate ())); String sDirectoryHostName = aValues.get (DIRECTORY_HOSTNAME); - if (sDirectoryHostName == null) - { + if (StringHelper.hasNoText (sDirectoryHostName)) sDirectoryHostName = ret.getDirectoryHostName (); - } ret.setDirectoryHostName (sDirectoryHostName); ret.setSMLEnabled (StringParser.parseBool (aValues.get (SML_ENABLED), ret.isSMLEnabled ())); ret.setSMLRequired (StringParser.parseBool (aValues.get (SML_REQUIRED), ret.isSMLRequired ())); @@ -210,12 +208,12 @@ private ISMPSettings _getSettingsFromDB (@Nonnull final SMPSettingsManagerJDBC a @Nonnull public ISMPSettings getSettings (@Nonnull final SMPSettingsManagerJDBC aMgr) { - ISMPSettings ret = m_aSettings; + ISMPSettings ret = m_aSMPSettings; if (ret == null) { if (LOGGER.isDebugEnabled ()) LOGGER.debug ("Loading SMP settings from DB"); - ret = m_aSettings = _getSettingsFromDB (aMgr); + ret = m_aSMPSettings = _createSettingsFromDB (aMgr); } else { diff --git a/phoss-smp-backend/src/main/java/com/helger/phoss/smp/settings/SMPSettings.java b/phoss-smp-backend/src/main/java/com/helger/phoss/smp/settings/SMPSettings.java index e6bf9ce19..fbf48e323 100644 --- a/phoss-smp-backend/src/main/java/com/helger/phoss/smp/settings/SMPSettings.java +++ b/phoss-smp-backend/src/main/java/com/helger/phoss/smp/settings/SMPSettings.java @@ -21,6 +21,7 @@ import org.slf4j.LoggerFactory; import com.helger.commons.ValueEnforcer; +import com.helger.commons.annotation.DevelopersNote; import com.helger.commons.annotation.Nonempty; import com.helger.commons.annotation.ReturnsMutableCopy; import com.helger.commons.state.EChange; @@ -79,6 +80,8 @@ public class SMPSettings implements ISMPSettings private final Settings m_aSettings = new Settings ("smp-settings"); + @Deprecated (forRemoval = true, since = "7.1.4") + @DevelopersNote ("Use the factory methods instead - the constructor will be made private") public SMPSettings (final boolean bInitFromConfiguration) { if (bInitFromConfiguration) @@ -200,8 +203,7 @@ public EChange setDirectoryIntegrationRequired (final boolean bPeppolDirectoryIn public boolean isDirectoryIntegrationEnabled () { - return m_aSettings.getAsBoolean (KEY_SMP_DIRECTORY_INTEGRATION_ENABLED, - DEFAULT_SMP_DIRECTORY_INTEGRATION_ENABLED); + return m_aSettings.getAsBoolean (KEY_SMP_DIRECTORY_INTEGRATION_ENABLED, DEFAULT_SMP_DIRECTORY_INTEGRATION_ENABLED); } @Nonnull @@ -255,4 +257,16 @@ public String toString () { return new ToStringGenerator (this).append ("Settings", m_aSettings).getToString (); } + + @Nonnull + public static SMPSettings createEmpty () + { + return new SMPSettings (false); + } + + @Nonnull + public static SMPSettings createInitializedFromConfiguration () + { + return new SMPSettings (true); + } } diff --git a/phoss-smp-backend/src/main/java/com/helger/phoss/smp/settings/SMPSettingsManagerXML.java b/phoss-smp-backend/src/main/java/com/helger/phoss/smp/settings/SMPSettingsManagerXML.java index 2e563d070..f0ce33add 100644 --- a/phoss-smp-backend/src/main/java/com/helger/phoss/smp/settings/SMPSettingsManagerXML.java +++ b/phoss-smp-backend/src/main/java/com/helger/phoss/smp/settings/SMPSettingsManagerXML.java @@ -36,7 +36,7 @@ @ThreadSafe public class SMPSettingsManagerXML extends AbstractPhotonSimpleDAO implements ISMPSettingsManager { - private final SMPSettings m_aSettings = new SMPSettings (true); + private final SMPSettings m_aSMPSettings = SMPSettings.createInitializedFromConfiguration (); private final CallbackList m_aCallbacks = new CallbackList <> (); public SMPSettingsManagerXML (@Nullable final String sFilename) throws DAOException @@ -51,7 +51,7 @@ protected EChange onRead (@Nonnull final IMicroDocument aDoc) { final SettingsMicroDocumentConverter aConverter = new SettingsMicroDocumentConverter <> (ISettingsFactory.newInstance ()); final ISettings aSettings = aConverter.convertToNative (aDoc.getDocumentElement ()); - m_aSettings.internalSetFromSettings (aSettings); + m_aSMPSettings.internalSetFromSettings (aSettings); return EChange.UNCHANGED; } @@ -61,7 +61,7 @@ protected IMicroDocument createWriteData () { final IMicroDocument ret = new MicroDocument (); final SettingsMicroDocumentConverter aConverter = new SettingsMicroDocumentConverter <> (ISettingsFactory.newInstance ()); - ret.appendChild (aConverter.convertToMicroElement (m_aSettings.internalGetAsMutableSettings (), null, "root")); + ret.appendChild (aConverter.convertToMicroElement (m_aSMPSettings.internalGetAsMutableSettings (), null, "root")); return ret; } @@ -75,7 +75,7 @@ public final CallbackList callbacks () @Nonnull public ISMPSettings getSettings () { - return m_aSettings; + return m_aSMPSettings; } @Nonnull @@ -92,14 +92,14 @@ public EChange updateSettings (final boolean bRESTWritableAPIDisabled, m_aRWLock.writeLock ().lock (); try { - eChange = eChange.or (m_aSettings.setRESTWritableAPIDisabled (bRESTWritableAPIDisabled)); - eChange = eChange.or (m_aSettings.setDirectoryIntegrationRequired (bDirectoryIntegrationRequired)); - eChange = eChange.or (m_aSettings.setDirectoryIntegrationEnabled (bDirectoryIntegrationEnabled)); - eChange = eChange.or (m_aSettings.setDirectoryIntegrationAutoUpdate (bDirectoryIntegrationAutoUpdate)); - eChange = eChange.or (m_aSettings.setDirectoryHostName (sDirectoryHostName)); - eChange = eChange.or (m_aSettings.setSMLRequired (bSMLRequired)); - eChange = eChange.or (m_aSettings.setSMLEnabled (bSMLActive)); - eChange = eChange.or (m_aSettings.setSMLInfoID (sSMLInfoID)); + eChange = eChange.or (m_aSMPSettings.setRESTWritableAPIDisabled (bRESTWritableAPIDisabled)); + eChange = eChange.or (m_aSMPSettings.setDirectoryIntegrationRequired (bDirectoryIntegrationRequired)); + eChange = eChange.or (m_aSMPSettings.setDirectoryIntegrationEnabled (bDirectoryIntegrationEnabled)); + eChange = eChange.or (m_aSMPSettings.setDirectoryIntegrationAutoUpdate (bDirectoryIntegrationAutoUpdate)); + eChange = eChange.or (m_aSMPSettings.setDirectoryHostName (sDirectoryHostName)); + eChange = eChange.or (m_aSMPSettings.setSMLRequired (bSMLRequired)); + eChange = eChange.or (m_aSMPSettings.setSMLEnabled (bSMLActive)); + eChange = eChange.or (m_aSMPSettings.setSMLInfoID (sSMLInfoID)); if (eChange.isChanged ()) markAsChanged (); } @@ -110,7 +110,7 @@ public EChange updateSettings (final boolean bRESTWritableAPIDisabled, // Invoke callbacks if (eChange.isChanged ()) - m_aCallbacks.forEach (x -> x.onSMPSettingsChanged (m_aSettings)); + m_aCallbacks.forEach (x -> x.onSMPSettingsChanged (m_aSMPSettings)); return eChange; } diff --git a/phoss-smp-backend/src/test/java/com/helger/phoss/smp/settings/SMPSettingsTest.java b/phoss-smp-backend/src/test/java/com/helger/phoss/smp/settings/SMPSettingsTest.java index eddcc61c2..c7787d024 100644 --- a/phoss-smp-backend/src/test/java/com/helger/phoss/smp/settings/SMPSettingsTest.java +++ b/phoss-smp-backend/src/test/java/com/helger/phoss/smp/settings/SMPSettingsTest.java @@ -24,7 +24,7 @@ public class SMPSettingsTest @Test public void testBasic () { - final SMPSettings aSettings = new SMPSettings (true); + final SMPSettings aSettings = SMPSettings.createInitializedFromConfiguration (); assertTrue (aSettings.isDirectoryIntegrationRequired ()); } }