Tuesday, July 12, 2011

[Level 2] How to allow using symbolic link in tomcat? (with python code)

If you want to allow using symbolic link in tomcat,
you could add and attribute called "context" with an key "allowLinking"
eg.
<Context path="/myapp" allowLinking="true"/>
and I wrote a sample python code to do this.
#!/bin/env python
import os,sys
from xml.dom import minidom
def getXMLDom(FILE):
    return minidom.parse(open(FILE,'r'))

def saveXML(FILE, dom):
    fd = open(FILE, 'w')
    fd.write(dom.toxml())
    fd.close()

def appendElement(FILE, base_element_name, element_name, attr_key_val, element_index=0, backup_flag='y'):
    if backup_flag == 'y': backupConfigFile(FILE)
    dom = getXMLDom(FILE)
    e = dom.createElement(element_name)
    for attr, val in attr_key_val:
        e.setAttribute(attr, val)
    dom.getElementsByTagName(base_element_name)[element_index].appendChild(e)
    saveXML(FILE, dom)

xmlFile = prefix+'/opt/ruckuswireless/3rdparty/tomcat/conf/server.xml'
base_element_name = 'Host'
element_name = 'Context'
attr_key_val = [ 
  ["path", "/myapp"],
  ["allowLinking", "true"]
]
appendElement(xmlFile, base_element_name, element_name, attr_key_val)
Wish this helps.  regards, Stanley Huang