2020
2121package password .pwm ;
2222
23- import lombok .Value ;
2423import password .pwm .bean .DomainID ;
2524import password .pwm .bean .ProfileID ;
2625import password .pwm .bean .SessionLabel ;
2726import password .pwm .config .AppConfig ;
2827import password .pwm .config .PwmSetting ;
2928import password .pwm .config .PwmSettingScope ;
3029import password .pwm .error .ErrorInformation ;
31- import password .pwm .error .PwmError ;
3230import password .pwm .error .PwmException ;
3331import password .pwm .error .PwmUnrecoverableException ;
3432import password .pwm .health .HealthService ;
5856import password .pwm .svc .wordlist .WordlistService ;
5957import password .pwm .util .MBeanUtility ;
6058import password .pwm .util .PwmScheduler ;
61- import password .pwm .util .java .FileSystemUtility ;
59+ import password .pwm .util .java .CollectionUtil ;
6260import password .pwm .util .java .StringUtil ;
6361import password .pwm .util .java .TimeDuration ;
6462import password .pwm .util .json .JsonFactory ;
6765import password .pwm .util .logging .PwmLogManager ;
6866import password .pwm .util .logging .PwmLogger ;
6967
70- import java .io .IOException ;
71- import java .nio .file .Files ;
7268import java .nio .file .Path ;
7369import java .time .Instant ;
7470import java .util .ArrayList ;
@@ -108,6 +104,7 @@ public class PwmApplication
108104 private String instanceID = PwmApplicationUtil .DEFAULT_INSTANCE_ID ;
109105 private LocalDB localDB ;
110106 private LocalDBLogger localDBLogger ;
107+ private Path tempDirectory ;
111108
112109 public PwmApplication ( final PwmEnvironment pwmEnvironment )
113110 throws PwmUnrecoverableException
@@ -156,22 +153,8 @@ private void initialize()
156153 fileLocker .waitForFileLock ();
157154 }
158155
159- // clear temp dir
160- if ( !pwmEnvironment .isInternalRuntimeInstance () )
161- {
162- final Path tempFileDirectory = getTempDirectory ();
163- try
164- {
165- LOGGER .debug ( sessionLabel , () -> "deleting directory (and sub-directory) contents in " + tempFileDirectory );
166- FileSystemUtility .deleteDirectoryContentsRecursively ( tempFileDirectory );
167- }
168- catch ( final Exception e )
169- {
170- throw new PwmUnrecoverableException ( new ErrorInformation ( PwmError .ERROR_STARTUP_ERROR ,
171- "unable to clear temp file directory '" + tempFileDirectory + "', error: " + e .getMessage ()
172- ) );
173- }
174- }
156+ // init temp dir
157+ tempDirectory = PwmApplicationUtil .initTempDirectory ( sessionLabel , pwmEnvironment );
175158
176159 if ( getApplicationMode () != PwmApplicationMode .READ_ONLY )
177160 {
@@ -509,7 +492,7 @@ public void writeLastLdapFailure( final DomainID domainID, final Map<ProfileID,
509492
510493 public Map <ProfileID , ErrorInformation > readLastLdapFailure ( final DomainID domainID )
511494 {
512- return readLastLdapFailure ().getRecords ().getOrDefault ( domainID , Collections .emptyMap () );
495+ return readLastLdapFailure ().records ().getOrDefault ( domainID , Collections .emptyMap () );
513496 }
514497
515498 private StoredErrorRecords readLastLdapFailure ()
@@ -534,34 +517,27 @@ private StoredErrorRecords readLastLdapFailure()
534517 return new StoredErrorRecords ( Collections .emptyMap () );
535518 }
536519
537- @ Value
538- private static class StoredErrorRecords
520+ private record StoredErrorRecords (
521+ Map <DomainID , Map <ProfileID , ErrorInformation >> records
522+ )
539523 {
540- private final Map <DomainID , Map <ProfileID , ErrorInformation >> records ;
541-
542- StoredErrorRecords ( final Map <DomainID , Map <ProfileID , ErrorInformation >> records )
543- {
544- this .records = records == null ? Collections .emptyMap () : Map .copyOf ( records );
545- }
546-
547- public Map <DomainID , Map <ProfileID , ErrorInformation >> getRecords ()
524+ private StoredErrorRecords ( final Map <DomainID , Map <ProfileID , ErrorInformation >> records )
548525 {
549- // required because json deserialization can still set records == null
550- return records == null ? Collections .emptyMap () : records ;
526+ this .records = CollectionUtil .stripNulls ( records );
551527 }
552528
553529 StoredErrorRecords addDomainErrorMap (
554530 final DomainID domainID ,
555531 final Map <ProfileID , ErrorInformation > errorInformationMap )
556532 {
557- final Map <DomainID , Map <ProfileID , ErrorInformation >> newRecords = new HashMap <>( getRecords () );
533+ final Map <DomainID , Map <ProfileID , ErrorInformation >> newRecords = new HashMap <>( records );
558534 newRecords .put ( domainID , Map .copyOf ( errorInformationMap ) );
559535 return new StoredErrorRecords ( newRecords );
560536 }
561537
562538 StoredErrorRecords stripOutdatedLdapErrors ( final TimeDuration maxAge )
563539 {
564- return new StoredErrorRecords ( getRecords () .entrySet ().stream ()
540+ return new StoredErrorRecords ( records .entrySet ().stream ()
565541 // outer map
566542 .collect ( Collectors .toUnmodifiableMap (
567543 Map .Entry ::getKey ,
@@ -787,39 +763,12 @@ public boolean isMultiDomain()
787763 return this .getConfig ().isMultiDomain ();
788764 }
789765
790- public Path getTempDirectory ( )
766+ public Optional < Path > getTempDirectory ( )
791767 throws PwmUnrecoverableException
792768 {
793- if ( pwmEnvironment .getApplicationPath () == null )
794- {
795- final ErrorInformation errorInformation = new ErrorInformation (
796- PwmError .ERROR_STARTUP_ERROR ,
797- "unable to establish temp work directory: application path unavailable"
798- );
799- throw new PwmUnrecoverableException ( errorInformation );
800- }
801- final Path tempDirectory = pwmEnvironment .getApplicationPath ().resolve ( "temp" );
802- if ( !Files .exists ( tempDirectory ) )
803- {
804- LOGGER .trace ( () -> "preparing to create temporary directory " + tempDirectory );
805- try
806- {
807- Files .createDirectories ( tempDirectory );
808- LOGGER .debug ( () -> "created " + tempDirectory );
809- }
810- catch ( final IOException e )
811- {
812- LOGGER .debug ( () -> "unable to create temporary directory " + tempDirectory );
813- final ErrorInformation errorInformation = new ErrorInformation (
814- PwmError .ERROR_STARTUP_ERROR ,
815- "unable to establish create temp work directory " + tempDirectory );
816- throw new PwmUnrecoverableException ( errorInformation );
817- }
818- }
819- return tempDirectory ;
769+ return Optional .ofNullable ( tempDirectory );
820770 }
821771
822-
823772 public PwmScheduler getPwmScheduler ()
824773 {
825774 return pwmScheduler ;
0 commit comments