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

📄 jdbcgroupdatabase.java

📁 jspwiki source code,jspwiki source code
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*     JSPWiki - a JSP-based WikiWiki clone.    Licensed to the Apache Software Foundation (ASF) under one    or more contributor license agreements.  See the NOTICE file    distributed with this work for additional information    regarding copyright ownership.  The ASF licenses this file    to you under the Apache License, Version 2.0 (the    "License"); you may not use this file except in compliance    with the License.  You may obtain a copy of the License at       http://www.apache.org/licenses/LICENSE-2.0    Unless required by applicable law or agreed to in writing,    software distributed under the License is distributed on an    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY    KIND, either express or implied.  See the License for the    specific language governing permissions and limitations    under the License.   */package com.ecyrd.jspwiki.auth.authorize;import java.security.Principal;import java.sql.*;import java.util.*;import java.util.Date;import javax.naming.Context;import javax.naming.InitialContext;import javax.naming.NamingException;import javax.sql.DataSource;import org.apache.log4j.Logger;import com.ecyrd.jspwiki.NoRequiredPropertyException;import com.ecyrd.jspwiki.WikiEngine;import com.ecyrd.jspwiki.auth.NoSuchPrincipalException;import com.ecyrd.jspwiki.auth.WikiPrincipal;import com.ecyrd.jspwiki.auth.WikiSecurityException;/** * <p> * Implementation of GroupDatabase that persists {@link Group} objects to a JDBC * DataSource, as might typically be provided by a web container. This * implementation looks up the JDBC DataSource using JNDI. The JNDI name of the * datasource, backing table and mapped columns used by this class are * configured via settings in <code>jspwiki.properties</code>. * </p> * <p> * Configurable properties are these: * </p> * <table> * <tr> <thead> * <th>Property</th> * <th>Default</th> * <th>Definition</th> * <thead> </tr> * <tr> * <td><code>jspwiki.groupdatabase.datasource</code></td> * <td><code>jdbc/GroupDatabase</code></td> * <td>The JNDI name of the DataSource</td> * </tr> * <tr> * <td><code>jspwiki.groupdatabase.table</code></td> * <td><code>groups</code></td> * <td>The table that stores the groups</td> * </tr> * <tr> * <td><code>jspwiki.groupdatabase.membertable</code></td> * <td><code>group_members</code></td> * <td>The table that stores the names of group members</td> * </tr> * <tr> * <td><code>jspwiki.groupdatabase.created</code></td> * <td><code>created</code></td> * <td>The column containing the group's creation timestamp</td> * </tr> * <tr> * <td><code>jspwiki.groupdatabase.creator</code></td> * <td><code>creator</code></td> * <td>The column containing the group creator's name</td> * </tr> * <tr> * <td><code>jspwiki.groupdatabase.name</code></td> * <td><code>name</code></td> * <td>The column containing the group's name</td> * </tr> * <tr> * <td><code>jspwiki.groupdatabase.member</code></td> * <td><code>member</code></td> * <td>The column containing the group member's name</td> * </tr> * <tr> * <td><code>jspwiki.groupdatabase.modified</code></td> * <td><code>modified</code></td> * <td>The column containing the group's last-modified timestamp</td> * </tr> * <tr> * <td><code>jspwiki.groupdatabase.modifier</code></td> * <td><code>modifier</code></td> * <td>The column containing the name of the user who last modified the group</td> * </tr> * </table> * <p> * This class is typically used in conjunction with a web container's JNDI * resource factory. For example, Tomcat versions 4 and higher provide a basic * JNDI factory for registering DataSources. To give JSPWiki access to the JNDI * resource named by <code>jdbc/GroupDatabase</code>, you would declare the * datasource resource similar to this: * </p> * <blockquote><code>&lt;Context ...&gt;<br/> *  &nbsp;&nbsp;...<br/> *  &nbsp;&nbsp;&lt;Resource name="jdbc/GroupDatabase" auth="Container"<br/> *  &nbsp;&nbsp;&nbsp;&nbsp;type="javax.sql.DataSource" username="dbusername" password="dbpassword"<br/> *  &nbsp;&nbsp;&nbsp;&nbsp;driverClassName="org.hsql.jdbcDriver" url="jdbc:HypersonicSQL:database"<br/> *  &nbsp;&nbsp;&nbsp;&nbsp;maxActive="8" maxIdle="4"/&gt;<br/> *  &nbsp;...<br/> * &lt;/Context&gt;</code></blockquote> * <p> * JDBC driver JARs should be added to Tomcat's <code>common/lib</code> * directory. For more Tomcat 5.5 JNDI configuration examples, see <a * href="http://tomcat.apache.org/tomcat-5.5-doc/jndi-resources-howto.html"> * http://tomcat.apache.org/tomcat-5.5-doc/jndi-resources-howto.html</a>. * </p> * <p> * JDBCGroupDatabase commits changes as transactions if the back-end database * supports them. If the database supports transactions, group changes are saved * to permanent storage only when the {@link #commit()} method is called. If the * database does <em>not</em> support transactions, then changes are made * immediately (during the {@link #save(Group, Principal)} method), and the * {@linkplain #commit()} method no-ops. Thus, callers should always call the * {@linkplain #commit()} method after saving a profile to guarantee that * changes are applied. * </p> *  * @author Andrew R. Jaquith * @since 2.3 */public class JDBCGroupDatabase implements GroupDatabase{    /** Default column name that stores the JNDI name of the DataSource. */    public static final String DEFAULT_GROUPDB_DATASOURCE = "jdbc/GroupDatabase";    /** Default table name for the table that stores groups. */    public static final String DEFAULT_GROUPDB_TABLE = "groups";    /** Default column name that stores the names of group members. */    public static final String DEFAULT_GROUPDB_MEMBER_TABLE = "group_members";    /** Default column name that stores the the group creation timestamps. */    public static final String DEFAULT_GROUPDB_CREATED = "created";    /** Default column name that stores group creator names. */    public static final String DEFAULT_GROUPDB_CREATOR = "creator";    /** Default column name that stores the group names. */    public static final String DEFAULT_GROUPDB_NAME = "name";    /** Default column name that stores group member names. */    public static final String DEFAULT_GROUPDB_MEMBER = "member";    /** Default column name that stores group last-modified timestamps. */    public static final String DEFAULT_GROUPDB_MODIFIED = "modified";    /** Default column name that stores names of users who last modified groups. */    public static final String DEFAULT_GROUPDB_MODIFIER = "modifier";    /** The JNDI name of the DataSource. */    public static final String PROP_GROUPDB_DATASOURCE = "jspwiki.groupdatabase.datasource";    /** The table that stores the groups. */    public static final String PROP_GROUPDB_TABLE = "jspwiki.groupdatabase.table";    /** The table that stores the names of group members. */    public static final String PROP_GROUPDB_MEMBER_TABLE = "jspwiki.groupdatabase.membertable";    /** The column containing the group's creation timestamp. */    public static final String PROP_GROUPDB_CREATED = "jspwiki.groupdatabase.created";    /** The column containing the group creator's name. */    public static final String PROP_GROUPDB_CREATOR = "jspwiki.groupdatabase.creator";    /** The column containing the group's name. */    public static final String PROP_GROUPDB_NAME = "jspwiki.groupdatabase.name";    /** The column containing the group member's name. */    public static final String PROP_GROUPDB_MEMBER = "jspwiki.groupdatabase.member";    /** The column containing the group's last-modified timestamp. */    public static final String PROP_GROUPDB_MODIFIED = "jspwiki.groupdatabase.modified";    /** The column containing the name of the user who last modified the group. */    public static final String PROP_GROUPDB_MODIFIER = "jspwiki.groupdatabase.modifier";    protected static final Logger log = Logger.getLogger( JDBCGroupDatabase.class );    private DataSource m_ds = null;    private String m_table = null;    private String m_memberTable = null;    private String m_created = null;    private String m_creator = null;    private String m_name = null;    private String m_member = null;    private String m_modified = null;    private String m_modifier = null;    private String m_findAll = null;    private String m_findGroup = null;    private String m_findMembers = null;    private String m_insertGroup = null;    private String m_insertGroupMembers = null;    private String m_updateGroup = null;    private String m_deleteGroup = null;    private String m_deleteGroupMembers = null;    private boolean m_supportsCommits = false;    private WikiEngine m_engine = null;    /**     * No-op method that in previous versions of JSPWiki was intended to     * atomically commit changes to the user database. Now, the     * {@link #save(Group, Principal)} and {@link #delete(Group)} methods are     * atomic themselves.     *      * @throws WikiSecurityException never...     * @deprecated there is no need to call this method because the save and     *             delete methods contain their own commit logic     */    @SuppressWarnings("deprecation")    public void commit() throws WikiSecurityException    {    }    /**     * Looks up and deletes a {@link Group} from the group database. If the     * group database does not contain the supplied Group. this method throws a     * {@link NoSuchPrincipalException}. The method commits the results of the     * delete to persistent storage.     *      * @param group the group to remove     * @throws WikiSecurityException if the database does not contain the     *             supplied group (thrown as {@link NoSuchPrincipalException})     *             or if the commit did not succeed     */    public void delete( Group group ) throws WikiSecurityException    {        if( !exists( group ) )        {            throw new NoSuchPrincipalException( "Not in database: " + group.getName() );        }        String groupName = group.getName();        Connection conn = null;        try        {            // Open the database connection            conn = m_ds.getConnection();            if( m_supportsCommits )            {                conn.setAutoCommit( false );            }            PreparedStatement ps = conn.prepareStatement( m_deleteGroup );            ps.setString( 1, groupName );            ps.execute();            ps.close();            ps = conn.prepareStatement( m_deleteGroupMembers );            ps.setString( 1, groupName );            ps.execute();            ps.close();            // Commit and close connection            if( m_supportsCommits )            {                conn.commit();            }        }        catch( SQLException e )        {            throw new WikiSecurityException( "Could not delete group " + groupName + ": " + e.getMessage() );        }        finally        {            try            {                if( conn != null ) conn.close();            }            catch( Exception e )            {            }        }    }    /**     * Returns all wiki groups that are stored in the GroupDatabase as an array     * of Group objects. If the database does not contain any groups, this     * method will return a zero-length array. This method causes back-end     * storage to load the entire set of group; thus, it should be called     * infrequently (e.g., at initialization time).     *      * @return the wiki groups     * @throws WikiSecurityException if the groups cannot be returned by the     *             back-end     */    public Group[] groups() throws WikiSecurityException    {        Set<Group> groups = new HashSet<Group>();        Connection conn = null;        try        {            // Open the database connection            conn = m_ds.getConnection();            PreparedStatement ps = conn.prepareStatement( m_findAll );            ResultSet rs = ps.executeQuery();            while ( rs.next() )            {                String groupName = rs.getString( m_name );                if( groupName == null )                {                    log.warn( "Detected null group name in JDBCGroupDataBase. Check your group database." );                }                else                {                    Group group = new Group( groupName, m_engine.getApplicationName() );                    group.setCreated( rs.getTimestamp( m_created ) );                    group.setCreator( rs.getString( m_creator ) );                    group.setLastModified( rs.getTimestamp( m_modified ) );                    group.setModifier( rs.getString( m_modifier ) );                    populateGroup( group );                    groups.add( group );                }            }            ps.close();        }        catch( SQLException e )        {            throw new WikiSecurityException( e.getMessage() );        }        finally        {            try            {

⌨️ 快捷键说明

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