Search in sources :

Example 1 with PersistenceException

use of cn.edu.zju.acm.onlinejudge.persistence.PersistenceException in project zoj by licheng.

the class JudgeClientJudgeThread method process.

private void process() throws IOException, InterruptedException {
    this.status = Status.CONNECTING;
    this.socket = new Socket();
    this.socket.setKeepAlive(true);
    this.socket.setSoTimeout(JudgeClient.READ_TIMEOUT);
    this.logger.info("Connecting to " + this.address.getAddress().getCanonicalHostName() + ":" + this.address.getPort());
    this.socket.connect(this.address, JudgeClient.CONNECTION_TIMEOUT);
    this.logger.info("Connected");
    this.in = new DataInputStream(this.socket.getInputStream());
    this.out = new DataOutputStream(this.socket.getOutputStream());
    this.status = Status.RUNNING;
    try {
        while (!this.isInterrupted()) {
            try {
                this.status = Status.WAITING;
                if (this.submissionQueueReader == null) {
                    CompoundSubmissionFilter submissionFilter = new CompoundSubmissionFilter();
                    submissionFilter.add(new SimpleSubmissionFilter(new NegationTest(new LanguageTest(this.client.getSupportedLanguages())), Priority.DENY));
                    submissionFilter.add(this.submissionFilter);
                    submissionFilter.add(this.client.getSubmissionFilter());
                    submissionFilter.add(this.client.getService().getSubmissionFilter());
                    this.submissionQueueReader = this.client.getService().getSubmissionQueue().getReader(submissionFilter);
                }
                // IMPORTANT: set to null here to avoid rejudging this one when queue.poll throws a
                // PersistenceException
                this.submission = null;
                this.submission = this.submissionQueueReader.poll(this);
                this.client.getService().judgeStart(this.submission);
                this.status = Status.RUNNING;
                try {
                    this.judge(this.submission);
                } catch (JudgeServerErrorException e) {
                    this.logger.error(e);
                    this.submission.setJudgeReply(JudgeReply.JUDGE_INTERNAL_ERROR);
                } catch (JudgeClientErrorException e) {
                    this.logger.error(e);
                    this.submission.setJudgeReply(JudgeReply.JUDGE_INTERNAL_ERROR);
                } catch (PersistenceException e) {
                    this.logger.error(e);
                    this.submission.setJudgeReply(JudgeReply.JUDGE_INTERNAL_ERROR);
                }
                this.submissionDAO.updateSubmission(this.submission, 1);
                this.submission.setContent(null);
            } catch (PersistenceException e) {
                this.client.getService().judge(this.submission, Priority.HIGH);
                Thread.sleep(60000);
            } finally {
                this.client.getService().judgeDone(this.submission);
            }
        }
    } finally {
        Utility.closeSocket(this.socket);
    }
}
Also used : SimpleSubmissionFilter(cn.edu.zju.acm.onlinejudge.judgeservice.submissionfilter.SimpleSubmissionFilter) DataOutputStream(java.io.DataOutputStream) LanguageTest(cn.edu.zju.acm.onlinejudge.judgeservice.submissiontest.LanguageTest) PersistenceException(cn.edu.zju.acm.onlinejudge.persistence.PersistenceException) DataInputStream(java.io.DataInputStream) CompoundSubmissionFilter(cn.edu.zju.acm.onlinejudge.judgeservice.submissionfilter.CompoundSubmissionFilter) NegationTest(cn.edu.zju.acm.onlinejudge.judgeservice.submissiontest.NegationTest) Socket(java.net.Socket)

Example 2 with PersistenceException

use of cn.edu.zju.acm.onlinejudge.persistence.PersistenceException in project zoj by licheng.

the class AuthorizationPersistenceImpl method manageRoleUsers.

private Map<String, Boolean> manageRoleUsers(List<String> users, long roleId, boolean remove) throws PersistenceException {
    Map<String, Boolean> result = new HashMap<String, Boolean>();
    if (users.size() == 0) {
        return result;
    }
    Connection conn = null;
    Map<Long, String> idMap = new HashMap<Long, String>();
    try {
        conn = Database.createConnection();
        conn.setAutoCommit(false);
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("SELECT handle, user_profile_id FROM user_profile where handle IN " + Database.createValues(users));
            rs = ps.executeQuery();
            while (rs.next()) {
                String handle = rs.getString(1);
                long id = rs.getLong(2);
                idMap.put(id, handle);
                result.put(handle, !remove);
            }
        } finally {
            Database.dispose(ps);
        }
        List<Long> existingIds = new ArrayList<Long>();
        if (idMap.size() > 0) {
            try {
                ps = conn.prepareStatement("SELECT user_profile_id FROM user_role " + "where role_id = ? AND user_profile_id IN " + Database.createNumberValues(idMap.keySet()));
                ps.setLong(1, roleId);
                rs = ps.executeQuery();
                while (rs.next()) {
                    long id = rs.getLong(1);
                    result.put(idMap.get(id), remove);
                    existingIds.add(id);
                }
            } finally {
                Database.dispose(ps);
            }
        }
        if (remove) {
            if (existingIds.size() > 0) {
                try {
                    ps = conn.prepareStatement("DELETE FROM user_role " + "WHERE role_id=? AND user_profile_id IN " + Database.createNumberValues(existingIds));
                    ps.setLong(1, roleId);
                    ps.executeUpdate();
                } finally {
                    Database.dispose(ps);
                }
            }
        } else {
            for (Long id : idMap.keySet()) {
                String handle = idMap.get(id);
                if (result.get(handle)) {
                    try {
                        ps = conn.prepareStatement("INSERT INTO user_role(role_id, user_profile_id) VALUES(?,?)");
                        ps.setLong(1, roleId);
                        ps.setLong(2, id);
                        ps.executeUpdate();
                    } finally {
                        Database.dispose(ps);
                    }
                }
            }
        }
        conn.commit();
    } catch (SQLException e) {
        Database.rollback(conn);
        throw new PersistenceException("Failed to manage role users.", e);
    } finally {
        Database.dispose(conn);
    }
    return result;
}
Also used : HashMap(java.util.HashMap) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) ResultSet(java.sql.ResultSet) PersistenceException(cn.edu.zju.acm.onlinejudge.persistence.PersistenceException)

Example 3 with PersistenceException

use of cn.edu.zju.acm.onlinejudge.persistence.PersistenceException in project zoj by licheng.

the class AuthorizationPersistenceImpl method addUserRole.

public void addUserRole(long userProfileId, long roleId) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement("INSERT INTO user_role(user_profile_id, role_id) VALUES(?,?)");
            ps.setLong(1, userProfileId);
            ps.setLong(2, roleId);
            ps.executeUpdate();
        } finally {
            Database.dispose(ps);
        }
    } catch (SQLException e) {
        throw new PersistenceException("Failed to add user role", e);
    } finally {
        Database.dispose(conn);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) PersistenceException(cn.edu.zju.acm.onlinejudge.persistence.PersistenceException) PreparedStatement(java.sql.PreparedStatement)

Example 4 with PersistenceException

use of cn.edu.zju.acm.onlinejudge.persistence.PersistenceException in project zoj by licheng.

the class AuthorizationPersistenceImpl method getContestRoles.

/**
     * <p>
     * Gets all roles of given contest from persistence layer.
     * </p>
     * 
     * @return a list of RoleSecurity instances
     * @throws PersistenceException
     *             wrapping a persistence implementation specific exception
     */
public List<RoleSecurity> getContestRoles(long contestId) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        PreparedStatement ps = null;
        try {
            // select the roles;
            ps = conn.prepareStatement("SELECT role_id, name, description FROM role WHERE role_id IN (SELECT role_id FROM contest_permission WHERE contest_id=? AND permission_level_id>1)");
            ps.setLong(1, contestId);
            ResultSet rs = ps.executeQuery();
            List<RoleSecurity> roles = new ArrayList<RoleSecurity>();
            while (rs.next()) {
                RoleSecurity role = new RoleSecurity(rs.getLong(1), rs.getString(2), rs.getString(3));
                roles.add(role);
            }
            return roles;
        } finally {
            Database.dispose(ps);
        }
    } catch (SQLException e) {
        throw new PersistenceException("Failed to get all roles", e);
    } finally {
        Database.dispose(conn);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) PersistenceException(cn.edu.zju.acm.onlinejudge.persistence.PersistenceException) PreparedStatement(java.sql.PreparedStatement) RoleSecurity(cn.edu.zju.acm.onlinejudge.security.RoleSecurity)

Example 5 with PersistenceException

use of cn.edu.zju.acm.onlinejudge.persistence.PersistenceException in project zoj by licheng.

the class AuthorizationPersistenceImpl method deleteUserRole.

public void deleteUserRole(long userProfileId, long roleId) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement("DELETE FROM user_role WHERE user_profile_id=? AND role_id=?");
            ps.setLong(1, userProfileId);
            ps.setLong(2, roleId);
            ps.executeUpdate();
        } finally {
            Database.dispose(ps);
        }
    } catch (SQLException e) {
        throw new PersistenceException("Failed to delete user role", e);
    } finally {
        Database.dispose(conn);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) PersistenceException(cn.edu.zju.acm.onlinejudge.persistence.PersistenceException) PreparedStatement(java.sql.PreparedStatement)

Aggregations

PersistenceException (cn.edu.zju.acm.onlinejudge.persistence.PersistenceException)90 Connection (java.sql.Connection)87 PreparedStatement (java.sql.PreparedStatement)87 SQLException (java.sql.SQLException)87 ResultSet (java.sql.ResultSet)55 Timestamp (java.sql.Timestamp)29 Date (java.util.Date)28 ArrayList (java.util.ArrayList)25 Limit (cn.edu.zju.acm.onlinejudge.bean.Limit)7 Language (cn.edu.zju.acm.onlinejudge.bean.enumeration.Language)6 Problem (cn.edu.zju.acm.onlinejudge.bean.Problem)5 Forum (cn.edu.zju.acm.onlinejudge.bean.Forum)4 Submission (cn.edu.zju.acm.onlinejudge.bean.Submission)4 UserProfile (cn.edu.zju.acm.onlinejudge.bean.UserProfile)4 RoleSecurity (cn.edu.zju.acm.onlinejudge.security.RoleSecurity)4 HashMap (java.util.HashMap)4 PersistenceCreationException (cn.edu.zju.acm.onlinejudge.persistence.PersistenceCreationException)3 AbstractContest (cn.edu.zju.acm.onlinejudge.bean.AbstractContest)2 Configuration (cn.edu.zju.acm.onlinejudge.bean.Configuration)2 Post (cn.edu.zju.acm.onlinejudge.bean.Post)2