@Test public void testGetProperties() { Properties props = XMLPersistenceUtil.getProperties("META-INF/persistence-test.xml", "testPU"); assertNotNull(props); assertEquals("create-drop", props.get("hibernate.hbm2ddl.auto")); assertEquals("false", props.get("hibernate.show_sql")); assertEquals("class", props.get("hibernate.ejb.autodetection")); assertEquals("false", props.get("hibernate.cache.use_second_level_cache")); assertEquals("false", props.get("hibernate.cache.use_query_cache")); }
@SuppressWarnings("unchecked") public static Properties getProperties (String persistenceFile, String persistenceUnit) { Properties properties = new Properties(); SAXReader reader = new SAXReader(); InputStream is = null; try { Document doc; is = XMLPersistenceUtil.class.getClassLoader().getResourceAsStream(persistenceFile); doc = reader.read(is); Element root = doc.getRootElement(); List<Element> elements = root.elements(); for (Element el:elements) { if (persistenceUnit.equals(el.attributeValue("name"))) { Element propsElement = el.element("properties"); List<Element> xmlProps = propsElement.elements("property"); for (Element prop:xmlProps) { properties.put(prop.attributeValue("name"), prop.attributeValue("value")); } } } } catch (DocumentException e) { _log.error("Failed to read file contents for "+persistenceFile,e); return null; } finally { if (is!=null) try { is.close(); } catch(Exception e) { }; } return properties; }
XMLPersistenceUtil { @SuppressWarnings("unchecked") public static Properties getProperties (String persistenceFile, String persistenceUnit) { Properties properties = new Properties(); SAXReader reader = new SAXReader(); InputStream is = null; try { Document doc; is = XMLPersistenceUtil.class.getClassLoader().getResourceAsStream(persistenceFile); doc = reader.read(is); Element root = doc.getRootElement(); List<Element> elements = root.elements(); for (Element el:elements) { if (persistenceUnit.equals(el.attributeValue("name"))) { Element propsElement = el.element("properties"); List<Element> xmlProps = propsElement.elements("property"); for (Element prop:xmlProps) { properties.put(prop.attributeValue("name"), prop.attributeValue("value")); } } } } catch (DocumentException e) { _log.error("Failed to read file contents for "+persistenceFile,e); return null; } finally { if (is!=null) try { is.close(); } catch(Exception e) { }; } return properties; } }
XMLPersistenceUtil { @SuppressWarnings("unchecked") public static Properties getProperties (String persistenceFile, String persistenceUnit) { Properties properties = new Properties(); SAXReader reader = new SAXReader(); InputStream is = null; try { Document doc; is = XMLPersistenceUtil.class.getClassLoader().getResourceAsStream(persistenceFile); doc = reader.read(is); Element root = doc.getRootElement(); List<Element> elements = root.elements(); for (Element el:elements) { if (persistenceUnit.equals(el.attributeValue("name"))) { Element propsElement = el.element("properties"); List<Element> xmlProps = propsElement.elements("property"); for (Element prop:xmlProps) { properties.put(prop.attributeValue("name"), prop.attributeValue("value")); } } } } catch (DocumentException e) { _log.error("Failed to read file contents for "+persistenceFile,e); return null; } finally { if (is!=null) try { is.close(); } catch(Exception e) { }; } return properties; } private  XMLPersistenceUtil(); }
XMLPersistenceUtil { @SuppressWarnings("unchecked") public static Properties getProperties (String persistenceFile, String persistenceUnit) { Properties properties = new Properties(); SAXReader reader = new SAXReader(); InputStream is = null; try { Document doc; is = XMLPersistenceUtil.class.getClassLoader().getResourceAsStream(persistenceFile); doc = reader.read(is); Element root = doc.getRootElement(); List<Element> elements = root.elements(); for (Element el:elements) { if (persistenceUnit.equals(el.attributeValue("name"))) { Element propsElement = el.element("properties"); List<Element> xmlProps = propsElement.elements("property"); for (Element prop:xmlProps) { properties.put(prop.attributeValue("name"), prop.attributeValue("value")); } } } } catch (DocumentException e) { _log.error("Failed to read file contents for "+persistenceFile,e); return null; } finally { if (is!=null) try { is.close(); } catch(Exception e) { }; } return properties; } private  XMLPersistenceUtil(); static List<String> getClasses(String persistenceFile, String persistenceUnit); @SuppressWarnings("unchecked") static Properties getProperties(String persistenceFile, String persistenceUnit); }
XMLPersistenceUtil { @SuppressWarnings("unchecked") public static Properties getProperties (String persistenceFile, String persistenceUnit) { Properties properties = new Properties(); SAXReader reader = new SAXReader(); InputStream is = null; try { Document doc; is = XMLPersistenceUtil.class.getClassLoader().getResourceAsStream(persistenceFile); doc = reader.read(is); Element root = doc.getRootElement(); List<Element> elements = root.elements(); for (Element el:elements) { if (persistenceUnit.equals(el.attributeValue("name"))) { Element propsElement = el.element("properties"); List<Element> xmlProps = propsElement.elements("property"); for (Element prop:xmlProps) { properties.put(prop.attributeValue("name"), prop.attributeValue("value")); } } } } catch (DocumentException e) { _log.error("Failed to read file contents for "+persistenceFile,e); return null; } finally { if (is!=null) try { is.close(); } catch(Exception e) { }; } return properties; } private  XMLPersistenceUtil(); static List<String> getClasses(String persistenceFile, String persistenceUnit); @SuppressWarnings("unchecked") static Properties getProperties(String persistenceFile, String persistenceUnit);  }