11package ai .timefold .solver .core .enterprise ;
22
33import java .lang .reflect .InvocationTargetException ;
4+ import java .security .cert .CertificateException ;
45import java .util .Objects ;
56import java .util .function .BiFunction ;
67import java .util .function .Function ;
@@ -57,7 +58,7 @@ static String identifySolverVersion() {
5758 return packaging + " " + version ;
5859 }
5960
60- private static String getVersionString (Class <?> clz ) {
61+ static String getVersionString (Class <?> clz ) {
6162 var version = clz .getPackage ().getImplementationVersion ();
6263 return (version == null ? DEVELOPMENT_SNAPSHOT : "v" + version );
6364 }
@@ -86,8 +87,15 @@ static TimefoldSolverEnterpriseService loadOrFail(Feature feature) {
8687 var communityVersion = getVersionString (TimefoldSolverEnterpriseService .class );
8788 var enterpriseVersion = getVersionString (service .getClass ());
8889 if (Objects .equals (communityVersion , enterpriseVersion )) { // Identical versions.
90+ // We validate the user license at this point
91+ try {
92+ service .validateUserLicense ();
93+ } catch (CertificateException e ) {
94+ throw new IllegalStateException (e );
95+ }
8996 return service ;
9097 } else if (enterpriseVersion .equals (DEVELOPMENT_SNAPSHOT )) { // Don't enforce when running Enterprise tests.
98+ // We do not validate the user license for development snapshots.
9199 return service ;
92100 }
93101 throw new IllegalStateException ("""
@@ -100,13 +108,25 @@ static TimefoldSolverEnterpriseService loadOrFail(Feature feature) {
100108 static <T > T buildOrDefault (Function <TimefoldSolverEnterpriseService , T > builder , Supplier <T > defaultValue ) {
101109 try {
102110 var service = load ();
111+ var enterpriseVersion = getVersionString (service .getClass ());
112+ if (!enterpriseVersion .equals (DEVELOPMENT_SNAPSHOT )) {
113+ // We validate the user license at this point
114+ service .validateUserLicense ();
115+ }
103116 return builder .apply (service );
104117 } catch (ClassNotFoundException | InvocationTargetException | NoSuchMethodException | InstantiationException
105118 | IllegalAccessException e ) {
106119 return defaultValue .get ();
120+ } catch (CertificateException e ) {
121+ throw new IllegalStateException (e );
107122 }
108123 }
109124
125+ /**
126+ * It performs a security validation to ensure that the user licenseo is valid.
127+ */
128+ void validateUserLicense () throws CertificateException ;
129+
110130 TopologicalOrderGraph buildTopologyGraph (int size );
111131
112132 Class <? extends ConstraintProvider >
0 commit comments