public static void deleteDataSource(File deploymentFile, String name) { Document doc; Element root; if (deploymentFile == null) { log.error("DeleteDatasource: passed file is null"); return; } if (deploymentFile.exists()) { try { SAXBuilder builder = new SAXBuilder(); SelectiveSkippingEntityResolver entityResolver = SelectiveSkippingEntityResolver.getDtdAndXsdSkippingInstance(); builder.setEntityResolver(entityResolver); doc = builder.build(deploymentFile); root = doc.getRootElement(); if (root != null) { if (!root.getName().equals("datasources")) { throw new RuntimeException( "Datasource file format exception on [" + deploymentFile + "], expected [datasources] element but found [" + root.getName() + "]"); } Element datasourceElement = findDatasourceElement(root, name); root.removeContent(datasourceElement); } updateFile(deploymentFile, doc); } catch (JDOMException e) { log.error("Parsing error occurred while deleting datasource at file: " + deploymentFile, e); } catch (IOException e) { log.error("IO error occurred while deleting datasource at file: " + deploymentFile, e); } } }
/** * Update or create a new datasource * * @param deploymentFile * @param name * @param config * @throws JDOMException * @throws IOException */ private static void updateDatasource(File deploymentFile, String name, Configuration config) throws JDOMException, IOException { Document doc; Element root; if (deploymentFile.exists() && !deploymentFile.canWrite()) { throw new RuntimeException( "Datasource file " + deploymentFile + " is not writable. Aborting."); } if (deploymentFile.exists()) { SAXBuilder builder = new SAXBuilder(); SelectiveSkippingEntityResolver entityResolver = SelectiveSkippingEntityResolver.getDtdAndXsdSkippingInstance(); builder.setEntityResolver(entityResolver); doc = builder.build(deploymentFile); root = doc.getRootElement(); } else { doc = new Document(); root = new Element("datasources"); doc.setRootElement(root); } if (!root.getName().equals("datasources")) { throw new RuntimeException( "Datasource file format exception on [" + deploymentFile + "], expected [datasources] element but found [" + root.getName() + "]"); } Element datasourceElement = findDatasourceElement(root, name); String type = config.getSimpleValue("type", null); boolean isNewDatasource = false; if (datasourceElement == null) { datasourceElement = new Element(type); isNewDatasource = true; } else if (!type.equals(datasourceElement.getName())) { datasourceElement.setName(type); } updateElements(datasourceElement, config, COMMON_PROPS); if (type.equals(XA_TX_TYPE)) { updateElements(datasourceElement, config, XA_PROPS); updateMap(datasourceElement, config, CONNECTION_PROPERTY, XA_DATASOURCE_PROPERTY); // updateXAElements(datasourceElement, config); } else { updateElements(datasourceElement, config, NON_XA_PROPS); updateMap(datasourceElement, config, CONNECTION_PROPERTY, CONNECTION_PROPERTY); } if (isNewDatasource) { root.addContent(datasourceElement); } updateFile(deploymentFile, doc); }
public static Configuration loadDatasource(File file, String name) { /* * <local-tx-datasource> <jndi-name>RHQDS</jndi-name> * <connection-url>${rhq.server.database.connection-url}</connection-url> * <driver-class>${rhq.server.database.driver-class}</driver-class> * <user-name>${rhq.server.database.user-name}</user-name> * <password>${rhq.server.database.password}</password> * * <!-- You can include connection properties that will get passed in the * DriverManager.getConnection(props) call; look at your Driver docs to see what these might be. --> * <connection-property name="char.encoding">UTF-8</connection-property> <!-- Tells an oracle * 10.2.0.2 driver to properly implement clobs. --> <connection-property * name="SetBigStringTryClob">true</connection-property> * * <transaction-isolation>TRANSACTION_READ_COMMITTED</transaction-isolation> * <min-pool-size>25</min-pool-size> <max-pool-size>100</max-pool-size> * <blocking-timeout-millis>5000</blocking-timeout-millis> <idle-timeout-minutes>15</idle-timeout-minutes> * <prepared-statement-cache-size>75</prepared-statement-cache-size> * * <type-mapping>${rhq.server.database.type-mapping}</type-mapping> * * </local-tx-datasource> */ try { SAXBuilder builder = new SAXBuilder(); SelectiveSkippingEntityResolver entityResolver = SelectiveSkippingEntityResolver.getDtdAndXsdSkippingInstance(); builder.setEntityResolver(entityResolver); Document doc = builder.build(file); // Get the root element Element root = doc.getRootElement(); if (!root.getName().equals("datasources")) { return null; } Element datasourceElement = findDatasourceElement(root, name); if (datasourceElement == null) { return null; } Configuration config = new Configuration(); String type = datasourceElement.getName(); config.put(new PropertySimple("type", type)); bindElements(datasourceElement, config, COMMON_PROPS); if (type.equals(XA_TX_TYPE)) { bindElements(datasourceElement, config, XA_PROPS); bindMap(datasourceElement, config, CONNECTION_PROPERTY, XA_DATASOURCE_PROPERTY); // bindXASpecialElements(datasourceElement,config); } else { bindElements(datasourceElement, config, NON_XA_PROPS); bindMap(datasourceElement, config, CONNECTION_PROPERTY, CONNECTION_PROPERTY); } return config; } catch (IOException e) { log.error("IO error occurred while reading file: " + file, e); } catch (JDOMException e) { log.error("Parsing error occurred while reading file: " + file, e); } return null; }