1 31 32 package org.opencms.scheduler; 33 34 import org.opencms.configuration.I_CmsConfigurationParameterHandler; 35 import org.opencms.main.CmsContextInfo; 36 import org.opencms.main.CmsIllegalArgumentException; 37 import org.opencms.main.CmsLog; 38 import org.opencms.main.CmsRuntimeException; 39 import org.opencms.util.CmsStringUtil; 40 41 import java.util.Collections ; 42 import java.util.Date ; 43 import java.util.Map ; 44 import java.util.SortedMap ; 45 import java.util.TreeMap ; 46 47 import org.apache.commons.logging.Log; 48 49 import org.quartz.CronTrigger; 50 import org.quartz.Trigger; 51 52 350 public class CmsScheduledJobInfo implements I_CmsConfigurationParameterHandler { 351 352 353 private static final Log LOG = CmsLog.getLog(CmsScheduledJobInfo.class); 354 355 356 private boolean m_active; 357 358 359 private String m_className; 360 361 362 private CmsContextInfo m_context; 363 364 365 private String m_cronExpression; 366 367 368 private boolean m_frozen; 369 370 371 private String m_id; 372 373 374 private I_CmsScheduledJob m_jobInstance; 375 376 377 private String m_jobName; 378 379 380 private SortedMap m_parameters; 381 382 383 private boolean m_reuseInstance; 384 385 386 private Trigger m_trigger; 387 388 391 public CmsScheduledJobInfo() { 392 393 m_reuseInstance = false; 394 m_frozen = false; 395 m_parameters = new TreeMap (); 397 m_active = true; 399 } 400 401 413 public CmsScheduledJobInfo( 414 String id, 415 String jobName, 416 String className, 417 CmsContextInfo context, 418 String cronExpression, 419 boolean reuseInstance, 420 boolean active, 421 SortedMap parameters) { 422 423 m_frozen = false; 424 setId(id); 425 if (CmsStringUtil.isNotEmpty(jobName)) { 426 setJobName(jobName); 428 } 429 setClassName(className); 430 setContextInfo(context); 431 setCronExpression(cronExpression); 432 setReuseInstance(reuseInstance); 433 setActive(active); 434 setParameters(parameters); 435 } 436 437 440 public void addConfigurationParameter(String paramName, String paramValue) { 441 442 checkFrozen(); 443 m_parameters.put(paramName, paramValue); 445 if (LOG.isDebugEnabled()) { 446 LOG.debug(org.opencms.configuration.Messages.get().getBundle().key( 447 org.opencms.configuration.Messages.LOG_ADD_CONFIG_PARAM_3, 448 paramName, 449 paramValue, 450 this)); 451 } 452 } 453 454 461 public void clearId() { 462 463 setId(null); 464 } 465 466 478 public Object clone() { 479 480 CmsScheduledJobInfo result = new CmsScheduledJobInfo(); 481 482 result.m_id = m_id; 483 result.m_active = false; 484 result.m_frozen = false; 485 result.m_className = m_className; 486 if (isReuseInstance()) { 487 result.m_jobInstance = m_jobInstance; 488 } 489 result.m_reuseInstance = m_reuseInstance; 490 result.m_context = (CmsContextInfo)m_context.clone(); 491 result.m_cronExpression = m_cronExpression; 492 result.m_jobName = m_jobName; 493 result.m_parameters = new TreeMap (m_parameters); 494 result.m_trigger = null; 495 496 return result; 497 } 498 499 504 public String getClassName() { 505 506 return m_className; 507 } 508 509 512 public Map getConfiguration() { 513 514 if (LOG.isDebugEnabled()) { 516 LOG.debug(org.opencms.configuration.Messages.get().getBundle().key( 517 org.opencms.configuration.Messages.LOG_GET_CONFIGURATION_1, 518 this)); 519 } 520 return getParameters(); 521 } 522 523 528 public CmsContextInfo getContextInfo() { 529 530 return m_context; 531 } 532 533 538 public String getCronExpression() { 539 540 return m_cronExpression; 541 } 542 543 551 public Date getExecutionTimeAfter(Date date) { 552 553 if (!m_active || (m_trigger == null)) { 554 return null; 556 } 557 558 return m_trigger.getFireTimeAfter(date); 559 } 560 561 568 public Date getExecutionTimeNext() { 569 570 if (!m_active || (m_trigger == null)) { 571 return null; 573 } 574 575 return m_trigger.getNextFireTime(); 576 } 577 578 585 public Date getExecutionTimePrevious() { 586 587 if (!m_active || (m_trigger == null)) { 588 return null; 590 } 591 592 return m_trigger.getPreviousFireTime(); 593 } 594 595 603 public String getId() { 604 605 return m_id; 606 } 607 608 616 public synchronized I_CmsScheduledJob getJobInstance() { 617 618 if (m_jobInstance != null) { 619 620 if (LOG.isDebugEnabled()) { 621 LOG.debug(Messages.get().getBundle().key( 622 Messages.LOG_REUSING_INSTANCE_1, 623 m_jobInstance.getClass().getName())); 624 } 625 626 return m_jobInstance; 628 } 629 630 I_CmsScheduledJob job = null; 631 632 try { 633 job = (I_CmsScheduledJob)Class.forName(getClassName()).newInstance(); 635 } catch (ClassNotFoundException e) { 636 LOG.error(Messages.get().getBundle().key(Messages.LOG_CLASS_NOT_FOUND_1, getClassName()), e); 637 } catch (IllegalAccessException e) { 638 LOG.error(Messages.get().getBundle().key(Messages.LOG_ILLEGAL_ACCESS_0), e); 639 } catch (InstantiationException e) { 640 LOG.error(Messages.get().getBundle().key(Messages.LOG_INSTANCE_GENERATION_0), e); 641 } catch (ClassCastException e) { 642 LOG.error(Messages.get().getBundle().key(Messages.LOG_BAD_INTERFACE_0), e); 643 } 644 645 if (m_reuseInstance) { 646 m_jobInstance = job; 648 } 649 650 if (LOG.isDebugEnabled()) { 651 LOG.debug(Messages.get().getBundle().key(Messages.LOG_JOB_CREATED_1, getClassName())); 652 } 653 654 return job; 655 } 656 657 662 public String getJobName() { 663 664 return m_jobName; 665 } 666 667 672 public SortedMap getParameters() { 673 674 return m_parameters; 675 } 676 677 686 public void initConfiguration() { 687 688 if (LOG.isDebugEnabled()) { 690 LOG.debug(org.opencms.configuration.Messages.get().getBundle().key( 691 org.opencms.configuration.Messages.LOG_INIT_CONFIGURATION_1, 692 this)); 693 } 694 setFrozen(true); 695 } 696 697 702 public boolean isActive() { 703 704 return m_active; 705 } 706 707 712 public boolean isReuseInstance() { 713 714 return m_reuseInstance; 715 } 716 717 722 public void setActive(boolean active) { 723 724 checkFrozen(); 725 m_active = active; 726 } 727 728 733 public void setClassName(String className) { 734 735 checkFrozen(); 736 if (!CmsStringUtil.isValidJavaClassName(className)) { 737 throw new CmsIllegalArgumentException( 738 Messages.get().container(Messages.ERR_BAD_JOB_CLASS_NAME_1, className)); 739 } 740 Class jobClass; 741 try { 742 jobClass = Class.forName(className); 743 } catch (ClassNotFoundException e) { 744 throw new CmsIllegalArgumentException(Messages.get().container( 745 Messages.ERR_JOB_CLASS_NOT_FOUND_1, 746 className)); 747 } 748 if (!I_CmsScheduledJob.class.isAssignableFrom(jobClass)) { 749 throw new CmsIllegalArgumentException(Messages.get().container( 750 Messages.ERR_JOB_CLASS_BAD_INTERFACE_2, 751 className, 752 I_CmsScheduledJob.class.getName())); 753 } 754 755 m_className = className; 756 if (getJobName() == null) { 757 setJobName(className); 759 } 760 } 761 762 771 public void setContextInfo(CmsContextInfo contextInfo) { 772 773 checkFrozen(); 774 if (contextInfo == null) { 775 throw new CmsIllegalArgumentException(Messages.get().container(Messages.ERR_BAD_CONTEXT_INFO_0)); 776 } 777 m_context = contextInfo; 778 } 779 780 785 public void setCronExpression(String cronExpression) { 786 787 checkFrozen(); 788 789 try { 790 new CronTrigger().setCronExpression(cronExpression); 792 } catch (Exception e) { 793 throw new CmsIllegalArgumentException(Messages.get().container( 794 Messages.ERR_BAD_CRON_EXPRESSION_2, 795 getJobName(), 796 cronExpression)); 797 } 798 799 m_cronExpression = cronExpression; 800 } 801 802 807 public void setJobName(String jobName) { 808 809 checkFrozen(); 810 if (CmsStringUtil.isEmpty(jobName) || !jobName.trim().equals(jobName)) { 811 throw new CmsIllegalArgumentException(Messages.get().container(Messages.ERR_BAD_JOB_NAME_1, jobName)); 812 813 } 814 m_jobName = jobName; 815 } 816 817 822 public void setParameters(SortedMap parameters) { 823 824 checkFrozen(); 825 if (parameters == null) { 826 throw new CmsIllegalArgumentException(Messages.get().container(Messages.ERR_BAD_JOB_PARAMS_0)); 827 } 828 m_parameters = new TreeMap (parameters); 830 } 831 832 838 public void setReuseInstance(boolean reuseInstance) { 839 840 checkFrozen(); 841 m_reuseInstance = reuseInstance; 842 } 843 844 849 protected void checkFrozen() throws CmsRuntimeException { 850 851 if (m_frozen) { 852 throw new CmsRuntimeException(Messages.get().container(Messages.ERR_JOB_INFO_FROZEN_1, getJobName())); 853 } 854 } 855 856 864 protected Trigger getTrigger() { 865 866 return m_trigger; 867 } 868 869 876 protected synchronized void setFrozen(boolean frozen) { 877 878 if (frozen && !m_frozen) { 879 m_parameters = Collections.unmodifiableSortedMap(m_parameters); 881 m_context.freeze(); 882 m_frozen = true; 883 } else if (!frozen && m_frozen) { 884 m_parameters = new TreeMap (m_parameters); 886 m_frozen = false; 887 } 888 } 889 890 898 protected void setId(String id) { 899 900 checkFrozen(); 901 m_id = id; 902 } 903 904 912 protected void setTrigger(Trigger trigger) { 913 914 checkFrozen(); 915 m_trigger = trigger; 916 } 917 } | Popular Tags |