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 java.util.Set;
 
 import  org.restlet.data.Request;
 import  org.restlet.resource.ResourceException;
 
 public abstract class AbstractScheduledServicePlexusResource
     extends AbstractFormFieldResource
 {
     @Requirement
     private NexusScheduler nexusScheduler;
 
     public static final String SCHEDULED_SERVICE_ID_KEY = "scheduledServiceId";
 
     private DateFormat timeFormat = new SimpleDateFormat"HH:mm" );
 
     protected NexusScheduler getNexusScheduler()
     {
         return ;
     }
 
     protected String getScheduleShortNameSchedule schedule )
     {
         if ( ManualRunSchedule.class.isAssignableFromschedule.getClass() ) )
         {
         }
         else if ( RunNowSchedule.class.isAssignableFromschedule.getClass() ) )
         {
         }
         else if ( OnceSchedule.class.isAssignableFromschedule.getClass() ) )
         {
         }
         else if ( HourlySchedule.class.isAssignableFromschedule.getClass() ) )
         {
         }
         else if ( DailySchedule.class.isAssignableFromschedule.getClass() ) )
         {
         }
         else if ( WeeklySchedule.class.isAssignableFromschedule.getClass() ) )
         {
         }
        else if ( MonthlySchedule.class.isAssignableFromschedule.getClass() ) )
        {
        }
        else if ( CronSchedule.class.isAssignableFromschedule.getClass() ) )
        {
        }
        else
        {
            return schedule.getClass().getName();
        }
    }
    protected String formatDateDate date )
    {
        return Long.toStringdate.getTime() );
    }
    protected String formatTimeDate date )
    {
        return .formatdate );
    }
    {
        for ( String key : map.keySet() )
        {
            if ( !TaskUtils.isPrivatePropertykey ) )
            {
                ScheduledServicePropertyResource prop = new ScheduledServicePropertyResource();
                prop.setKeykey );
                prop.setValuemap.getkey ) );
                list.addprop );
            }
        }
        return list;
    }
    protected List<StringformatRecurringDayOfWeekSet<Integerdays )
    {
        List<Stringlist = new ArrayList<String>();
        for ( Integer day : days )
        {
            switch ( day.intValue() )
            {
                case 1:
                {
                    list.add"sunday" );
                    break;
                }
                case 2:
                {
                    list.add"monday" );
                    break;
                }
                case 3:
                {
                    list.add"tuesday" );
                    break;
                }
                case 4:
                {
                    list.add"wednesday" );
                    break;
                }
                case 5:
                {
                    list.add"thursday" );
                    break;
                }
                case 6:
                {
                    list.add"friday" );
                    break;
                }
                case 7:
                {
                    list.add"saturday" );
                    break;
                }
            }
        }
        return list;
    }
    protected Set<IntegerformatRecurringDayOfWeekList<Stringdays )
    {
        Set<Integerset = new HashSet<Integer>();
        for ( String day : days )
        {
            if ( "sunday".equalsday ) )
            {
                set.addnew Integer( 1 ) );
            }
            else if ( "monday".equalsday ) )
            {
                set.addnew Integer( 2 ) );
            }
            else if ( "tuesday".equalsday ) )
            {
                set.addnew Integer( 3 ) );
            }
            else if ( "wednesday".equalsday ) )
            {
                set.addnew Integer( 4 ) );
            }
            else if ( "thursday".equalsday ) )
            {
                set.addnew Integer( 5 ) );
            }
            else if ( "friday".equalsday ) )
            {
                set.addnew Integer( 6 ) );
            }
            else if ( "saturday".equalsday ) )
            {
                set.addnew Integer( 7 ) );
            }
        }
        return set;
    }
    protected List<StringformatRecurringDayOfMonthSet<Integerdays )
    {
        List<Stringlist = new ArrayList<String>();
        for ( Integer day : days )
        {
            if ( ..equalsday ) )
            {
                list.add"last" );
            }
            else
            {
                list.add( String.valueOfday ) );
            }
        }
        return list;
    }
    protected Set<IntegerformatRecurringDayOfMonthList<Stringdays )
    {
        Set<Integerset = new HashSet<Integer>();
        for ( String day : days )
        {
            if ( "last".equalsday ) )
            {
                set.add. );
            }
            else
            {
                set.add( Integer.valueOfday ) );
            }
        }
        return set;
    }
    protected Date parseDateString dateString time )
    {
        Calendar cal = Calendar.getInstance();
        Calendar timeCalendar = Calendar.getInstance();
        try
        {
            timeCalendar.setTime.parsetime ) );
            cal.setTimenew Date( Long.parseLongdate ) ) );
            cal.add.timeCalendar.get. ) );
            cal.add.timeCalendar.get. ) );
            if ( getLogger().isDebugEnabled() )
            {
                getLogger().debug"Parsed date from task creation request: " + cal.getTime() );
            }
        }
        catch ( ParseException e )
        {
            cal = null;
        }
        return cal == null ? null : cal.getTime();
    }
    {
        return model.getName();
    }
    public NexusTask<?> getModelNexusTaskScheduledServiceBaseResource model, Request request )
        throws IllegalArgumentException, ResourceException
    {
        String serviceType = model.getTypeId();
        NexusTask<?> task = getNexusScheduler().createTaskInstanceserviceType );
        for ( Iterator iter = model.getProperties().iterator(); iter.hasNext(); )
        {
            task.addParameterprop.getKey(), prop.getValue() );
        }
        TaskUtils.setAlertEmailtaskmodel.getAlertEmail() );
        TaskUtils.setIdtaskmodel.getId() );
        TaskUtils.setNametaskmodel.getName() );
        return task;
    }
    public void validateStartDateString date )
        throws InvalidConfigurationException
    {
        Calendar cal = Calendar.getInstance();
        Date startDate = new Date( Long.parseLongdate ) );
        cal.setTimestartDate );
        Calendar nowCal = Calendar.getInstance();
        nowCal.add., -1 );
        nowCal.set., 0 );
        nowCal.set., 0 );
        nowCal.set., 0 );
        nowCal.set., 0 );
        // This is checking just the year/month/day, time isn't of concern right now
        // basic check that the day timestamp is roughly in the correct range
        if ( cal.beforenowCal ) )
        {
            if ( getLogger().isDebugEnabled() )
            {
                getLogger().debug"Validation error for startDate: " + startDate.toString() );
            }
            ValidationResponse vr = new ApplicationValidationResponse();
            ValidationMessage vm = new ValidationMessage"startDate""Date cannot be in the past." );
            vr.addValidationErrorvm );
            throw new InvalidConfigurationExceptionvr );
        }
    }
    public void validateTimeString keyDate date )
        throws InvalidConfigurationException
    {
        if ( date.beforenew Date() ) )
        {
            ValidationResponse vr = new ApplicationValidationResponse();
            ValidationMessage vm = new ValidationMessagekey"Time cannot be in the past." );
            vr.addValidationErrorvm );
            throw new InvalidConfigurationExceptionvr );
        }
    }
    {
        Schedule schedule = null;
        if ( ScheduledServiceAdvancedResource.class.isAssignableFrommodel.getClass() ) )
        {
            schedule = new CronSchedule( ( (ScheduledServiceAdvancedResourcemodel ).getCronCommand() );
        }
        else if ( ScheduledServiceMonthlyResource.class.isAssignableFrommodel.getClass() ) )
        {
            Date date =
                parseDate( ( (ScheduledServiceMonthlyResourcemodel ).getStartDate(),
                    ( (ScheduledServiceMonthlyResourcemodel ).getRecurringTime() );
            schedule =
                new MonthlyScheduledatenull,
                    formatRecurringDayOfMonth( ( (ScheduledServiceMonthlyResourcemodel ).getRecurringDay() ) );
        }
        else if ( ScheduledServiceWeeklyResource.class.isAssignableFrommodel.getClass() ) )
        {
            Date date =
                parseDate( ( (ScheduledServiceWeeklyResourcemodel ).getStartDate(),
                    ( (ScheduledServiceWeeklyResourcemodel ).getRecurringTime() );
            schedule =
                new WeeklyScheduledatenull,
                    formatRecurringDayOfWeek( ( (ScheduledServiceWeeklyResourcemodel ).getRecurringDay() ) );
        }
        else if ( ScheduledServiceDailyResource.class.isAssignableFrommodel.getClass() ) )
        {
            Date date =
                parseDate( ( (ScheduledServiceDailyResourcemodel ).getStartDate(),
                    ( (ScheduledServiceDailyResourcemodel ).getRecurringTime() );
            schedule = new DailyScheduledatenull );
        }
        else if ( ScheduledServiceHourlyResource.class.isAssignableFrommodel.getClass() ) )
        {
            Date date =
                parseDate( ( (ScheduledServiceHourlyResourcemodel ).getStartDate(),
                    ( (ScheduledServiceHourlyResourcemodel ).getStartTime() );
            schedule = new HourlyScheduledatenull );
        }
        else if ( ScheduledServiceOnceResource.class.isAssignableFrommodel.getClass() ) )
        {
            Date date =
                parseDate( ( (ScheduledServiceOnceResourcemodel ).getStartDate(),
                    ( (ScheduledServiceOnceResourcemodel ).getStartTime() );
            validateStartDate( ( (ScheduledServiceOnceResourcemodel ).getStartDate() );
            validateTime"startTime"date );
            schedule =
                new OnceScheduleparseDate( ( (ScheduledServiceOnceResourcemodel ).getStartDate(),
                    ( (ScheduledServiceOnceResourcemodel ).getStartTime() ) );
        }
        else
        {
            schedule = new ManualRunSchedule();
        }
        return schedule;
    }
    {
        ScheduledServiceBaseResource resource = null;
        if ( RunNowSchedule.class.isAssignableFromtask.getSchedule().getClass() )
            || ManualRunSchedule.class.isAssignableFromtask.getSchedule().getClass() ) )
        {
            resource = new ScheduledServiceBaseResource();
        }
        else if ( OnceSchedule.class.isAssignableFromtask.getSchedule().getClass() ) )
        {
            resource = new ScheduledServiceOnceResource();
            OnceSchedule taskSchedule = (OnceScheduletask.getSchedule();
            ScheduledServiceOnceResource res = (ScheduledServiceOnceResourceresource;
            res.setStartDateformatDatetaskSchedule.getStartDate() ) );
            res.setStartTimeformatTimetaskSchedule.getStartDate() ) );
        }
        else if ( HourlySchedule.class.isAssignableFromtask.getSchedule().getClass() ) )
        {
            resource = new ScheduledServiceHourlyResource();
            HourlySchedule taskSchedule = (HourlyScheduletask.getSchedule();
            ScheduledServiceHourlyResource res = (ScheduledServiceHourlyResourceresource;
            res.setStartDateformatDatetaskSchedule.getStartDate() ) );
            res.setStartTimeformatTimetaskSchedule.getStartDate() ) );
        }
        else if ( DailySchedule.class.isAssignableFromtask.getSchedule().getClass() ) )
        {
            resource = new ScheduledServiceDailyResource();
            DailySchedule taskSchedule = (DailyScheduletask.getSchedule();
            ScheduledServiceDailyResource res = (ScheduledServiceDailyResourceresource;
            res.setStartDateformatDatetaskSchedule.getStartDate() ) );
            res.setRecurringTimeformatTimetaskSchedule.getStartDate() ) );
        }
        else if ( WeeklySchedule.class.isAssignableFromtask.getSchedule().getClass() ) )
        {
            resource = new ScheduledServiceWeeklyResource();
            WeeklySchedule taskSchedule = (WeeklyScheduletask.getSchedule();
            ScheduledServiceWeeklyResource res = (ScheduledServiceWeeklyResourceresource;
            res.setStartDateformatDatetaskSchedule.getStartDate() ) );
            res.setRecurringTimeformatTimetaskSchedule.getStartDate() ) );
            res.setRecurringDayformatRecurringDayOfWeektaskSchedule.getDaysToRun() ) );
        }
        else if ( MonthlySchedule.class.isAssignableFromtask.getSchedule().getClass() ) )
        {
            resource = new ScheduledServiceMonthlyResource();
            MonthlySchedule taskSchedule = (MonthlyScheduletask.getSchedule();
            ScheduledServiceMonthlyResource res = (ScheduledServiceMonthlyResourceresource;
            res.setStartDateformatDatetaskSchedule.getStartDate() ) );
            res.setRecurringTimeformatTimetaskSchedule.getStartDate() ) );
            res.setRecurringDayformatRecurringDayOfMonthtaskSchedule.getDaysToRun() ) );
        }
        else if ( CronSchedule.class.isAssignableFromtask.getSchedule().getClass() ) )
        {
            resource = new ScheduledServiceAdvancedResource();
            CronSchedule taskSchedule = (CronScheduletask.getSchedule();
            ScheduledServiceAdvancedResource res = (ScheduledServiceAdvancedResourceresource;
            res.setCronCommandtaskSchedule.getCronString() );
        }
        if ( resource != null )
        {
            resource.setIdtask.getId() );
            resource.setEnabledtask.isEnabled() );
            resource.setNametask.getName() );
            resource.setSchedulegetScheduleShortNametask.getSchedule() ) );
            resource.setTypeIdtask.getType() );
            resource.setPropertiesformatServicePropertiestask.getTaskParams() ) );
            resource.setAlertEmail( TaskUtils.getAlertEmailtask ) );
        }
        return resource;
    }
    protected <T> Date getNextRunTimeScheduledTask<T> task )
    {
        Date nextRunTime = null;
        // Run now type tasks should never have a next run time
        if ( !task.getSchedule().getClass().isAssignableFromRunNowSchedule.class ) && task.getNextRun() != null )
        {
            nextRunTime = task.getNextRun();
        }
        return nextRunTime;
    }
    protected String getLastRunResultScheduledTask<?> task )
    {
        String lastRunResult = "n/a";
        if ( task.getLastStatus() != null )
        {
            lastRunResult = ..equalstask.getLastStatus() ) ? "Error" : "Ok";
            if ( task.getDuration() != 0 )
            {
                long milliseconds = task.getDuration();
                int hours = (int) ( ( milliseconds / 1000 ) / 3600 );
                int minutes = (int) ( ( milliseconds / 1000 ) / 60 - hours * 60 );
                int seconds = (int) ( ( milliseconds / 1000 ) % 60 );
                lastRunResult += " [";
                if ( hours != 0 )
                {
                    lastRunResult += hours;
                    lastRunResult += "h";
                }
                if ( minutes != 0 || hours != 0 )
                {
                    lastRunResult += minutes;
                    lastRunResult += "m";
                }
                lastRunResult += seconds;
                lastRunResult += "s";
                lastRunResult += "]";
            }
        }
        return lastRunResult;
    }
    protected String getReadableStateTaskState taskState )
    {
        switch ( taskState )
        {
            case :
            case :
            case :
            case :
                return "Waiting";
            case :
                return "Running";
            case :
                return "Blocked";
            case :
                return "Cancelling";
            case :
                return "Cancelled";
            default:
                throw new IllegalStateException();
        }
    }
New to GrepCode? Check out our FAQ X