Date Created: Tue 15-Jan-2008

Related Document Categories:



    Java utility to change password in server.xml for IBM WebSphere Application Server

    waspassword_v4_18-jan-2008.zip

    The attached file is a simple demonstration project built using Rapid Application developer 7. The project shows how to write a Java application which can be used to change IBM WebSphere Application Server passwords as used by security registry's. The code was created to show how Java can be used to find and replace passwords in security.xml. Useful if you have enterprise system comprising of Java code that might want to use Java to do this kind of work.

    In fact the code now supports the generic ability to find and replace attributes in nodes found by an XPath query in any XML document, not necessarily a WebSphere configuration document.

    Sample code:

    StringUtils.javaFileUtils.javasecurity.propsPropertiesManager.javaXMLObject.javaWASPasswordChange.java

    Sample code from the main class:

    package com.webspheretools.utils.passwordchange;

    import java.io.*;
    import java.text.*;
    import java.lang.String;
    import java.util.ArrayList;

    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.transform.*;
    import javax.xml.transform.dom.*;
    import javax.xml.transform.stream.StreamResult;

    import org.w3c.dom.*;

    import com.webspheretools.utils.props.PropertiesManager;
    import com.webspheretools.utils.*;

    public class WASPasswordChange {
    static org.w3c.dom.Document doc = null;
    static String fileseparator = System.getProperty("file.separator");

    class temp {
    int i = 0;
    }

    public static void main(String[] args) {
    //temp mytemp = new WASPasswordChange.temp();

    System.out.println(System.getProperty("java.class.path"));

    ArrayList<String> xmlFiles = null;
    xmlFiles = getXMLFiles();

    if(xmlFiles.size() > 0)
    {
    for (int i = 0 ; i < xmlFiles.size() ; i++) {
    //System.out.println("file: " + xmlFiles[i]);
    File f = new File(xmlFiles.get(i));
    if(f.exists())
    {
    searchAndReplace(xmlFiles.get(i));
    }
    else
    {
    System.out.println("file: " + xmlFiles.get(i) + " does not exist!");
    }

    }//End for loop
    }else
    {
    System.out.println("No security.xml file(s) listed in the com.webspheretools.utils.props[security.props] file!");
    }

    }

    private static void searchAndReplace(String fullFilePath)
    {
    //Backup files
    backUpFile(fullFilePath);

    System.out.println("fullFilePath=" + fullFilePath);
    doc = getDocument(fullFilePath);

    String xpath = "";
    boolean save = true;

    ArrayList<String> xPathQueries = null;
    xPathQueries = getXPathQueries();
    xpath = xPathQueries.get(0);
    System.out.println("Using xpath query: " + xpath);
    //xmi:id="LDAPUserRegistry_1"

    try {
    // Get the matching elements
    NodeList nodelist = org.apache.xpath.XPathAPI.selectNodeList(doc, xpath);
    int nodeCount = nodelist.getLength();
    System.out.println("Node count=" + nodeCount);

    if( nodeCount > 0 )
    {
    // Process the elements in the nodelist
    for (int i=0; i<nodeCount; i++) {
    // Get element
    Element elem = (Element)nodelist.item(i);

    //Loop through attributes (elem) to replace attribute values
    ArrayList<XMLObject> myXMLObjectList = new ArrayList<XMLObject>();
    myXMLObjectList = getValueinfo(elem);
    if(myXMLObjectList.size() > 0)
    {
    for (int objecti = 0 ; objecti < myXMLObjectList.size() ; objecti++) {
    //ReplaceByID(elem,"serverPassword","Password1");
    //ReplaceByID(elem,"bindPassword","Password1");
    System.out.println("Attribute Name=" + myXMLObjectList.get(objecti).AttributeName);
    System.out.println("New Value is now=" + myXMLObjectList.get(objecti).NewValue);
    ReplaceByID(elem,myXMLObjectList.get(objecti).AttributeName,myXMLObjectList.get(objecti).NewValue);
    //System.out.println("Existing Value=" + myXMLObjectList.get(objecti).ExistingValue);


    }//End for loop
    }else
    {
    System.out.println("No attribute value replacements were listed in the com.webspheretools.utils.props[security.props] file!");
    save = false;
    }

    }
    }else
    {
    System.out.println("No node(s) found that match xpath:" + "\n" + xpath);
    save = false;

    }
    } catch (javax.xml.transform.TransformerException e) {
    System.out.println(e.getMessage());
    }
    if(save)
    {
    System.out.println("Saving dom to file");
    saveXMLDocToFile(fullFilePath,doc);
    }


    }//End searchAndReplace

    static void saveXMLDocToFile(String sFullPathName,Document document)
    {
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer=null;
    try {
    transformer = tFactory.newTransformer();
    } catch(TransformerException te) {

    }

    DOMSource source = new DOMSource(document);
    StreamResult result = new StreamResult(new File(sFullPathName));
    try {
    transformer.transform(source, result);
    } catch(TransformerException te) {
    System.out.println(te.getMessage());
    }
    }


    static Document getDocument(String fileName)
    {
    org.w3c.dom.Document document=null;
    try {
    // load the document from a file:
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder loader = factory.newDocumentBuilder();
    document = loader.parse(fileName);


    } catch (Exception ex) {
    ex.printStackTrace();
    }
    return document;
    }

    static ArrayList<String> getXMLFiles()
    {
    PropertiesManager propman = new PropertiesManager();
    String securityFile = null;
    String sFileName = "security.props";
    ArrayList<String> list = new ArrayList<String>();

    int i=0;
    try{
    for(i=0;propman.getProperty(sFileName,"security.xml.file_"+i).length() > 0;i++)
    {
    securityFile = propman.getProperty(sFileName,"security.xml.file_"+i);

    if(securityFile.length() == 0) {
    System.out.println("There were no security.xml files passed in to scan");
    }else
    {
    list.add(securityFile);
    System.out.println("security.xml.file_" + i + "=" + securityFile);
    }
    }//end for loop
    } catch(NullPointerException npe)
    {
    //I catch a null pointer this means I have no more properties to get
    System.out.println("..End of properties");
    //System.out.println("Entry for security.xml.file_" + i + "=" + securityFile + " Was Null!");
    }

    return list;

    }//End getProperties


    static ArrayList<String> getXPathQueries()
    {
    PropertiesManager propman = new PropertiesManager();
    String xPathQuery = null;
    String sFileName = "security.props";
    ArrayList<String> list = new ArrayList<String>();

    int i=0;
    try{
    for(i=0;propman.getProperty(sFileName,"xpath.query_"+i).length() > 0;i++)
    {
    xPathQuery = propman.getProperty(sFileName,"xpath.query_"+i);

    if(xPathQuery.length() == 0) {
    System.out.println("There were no xpath queries set in property file");
    }else
    {
    list.add(xPathQuery);
    System.out.println("xpath.query_" + i + "=" + xPathQuery);
    }
    }//end for loop
    } catch(NullPointerException npe)
    {
    //I catch a null pointer this means I have no more properties to get
    System.out.println("..End of properties");
    //System.out.println("Entry for security.xml.file_" + i + "=" + securityFile + " Was Null!");
    }

    return list;

    }//End getXPath

    static ArrayList<XMLObject> getValueinfo(Element elem)
    {
    PropertiesManager propman = new PropertiesManager();
    String sFileName = "security.props";
    XMLObject myXMLObject= new XMLObject();
    ArrayList<XMLObject> myXMLObjectList = new ArrayList<XMLObject>();

    int i=0;
    try{
    //for(i=0;propman.getProperty(sFileName,"xpath.query_"+i).length() > 0;i++)
    //{
    //myXMLObject.ExistingValue = propman.getProperty(sFileName,"xml.value.existing_"+i);
    myXMLObject.NewValue = propman.getProperty(sFileName,"xml.value.new_"+i);
    myXMLObject.NodeName = elem.getNodeName();
    myXMLObject.AttributeName = propman.getProperty(sFileName,"xml.attribute_"+i);

    if(myXMLObject.NewValue.length() == 0) {
    System.out.println("The property xml.value.existing has not been set in the property file");
    }else
    {
    myXMLObjectList.add(myXMLObject);
    System.out.println("Adding XMLObject to List");
    }
    //}//end for loop
    } catch(NullPointerException npe)
    {
    //I catch a null pointer this means I have no more properties to get
    System.out.println("..End of properties");
    //System.out.println("Entry for security.xml.file_" + i + "=" + securityFile + " Was Null!");
    }

    return myXMLObjectList;

    }//End getProperties

    static void ReplaceByID(Element elem,String attributeName,String newValue) {

    if(elem == null) {
    System.out.println("There is no element with the attribute: " + attributeName);
    } else {

    System.out.println("Updating Node:" + elem.getNodeName());
    System.out.println("Original Attrbute value:" + attributeName + "=" + elem.getAttribute(attributeName));
    System.out.println("Setting new value ...");
    elem.setAttribute(attributeName, newValue);
    System.out.println("New Attribute value: " + attributeName + "=" + elem.getAttribute(attributeName));
    }
    }

    static void backUpFile(String fullFilePath)
    {
    /* Backup files */
    File SrcFile = new File(fullFilePath);
    SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd-hhmmss");
    java.sql.Timestamp ts = new java.sql.Timestamp(System.currentTimeMillis());

    String newFullFilePath = "";
    int pos = 0;
    pos = fullFilePath.indexOf(".");
    newFullFilePath = com.webspheretools.utils.StringUtils.Left(fullFilePath,pos) + "_" + ft.format(ts) + ".xml";
    System.out.println("newFullFilePath=" + newFullFilePath);

    File dstFile = new File(newFullFilePath);
    try {
    FileUtils.copyFile(SrcFile, dstFile);
    }
    catch(IOException ioe)
    {
    System.out.println(ioe.getMessage());
    }
    }
    }//end class

Middleware Mentor - Steven Charles Robinson

About Me

Steve Robinson has been working in IT for over 15 years and has provided solutions for many large-enterprise corporate companies across the world. Steve specialises in Java and Middleware consulting. Steve comes from both an administration and development background.

Before moving to JEE, Steve was an accomplished developer and consultant for both IBM Lotus Notes and Microsoft .NET Technologies.

Follow Steve as @stevencrobinson on twitter.

Read my books?

IBM WebSphere Application Server 8.0 Administration Guide

IBM WebSphere Application Server 8.0 Administration Guide

WebSphere Application Server 7.0 Administration Guide

WebSphere Application Server 7.0 Administration Guide

WebSphere Categories

Oracle WebLogic Categories

JBoss Categories

Other Categories