44 */
55package software .amazon .smithy .rulesengine .aws .language .functions ;
66
7+ import java .io .IOException ;
8+ import java .io .InputStream ;
9+ import java .io .UncheckedIOException ;
10+ import java .nio .file .Files ;
11+ import java .nio .file .Paths ;
712import java .util .ArrayList ;
813import java .util .Collections ;
914import java .util .HashMap ;
3237@ SmithyUnstableApi
3338public final class AwsPartition extends LibraryFunction {
3439 public static final String ID = "aws.partition" ;
40+ public static final String AWS_PARTITIONS_FILE_OVERRIDE = "AWS_PARTITIONS_FILE_OVERRIDE" ;
3541 public static final Identifier NAME = Identifier .of ("name" );
3642 public static final Identifier DNS_SUFFIX = Identifier .of ("dnsSuffix" );
3743 public static final Identifier DUAL_STACK_DNS_SUFFIX = Identifier .of ("dualStackDnsSuffix" );
@@ -51,10 +57,25 @@ public final class AwsPartition extends LibraryFunction {
5157 private static Partition AWS_PARTITION ;
5258
5359 static {
54- PARTITIONS .addAll (Partitions .fromNode (
55- Node .parse (Partitions .class .getResourceAsStream ("partitions.json" )))
56- .getPartitions ());
57- initializeRegionMap ();
60+ // Use the override if present in the environment. Ignore an empty string value.
61+ String override = System .getenv (AWS_PARTITIONS_FILE_OVERRIDE );
62+ if (override != null && override .isEmpty ()) {
63+ override = null ;
64+ }
65+
66+ try (InputStream in = override != null
67+ ? Files .newInputStream (Paths .get (override ))
68+ : Partitions .class .getResourceAsStream ("partitions.json" )) {
69+ if (in == null ) {
70+ throw new IllegalStateException ("partitions.json not found in JAR" ); // should never happen
71+ }
72+ PARTITIONS .addAll (Partitions .fromNode (Node .parse (in )).getPartitions ());
73+ initializeRegionMap ();
74+ } catch (IOException io ) {
75+ throw new UncheckedIOException (
76+ "Failed to load partitions data from " + (override != null ? override : "JAR" ),
77+ io );
78+ }
5879 }
5980
6081 private AwsPartition (FunctionNode functionNode ) {
0 commit comments