📄 xmlgroupdatabase.java
字号:
// If new, set created info group.setCreated( modDate ); group.setCreator( modifier.getName() ); } group.setModifier( modifier.getName() ); group.setLastModified( modDate ); // Add the group to the 'saved' list m_groups.put( index, group ); // Commit to disk saveDOM(); } private void buildDOM() throws WikiSecurityException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating( false ); factory.setExpandEntityReferences( false ); factory.setIgnoringComments( true ); factory.setNamespaceAware( false ); try { m_dom = factory.newDocumentBuilder().parse( m_file ); log.debug( "Database successfully initialized" ); m_lastModified = m_file.lastModified(); m_lastCheck = System.currentTimeMillis(); } catch( ParserConfigurationException e ) { log.error( "Configuration error: " + e.getMessage() ); } catch( SAXException e ) { log.error( "SAX error: " + e.getMessage() ); } catch( FileNotFoundException e ) { log.info( "Group database not found; creating from scratch..." ); } catch( IOException e ) { log.error( "IO error: " + e.getMessage() ); } if ( m_dom == null ) { try { // // Create the DOM from scratch // m_dom = factory.newDocumentBuilder().newDocument(); m_dom.appendChild( m_dom.createElement( "groups" ) ); } catch( ParserConfigurationException e ) { log.fatal( "Could not create in-memory DOM" ); } } // Ok, now go and read this sucker in if ( m_dom != null ) { NodeList groupNodes = m_dom.getElementsByTagName( GROUP_TAG ); for( int i = 0; i < groupNodes.getLength(); i++ ) { Element groupNode = (Element) groupNodes.item( i ); String groupName = groupNode.getAttribute( GROUP_NAME ); if ( groupName == null ) { log.warn( "Detected null group name in XMLGroupDataBase. Check your group database." ); } else { Group group = buildGroup( groupNode, groupName ); m_groups.put( groupName, group ); } } } } private long m_lastCheck = 0; private long m_lastModified = 0; private void checkForRefresh() { long time = System.currentTimeMillis(); if( time - m_lastCheck > 60*1000L ) { long lastModified = m_file.lastModified(); if( lastModified > m_lastModified ) { try { buildDOM(); } catch( WikiSecurityException e ) { log.error("Could not refresh DOM",e); } } } } /** * Constructs a Group based on a DOM group node. * @param groupNode the node in the DOM containing the node * @param name the name of the group * @throws NoSuchPrincipalException * @throws WikiSecurityException */ private Group buildGroup( Element groupNode, String name ) { // It's an error if either param is null (very odd) if ( groupNode == null || name == null ) { throw new IllegalArgumentException( "DOM element or name cannot be null." ); } // Construct a new group Group group = new Group( name, m_engine.getApplicationName() ); // Get the users for this group, and add them NodeList members = groupNode.getElementsByTagName( MEMBER_TAG ); for( int i = 0; i < members.getLength(); i++ ) { Element memberNode = (Element) members.item( i ); String principalName = memberNode.getAttribute( PRINCIPAL ); Principal member = new WikiPrincipal( principalName ); group.add( member ); } // Add the created/last-modified info String creator = groupNode.getAttribute( CREATOR ); String created = groupNode.getAttribute( CREATED ); String modifier = groupNode.getAttribute( MODIFIER ); String modified = groupNode.getAttribute( LAST_MODIFIED ); try { group.setCreated( m_format.parse( created ) ); group.setLastModified( m_format.parse( modified ) ); } catch ( ParseException e ) { // If parsing failed, use the platform default try { group.setCreated( m_defaultFormat.parse( created ) ); group.setLastModified( m_defaultFormat.parse( modified ) ); } catch ( ParseException e2) { log.warn( "Could not parse 'created' or 'lastModified' " + "attribute for " + " group'" + group.getName() + "'." + " It may have been tampered with." ); } } group.setCreator( creator ); group.setModifier( modifier ); return group; } private void saveDOM() throws WikiSecurityException { if ( m_dom == null ) { log.fatal( "Group database doesn't exist in memory." ); } File newFile = new File( m_file.getAbsolutePath() + ".new" ); try { BufferedWriter io = new BufferedWriter( new OutputStreamWriter( new FileOutputStream( newFile ), "UTF-8" ) ); // Write the file header and document root io.write( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" ); io.write( "<groups>\n" ); // Write each profile as a <group> node for( Group group : m_groups.values() ) { io.write( " <" + GROUP_TAG + " " ); io.write( GROUP_NAME ); io.write( "=\"" + StringEscapeUtils.escapeXml( group.getName() )+ "\" " ); io.write( CREATOR ); io.write( "=\"" + StringEscapeUtils.escapeXml( group.getCreator() ) + "\" " ); io.write( CREATED ); io.write( "=\"" + m_format.format( group.getCreated() ) + "\" " ); io.write( MODIFIER ); io.write( "=\"" + group.getModifier() + "\" " ); io.write( LAST_MODIFIED ); io.write( "=\"" + m_format.format( group.getLastModified() ) + "\"" ); io.write( ">\n" ); // Write each member as a <member> node for( Principal member : group.members() ) { io.write( " <" + MEMBER_TAG + " " ); io.write( PRINCIPAL ); io.write( "=\"" + StringEscapeUtils.escapeXml(member.getName()) + "\" " ); io.write( "/>\n" ); } // Close tag io.write( " </" + GROUP_TAG + ">\n" ); } io.write( "</groups>" ); io.close(); } catch( IOException e ) { throw new WikiSecurityException( e.getLocalizedMessage() ); } // Copy new file over old version File backup = new File( m_file.getAbsolutePath() + ".old" ); if ( backup.exists() ) { if ( !backup.delete() ) { log.error( "Could not delete old group database backup: " + backup ); } } if ( !m_file.renameTo( backup ) ) { log.error( "Could not create group database backup: " + backup ); } if ( !newFile.renameTo( m_file ) ) { log.error( "Could not save database: " + backup + " restoring backup." ); if ( !backup.renameTo( m_file ) ) { log.error( "Restore failed. Check the file permissions." ); } log.error( "Could not save database: " + m_file + ". Check the file permissions" ); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -