Search in sources :

Example 1 with AbstractJobSpecification

use of org.haiku.haikudepotserver.job.model.AbstractJobSpecification in project haikudepotserver by haiku.

the class PkgJobApiImpl method queueSimplePkgJob.

private <R extends AbstractQueueJobResult> R queueSimplePkgJob(Class<R> resultClass, Class<? extends AbstractJobSpecification> jobSpecificationClass, Permission permission) {
    final ObjectContext context = serverRuntime.newContext();
    Optional<User> user = tryObtainAuthenticatedUser(context);
    if (user.isEmpty()) {
        throw new AccessDeniedException("attempt to queue [" + jobSpecificationClass.getSimpleName() + "] without a user");
    }
    if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), null, permission)) {
        throw new AccessDeniedException("attempt to queue [" + jobSpecificationClass.getSimpleName() + "] without sufficient authorization");
    }
    AbstractJobSpecification spec;
    try {
        spec = jobSpecificationClass.newInstance();
    } catch (InstantiationException | IllegalAccessException e) {
        throw new RuntimeException("unable to create the job specification for class; " + jobSpecificationClass.getSimpleName(), e);
    }
    spec.setOwnerUserNickname(user.get().getNickname());
    R result;
    try {
        result = resultClass.newInstance();
    } catch (InstantiationException | IllegalAccessException e) {
        throw new RuntimeException("unable to create the result; " + resultClass.getSimpleName(), e);
    }
    result.guid = jobService.submit(spec, JobSnapshot.COALESCE_STATUSES_QUEUED_STARTED);
    return result;
}
Also used : AccessDeniedException(org.springframework.security.access.AccessDeniedException) org.haiku.haikudepotserver.dataobjects.auto._User(org.haiku.haikudepotserver.dataobjects.auto._User) User(org.haiku.haikudepotserver.dataobjects.User) ObjectContext(org.apache.cayenne.ObjectContext) AbstractJobSpecification(org.haiku.haikudepotserver.job.model.AbstractJobSpecification)

Aggregations

ObjectContext (org.apache.cayenne.ObjectContext)1 User (org.haiku.haikudepotserver.dataobjects.User)1 org.haiku.haikudepotserver.dataobjects.auto._User (org.haiku.haikudepotserver.dataobjects.auto._User)1 AbstractJobSpecification (org.haiku.haikudepotserver.job.model.AbstractJobSpecification)1 AccessDeniedException (org.springframework.security.access.AccessDeniedException)1