Example usage for org.apache.commons.dbutils DbUtils commitAndClose

List of usage examples for org.apache.commons.dbutils DbUtils commitAndClose

Introduction

In this page you can find the example usage for org.apache.commons.dbutils DbUtils commitAndClose.

Prototype

public static void commitAndClose(Connection conn) throws SQLException 

Source Link

Document

Commits a Connection then closes it, avoid closing if null.

Usage

From source file:org.gaixie.jibu.security.service.AuthorityServiceTest.java

@Before
public void setup() throws Exception {
    authService = getInjector().getInstance(AuthorityService.class);
    roleService = getInjector().getInstance(RoleService.class);
    userService = getInjector().getInstance(UserService.class);

    userService.add(new User("Administrator", "admin", "123456", "jibu.gaixie@gmail.com", true));

    authService.add(new Authority("sec.ast-v-auth1", "/ast-v-auth1.z"));
    authService.add(new Authority("sec.ast-v-auth2", "/ast-v-auth2.z"));
    authService.add(new Authority("sec.ast-v-auth3", "/ast-v-auth3.z"));
    // ??(value.length <5)??????
    authService.add(new Authority("ci", "/ast-v-auth1.z?ci=addUser"));
    userService.add(new User("ast-v-user1", "ast-v-user1", "123456", "ast-v-user1@x.xxx", true));

    /*/*from   w w w  . j a va  2 s. co  m*/
     * 
     * ROLE_BASE
     *     |-----ROLE_ADMIN
     *     |
     *     |-----ast-v-role1
     *     |          |------ast-v-role2
     *     |-----ast-v-role3
     */
    // ? Role 
    Connection conn = ConnectionUtils.getConnection();
    QueryRunner run = new QueryRunner();
    run.update(conn, "INSERT INTO roles (name,description,lft,rgt) values ('ROLE_BASE','ROLE_BASE',1,2)");
    DbUtils.commitAndClose(conn);

    Role parent = roleService.get("ROLE_BASE");
    roleService.add(new Role("ROLE_ADMIN", "ROLE_ADMIN"), parent);
    roleService.add(new Role("ast-v-role1", "ast-v-role1"), parent);
    roleService.add(new Role("ast-v-role3", "ast-v-role3"), parent);
    parent = roleService.get("ast-v-role1");
    roleService.add(new Role("ast-v-role2", "ast-v-role2"), parent);

    /* ????
     * ROLE_BASE 
     *     |-----ROLE_ADMIN
     *     |
     *     |-----ast-v-role1 <==> (/ast-v-auth1.z ) (/ast-v-auth1.z?ci=addUser)
     *     |          |------ast-v-role2 
     *     |-----ast-v-role3 <==> (/ast-v-auth2.z )
     */
    Authority auth = authService.get("/ast-v-auth1.z");
    Role role = roleService.get("ast-v-role1");
    roleService.bind(role, auth);
    auth = authService.get("/ast-v-auth1.z?ci=addUser");
    roleService.bind(role, auth);
    auth = authService.get("/ast-v-auth2.z");
    role = roleService.get("ast-v-role3");
    roleService.bind(role, auth);

    /* ast-v-role2, admin  ROLE_ADMIN
     * ROLE_BASE
     *     |-----ROLE_ADMIN <==> (admin)
     *     |
     *     |-----ast-v-role1
     *     |          |------ast-v-role2 <==> (ast-v-user1)
     *     |-----ast-v-role3 
     */
    role = roleService.get("ast-v-role2");
    User user = userService.get("ast-v-user1");
    roleService.bind(role, user);
    role = roleService.get("ROLE_ADMIN");
    user = userService.get("admin");
    roleService.bind(role, user);
}

From source file:org.gaixie.jibu.security.service.AuthorityServiceTest.java

@After
public void tearDown() {
    Connection conn = null;// www  .jav  a  2 s. co m
    try {
        conn = ConnectionUtils.getConnection();
        QueryRunner run = new QueryRunner();
        run.update(conn, "DELETE from role_authority_map ");
        run.update(conn, "DELETE from user_role_map");
        run.update(conn, "DELETE from roles");
        run.update(conn, "DELETE from authorities ");
        run.update(conn, "DELETE from userbase");
        DbUtils.commitAndClose(conn);
    } catch (SQLException e) {
        DbUtils.rollbackAndCloseQuietly(conn);
        System.out.println(e.getMessage());
    }
}

From source file:org.gaixie.jibu.security.service.impl.AuthorityServiceImpl.java

/**
 * {@inheritDoc}//from  ww  w.java  2s  .  c om
 * <p>
 * ? Cache  authorities  value
 * @see #getAll()
 */
public void add(Authority auth) throws JibuException {
    Connection conn = null;
    try {
        conn = ConnectionUtils.getConnection();
        authDAO.save(conn, auth);
        DbUtils.commitAndClose(conn);
        Cache cache = CacheUtils.getAuthCache();
        cache.remove("authorities");
    } catch (SQLException e) {
        DbUtils.rollbackAndCloseQuietly(conn);
        throw new JibuException(e.getMessage());
    }
}

From source file:org.gaixie.jibu.security.service.impl.AuthorityServiceImpl.java

/**
 * {@inheritDoc}/*from  w  w w. java  2 s. c  om*/
 * <p>
 * ? Cache  authorities  value
 * @see #getAll()
 */
public void delete(Authority auth) throws JibuException {
    Connection conn = null;
    try {
        conn = ConnectionUtils.getConnection();
        authDAO.delete(conn, auth);
        DbUtils.commitAndClose(conn);
        Cache cache = CacheUtils.getAuthCache();
        cache.remove("authorities");
    } catch (SQLException e) {
        DbUtils.rollbackAndCloseQuietly(conn);
        throw new JibuException(e.getMessage());
    }
}

From source file:org.gaixie.jibu.security.service.impl.AuthorityServiceImpl.java

/**
 * {@inheritDoc}// w w w . j  a  va 2 s .  co  m
 * <p>
 * ? Cache  authorities  value 
 * key = auth.getValue() ?
 * @see #getAll()
 */
public void update(Authority auth) throws JibuException {
    Connection conn = null;
    try {
        conn = ConnectionUtils.getConnection();
        Authority old = authDAO.get(conn, auth.getId());
        authDAO.update(conn, auth);
        DbUtils.commitAndClose(conn);
        Cache cache = CacheUtils.getAuthCache();
        cache.remove("authorities");
        cache.remove(old.getValue());
    } catch (SQLException e) {
        DbUtils.rollbackAndCloseQuietly(conn);
        throw new JibuException(e.getMessage());
    }
}

From source file:org.gaixie.jibu.security.service.impl.LoginServiceImpl.java

public Token generateToken(String username) {
    Connection conn = null;//from   ww w  .  ja  va  2  s  .  co m
    Token token = null;
    try {
        conn = ConnectionUtils.getConnection();
        User user = userDAO.get(conn, username);
        if (user == null)
            return null;

        Random randomGenerator = new Random();
        long randomLong = randomGenerator.nextLong();

        //            Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        //calendar.setTime(date);
        calendar.add(calendar.DAY_OF_MONTH, +1);
        long time = calendar.getTimeInMillis();
        Timestamp ts = new Timestamp(time);
        String key = MD5.encodeString(Long.toString(randomLong) + time, null);
        token = new Token(key, "password", ts, user.getId());

        tokenDAO.save(conn, token);
        DbUtils.commitAndClose(conn);
    } catch (SQLException e) {
        DbUtils.rollbackAndCloseQuietly(conn);
        logger.error(e.getMessage());
        return null;
    } finally {
        DbUtils.closeQuietly(conn);
    }
    return token;
}

From source file:org.gaixie.jibu.security.service.impl.LoginServiceImpl.java

public void resetPassword(String tokenValue, String password) throws JibuException {
    Connection conn = null;//ww w.  j  av a 2 s.  c o  m
    try {
        conn = ConnectionUtils.getConnection();
        Token token = tokenDAO.get(conn, tokenValue);

        if (token == null)
            throw new TokenException("Token is null");
        Calendar calendar = Calendar.getInstance();
        long time = calendar.getTimeInMillis();
        Timestamp ts = new Timestamp(time);
        if (ts.after(token.getExpiration()))
            throw new TokenException("Token expired.");
        if (password == null || password.isEmpty()) {
            throw new JibuException("Password is invalid.");
        }

        User user = new User();
        String cryptpassword = MD5.encodeString(password, null);
        user.setPassword(cryptpassword);
        user.setId(token.getUser_id());
        userDAO.update(conn, user);
        DbUtils.commitAndClose(conn);
    } catch (SQLException e) {
        DbUtils.rollbackAndCloseQuietly(conn);
        throw new JibuException(e.getMessage());
    } finally {
        DbUtils.closeQuietly(conn);
    }

}

From source file:org.gaixie.jibu.security.service.impl.RoleServiceImpl.java

public void add(Role role, Role parent) throws JibuException {
    Connection conn = null;//from   w  w w.  jav a  2s.  c  o m
    try {
        conn = ConnectionUtils.getConnection();
        roleDAO.save(conn, role, parent);
        DbUtils.commitAndClose(conn);
    } catch (SQLException e) {
        DbUtils.rollbackAndCloseQuietly(conn);
        throw new JibuException(e.getMessage());
    }
}

From source file:org.gaixie.jibu.security.service.impl.RoleServiceImpl.java

public void delete(Role role) throws JibuException {
    Connection conn = null;//from   w w w.  j  av  a 2  s  . c om
    try {
        conn = ConnectionUtils.getConnection();
        role = roleDAO.get(conn, role.getId());
        if ((role.getRgt() - role.getLft()) > 1)
            throw new RoleException("The role is not leaf node.");
        roleDAO.delete(conn, role);
        DbUtils.commitAndClose(conn);
    } catch (SQLException e) {
        DbUtils.rollbackAndCloseQuietly(conn);
        throw new JibuException(e.getMessage());
    } finally {
        DbUtils.closeQuietly(conn);
    }

}

From source file:org.gaixie.jibu.security.service.impl.RoleServiceImpl.java

/**
 * {@inheritDoc}/*from  www .  j a va 2  s.  c  o  m*/
 * <p>
 *  Role??? Cache ??
 */
public void update(Role role) throws JibuException {
    Connection conn = null;
    try {
        conn = ConnectionUtils.getConnection();
        // ?
        role.setLft(null);
        role.setRgt(null);
        roleDAO.update(conn, role);
        DbUtils.commitAndClose(conn);
        CacheUtils.getAuthCache().clear();
        CacheUtils.getUserCache().clear();
    } catch (SQLException e) {
        DbUtils.rollbackAndCloseQuietly(conn);
        throw new JibuException(e.getMessage());
    }
}