⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tomcat_users_xml.java

📁 java 写的一个新闻发布系统
💻 JAVA
字号:
////                                   ____.//                       __/\ ______|    |__/\.     _______//            __   .____|    |       \   |    +----+       \//    _______|  /--|    |    |    -   \  _    |    :    -   \_________//   \\______: :---|    :    :           |    :    |         \________>//           |__\---\_____________:______:    :____|____:_____\//                                      /_____|////                 . . . i n   j a h i a   w e   t r u s t . . .////////  Tomcat_Users_Xml////  NK      02.02.2001////package org.jahia.services.webapps_deployer.tomcat;import java.io.*;import java.util.*;//import com.sun.xml.tree.*;import org.w3c.dom.*;import org.xml.sax.*;import org.jahia.exceptions.*;import org.jahia.utils.*;               // JahiaConsoleimport org.jahia.data.xml.*;            // JahiaXmlDocumentimport org.jahia.utils.xml.*;           // XMLParser/** * Holds Informations about the Tomcat tomcat-users.xml file * We need to modify this file to add the "manager" role to * use the Tomcat Management Application * * <tomcat-users> *    <user name="tomcat" password="tomcat" roles="tomcat,manager" /> *    <user name="role1"  password="tomcat" roles="role1"  /> *    <user name="both"   password="tomcat" roles="tomcat,role1" /> * </tomcat-users> * * @author Khue ng * @version 1.0 */public class Tomcat_Users_Xml extends JahiaXmlDocument {   /** The list of Tomcat users Nodes **/   private Vector m_UserNodes = new Vector();   private static String name_att = "name";   /**    * Constructor    *    * @param (String) path, the full path to the tomcat-users.xml file    */    public Tomcat_Users_Xml (String docPath) throws JahiaException    {      super(docPath);      extractDocumentData();    }   /**    * Extracts data from the Tomcat-users.xml file. Build the JahiaWebAppsWarPackage object    * to store extracted data    */   public void extractDocumentData() throws JahiaException {      JahiaConsole.println("Tomcat_Users_Xml::extractDocumentData","started ");       if (m_XMLDocument == null) {          throw new JahiaException( "Tomcat_Users_Xml",                                    "Parsed Tomcat-users.xml document is null",                                       JahiaException.ERROR,                                       JahiaException.SERVICE_ERROR);       }       if (!m_XMLDocument.hasChildNodes()) {          throw new JahiaException( "Tomcat_Users_Xml",                                       "Main document node has no children",                                       JahiaException.ERROR,                                       JahiaException.SERVICE_ERROR);       }      // get <tomcat-users> node      Element docElNode = (Element) m_XMLDocument.getDocumentElement();       if (!docElNode.getNodeName().equalsIgnoreCase("tomcat-users")) {          throw new JahiaException(  "Invalid XML format",                                        "tomcat-users tag is not present as starting tag in file",                                        JahiaException.ERROR,                                        JahiaException.SERVICE_ERROR);       }      JahiaConsole.println("Tomcat_Users_Xml.extractDocumentData","Tomcat-users.xml file has tomcat-users element");      // get users elements       m_UserNodes = XMLParser.getChildNodes(docElNode,"user");      JahiaConsole.println("Tomcat_Users_Xml.extractDocumentData","done");   }   /**    * Return the list of Users Nodes    *    * @return (Vector) list of Users Nodes    */   public Vector getUserNodes(){      return m_UserNodes;   }   /**    * Append a new <user..> Element in the xml file    * Add it in the user nodes list    *    * @param (String) name, tomcat user    * @param (String) password    * @param (String) roles , ex: "tomcat,manager"    */   public void addUser(                        String name,                        String password,                        String roles                        ) {      Element docElNode = (Element) m_XMLDocument.getDocumentElement();      Element newNode = (Element)m_XMLDocument.createElement("user");      XMLParser.setAttribute(newNode, name_att,             name);      XMLParser.setAttribute(newNode, "password",         password);      XMLParser.setAttribute(newNode, "roles",            roles);      if ( m_UserNodes.size()>0 ){          Node lastUserNode = (Node)m_UserNodes.get( (m_UserNodes.size()-1) );          docElNode.insertBefore(newNode,lastUserNode);      } else {          docElNode.appendChild(newNode);      }      m_UserNodes.add(newNode);   }   /**    * Change user password for a user with of a gived name and a having a gived role    *    * @param (String) name, tomcat user    * @param (String) password    * @param (String) role,    * @param (boolean) return false on error    */   public boolean updateUser(                              String name,                              String password,                              String role                           ){      int size = m_UserNodes.size();      for( int i=0 ; i<size ; i++ ){         Element user = (Element)m_UserNodes.get(i);         if ( XMLParser.getAttributeValue(user,"name") == null ){             name_att = "username";         } else {             name_att = "name";         }         if ( name.equalsIgnoreCase(XMLParser.getAttributeValue(user,name_att) )               && role.equalsIgnoreCase(XMLParser.getAttributeValue(user,"roles") ) ) {            XMLParser.setAttribute(user,"password", password);            return true;         }      }      return false;   }   /**    * Check if a user exist    *    * @param (String) name, tomcat user    * @param (String) password,    */   public boolean checkUser(                              String name,                              String password                           ){      int size = m_UserNodes.size();      for( int i=0 ; i<size ; i++ ){         Element user = (Element)m_UserNodes.get(i);         if ( XMLParser.getAttributeValue(user,"name") == null ){             name_att = "username";         } else {             name_att = "name";         }         if ( name.equalsIgnoreCase(XMLParser.getAttributeValue(user,name_att) )              && password.equalsIgnoreCase(XMLParser.getAttributeValue(user,"password") ) ) {             return true;         }      }      return false;   }   /**    * Return the password of a user with a gived name and a gived role    *    * @param (String) name, tomcat user    * @param (String) role,    */   public String getUserPassword(                              String name,                              String role                           ){      int size = m_UserNodes.size();      for( int i=0 ; i<size ; i++ ){         Element user = (Element)m_UserNodes.get(i);         if ( XMLParser.getAttributeValue(user,"name") == null ){             name_att = "username";         } else {             name_att = "name";         }         if ( name.equalsIgnoreCase(XMLParser.getAttributeValue(user,name_att) )               && role.equalsIgnoreCase(XMLParser.getAttributeValue(user,"roles") ) ) {            return XMLParser.getAttributeValue(user,"password");         }      }      return null;   }} // end Tomcat_Users_Xml

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -