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);
}
}
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;
}
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);
}
}
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);
}
}
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);
}
}
Aggregations