public void transform(XmlConfiguration configuration) { InputStream in = null; Document doc = null; String xmlFile = configuration.getFileName(); try { displayMessageln(xmlFile); in = new BufferedInputStream(new FileInputStream(xmlFile)); DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); docFactory.setValidating(false); docFactory.setIgnoringElementContentWhitespace(false); DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); docBuilder.setEntityResolver(new ClasspathEntityResolver(null)); doc = docBuilder.parse(in); applyTransformation(configuration, doc); } catch (SAXException ex) { Logger.getLogger(XPathTransformer.class.getName()).log(Level.SEVERE, null, ex); } catch (ParserConfigurationException ex) { Logger.getLogger(XPathTransformer.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ioex) { Logger.getLogger(XPathTransformer.class.getName()).log(Level.SEVERE, null, ioex); } finally { IOUtils.closeQuietly(in); } if (doc != null) { saveDoc(xmlFile, doc); } }
/** * Transform the DOM tree using the configuration. * * @param configuration the transformation configuration. * @param doc the DOM tree to be updated. */ protected void applyTransformation(XmlConfiguration configuration, Document doc) { List<Parameter> parameters = configuration.getParameters(); for (Parameter parameter : parameters) { displayMessageln("\t" + parameter.getKey() + " (mode:" + parameter.getMode() + ")"); Node rootXpathNode = getMatchingNode(parameter.getKey(), doc); if (rootXpathNode != null) { for (Value value : parameter.getValues()) { switch (value.getMode()) { case XmlTreeHandler.MODE_INSERT: { createNewNode(doc, rootXpathNode, value); } break; case XmlTreeHandler.MODE_DELETE: { Node deletedNode = getMatchingNode(value.getLocation(), rootXpathNode); rootXpathNode.removeChild(deletedNode); } break; case XmlTreeHandler.MODE_UPDATE: { Node oldNode = getMatchingNode(value.getLocation(), rootXpathNode); if (oldNode == null) { createNewNode(doc, rootXpathNode, value); } else { if (rootXpathNode.equals(oldNode)) { rootXpathNode = rootXpathNode.getParentNode(); } Node newNode = oldNode.cloneNode(true); if (oldNode instanceof Element) { ((Element) newNode).setTextContent(value.getValue()); rootXpathNode.replaceChild(newNode, oldNode); } else { ((Attr) newNode).setValue(value.getValue()); rootXpathNode.getAttributes().setNamedItem(newNode); } } break; } } } } } }
/** * Loads jetty configuration from the given URL. * * @param cfgUrl URL to load configuration from. * @throws GridException if load failed. */ private void loadJettyConfiguration(@Nullable URL cfgUrl) throws GridException { if (cfgUrl == null) { HttpConfiguration httpCfg = new HttpConfiguration(); httpCfg.setSecureScheme("https"); httpCfg.setSecurePort(8443); httpCfg.setSendServerVersion(true); httpCfg.setSendDateHeader(true); String srvPortStr = System.getProperty(GG_JETTY_PORT, "8080"); int srvPort; try { srvPort = Integer.valueOf(srvPortStr); } catch (NumberFormatException ignore) { throw new GridException( "Failed to start Jetty server because GRIDGAIN_JETTY_PORT system property " + "cannot be cast to integer: " + srvPortStr); } httpSrv = new Server(new QueuedThreadPool(20, 200)); ServerConnector srvConn = new ServerConnector(httpSrv, new HttpConnectionFactory(httpCfg)); srvConn.setHost(System.getProperty(GG_JETTY_HOST, "localhost")); srvConn.setPort(srvPort); srvConn.setIdleTimeout(30000L); srvConn.setReuseAddress(true); httpSrv.addConnector(srvConn); httpSrv.setStopAtShutdown(false); } else { XmlConfiguration cfg; try { cfg = new XmlConfiguration(cfgUrl); } catch (FileNotFoundException e) { throw new GridSpiException("Failed to find configuration file: " + cfgUrl, e); } catch (SAXException e) { throw new GridSpiException("Failed to parse configuration file: " + cfgUrl, e); } catch (IOException e) { throw new GridSpiException("Failed to load configuration file: " + cfgUrl, e); } catch (Exception e) { throw new GridSpiException( "Failed to start HTTP server with configuration file: " + cfgUrl, e); } try { httpSrv = (Server) cfg.configure(); } catch (Exception e) { throw new GridException("Failed to start Jetty HTTP server.", e); } } assert httpSrv != null; httpSrv.setHandler(jettyHnd); override(getJettyConnector()); }