Search in sources :

Example 51 with RepositoryException

use of org.platformlayer.RepositoryException in project platformlayer by platformlayer.

the class JdbcJobRepository method actionFromXml.

private Action actionFromXml(String actionXml) throws RepositoryException {
    Object o;
    try {
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        o = unmarshaller.unmarshal(new StringReader(actionXml));
    } catch (JAXBException e) {
        throw new RepositoryException("Error deserializing action", e);
    }
    return Casts.checkedCast(o, Action.class);
}
Also used : JAXBException(javax.xml.bind.JAXBException) StringReader(java.io.StringReader) RepositoryException(org.platformlayer.RepositoryException) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 52 with RepositoryException

use of org.platformlayer.RepositoryException in project platformlayer by platformlayer.

the class JdbcJobRepository method listRecentJobs.

// @Override
// @JdbcTransaction
// public List<JobExecutionData> listRecentExecutions(JobQuery jobQuery) throws RepositoryException {
// DbHelper db = new DbHelper();
// try {
// // We use JoinedQueryResult because we have a compound PK (projectId / jobId)
// // and JPA makes this really complicated.
//
// ProjectId projectId = jobQuery.project;
// Preconditions.checkNotNull(projectId);
// int project = db.mapToValue(projectId);
//
// Long maxAge = null;
// if (jobQuery.maxAge != null) {
// maxAge = jobQuery.maxAge.getTotalSeconds();
// }
//
// Integer limit = jobQuery.limit;
// String filterTarget = jobQuery.target != null ? jobQuery.target.getUrl() : null;
//
// JoinedQueryResult results = db.queries.listRecentExecutions(project, maxAge, filterTarget, limit);
//
// List<JobExecutionData> ret = Lists.newArrayList();
// Map<String, JobData> jobs = Maps.newHashMap();
//
// for (JobEntity job : results.getAll(JobEntity.class)) {
// ManagedItemId jobId = new ManagedItemId(job.jobId);
// PlatformLayerKey jobKey = JobData.buildKey(projectId, jobId);
// jobs.put(job.jobId, mapFromEntity(job, jobKey));
// }
//
// for (JobExecutionEntity execution : results.getAll(JobExecutionEntity.class)) {
// JobData jobData = jobs.get(execution.jobId);
// if (jobData == null) {
// throw new IllegalStateException();
// }
//
// ManagedItemId jobId = new ManagedItemId(execution.jobId);
// PlatformLayerKey jobKey = JobData.buildKey(projectId, jobId);
// JobExecutionData run = mapFromEntity(execution, jobKey);
// run.job = jobData;
// ret.add(run);
// }
//
// sort(ret);
//
// return ret;
// } catch (SQLException e) {
// throw new RepositoryException("Error listing job executions", e);
// } finally {
// db.close();
// }
// }
@Override
@JdbcTransaction
public List<JobData> listRecentJobs(JobQuery jobQuery) throws RepositoryException {
    DbHelper db = new DbHelper();
    try {
        ProjectId projectId = jobQuery.project;
        Preconditions.checkNotNull(projectId);
        int project = db.mapToValue(projectId);
        Long maxAge = null;
        if (jobQuery.maxAge != null) {
            maxAge = jobQuery.maxAge.getTotalSeconds();
        }
        Integer limit = jobQuery.limit;
        String filterTarget = jobQuery.target != null ? jobQuery.target.getUrl() : null;
        // TODO: Include currently running jobs...
        List<JobEntity> results = db.queries.listRecentJobs(project, maxAge, filterTarget, limit);
        List<JobData> ret = Lists.newArrayList();
        for (JobEntity job : results) {
            ManagedItemId jobId = new ManagedItemId(job.jobId);
            PlatformLayerKey jobKey = JobData.buildKey(projectId, jobId);
            JobData jobData = mapFromEntity(job, jobKey);
            ret.add(jobData);
        }
        sortJobs(ret);
        return ret;
    } catch (SQLException e) {
        throw new RepositoryException("Error listing job executions", e);
    } finally {
        db.close();
    }
}
Also used : SQLException(java.sql.SQLException) ProjectId(org.platformlayer.ids.ProjectId) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey) RepositoryException(org.platformlayer.RepositoryException) ManagedItemId(org.platformlayer.ids.ManagedItemId) JobData(org.platformlayer.jobs.model.JobData) JdbcTransaction(com.fathomdb.jdbc.JdbcTransaction)

Example 53 with RepositoryException

use of org.platformlayer.RepositoryException in project platformlayer by platformlayer.

the class PersistentJobRegistry method enqueueOperation.

@Override
public JobData enqueueOperation(Action action, ProjectAuthorization auth, PlatformLayerKey targetItem) throws OpsException {
    ProjectId projectId;
    try {
        projectId = opsContextBuilder.getRunAsProjectId(auth);
    } catch (OpsException e) {
        throw new OpsException("Error getting projectId", e);
    }
    JobData jobData = new JobData();
    jobData.action = action;
    jobData.targetId = targetItem;
    try {
        String jobId = repository.insertJob(projectId, jobData);
        jobData.key = JobData.buildKey(projectId, new ManagedItemId(jobId));
    } catch (RepositoryException e) {
        throw new OpsException("Error inserting job", e);
    }
    operationQueue.submit(auth, jobData);
    return jobData;
}
Also used : OpsException(org.platformlayer.ops.OpsException) ProjectId(org.platformlayer.ids.ProjectId) RepositoryException(org.platformlayer.RepositoryException) JobData(org.platformlayer.jobs.model.JobData) ManagedItemId(org.platformlayer.ids.ManagedItemId)

Example 54 with RepositoryException

use of org.platformlayer.RepositoryException in project platformlayer by platformlayer.

the class MetricsResource method getMetrics.

@POST
@Produces({ XML, JSON })
public MetricDataSource getMetrics(final MetricQuery query) throws RepositoryException, OpsException {
    final ItemBase managedItem = getManagedItem();
    final ServiceProvider serviceProvider = getServiceProvider();
    OpsContextBuilder opsContextBuilder = objectInjector.getInstance(OpsContextBuilder.class);
    final OpsContext opsContext = opsContextBuilder.buildTemporaryOpsContext(getServiceType(), getProjectAuthorization());
    return OpsContext.runInContext(opsContext, new CheckedCallable<MetricDataSource, Exception>() {

        @Override
        public MetricDataSource call() throws Exception {
            BindingScope bindingScope = BindingScope.push(managedItem, managedItem);
            try {
                MetricDataSource metrics = serviceProvider.getMetricValues(managedItem, query);
                return metrics;
            } finally {
                bindingScope.pop();
            }
        }
    });
}
Also used : MetricDataSource(org.platformlayer.metrics.model.MetricDataSource) ItemBase(org.platformlayer.core.model.ItemBase) ServiceProvider(org.platformlayer.xaas.services.ServiceProvider) OpsContext(org.platformlayer.ops.OpsContext) RepositoryException(org.platformlayer.RepositoryException) OpsException(org.platformlayer.ops.OpsException) OpsContextBuilder(org.platformlayer.ops.tasks.OpsContextBuilder) BindingScope(org.platformlayer.ops.BindingScope) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces)

Example 55 with RepositoryException

use of org.platformlayer.RepositoryException in project platformlayer by platformlayer.

the class KeystoneRepositoryAuthenticator method getUserFromToken.

@Override
public UserEntity getUserFromToken(String userIdString, byte[] tokenSecret) throws AuthenticatorException {
    int userId;
    try {
        userId = Integer.parseInt(userIdString);
    } catch (NumberFormatException e) {
        throw new AuthenticatorException("Invalid user id", e);
    }
    if (tokenSecret.length < 1) {
        throw new IllegalArgumentException();
    }
    CryptoKey userSecret = authenticationSecrets.decryptSecretFromToken(tokenSecret);
    if (userSecret == null) {
        throw new AuthenticatorException("Authentication timed out");
    }
    UserEntity user;
    try {
        user = repository.findUserById(userId);
    } catch (RepositoryException e) {
        throw new AuthenticatorException("Error while authenticating user", e);
    }
    user.unlock(userSecret);
    if (user.isLocked()) {
        return null;
    }
    return user;
}
Also used : CryptoKey(com.fathomdb.crypto.CryptoKey) AuthenticatorException(org.platformlayer.auth.AuthenticatorException) RepositoryException(org.platformlayer.RepositoryException) UserEntity(org.platformlayer.auth.UserEntity)

Aggregations

RepositoryException (org.platformlayer.RepositoryException)56 JdbcTransaction (com.fathomdb.jdbc.JdbcTransaction)30 SQLException (java.sql.SQLException)30 OpsException (org.platformlayer.ops.OpsException)18 ProjectId (org.platformlayer.ids.ProjectId)14 ItemBase (org.platformlayer.core.model.ItemBase)10 CryptoKey (com.fathomdb.crypto.CryptoKey)8 PlatformLayerKey (org.platformlayer.core.model.PlatformLayerKey)7 ManagedItemId (org.platformlayer.ids.ManagedItemId)7 OpsContext (org.platformlayer.ops.OpsContext)7 ServiceProvider (org.platformlayer.xaas.services.ServiceProvider)7 IOException (java.io.IOException)6 AesCryptoKey (com.fathomdb.crypto.AesCryptoKey)5 PublicKey (java.security.PublicKey)4 JAXBException (javax.xml.bind.JAXBException)4 Tag (org.platformlayer.core.model.Tag)4 ServiceType (org.platformlayer.ids.ServiceType)4 JobData (org.platformlayer.jobs.model.JobData)4 JoinedQueryResult (com.fathomdb.jpa.impl.JoinedQueryResult)3 X509Certificate (java.security.cert.X509Certificate)3