Skip to content

Commit

Permalink
Some #267 post processing
Browse files Browse the repository at this point in the history
  • Loading branch information
phax committed Jul 30, 2024
1 parent c4c034b commit dfa31bb
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 <ISMPSettingsCallback> m_aCallbacks = new CallbackList <> ();
private final AtomicBoolean m_aInsertDocument = new AtomicBoolean ();

Expand All @@ -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,
Expand All @@ -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));
Expand All @@ -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 <ISMPSettingsCallback> callbacks ()
{
return m_aCallbacks;
Expand All @@ -114,7 +119,7 @@ public final CallbackList <ISMPSettingsCallback> callbacks ()
@Nonnull
public ISMPSettings getSettings ()
{
return m_aSettings;
return m_aSMPSettings;
}

@Nonnull
Expand All @@ -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 ())
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 <String, String> 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),
Expand All @@ -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 ()));
Expand All @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 <ISMPSettingsCallback> m_aCallbacks = new CallbackList <> ();

public SMPSettingsManagerXML (@Nullable final String sFilename) throws DAOException
Expand All @@ -51,7 +51,7 @@ protected EChange onRead (@Nonnull final IMicroDocument aDoc)
{
final SettingsMicroDocumentConverter <Settings> aConverter = new SettingsMicroDocumentConverter <> (ISettingsFactory.newInstance ());
final ISettings aSettings = aConverter.convertToNative (aDoc.getDocumentElement ());
m_aSettings.internalSetFromSettings (aSettings);
m_aSMPSettings.internalSetFromSettings (aSettings);
return EChange.UNCHANGED;
}

Expand All @@ -61,7 +61,7 @@ protected IMicroDocument createWriteData ()
{
final IMicroDocument ret = new MicroDocument ();
final SettingsMicroDocumentConverter <Settings> 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;
}

Expand All @@ -75,7 +75,7 @@ public final CallbackList <ISMPSettingsCallback> callbacks ()
@Nonnull
public ISMPSettings getSettings ()
{
return m_aSettings;
return m_aSMPSettings;
}

@Nonnull
Expand All @@ -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 ();
}
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ());
}
}

0 comments on commit dfa31bb

Please sign in to comment.