1818 */
1919package org .jooby .ebean ;
2020
21- import java .util .Arrays ;
2221import java .util .HashSet ;
22+ import java .util .List ;
2323import java .util .Properties ;
2424import java .util .Set ;
25+ import java .util .concurrent .atomic .AtomicReference ;
2526import java .util .function .Consumer ;
2627
2728import org .jooby .Env ;
3031import org .jooby .internal .ebean .EbeanManaged ;
3132import org .jooby .jdbc .Jdbc ;
3233
34+ import com .google .common .collect .ImmutableList ;
3335import com .google .inject .Binder ;
3436import com .google .inject .Key ;
3537import com .typesafe .config .Config ;
3840import io .ebean .EbeanServer ;
3941import io .ebean .config .ContainerConfig ;
4042import io .ebean .config .ServerConfig ;
43+ import javaslang .control .Try .CheckedRunnable ;
4144
4245/**
4346 * <h1>ebean module</h1>
151154 */
152155public class Ebeanby extends Jdbc {
153156
154- private Set <String > packages = new HashSet <>();
157+ /** package for test */
158+ static final AtomicReference <Set <String >> PKG = new AtomicReference <>(new HashSet <>());
155159
156160 static {
157161 // Turn off ebean shutdown hook:
@@ -173,33 +177,13 @@ public Ebeanby(final String name) {
173177 public Ebeanby () {
174178 }
175179
176- /**
177- * <p>
178- * Add one ore more packages. Packages are used by the agent enhancement (if present) and to
179- * search for entities via class path search when classes have not been explicitly specified.
180- * </p>
181- *
182- * @param packages Packages to enhancement and search for.
183- * @return This module.
184- */
185- public Ebeanby packages (final String ... packages ) {
186- Arrays .stream (packages ).forEach (this .packages ::add );
187- return this ;
188- }
189-
190180 @ Override
191181 public void configure (final Env env , final Config conf , final Binder binder ) {
192182 configure (env , conf , binder , (name , ds ) -> {
193183 ServerConfig config = new ServerConfig ();
194184
195- this .packages .add (conf .getString ("application.ns" ));
196-
197- EbeanEnhancer .newEnhancer ().run (packages );
198-
199185 config .setName (name );
200186
201- packages .forEach (config ::addPackage );
202-
203187 Config cprops = conf .getConfig ("ebean" );
204188 if (conf .hasPath ("ebean." + name )) {
205189 cprops = conf .getConfig ("ebean." + name )
@@ -220,6 +204,13 @@ public void configure(final Env env, final Config conf, final Binder binder) {
220204
221205 callback (config , conf );
222206
207+ List <String > packages = config .getPackages ();
208+ if (packages == null || packages .size () == 0 ) {
209+ packages = ImmutableList .of (conf .getString ("application.ns" ));
210+ }
211+ config .setPackages (packages );
212+ PKG .get ().addAll (packages );
213+
223214 EbeanManaged server = new EbeanManaged (conf , config );
224215 env .onStart (server ::start );
225216 env .onStop (server ::stop );
@@ -232,6 +223,9 @@ public void configure(final Env env, final Config conf, final Binder binder) {
232223 }
233224 keys .generate (EbeanServer .class , name , provider );
234225 });
226+
227+ // Run enhancer once per JVM
228+ env .onStart (runEnhancer ());
235229 }
236230
237231 @ Override
@@ -248,4 +242,19 @@ private Properties props(final Config config) {
248242 });
249243 return props ;
250244 }
245+
246+ /**
247+ * package for test.
248+ *
249+ * @return An enhancer task. It runs once per classloader instance.
250+ */
251+ static CheckedRunnable runEnhancer () {
252+ return () -> {
253+ Set <String > packages = PKG .getAndSet (null );
254+ if (packages != null ) {
255+ EbeanEnhancer .newEnhancer ().run (packages );
256+ }
257+ };
258+ }
259+
251260}
0 commit comments