11package ai .timefold .solver .core .enterprise ;
22
33import java .lang .reflect .InvocationTargetException ;
4+ import java .util .Objects ;
45import java .util .function .BiFunction ;
56
67import ai .timefold .solver .core .api .score .stream .ConstraintProvider ;
3334
3435public interface TimefoldSolverEnterpriseService {
3536
37+ String SOLVER_NAME = "Timefold Solver" ;
38+ String COMMUNITY_NAME = "Community Edition" ;
39+ String COMMUNITY_COORDINATES = "ai.timefold.solver:timefold-solver-core" ;
40+ String ENTERPRISE_NAME = "Enterprise Edition" ;
41+ String ENTERPRISE_COORDINATES = "ai.timefold.solver.enterprise:timefold-solver-enterprise-core" ;
42+ String DEVELOPMENT_SNAPSHOT = "Development Snapshot" ;
43+
3644 static String identifySolverVersion () {
37- var packaging = "Community Edition" ;
45+ var packaging = COMMUNITY_NAME ;
3846 try {
39- TimefoldSolverEnterpriseService . load ();
40- packaging = "Enterprise Edition" ;
47+ load ();
48+ packaging = ENTERPRISE_NAME ;
4149 } catch (Exception e ) {
4250 // No need to do anything, just checking if Enterprise exists.
4351 }
44- var version = SolverFactory .class .getPackage ().getImplementationVersion ();
45- return packaging + " " + (version == null ? "(Development snapshot)" : "v" + version );
52+ var version = getVersionString (SolverFactory .class );
53+ return packaging + " " + version ;
54+ }
55+
56+ private static String getVersionString (Class <?> clz ) {
57+ var version = clz .getPackage ().getImplementationVersion ();
58+ return (version == null ? DEVELOPMENT_SNAPSHOT : "v" + version );
4659 }
4760
4861 static TimefoldSolverEnterpriseService load () throws ClassNotFoundException , NoSuchMethodException ,
@@ -54,16 +67,30 @@ static TimefoldSolverEnterpriseService load() throws ClassNotFoundException, NoS
5467 }
5568
5669 static TimefoldSolverEnterpriseService loadOrFail (Feature feature ) {
70+ TimefoldSolverEnterpriseService service ;
5771 try {
58- return load ();
72+ service = load ();
5973 } catch (Exception cause ) {
6074 throw new IllegalStateException ("""
61- %s requested but Timefold Solver Enterprise Edition not found on classpath
62- Either add the ai.timefold.solver.enterprise:timefold-solver-enterprise-core dependency,
63- or %s.
64- "Note: Timefold Solver Enterprise Edition is a commercial product."""
65- .formatted (feature .getName (), feature .getWorkaround ()), cause );
75+ %s requested but %s %s not found on classpath.
76+ Either add the %s dependency, or %s.
77+ Note: %s %s is a commercial product. Visit https://timefold.ai to find out more."""
78+ .formatted (feature .getName (), SOLVER_NAME , ENTERPRISE_NAME , feature .getWorkaround (),
79+ ENTERPRISE_COORDINATES , SOLVER_NAME , ENTERPRISE_NAME ),
80+ cause );
81+ }
82+ var communityVersion = getVersionString (TimefoldSolverEnterpriseService .class );
83+ var enterpriseVersion = getVersionString (service .getClass ());
84+ if (Objects .equals (communityVersion , enterpriseVersion )) { // Identical versions.
85+ return service ;
86+ } else if (enterpriseVersion .equals (DEVELOPMENT_SNAPSHOT )) { // Don't enforce when running Enterprise tests.
87+ return service ;
6688 }
89+ throw new IllegalStateException ("""
90+ Detected mismatch between versions of %s %s (%s) and %s (%s).
91+ Ensure your project uses the same version of %s and %s dependencies."""
92+ .formatted (SOLVER_NAME , COMMUNITY_NAME , communityVersion , ENTERPRISE_NAME , enterpriseVersion ,
93+ COMMUNITY_COORDINATES , ENTERPRISE_COORDINATES ));
6794 }
6895
6996 Class <? extends ConstraintProvider >
0 commit comments