Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Sonatype Nexus (TM) Open Source Version Copyright (c) 2007-2012 Sonatype, Inc. All rights reserved. Includes the third-party code listed at http://links.sonatype.com/products/nexus/oss/attributions. This program and the accompanying materials are made available under the terms of the Eclipse Public License Version 1.0, which accompanies this distribution and is available at http://www.eclipse.org/legal/epl-v10.html. Sonatype Nexus (TM) Professional Version is available from Sonatype, Inc. "Sonatype" and "Sonatype Nexus" are trademarks of Sonatype, Inc. Apache Maven is a trademark of the Apache Software Foundation. M2eclipse is a trademark of the Eclipse Foundation. All other trademarks are the property of their respective owners.
 
 package org.sonatype.nexus.rest.schedules;
 
 import java.util.Date;
 import java.util.List;
 import java.util.Map;
 
 import  javax.ws.rs.Consumes;
 import  javax.ws.rs.GET;
 import  javax.ws.rs.POST;
 import  javax.ws.rs.Path;
 import  javax.ws.rs.Produces;
 
 import  org.codehaus.enunciate.contract.jaxrs.ResourceMethodSignature;
 import  org.restlet.Context;
 import  org.restlet.data.Form;
 import  org.restlet.data.Request;
 import  org.restlet.data.Response;
 import  org.restlet.data.Status;
 import  org.restlet.resource.ResourceException;
 import  org.restlet.resource.Variant;

Author(s):
tstevens
 
 @Component( role = PlexusResource.class, hint = "ScheduledServiceListPlexusResource" )
 @Produces( { "application/xml""application/json" } )
 @Consumes( { "application/xml""application/json" } )
 {
     public static final String RESOURCE_URI = "/schedules";
 
     private static final Long UNKNOWN = null;
 
     {
         this.setModifiabletrue );
     }
 
     @Override
     public Object getPayloadInstance()
     {
         return new ScheduledServiceResourceResponse();
     }
 
     @Override
     public String getResourceUri()
     {
         return ;
     }
 
     @Override
     {
         return new PathProtectionDescriptorgetResourceUri(), "authcBasic,perms[nexus:tasks]" );
     }
 
     private boolean isAllTasks( Request request )
     {
         Form form = request.getResourceRef().getQueryAsForm();
 
         if ( form != null )
         {
             String result = form.getFirstValue( "allTasks" );
 
             if ( result != null )
            {
                return result.equalsIgnoreCase"true" );
            }
        }
        return false;
    }

    
Retrieve a list of scheduled tasks currently configured in nexus.

Parameters:
allTasks If true, will return all tasks, even non-exposed tasks.
    @Override
    @GET
    @ResourceMethodSignature( output = ScheduledServiceListResourceResponse.class )
    public Object get( Context context, Request request, Response response, Variant variant )
        throws ResourceException
    {
        boolean allTasks = isAllTasksrequest );
        Map<StringList<ScheduledTask<?>>> tasksMap = getNexusScheduler().getAllTasks();
        for ( String key : tasksMap.keySet() )
        {
            List<ScheduledTask<?>> tasks = tasksMap.getkey );
            for ( ScheduledTask<?> task : tasks )
            {
                boolean isExposed = true;
                SchedulerTask<?> st = task.getSchedulerTask();
                if ( st != null && st instanceof NexusTask<?> )
                {
                    isExposed = ( (NexusTask<?>) st ).isExposed();
                }
                if ( allTasks || isExposed )
                {
                    if ( getLogger().isDebugEnabled() )
                    {
                        getLogger().debug"Building task '" + task.getName() + "' of type '" + task.getType() + "'." );
                    }
                    ScheduledServiceListResource item = new ScheduledServiceListResource();
                    item.setResourceURIcreateChildReferencerequestthistask.getId() ).toString() );
                    item.setLastRunResultgetLastRunResulttask ) );
                    item.setIdtask.getId() );
                    item.setNametask.getName() );
                    item.setStatustask.getTaskState().toString() );
                    item.setReadableStatusgetReadableStatetask.getTaskState() ) );
                    item.setTypeIdtask.getType() );
                    ScheduledTaskDescriptor descriptor =
                        getNexusConfiguration().getScheduledTaskDescriptortask.getType() );
                    if ( descriptor != null )
                    {
                        item.setTypeNamedescriptor.getName() );
                    }
                    item.setCreatedtask.getScheduledAt() == null ? "n/a" : task.getScheduledAt().toString() );
                    item.setLastRunTimetask.getLastRun() == null ? "n/a" : task.getLastRun().toString() );
                    final Date nextRunTime = getNextRunTimetask );
                    item.setNextRunTimenextRunTime == null ? "n/a" : nextRunTime.toString() );
                    if ( task.getScheduledAt() != null )
                    {
                        item.setCreatedInMillistask.getScheduledAt().getTime() );
                    }
                    if ( task.getLastRun() != null )
                    {
                        item.setLastRunTimeInMillistask.getLastRun().getTime() );
                    }
                    if ( nextRunTime != null )
                    {
                        item.setNextRunTimeInMillisnextRunTime.getTime() );
                    }
                    item.setSchedulegetScheduleShortNametask.getSchedule() ) );
                    item.setEnabledtask.isEnabled() );
                    result.addDataitem );
                }
            }
        }
        return result;
    }

    
Add a new scheduled service to nexus.
    @Override
    @POST
    @ResourceMethodSignature( input = ScheduledServiceResourceResponse.class, output = ScheduledServiceResourceStatusResponse.class )
    public Object post( Context context, Request request, Response responseObject payload )
        throws ResourceException
    {
        ScheduledServiceResourceResponse serviceRequest = (ScheduledServiceResourceResponsepayload;
        ScheduledServiceResourceStatusResponse result = null;
        if ( serviceRequest != null )
        {
            ScheduledServiceBaseResource serviceResource = serviceRequest.getData();
            try
            {
                Schedule schedule = getModelScheduleserviceRequest.getData() );
                ScheduledTask<?> task = null;
                if ( schedule != null )
                {
                    task =
                        getNexusScheduler().schedulegetModelNameserviceResource ),
                            getModelNexusTaskserviceResourcerequest ), schedule );
                }
                else
                {
                    task =
                        getNexusScheduler().schedulegetModelNameserviceResource ),
                            getModelNexusTaskserviceResourcerequest ), new ManualRunSchedule() );
                }
                task.setEnabledserviceResource.isEnabled() );
                // Need to store the enabled flag update
                getNexusScheduler().updateScheduletask );
                ScheduledServiceResourceStatus resourceStatus = new ScheduledServiceResourceStatus();
                resourceStatus.setResourceserviceResource );
                // Just need to update the id, as the incoming data is a POST w/ no id
                resourceStatus.getResource().setIdtask.getId() );
                resourceStatus.setResourceURIcreateChildReferencerequestthistask.getId() ).toString() );
                resourceStatus.setStatustask.getTaskState().toString() );
                resourceStatus.setReadableStatusgetReadableStatetask.getTaskState() ) );
                resourceStatus.setCreatedtask.getScheduledAt() == null ? "n/a" : task.getScheduledAt().toString() );
                resourceStatus.setLastRunResult..equalstask.getTaskState() ) ? "Error" : "Ok" );
                resourceStatus.setLastRunTimetask.getLastRun() == null ? "n/a" : task.getLastRun().toString() );
                resourceStatus.setNextRunTimetask.getNextRun() == null ? "n/a" : task.getNextRun().toString() );
                if ( task.getScheduledAt() != null )
                {
                    resourceStatus.setCreatedInMillistask.getScheduledAt().getTime() );
                }
                if ( task.getLastRun() != null )
                {
                    resourceStatus.setLastRunTimeInMillistask.getLastRun().getTime() );
                }
                if ( task.getNextRun() != null )
                {
                    resourceStatus.setNextRunTimeInMillistask.getNextRun().getTime() );
                }
                result = new ScheduledServiceResourceStatusResponse();
                result.setDataresourceStatus );
            }
            catch ( RejectedExecutionException e )
            {
                getLogger().warn"Execution of task " + getModelNameserviceResource ) + " rejected." );
                throw new ResourceException( Status.CLIENT_ERROR_CONFLICT, e.getMessage() );
            }
            catch ( ParseException e )
            {
                getLogger().warn"Unable to parse data for task " + getModelNameserviceResource ) );
                throw new PlexusResourceException( Status.CLIENT_ERROR_BAD_REQUEST, e.getMessage(),
                    getNexusErrorResponse"cronCommand"e.getMessage() ) );
            }
            catch ( InvalidConfigurationException e )
            {
                handleConfigurationExceptione );
            }
        }
        return result;
    }
New to GrepCode? Check out our FAQ X