37f2f2df by Adam Heath

Don't use a singleton instance of keycloak, but create one each time

getInstance() is called.
1 parent 89286c2a
...@@ -32,7 +32,7 @@ class KeycloakToolFactory implements ToolFactory<Keycloak> { ...@@ -32,7 +32,7 @@ class KeycloakToolFactory implements ToolFactory<Keycloak> {
32 protected final static Logger logger = LoggerFactory.getLogger(KeycloakToolFactory.class) 32 protected final static Logger logger = LoggerFactory.getLogger(KeycloakToolFactory.class)
33 final static String TOOL_NAME = "Keycloak" 33 final static String TOOL_NAME = "Keycloak"
34 34
35 protected Keycloak keycloak = null 35 protected KeycloakBuilder keycloakBuilder = null
36 36
37 KeycloakToolFactory() { } 37 KeycloakToolFactory() { }
38 38
...@@ -44,34 +44,33 @@ class KeycloakToolFactory implements ToolFactory<Keycloak> { ...@@ -44,34 +44,33 @@ class KeycloakToolFactory implements ToolFactory<Keycloak> {
44 @Override 44 @Override
45 void init(ExecutionContextFactory ecf) { 45 void init(ExecutionContextFactory ecf) {
46 logger.info("Starting Keycloak") 46 logger.info("Starting Keycloak")
47 Keycloak keycloak = getInstance()
47 try { 48 try {
48 ServerInfoRepresentation serverInfo = keycloak.serverInfo().getInfo() 49 ServerInfoRepresentation serverInfo = keycloak.serverInfo().getInfo()
49 } catch (Exception e) { 50 } catch (Exception e) {
50 logger.error("Could not connect to keycloak server") 51 logger.error("Could not connect to keycloak server")
51 throw e 52 throw e
53 } finally {
54 keycloak.close()
52 } 55 }
53 } 56 }
54 57
55 @Override 58 @Override
56 void preFacadeInit(ExecutionContextFactory ecf) { 59 void preFacadeInit(ExecutionContextFactory ecf) {
57 this.keycloak = KeycloakBuilder.builder() 60 this.keycloakBuilder = KeycloakBuilder.builder()
58 .serverUrl((String) System.getProperty("moqui_keycloak_server_url")) 61 .serverUrl((String) System.getProperty("moqui_keycloak_server_url"))
59 .realm((String) System.getProperty("moqui_keycloak_realm")) 62 .realm((String) System.getProperty("moqui_keycloak_realm"))
60 .grantType((String) OAuth2Constants.CLIENT_CREDENTIALS) 63 .grantType((String) OAuth2Constants.CLIENT_CREDENTIALS)
61 .clientId((String) System.getProperty("moqui_keycloak_client_id")) 64 .clientId((String) System.getProperty("moqui_keycloak_client_id"))
62 .clientSecret((String) System.getProperty("moqui_keycloak_client_secret")) 65 .clientSecret((String) System.getProperty("moqui_keycloak_client_secret"))
63 .build()
64 } 66 }
65 67
66 @Override 68 @Override
67 Keycloak getInstance(Object... parameters) { 69 Keycloak getInstance(Object... parameters) {
68 return keycloak 70 return keycloakBuilder.build()
69 } 71 }
70 72
71 @Override 73 @Override
72 void destroy() { 74 void destroy() {
73 if (keycloak != null) {
74 keycloak.close()
75 }
76 } 75 }
77 } 76 }
......