From a6e82764bca9c631a683ff98b0ad4a712cd0283a Mon Sep 17 00:00:00 2001 From: ghilaima Date: Thu, 3 Dec 2015 14:16:23 +0100 Subject: [PATCH 3/3] JUDDI-951 - EntityManagerFactory not closed when undeploying juddi --- .../apache/juddi/adminconsole/StartupServlet.java | 181 ++++++++++---------- .../apache/juddi/adminconsole/StartupServlet.java | 183 +++++++++++---------- 2 files changed, 184 insertions(+), 180 deletions(-) diff --git a/juddiv3-war-repl/src/main/java/org/apache/juddi/adminconsole/StartupServlet.java b/juddiv3-war-repl/src/main/java/org/apache/juddi/adminconsole/StartupServlet.java index c600522..b3b90ba 100644 --- a/juddiv3-war-repl/src/main/java/org/apache/juddi/adminconsole/StartupServlet.java +++ b/juddiv3-war-repl/src/main/java/org/apache/juddi/adminconsole/StartupServlet.java @@ -16,12 +16,12 @@ */ package org.apache.juddi.adminconsole; +import javax.servlet.ServletContextEvent; import java.io.FileOutputStream; import java.io.InputStream; import java.util.Properties; import java.util.logging.Level; import java.util.logging.Logger; -import javax.servlet.ServletContextEvent; /** * This startup servlet's job is to generate an encryption key which will be @@ -31,101 +31,102 @@ import javax.servlet.ServletContextEvent; */ public class StartupServlet implements javax.servlet.ServletContextListener { - static final Logger log = Logger.getLogger(StartupServlet.class.getCanonicalName()); + static final Logger log = Logger.getLogger(StartupServlet.class.getCanonicalName()); - /** - * creates a new AES key and stores it to the properties files - * - * @param sce - */ - public void contextInitialized(ServletContextEvent sce) { - log.info("juddi-admin gui startup"); - FileOutputStream fos = null; - try { - //URL resource = sce.getServletContext().getResource("/META-INF/config.properties"); - Properties p = new Properties(); + /** + * creates a new AES key and stores it to the properties files + * + * @param sce + */ + @Override + public void contextInitialized(ServletContextEvent sce) { + log.info("juddi-admin gui startup"); + FileOutputStream fos = null; + try { + Properties p = new Properties(); + String key = generateKey(); + if (key == null) return; + p.put("key", key); + fos = new FileOutputStream(sce.getServletContext().getRealPath("/WEB-INF/config.properties")); + log.log(Level.INFO, "Storing key to " + sce.getServletContext().getRealPath("/WEB-INF/config.properties")); + p.store(fos, "No comments"); + fos.flush(); + fos.close(); + } catch (Exception ex) { + log.log(Level.WARNING, null, ex); + try { + if (fos != null) { + fos.close(); + } + } catch (Exception e) { + } + } + } - log.info("Attempting to generate 256 bit AES key"); - boolean ok = false; - String key = AES.GEN(256); - if (key == null) { - ok = false; - } else { - if (AES.ValidateKey(key)) { - log.info("Generation of 256 bit AES key successful"); - ok = true; - } else { - log.warning("256 bit key validation failed. To use higher key sizes, try installing the Java Cryptographic Extensions (JCE) Unlimited Strength"); - } - } - if (!ok) { - log.info("Attempting to generate 128 bit AES key"); - key = AES.GEN(128); - if (key == null) { - log.log(Level.SEVERE, "128 bit key generation failed! user's won't be able to login!"); - return; - } else if (AES.ValidateKey(key)) { - log.info("Generation of 128 bit AES key successful"); - } else { - log.severe("128 bit key validation failed! giving up, user's won't be able to login! "); - return; + private String generateKey() { + String key = generateKey(256); + if (key == null) { + key = generateKey(128); + } + if (key == null) { + log.severe("128 bit key validation failed! giving up, user's won't be able to login! "); + return null; + } + return key; + } - } - } + private String generateKey(int length) { + log.info("Attempting to generate "+length+" bit AES key"); + String key = AES.GEN(length); + if (key != null) { + if (AES.ValidateKey(key)) { + log.info("Generation of "+length+" bit AES key successful"); + } else { + log.warning(length+" bit key validation failed. To use higher key sizes, try installing the Java Cryptographic Extensions (JCE) Unlimited Strength"); + return null; + } + } + return key; + } - p.put("key", key); - fos = new FileOutputStream(sce.getServletContext().getRealPath("/WEB-INF/config.properties")); + /** + * + * @param sce + */ + @Override + public void contextDestroyed(ServletContextEvent sce) { + removeKeyFromConfigurationFile(sce); + } - log.log(Level.INFO, "Storing key to " + sce.getServletContext().getRealPath("/WEB-INF/config.properties")); - p.store(fos, "No comments"); - fos.flush(); - fos.close(); - } catch (Exception ex) { - log.log(Level.WARNING, null, ex); - try { - if (fos != null) { - fos.close(); - } - } catch (Exception e) { - } + private void removeKeyFromConfigurationFile(ServletContextEvent sce) { + FileOutputStream fos = null; + try { + log.info("Cleaning up juddi-admin"); + Properties p = new Properties(); + InputStream is = sce.getServletContext().getResourceAsStream("/WEB-INF/config.properties"); + p.load(is); + p.remove("key"); + is.close(); + fos = new FileOutputStream(sce.getServletContext().getRealPath("/WEB-INF/config.properties")); + p.store(fos, "No comments"); + fos.flush(); + fos.close(); + } catch (Exception ex) { + log.log(Level.WARNING, null, ex); + try { + if (fos != null) { + fos.close(); } + } catch (Exception e) { + } } - - /** - * does nothing - * - * @param sce - */ - public void contextDestroyed(ServletContextEvent sce) { - FileOutputStream fos = null; - try { - log.info("Cleaning up juddi-admin"); - Properties p = new Properties(); - InputStream is = sce.getServletContext().getResourceAsStream("/WEB-INF/config.properties"); - p.load(is); - p.remove("key"); - is.close(); - fos = new FileOutputStream(sce.getServletContext().getRealPath("/WEB-INF/config.properties")); - p.store(fos, "No comments"); - fos.flush(); - fos.close(); - } catch (Exception ex) { - log.log(Level.WARNING, null, ex); - try { - if (fos != null) { - fos.close(); - } - } catch (Exception e) { - } - } - try { - sce.getServletContext().removeAttribute("username"); - sce.getServletContext().removeAttribute("password"); - sce.getServletContext().removeAttribute("locale"); - sce.getServletContext().removeAttribute("hub"); - } catch (Exception ex) { - log.log(Level.WARNING, null, ex); - } - + try { + sce.getServletContext().removeAttribute("username"); + sce.getServletContext().removeAttribute("password"); + sce.getServletContext().removeAttribute("locale"); + sce.getServletContext().removeAttribute("hub"); + } catch (Exception ex) { + log.log(Level.WARNING, null, ex); } + } } diff --git a/juddiv3-war/src/main/java/org/apache/juddi/adminconsole/StartupServlet.java b/juddiv3-war/src/main/java/org/apache/juddi/adminconsole/StartupServlet.java index 9cbb028..cfe7e4c 100644 --- a/juddiv3-war/src/main/java/org/apache/juddi/adminconsole/StartupServlet.java +++ b/juddiv3-war/src/main/java/org/apache/juddi/adminconsole/StartupServlet.java @@ -16,12 +16,14 @@ */ package org.apache.juddi.adminconsole; +import org.apache.juddi.config.PersistenceManager; + +import javax.servlet.ServletContextEvent; import java.io.FileOutputStream; import java.io.InputStream; import java.util.Properties; import java.util.logging.Level; import java.util.logging.Logger; -import javax.servlet.ServletContextEvent; /** * This startup servlet's job is to generate an encryption key which will be @@ -31,101 +33,102 @@ import javax.servlet.ServletContextEvent; */ public class StartupServlet implements javax.servlet.ServletContextListener { - static final Logger log = Logger.getLogger(StartupServlet.class.getCanonicalName()); + static final Logger log = Logger.getLogger(StartupServlet.class.getCanonicalName()); - /** - * creates a new AES key and stores it to the properties files - * - * @param sce - */ - public void contextInitialized(ServletContextEvent sce) { - log.info("juddi-admin gui startup"); - FileOutputStream fos = null; - try { - //URL resource = sce.getServletContext().getResource("/META-INF/config.properties"); - Properties p = new Properties(); + /** + * creates a new AES key and stores it to the properties files + * + * @param sce + */ + @Override + public void contextInitialized(ServletContextEvent sce) { + log.info("juddi-admin gui startup"); + FileOutputStream fos = null; + try { + Properties p = new Properties(); + String key = generateKey(); + if (key == null) return; + p.put("key", key); + fos = new FileOutputStream(sce.getServletContext().getRealPath("/WEB-INF/config.properties")); + log.log(Level.INFO, "Storing key to " + sce.getServletContext().getRealPath("/WEB-INF/config.properties")); + p.store(fos, "No comments"); + fos.flush(); + fos.close(); + } catch (Exception ex) { + log.log(Level.WARNING, null, ex); + try { + if (fos != null) { + fos.close(); + } + } catch (Exception e) { + } + } + } - log.info("Attempting to generate 256 bit AES key"); - boolean ok = false; - String key = AES.GEN(256); - if (key == null) { - ok = false; - } else { - if (AES.ValidateKey(key)) { - log.info("Generation of 256 bit AES key successful"); - ok = true; - } else { - log.warning("256 bit key validation failed. To use higher key sizes, try installing the Java Cryptographic Extensions (JCE) Unlimited Strength"); - } - } - if (!ok) { - log.info("Attempting to generate 128 bit AES key"); - key = AES.GEN(128); - if (key == null) { - log.log(Level.SEVERE, "128 bit key generation failed! user's won't be able to login!"); - return; - } else if (AES.ValidateKey(key)) { - log.info("Generation of 128 bit AES key successful"); - } else { - log.severe("128 bit key validation failed! giving up, user's won't be able to login! "); - return; + private String generateKey() { + String key = generateKey(256); + if (key == null) { + key = generateKey(128); + } + if (key == null) { + log.severe("128 bit key validation failed! giving up, user's won't be able to login! "); + return null; + } + return key; + } - } - } + private String generateKey(int length) { + log.info("Attempting to generate " + length + " bit AES key"); + String key = AES.GEN(length); + if (key != null) { + if (AES.ValidateKey(key)) { + log.info("Generation of " + length + " bit AES key successful"); + } else { + log.warning(length + " bit key validation failed. To use higher key sizes, try installing the Java Cryptographic Extensions (JCE) Unlimited Strength"); + return null; + } + } + return key; + } - p.put("key", key); - fos = new FileOutputStream(sce.getServletContext().getRealPath("/WEB-INF/config.properties")); + /** + * @param sce + */ + @Override + public void contextDestroyed(ServletContextEvent sce) { + removeKeyFromConfigurationFile(sce); + PersistenceManager.closeEntityManager(); + } - log.log(Level.INFO, "Storing key to " + sce.getServletContext().getRealPath("/WEB-INF/config.properties")); - p.store(fos, "No comments"); - fos.flush(); - fos.close(); - } catch (Exception ex) { - log.log(Level.WARNING, null, ex); - try { - if (fos != null) { - fos.close(); - } - } catch (Exception e) { - } + private void removeKeyFromConfigurationFile(ServletContextEvent sce) { + FileOutputStream fos = null; + try { + log.info("Cleaning up juddi-admin"); + Properties p = new Properties(); + InputStream is = sce.getServletContext().getResourceAsStream("/WEB-INF/config.properties"); + p.load(is); + p.remove("key"); + is.close(); + fos = new FileOutputStream(sce.getServletContext().getRealPath("/WEB-INF/config.properties")); + p.store(fos, "No comments"); + fos.flush(); + fos.close(); + } catch (Exception ex) { + log.log(Level.WARNING, null, ex); + try { + if (fos != null) { + fos.close(); } + } catch (Exception e) { + } } - - /** - * does nothing - * - * @param sce - */ - public void contextDestroyed(ServletContextEvent sce) { - FileOutputStream fos = null; - try { - log.info("Cleaning up juddi-admin"); - Properties p = new Properties(); - InputStream is = sce.getServletContext().getResourceAsStream("/WEB-INF/config.properties"); - p.load(is); - p.remove("key"); - is.close(); - fos = new FileOutputStream(sce.getServletContext().getRealPath("/WEB-INF/config.properties")); - p.store(fos, "No comments"); - fos.flush(); - fos.close(); - } catch (Exception ex) { - log.log(Level.WARNING, null, ex); - try { - if (fos != null) { - fos.close(); - } - } catch (Exception e) { - } - } - try { - sce.getServletContext().removeAttribute("username"); - sce.getServletContext().removeAttribute("password"); - sce.getServletContext().removeAttribute("locale"); - sce.getServletContext().removeAttribute("hub"); - } catch (Exception ex) { - log.log(Level.WARNING, null, ex); - } - + try { + sce.getServletContext().removeAttribute("username"); + sce.getServletContext().removeAttribute("password"); + sce.getServletContext().removeAttribute("locale"); + sce.getServletContext().removeAttribute("hub"); + } catch (Exception ex) { + log.log(Level.WARNING, null, ex); } + } } -- 1.9.5.msysgit.1