Search in sources :

Example 21 with ProjectId

use of org.platformlayer.ids.ProjectId in project platformlayer by platformlayer.

the class PlatformLayerKey method parse.

public static PlatformLayerKey parse(String s) {
    if (!s.contains("://")) {
        int slashCount = 0;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c == '/') {
                slashCount++;
            }
        }
        String extraSlashes = "";
        if (slashCount < 4) {
            extraSlashes = Strings.repeat("/", 4 - slashCount);
        }
        s = SCHEME + "://" + extraSlashes + s;
    }
    URI uri;
    try {
        uri = new URI(s);
    } catch (URISyntaxException e) {
        throw new IllegalArgumentException("Error parsing URI", e);
    }
    if (!Objects.equal(SCHEME, uri.getScheme())) {
        throw new IllegalArgumentException();
    }
    FederationKey hostKey = null;
    String host = uri.getHost();
    if (!Strings.isNullOrEmpty(host)) {
        hostKey = FederationKey.build(host);
    }
    String path = uri.getPath();
    if (path.startsWith("/")) {
        path = path.substring(1);
    }
    ArrayList<String> components = Lists.newArrayList(Splitter.on('/').split(path));
    if (components.size() < 4) {
        throw new IllegalArgumentException();
    }
    String componentProject = components.get(0);
    ProjectId project = !Strings.isNullOrEmpty(componentProject) ? new ProjectId(componentProject) : null;
    String serviceComponent = components.get(1);
    ServiceType serviceType = !Strings.isNullOrEmpty(serviceComponent) ? new ServiceType(serviceComponent) : null;
    ItemType itemType = new ItemType(components.get(2));
    ManagedItemId itemId = new ManagedItemId(Joiner.on("/").join(components.subList(3, components.size())));
    return new PlatformLayerKey(hostKey, project, serviceType, itemType, itemId);
}
Also used : ItemType(org.platformlayer.ids.ItemType) ProjectId(org.platformlayer.ids.ProjectId) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) ManagedItemId(org.platformlayer.ids.ManagedItemId) FederationKey(org.platformlayer.ids.FederationKey) ServiceType(org.platformlayer.ids.ServiceType)

Example 22 with ProjectId

use of org.platformlayer.ids.ProjectId in project platformlayer by platformlayer.

the class JdbcJobRepository method listExecutions.

// @Override
// @JdbcTransaction
// public void recordJob(PlatformLayerKey jobId, PlatformLayerKey itemKey, JobState jobState, JobLog jobLog)
// throws RepositoryException {
// DbHelper db = new DbHelper();
// try {
// String data;
//
// // TODO: More compact encoding?? XML InfoSet? GZIP?
// try {
// data = JaxbHelper.toXml(jobLog, false);
// } catch (JAXBException e) {
// throw new RepositoryException("Error serializing job log", e);
// }
//
// int updateCount = db.insertJobLog(itemKey.getServiceType(), itemKey.getItemType(), itemKey.getProject(),
// itemKey.getItemId(), jobState, data);
//
// if (updateCount != 1) {
// throw new IllegalStateException("Unexpected number of rows inserted");
// }
// } catch (SQLException e) {
// throw new RepositoryException("Error saving job log", e);
// } finally {
// db.close();
// }
// }
@Override
@JdbcTransaction
public List<JobExecutionData> listExecutions(PlatformLayerKey jobKey) throws RepositoryException {
    DbHelper db = new DbHelper();
    try {
        ProjectId projectId = jobKey.getProject();
        String jobId = jobKey.getItemIdString();
        List<JobExecutionEntity> executions = db.queries.listExecutions(db.mapToValue(projectId), jobId);
        List<JobExecutionData> ret = Lists.newArrayList();
        for (JobExecutionEntity execution : executions) {
            ret.add(mapFromEntity(execution, jobKey));
        }
        sort(ret);
        return ret;
    } catch (SQLException e) {
        throw new RepositoryException("Error listing job executions", e);
    } finally {
        db.close();
    }
}
Also used : JobExecutionData(org.platformlayer.jobs.model.JobExecutionData) SQLException(java.sql.SQLException) ProjectId(org.platformlayer.ids.ProjectId) RepositoryException(org.platformlayer.RepositoryException) JdbcTransaction(com.fathomdb.jdbc.JdbcTransaction)

Example 23 with ProjectId

use of org.platformlayer.ids.ProjectId in project platformlayer by platformlayer.

the class JdbcJobRepository method findExecution.

@Override
@JdbcTransaction
public JobExecutionData findExecution(PlatformLayerKey jobKey, String executionId) throws RepositoryException {
    DbHelper db = new DbHelper();
    try {
        ProjectId projectId = jobKey.getProject();
        String jobId = jobKey.getItemIdString();
        JobExecutionEntity execution = db.queries.findExecution(db.mapToValue(projectId), jobId, executionId);
        if (execution == null) {
            return null;
        }
        return mapFromEntity(execution, jobKey);
    } 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) RepositoryException(org.platformlayer.RepositoryException) JdbcTransaction(com.fathomdb.jdbc.JdbcTransaction)

Example 24 with ProjectId

use of org.platformlayer.ids.ProjectId in project platformlayer by platformlayer.

the class JdbcJobRepository method insertExecution.

@Override
@JdbcTransaction
public String insertExecution(PlatformLayerKey jobKey, Date startedAt) throws RepositoryException {
    DbHelper db = new DbHelper();
    try {
        ProjectId projectId = jobKey.getProject();
        String jobId = jobKey.getItemIdString();
        String executionId = UUID.randomUUID().toString();
        JobExecutionEntity execution = new JobExecutionEntity();
        execution.project = db.mapToValue(projectId);
        execution.startedAt = startedAt;
        execution.state = JobState.RUNNING;
        execution.executionId = executionId;
        execution.jobId = jobId;
        int updateCount = db.queries.insert(execution);
        if (updateCount != 1) {
            throw new RepositoryException("Unexpected number of rows inserted");
        }
        return execution.executionId;
    } catch (SQLException e) {
        throw new RepositoryException("Error inserting job execution", e);
    } finally {
        db.close();
    }
}
Also used : SQLException(java.sql.SQLException) ProjectId(org.platformlayer.ids.ProjectId) RepositoryException(org.platformlayer.RepositoryException) JdbcTransaction(com.fathomdb.jdbc.JdbcTransaction)

Example 25 with ProjectId

use of org.platformlayer.ids.ProjectId 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)

Aggregations

ProjectId (org.platformlayer.ids.ProjectId)33 RepositoryException (org.platformlayer.RepositoryException)14 FederationKey (org.platformlayer.ids.FederationKey)11 PlatformLayerKey (org.platformlayer.core.model.PlatformLayerKey)10 ManagedItemId (org.platformlayer.ids.ManagedItemId)9 OpsException (org.platformlayer.ops.OpsException)8 JdbcTransaction (com.fathomdb.jdbc.JdbcTransaction)7 SQLException (java.sql.SQLException)7 ServiceType (org.platformlayer.ids.ServiceType)7 ItemType (org.platformlayer.ids.ItemType)6 ProjectAuthorization (org.platformlayer.model.ProjectAuthorization)4 PlatformLayerClient (org.platformlayer.PlatformLayerClient)3 ItemBase (org.platformlayer.core.model.ItemBase)3 JobData (org.platformlayer.jobs.model.JobData)3 TypedPlatformLayerClient (org.platformlayer.TypedPlatformLayerClient)2 Authenticator (org.platformlayer.auth.Authenticator)2 DirectAuthenticator (org.platformlayer.auth.DirectAuthenticator)2 FederatedPlatformLayerClient (org.platformlayer.federation.FederatedPlatformLayerClient)2 PlatformLayerConnectionConfiguration (org.platformlayer.federation.model.PlatformLayerConnectionConfiguration)2 DirectPlatformLayerClient (org.platformlayer.ops.DirectPlatformLayerClient)2