Skip to content

Commit 682081e

Browse files
committed
improve filesummary reading
1 parent b7e4866 commit 682081e

File tree

5 files changed

+235
-80
lines changed

5 files changed

+235
-80
lines changed

server/src/main/java/password/pwm/http/servlet/configmanager/DebugItemGenerator.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import password.pwm.svc.stats.Statistic;
4747
import password.pwm.svc.stats.StatisticsManager;
4848
import password.pwm.util.LDAPPermissionCalculator;
49+
import password.pwm.util.java.ClosableIterator;
4950
import password.pwm.util.java.DebugOutputBuilder;
5051
import password.pwm.util.java.FileSystemUtility;
5152
import password.pwm.util.java.JavaHelper;
@@ -447,9 +448,9 @@ public String getFilename( )
447448
@Override
448449
public void outputItem( final DebugItemInput debugItemInput, final OutputStream outputStream ) throws Exception
449450
{
450-
final List<FileSystemUtility.FileSummaryInformation> fileSummaryInformations = new ArrayList<>();
451451
final PwmApplication pwmApplication = debugItemInput.getPwmApplication();
452452
final File applicationPath = pwmApplication.getPwmEnvironment().getApplicationPath();
453+
final List<File> interestedFiles = new ArrayList<>( );
453454

454455
if ( pwmApplication.getPwmEnvironment().getContextManager() != null )
455456
{
@@ -462,7 +463,7 @@ public void outputItem( final DebugItemInput debugItemInput, final OutputStream
462463

463464
if ( servletRootPath != null )
464465
{
465-
fileSummaryInformations.addAll( FileSystemUtility.readFileInformation( webInfPath ) );
466+
interestedFiles.add( webInfPath );
466467
}
467468
}
468469
}
@@ -476,14 +477,16 @@ public void outputItem( final DebugItemInput debugItemInput, final OutputStream
476477
{
477478
try
478479
{
479-
fileSummaryInformations.addAll( FileSystemUtility.readFileInformation( applicationPath ) );
480+
interestedFiles.add( applicationPath );
480481
}
481482
catch ( Exception e )
482483
{
483484
LOGGER.error( debugItemInput.getSessionLabel(), "unable to generate appPath fileMd5sums during zip debug building: " + e.getMessage() );
484485
}
485486
}
486487

488+
489+
try ( ClosableIterator<FileSystemUtility.FileSummaryInformation> iter = FileSystemUtility.readFileInformation( interestedFiles ); )
487490
{
488491
final CSVPrinter csvPrinter = JavaHelper.makeCsvPrinter( outputStream );
489492
{
@@ -495,8 +498,10 @@ public void outputItem( final DebugItemInput debugItemInput, final OutputStream
495498
headerRow.add( "Checksum" );
496499
csvPrinter.printComment( StringUtil.join( headerRow, "," ) );
497500
}
498-
for ( final FileSystemUtility.FileSummaryInformation fileSummaryInformation : fileSummaryInformations )
501+
502+
while ( iter.hasNext() )
499503
{
504+
final FileSystemUtility.FileSummaryInformation fileSummaryInformation = iter.next();
500505
try
501506
{
502507
final List<String> dataRow = new ArrayList<>();

server/src/main/java/password/pwm/http/servlet/resource/ResourceServletService.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import password.pwm.health.HealthRecord;
3232
import password.pwm.http.PwmRequest;
3333
import password.pwm.svc.PwmService;
34+
import password.pwm.util.java.ClosableIterator;
3435
import password.pwm.util.java.FileSystemUtility;
3536
import password.pwm.util.java.JavaHelper;
3637
import password.pwm.util.java.MovingAverage;
@@ -277,9 +278,15 @@ private static void checksumResourceFilePath( final PwmApplication pwmApplicatio
277278
final File resourcePath = new File( basePath.getAbsolutePath() + File.separator + "public" + File.separator + "resources" );
278279
if ( resourcePath.exists() )
279280
{
280-
for ( final FileSystemUtility.FileSummaryInformation fileSummaryInformation : FileSystemUtility.readFileInformation( resourcePath ) )
281+
try ( ClosableIterator<FileSystemUtility.FileSummaryInformation> iter =
282+
FileSystemUtility.readFileInformation( Collections.singletonList( resourcePath ) ) )
281283
{
282-
checksumStream.write( JavaHelper.longToBytes( fileSummaryInformation.getChecksum() ) );
284+
while ( iter.hasNext() )
285+
{
286+
final FileSystemUtility.FileSummaryInformation fileSummaryInformation = iter.next();
287+
checksumStream.write( JavaHelper.longToBytes( fileSummaryInformation.getChecksum() ) );
288+
}
289+
283290
}
284291
}
285292
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Password Management Servlets (PWM)
3+
* http://www.pwm-project.org
4+
*
5+
* Copyright (c) 2006-2009 Novell, Inc.
6+
* Copyright (c) 2009-2019 The PWM Project
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
21+
package password.pwm.util.java;
22+
23+
import java.util.Iterator;
24+
import java.util.NoSuchElementException;
25+
import java.util.Optional;
26+
import java.util.concurrent.atomic.AtomicBoolean;
27+
import java.util.concurrent.atomic.AtomicReference;
28+
import java.util.function.Supplier;
29+
30+
public class ConcurrentClosableIteratorWrapper<T> implements Iterator<T>, ClosableIterator<T>
31+
{
32+
private final AtomicReference<T> nextReference = new AtomicReference<>();
33+
private final AtomicBoolean completed = new AtomicBoolean( false );
34+
35+
private final Supplier<Optional<T>> nextSupplier;
36+
private final Runnable closeFunction;
37+
38+
public ConcurrentClosableIteratorWrapper( final Supplier<Optional<T>> nextFunction, final Runnable closeFunction )
39+
{
40+
this.nextSupplier = nextFunction;
41+
this.closeFunction = closeFunction;
42+
}
43+
44+
@Override
45+
public synchronized boolean hasNext()
46+
{
47+
if ( completed.get() )
48+
{
49+
return false;
50+
}
51+
work();
52+
return nextReference.get() != null;
53+
}
54+
55+
@Override
56+
public synchronized T next()
57+
{
58+
if ( completed.get() )
59+
{
60+
throw new NoSuchElementException( );
61+
}
62+
work();
63+
final T fileSummaryInformation = nextReference.get();
64+
if ( fileSummaryInformation == null )
65+
{
66+
throw new NoSuchElementException( );
67+
}
68+
nextReference.set( null );
69+
return fileSummaryInformation;
70+
}
71+
72+
@Override
73+
public synchronized void close()
74+
{
75+
closeImpl();
76+
}
77+
78+
private void closeImpl()
79+
{
80+
if ( !completed.get() )
81+
{
82+
completed.set( true );
83+
nextReference.set( null );
84+
closeFunction.run();
85+
}
86+
}
87+
88+
private void work()
89+
{
90+
if ( nextReference.get() == null && !completed.get() )
91+
{
92+
final Optional<T> next = nextSupplier.get();
93+
if ( next.isPresent() )
94+
{
95+
nextReference.set( next.get() );
96+
}
97+
else
98+
{
99+
closeImpl();
100+
}
101+
}
102+
}
103+
}

0 commit comments

Comments
 (0)