Skip to content

Commit

Permalink
Memory improvement for change URL/Certificate; #291
Browse files Browse the repository at this point in the history
  • Loading branch information
phax committed Aug 19, 2024
1 parent fb8c147 commit 2dac81d
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -432,10 +432,15 @@ public EChange deleteSMPProcess (@Nullable final ISMPServiceInformation aSMPServ
public ICommonsList <ISMPServiceInformation> getAllSMPServiceInformation ()
{
final ICommonsList <ISMPServiceInformation> ret = new CommonsArrayList <> ();
getCollection ().find ().forEach ((Consumer <Document>) x -> ret.add (toServiceInformation (x, true)));
forEachSMPServiceInformation (ret::add);
return ret;
}

public void forEachSMPServiceInformation (@Nonnull final Consumer <? super ISMPServiceInformation> aConsumer)
{
getCollection ().find ().forEach (x -> aConsumer.accept (toServiceInformation (x, true)));
}

@Nonnegative
public long getSMPServiceInformationCount ()
{
Expand All @@ -461,8 +466,8 @@ public ICommonsList <IDocumentTypeIdentifier> getAllSMPDocumentTypesOfServiceGro
if (aServiceGroup != null)
{
getCollection ().find (new Document (BSON_SERVICE_GROUP_ID, aServiceGroup.getID ()))
.forEach ((Consumer <Document>) x -> ret.add (toServiceInformation (x,
false).getDocumentTypeIdentifier ()));
.forEach ((Consumer <Document>) x -> ret.add (toServiceInformation (x, false)
.getDocumentTypeIdentifier ()));
}
return ret;
}
Expand Down Expand Up @@ -499,8 +504,6 @@ public boolean containsAnyEndpointWithTransportProfile (@Nullable final String s

// As simple as it can be
return getCollection ().find (new Document (BSON_PROCESSES + "." + BSON_ENDPOINTS + "." + BSON_TRANSPORT_PROFILE,
sTransportProfileID))
.iterator ()
.hasNext ();
sTransportProfileID)).iterator ().hasNext ();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.helger.phoss.smp.backend.sql.mgr;

import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Supplier;

import javax.annotation.Nonnegative;
Expand Down Expand Up @@ -385,6 +386,12 @@ public EChange deleteSMPProcess (@Nullable final ISMPServiceInformation aSMPServ
public ICommonsList <ISMPServiceInformation> getAllSMPServiceInformation ()
{
final ICommonsList <ISMPServiceInformation> ret = new CommonsArrayList <> ();
forEachSMPServiceInformation (ret::add);
return ret;
}

public void forEachSMPServiceInformation (@Nonnull final Consumer <? super ISMPServiceInformation> aConsumer)
{
final ICommonsList <DBResultRow> aDBResult = newExecutor ().queryAll ("SELECT sm.businessIdentifierScheme, sm.businessIdentifier, sm.documentIdentifierScheme, sm.documentIdentifier, sm.extension," +
" sp.processIdentifierType, sp.processIdentifier, sp.extension," +
" se.transportProfile, se.endpointReference, se.requireBusinessLevelSignature, se.minimumAuthenticationLevel," +
Expand Down Expand Up @@ -459,11 +466,9 @@ public ICommonsList <ISMPServiceInformation> getAllSMPServiceInformation ()
}

final DocTypeAndExtension aDE = aEntry2.getKey ();
ret.add (new SMPServiceInformation (aServiceGroup, aDE.m_aDocTypeID, aProcesses, aDE.m_sExt));
aConsumer.accept (new SMPServiceInformation (aServiceGroup, aDE.m_aDocTypeID, aProcesses, aDE.m_sExt));
}
}

return ret;
}

@Nonnegative
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package com.helger.phoss.smp.backend.xml.mgr;

import java.util.function.Consumer;

import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -119,9 +121,7 @@ public ESuccess mergeSMPServiceInformation (@Nonnull final ISMPServiceInformatio
if (bChangeExisting)
{
// Edit existing
m_aRWLock.writeLocked ( () -> {
internalUpdateItem (aOldInformation);
});
m_aRWLock.writeLocked ( () -> { internalUpdateItem (aOldInformation); });

AuditHelper.onAuditModifySuccess (SMPServiceInformation.OT,
"set-all",
Expand Down Expand Up @@ -302,6 +302,11 @@ public ICommonsList <ISMPServiceInformation> getAllSMPServiceInformation ()
return getAll ();
}

public void forEachSMPServiceInformation (@Nonnull final Consumer <? super ISMPServiceInformation> aConsumer)
{
forEachValue (aConsumer);
}

@Nonnegative
public long getSMPServiceInformationCount ()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (C) 2015-2024 Philip Helger (www.helger.com)
* philip[at]helger[dot]com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.helger.phoss.smp;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Internal interface to act as a marker for future SMP V8 changes.
*
* @author Philip Helger
*/
@Retention (RetentionPolicy.SOURCE)
@Target ({ ElementType.TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR })
public @interface ChangeSMPV8
{
String value() default "";
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
*/
package com.helger.phoss.smp.domain.serviceinfo;

import java.util.function.Consumer;

import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
Expand All @@ -23,6 +25,7 @@
import com.helger.peppol.smp.ISMPTransportProfile;
import com.helger.peppolid.IDocumentTypeIdentifier;
import com.helger.peppolid.IProcessIdentifier;
import com.helger.phoss.smp.ChangeSMPV8;
import com.helger.phoss.smp.domain.servicegroup.ISMPServiceGroup;

/**
Expand Down Expand Up @@ -74,6 +77,7 @@ public interface ISMPServiceInformationManager
* @see #getSMPServiceInformationOfServiceGroupAndDocumentType(ISMPServiceGroup,
* IDocumentTypeIdentifier)
*/
@ChangeSMPV8 ("Rename to findSMPServiceInformation")
@Nullable
ISMPServiceInformation findServiceInformation (@Nullable ISMPServiceGroup aServiceGroup,
@Nullable IDocumentTypeIdentifier aDocTypeID,
Expand Down Expand Up @@ -131,6 +135,16 @@ ISMPServiceInformation findServiceInformation (@Nullable ISMPServiceGroup aServi
@ReturnsMutableCopy
ICommonsList <ISMPServiceInformation> getAllSMPServiceInformation ();

/**
* Iterate each Service Information element and invoke the provided consumer
* for it.
*
* @param aConsumer
* The consumer to invoke. May not be <code>null</code>.
* @since 7.1.5
*/
void forEachSMPServiceInformation (@Nonnull Consumer <? super ISMPServiceInformation> aConsumer);

/**
* @return The count of all service information objects. Always &ge; 0.
*/
Expand Down Expand Up @@ -189,7 +203,7 @@ ISMPServiceInformation getSMPServiceInformationOfServiceGroupAndDocumentType (@N

/**
* Check if the passed transport profile is used or not.
*
*
* @param sTransportProfileID
* The transport profile ID to be checked. May be <code>null</code>.
* @return <code>true</code> if at least one endpoint uses the provided
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
*/
package com.helger.phoss.smp.mock;

import java.util.function.Consumer;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

Expand Down Expand Up @@ -66,6 +68,11 @@ public ICommonsList <ISMPServiceInformation> getAllSMPServiceInformationOfServic
throw new UnsupportedOperationException ();
}

public void forEachSMPServiceInformation (@Nonnull final Consumer <? super ISMPServiceInformation> aConsumer)
{
throw new UnsupportedOperationException ();
}

@Nonnull
public ICommonsList <ISMPServiceInformation> getAllSMPServiceInformation ()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.helger.commons.collection.impl.ICommonsSortedSet;
import com.helger.commons.compare.ESortOrder;
import com.helger.commons.datetime.PDTFactory;
import com.helger.commons.mutable.MutableInt;
import com.helger.commons.state.EValidity;
import com.helger.commons.state.IValidityIndicator;
import com.helger.commons.string.StringHelper;
Expand All @@ -59,7 +60,6 @@
import com.helger.phoss.smp.domain.servicegroup.ISMPServiceGroupManager;
import com.helger.phoss.smp.domain.serviceinfo.ISMPEndpoint;
import com.helger.phoss.smp.domain.serviceinfo.ISMPProcess;
import com.helger.phoss.smp.domain.serviceinfo.ISMPServiceInformation;
import com.helger.phoss.smp.domain.serviceinfo.ISMPServiceInformationManager;
import com.helger.phoss.smp.domain.serviceinfo.SMPEndpoint;
import com.helger.phoss.smp.ui.AbstractSMPWebPage;
Expand Down Expand Up @@ -97,19 +97,16 @@ private static final class BulkChangeCertificate extends AbstractLongRunningJobR
{
private static final AtomicInteger RUNNING_JOBS = new AtomicInteger (0);

private final ICommonsList <ISMPServiceInformation> m_aAllSIs;
private final Locale m_aDisplayLocale;
private final String m_sOldUnifiedCert;
private final String m_sNewCert;

public BulkChangeCertificate (@Nonnull final ICommonsList <ISMPServiceInformation> aAllSIs,
@Nonnull final Locale aDisplayLocale,
public BulkChangeCertificate (@Nonnull final Locale aDisplayLocale,
@Nonnull final String sOldUnifiedCert,
@Nonnull final String sNewCert)
{
super ("BulkChangeCertificate",
new ReadOnlyMultilingualText (CSMPServer.DEFAULT_LOCALE, "Bulk change certificate"));
m_aAllSIs = aAllSIs;
m_aDisplayLocale = aDisplayLocale;
m_sOldUnifiedCert = sOldUnifiedCert;
m_sNewCert = sNewCert;
Expand All @@ -124,43 +121,42 @@ public LongRunningJobResult createLongRunningJobResult ()
final ISMPServiceInformationManager aServiceInfoMgr = SMPMetaManager.getServiceInformationMgr ();

// Modify all endpoints
int nChangedEndpoints = 0;
int nSaveErrors = 0;
final MutableInt aChangedEndpoints = new MutableInt (0);
final MutableInt aSaveErrors = new MutableInt (0);
final ICommonsSortedSet <String> aChangedServiceGroup = new CommonsTreeSet <> ();
for (final ISMPServiceInformation aSI : m_aAllSIs)
{
aServiceInfoMgr.forEachSMPServiceInformation (aSI -> {
boolean bChanged = false;
for (final ISMPProcess aProcess : aSI.getAllProcesses ())
for (final ISMPEndpoint aEndpoint : aProcess.getAllEndpoints ())
if (m_sOldUnifiedCert.equals (_getUnifiedCert (aEndpoint.getCertificate ())))
{
bChanged = true;
((SMPEndpoint) aEndpoint).setCertificate (m_sNewCert);
++nChangedEndpoints;
aChangedEndpoints.inc ();
}
if (bChanged)
{
if (aServiceInfoMgr.mergeSMPServiceInformation (aSI).isFailure ())
nSaveErrors++;
aSaveErrors.inc ();
aChangedServiceGroup.add (aSI.getServiceGroupID ());
}
}
});

final IHCNode aRes;
if (nChangedEndpoints > 0)
if (aChangedEndpoints.isGT0 ())
{
final HCUL aUL = new HCUL ();
for (final String sChangedServiceGroupID : aChangedServiceGroup)
aUL.addItem (sChangedServiceGroupID);

final HCNodeList aNodes = new HCNodeList ().addChildren (div ("The old certificate was changed in " +
nChangedEndpoints +
aChangedEndpoints.intValue () +
" endpoints to the new certificate:"),
_getCertificateDisplay (m_sNewCert,
m_aDisplayLocale),
div ("Effected service groups are:"),
aUL);
if (nSaveErrors == 0)
if (aSaveErrors.is0 ())
aRes = success (aNodes);
else
{
Expand Down Expand Up @@ -288,10 +284,8 @@ protected void fillContent (@Nonnull final WebPageExecutionContext aWPEC)

final ICommonsMap <String, ICommonsList <ISMPEndpoint>> aEndpointsGroupedPerURL = new CommonsHashMap <> ();
final ICommonsMap <String, ICommonsSet <ISMPServiceGroup>> aServiceGroupsGroupedPerURL = new CommonsHashMap <> ();
final ICommonsList <ISMPServiceInformation> aAllSIs = aServiceInfoMgr.getAllSMPServiceInformation ();
int nTotalEndpointCount = 0;
for (final ISMPServiceInformation aSI : aAllSIs)
{
final MutableInt aTotalEndpointCount = new MutableInt (0);
aServiceInfoMgr.forEachSMPServiceInformation (aSI -> {
final ISMPServiceGroup aSG = aSI.getServiceGroup ();
for (final ISMPProcess aProcess : aSI.getAllProcesses ())
for (final ISMPEndpoint aEndpoint : aProcess.getAllEndpoints ())
Expand All @@ -300,9 +294,9 @@ protected void fillContent (@Nonnull final WebPageExecutionContext aWPEC)
aEndpointsGroupedPerURL.computeIfAbsent (sUnifiedCertificate, k -> new CommonsArrayList <> ())
.add (aEndpoint);
aServiceGroupsGroupedPerURL.computeIfAbsent (sUnifiedCertificate, k -> new CommonsHashSet <> ()).add (aSG);
++nTotalEndpointCount;
aTotalEndpointCount.inc ();
}
}
});

{
final BootstrapButtonToolbar aToolbar = new BootstrapButtonToolbar (aWPEC);
Expand Down Expand Up @@ -356,7 +350,7 @@ protected void fillContent (@Nonnull final WebPageExecutionContext aWPEC)
{
PhotonWorkerPool.getInstance ()
.run ("BulkChangeCertificate",
new BulkChangeCertificate (aAllSIs, aDisplayLocale, sOldUnifiedCert, sNewCert));
new BulkChangeCertificate (aDisplayLocale, sOldUnifiedCert, sNewCert));

aWPEC.postRedirectGetInternal (success ().addChildren (div ("The bulk change of the endpoint certificate to"),
_getCertificateDisplay (sNewUnifiedCert,
Expand Down Expand Up @@ -404,9 +398,9 @@ protected void fillContent (@Nonnull final WebPageExecutionContext aWPEC)
{
aNodeList.addChild (info ().addChildren (div ("This page lets you change the certificates of multiple endpoints at once. This is e.g. helpful when the old certificate expired."),
div ("Currently " +
(nTotalEndpointCount == 1 ? "1 endpoint is"
: nTotalEndpointCount +
" endpoints are") +
(aTotalEndpointCount.intValue () == 1 ? "1 endpoint is"
: aTotalEndpointCount.intValue () +
" endpoints are") +
" registered.")));

final HCTable aTable = new HCTable (new DTCol ("Certificate").setInitialSorting (ESortOrder.ASCENDING),
Expand Down
Loading

0 comments on commit 2dac81d

Please sign in to comment.