diff --git a/src/main/java/io/ebean/config/ServerConfig.java b/src/main/java/io/ebean/config/ServerConfig.java index e6a978d46..2d22890bd 100644 --- a/src/main/java/io/ebean/config/ServerConfig.java +++ b/src/main/java/io/ebean/config/ServerConfig.java @@ -599,6 +599,48 @@ public class ServerConfig { return serviceObject.get(key); } + /** + * Put a service object into configuration such that it can be passed to a plugin. + * + *
{@code
+ *
+ * JedisPool jedisPool = ..
+ *
+ * serverConfig.putServiceObject(jedisPool);
+ *
+ * }
+ */
+ public void putServiceObject(Object configObject) {
+ String key = serviceObjectKey(configObject);
+ serviceObject.put(key, configObject);
+ }
+
+ private String serviceObjectKey(Object configObject) {
+ return serviceObjectKey(configObject.getClass());
+ }
+
+ private String serviceObjectKey(Class> cls) {
+ String simpleName = cls.getSimpleName();
+ return Character.toLowerCase(simpleName.charAt(0)) + simpleName.substring(1);
+ }
+
+ /**
+ * Used by plugins to obtain service objects.
+ *
+ * {@code
+ *
+ * JedisPool jedisPool = serverConfig.getServiceObject(JedisPool.class);
+ *
+ * }
+ *
+ * @param cls The
+ * @param + * @return + */ + public
P getServiceObject(Class
cls) { + return (P)serviceObject.get(serviceObjectKey(cls)); + } + /** * Return the Jackson JsonFactory to use. *
diff --git a/src/test/java/io/ebean/config/ServerConfigTest.java b/src/test/java/io/ebean/config/ServerConfigTest.java index 5ac810e8b..e5487b2b9 100644 --- a/src/test/java/io/ebean/config/ServerConfigTest.java +++ b/src/test/java/io/ebean/config/ServerConfigTest.java @@ -1,5 +1,6 @@ package io.ebean.config; +import com.fasterxml.jackson.databind.ObjectMapper; import io.ebean.annotation.PersistBatch; import io.ebean.config.dbplatform.IdType; import io.ebean.datasource.DataSourceConfig; @@ -121,4 +122,19 @@ public class ServerConfigTest { serverConfig.setIdGeneratorAutomatic(false); assertFalse(serverConfig.isIdGeneratorAutomatic()); } + + @Test + public void test_putServiceObject() { + + ObjectMapper objectMapper = new ObjectMapper(); + + ServerConfig config = new ServerConfig(); + config.putServiceObject(objectMapper); + + ObjectMapper mapper0 = config.getServiceObject(ObjectMapper.class); + ObjectMapper mapper1 = (ObjectMapper)config.getServiceObject("objectMapper"); + + assertThat(objectMapper).isSameAs(mapper0); + assertThat(objectMapper).isSameAs(mapper1); + } }