1 20 package org.jahia.services.webapps_deployer.orion; 21 22 23 import org.jahia.data.xml.JahiaXmlDocument; 24 import org.jahia.exceptions.JahiaException; 25 import org.jahia.utils.xml.XMLParser; 26 import org.w3c.dom.Element ; 27 import org.w3c.dom.NamedNodeMap ; 28 import org.w3c.dom.Node ; 29 30 import java.util.Vector ; 31 32 33 60 public class Server_Xml extends JahiaXmlDocument { 61 62 63 private Vector m_Applications = new Vector (); 64 65 private Vector m_ApplicationNodes = new Vector (); 66 67 private static org.apache.log4j.Logger logger = 68 org.apache.log4j.Logger.getLogger (Server_Xml.class); 69 70 75 public Server_Xml (String docPath) throws JahiaException { 76 super (docPath); 77 extractDocumentData (); 78 79 } 80 81 82 88 public Server_Xml (String docPath, org.xml.sax.helpers.ParserAdapter parser) 89 throws JahiaException { 90 super (docPath, parser); 91 } 92 93 94 98 public void extractDocumentData () throws JahiaException { 99 100 logger.debug (" Server_Xml::extractDocumentData started "); 101 102 if (m_XMLDocument == null) { 103 104 throw new JahiaException ("Server_Xml", 105 "Parsed web.xml document is null", 106 JahiaException.ERROR_SEVERITY, 107 JahiaException.SERVICE_ERROR); 108 } 109 110 111 if (!m_XMLDocument.hasChildNodes ()) { 112 113 throw new JahiaException ("Server_Xml", 114 "Main document node has no children", 115 JahiaException.ERROR_SEVERITY, 116 JahiaException.SERVICE_ERROR); 117 } 118 119 120 Element appServNode; 122 appServNode = (Element ) m_XMLDocument.getDocumentElement (); 123 124 if (!appServNode.getNodeName ().equalsIgnoreCase ("application-server")) { 125 126 throw new JahiaException ("Invalid XML format", 127 "application-server tag is not present as starting tag in file", 128 JahiaException.ERROR_SEVERITY, 129 JahiaException.SERVICE_ERROR); 130 } 131 132 logger.debug ("server.xml file has application-server element"); 133 134 Vector appNodes = XMLParser.getChildNodes (appServNode, "application"); 136 Node nodeItem = null; 137 Application_Element appEl = null; 138 NamedNodeMap attribs = null; 139 140 String name; 141 String path; 142 String deployDir; 143 String parent; 144 String autoStart; 145 146 int size = appNodes.size (); 147 148 for (int i = 0; i < size; i++) { 149 150 nodeItem = (Node ) appNodes.get (i); 151 152 logger.debug ("application element " + nodeItem.getNodeName ()); 153 154 name = XMLParser.getAttributeValue (nodeItem, "name"); 155 path = XMLParser.getAttributeValue (nodeItem, "path"); 156 deployDir = XMLParser.getAttributeValue (nodeItem, "deployment-directory"); 157 parent = XMLParser.getAttributeValue (nodeItem, "parent"); 158 autoStart = XMLParser.getAttributeValue (nodeItem, "auto-start"); 159 160 if (name != null && path != null) { 161 162 appEl = new Application_Element (name, 163 path, 164 deployDir, 165 parent, 166 autoStart 167 ); 168 169 m_Applications.add (appEl); 170 m_ApplicationNodes.add (nodeItem); 171 172 logger.debug (" Application Element name :" + appEl.getName ()); 173 logger.debug (" path :" + appEl.getPath ()); 174 logger.debug (" deployDir :" + appEl.getDeployDir ()); 175 logger.debug (" parent :" + appEl.getParent ()); 176 logger.debug (" autoStart :" + appEl.getAutoStart ()); 177 } 178 } 179 180 logger.debug (" done"); 181 } 182 183 184 189 public Vector getApplications () { 190 191 return m_Applications; 192 193 } 194 195 196 204 public boolean appendApplication (Application_Element appEl) { 205 206 if (!isDeclared (appEl.getName ())) { 207 208 Element docElNode = (Element ) m_XMLDocument.getDocumentElement (); 209 210 Element newNode = (Element ) m_XMLDocument.createElement ("application"); 211 212 XMLParser.setAttribute (newNode, "name", appEl.getName ()); 213 XMLParser.setAttribute (newNode, "path", appEl.getPath ()); 214 XMLParser.setAttribute (newNode, "deployment-directory", appEl.getDeployDir ()); 215 XMLParser.setAttribute (newNode, "parent", appEl.getParent ()); 216 XMLParser.setAttribute (newNode, "auto-start", appEl.getAutoStart ()); 217 218 if (m_ApplicationNodes.size () > 0) { 219 Node lastAppNode = (Node ) m_ApplicationNodes.get ( 220 (m_ApplicationNodes.size () - 1)); 221 docElNode.insertBefore (newNode, lastAppNode); 222 } else { 223 docElNode.appendChild (newNode); 224 } 225 226 m_Applications.add (appEl); 227 m_ApplicationNodes.add (newNode); 228 return true; 229 } 230 231 return false; 232 } 233 234 235 240 public boolean isDeclared (String name) { 241 242 Application_Element appEl = null; 243 int size = m_Applications.size (); 244 for (int i = 0; i < size; i++) { 245 appEl = (Application_Element) m_Applications.get (i); 246 if (appEl.getName ().equals (name)) { 247 return true; 248 } 249 } 250 251 return false; 252 } 253 254 255 260 public void removeApplication (String appName) { 261 262 Application_Element appEl = null; 263 264 int size = m_Applications.size (); 265 for (int i = 0; i < size; i++) { 266 appEl = (Application_Element) m_Applications.get (i); 267 if (appEl.getName ().equals (appName)) { 268 269 m_Applications.remove (i); 270 Element node = (Element ) m_ApplicationNodes.get (i); 271 Element docElNode = (Element ) m_XMLDocument.getDocumentElement (); 272 docElNode.removeChild (node); 273 m_ApplicationNodes.remove (i); 274 break; 275 } 276 } 277 } 278 279 280 } | Popular Tags |