1   /*
2    * Copyright 2003 - 2013 The eFaps Team
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   * Revision:        $Rev$
17   * Last Changed:    $Date$
18   * Last Changed By: $Author$
19   */
20  
21  package org.efaps.admin.common;
22  
23  import java.util.Properties;
24  import java.util.UUID;
25  
26  import javax.naming.InitialContext;
27  import javax.naming.NamingException;
28  
29  import org.efaps.admin.EFapsSystemConfiguration;
30  import org.efaps.admin.KernelSettings;
31  import org.efaps.admin.user.Person;
32  import org.efaps.db.transaction.DelegatingUserTransaction;
33  import org.efaps.init.INamingBinds;
34  import org.efaps.message.MessageStatusHolder;
35  import org.efaps.util.EFapsException;
36  import org.quartz.JobBuilder;
37  import org.quartz.JobDetail;
38  import org.quartz.JobKey;
39  import org.quartz.Scheduler;
40  import org.quartz.SchedulerException;
41  import org.quartz.SimpleScheduleBuilder;
42  import org.quartz.Trigger;
43  import org.quartz.TriggerBuilder;
44  import org.quartz.TriggerKey;
45  import org.quartz.impl.StdSchedulerFactory;
46  import org.slf4j.Logger;
47  import org.slf4j.LoggerFactory;
48  
49  /**
50   * Quartz for eFaps.
51   *
52   * @author The eFaps Team
53   * @version $Id$
54   */
55  public final class Quartz
56  {
57  
58      /**
59       * Quartz Group Name.
60       */
61      public static final String QUARTZGROUP = "eFapsQuartzGroup";
62  
63  
64      /**
65       * Logging instance used in this class.
66       */
67      private static final Logger LOG = LoggerFactory.getLogger(Quartz.class);
68  
69      /**
70       * Contains the instance of this singelton.
71       */
72      private static Quartz QUARTZ;
73  
74      /**
75       * Scheduler used.
76       */
77      private Scheduler scheduler;
78  
79      /**
80       * Private Constructor.
81       */
82      private Quartz()
83      {
84      }
85  
86      /**
87       * Method to initialize the Quartz.
88       * @throws EFapsException on error
89       */
90      public static void initialize()
91          throws EFapsException
92      {
93          Quartz.QUARTZ = new Quartz();
94          try {
95              //Kernel-Configuration
96              final SystemConfiguration config = EFapsSystemConfiguration.get();
97              final Properties props = config.getAttributeValueAsProperties(KernelSettings.QUARTZPROPS);
98  
99              final StdSchedulerFactory schedFact = new StdSchedulerFactory();
100             javax.naming.Context envCtx = null;
101             String lookup = "java:global/";
102             try {
103                 final InitialContext initCtx = new InitialContext();
104                 envCtx = (javax.naming.Context) initCtx.lookup(lookup);
105             } catch (final NamingException e) {
106                 Quartz.LOG.info("Catched NamingException on evaluation for Quartz");
107             }
108             // for a build the context might be different, try this before surrender
109             if (envCtx == null) {
110                 try {
111                     lookup = "java:comp/env";
112                     final InitialContext initCtx = new InitialContext();
113                     envCtx = (javax.naming.Context) initCtx.lookup(lookup);
114                 } catch (final NamingException e) {
115                     Quartz.LOG.info("Catched NamingException on evaluation for Quartz");
116                 }
117             }
118             try {
119                 final DelegatingUserTransaction trans = (DelegatingUserTransaction) envCtx
120                                 .lookup(INamingBinds.RESOURCE_USERTRANSACTION);
121                 // QuartzTrigger
122                 trans.setUserName(Person.get(UUID.fromString("df2f02a7-c556-49ad-b019-e13db66e1cbf")).getName());
123             } catch (final NamingException e) {
124                 // TODO Auto-generated catch block
125                 e.printStackTrace();
126             }
127 
128             props.put(StdSchedulerFactory.PROP_SCHED_USER_TX_URL, lookup + "/" + INamingBinds.RESOURCE_USERTRANSACTION);
129             props.put(StdSchedulerFactory.PROP_SCHED_WRAP_JOB_IN_USER_TX, "true");
130             props.put(StdSchedulerFactory.PROP_THREAD_POOL_CLASS, "org.quartz.simpl.SimpleThreadPool");
131             props.put("org.quartz.plugin.jobInitializer.class", "org.efaps.admin.common.QuartzSchedulerPlugin");
132 
133             if (!props.containsKey(StdSchedulerFactory.PROP_SCHED_MAKE_SCHEDULER_THREAD_DAEMON)) {
134                 props.put(StdSchedulerFactory.PROP_SCHED_MAKE_SCHEDULER_THREAD_DAEMON, "true");
135             }
136             if (!props.containsKey(StdSchedulerFactory.PROP_SCHED_INSTANCE_NAME)) {
137                 props.put(StdSchedulerFactory.PROP_SCHED_INSTANCE_NAME, "eFapsScheduler");
138             }
139             if (!props.containsKey("org.quartz.threadPool.threadCount")) {
140                 props.put("org.quartz.threadPool.threadCount", "2");
141             }
142 
143             if (!props.containsKey("org.quartz.plugin.triggHistory.class")) {
144                 props.put("org.quartz.plugin.triggHistory.class",
145                                 "org.quartz.plugins.history.LoggingTriggerHistoryPlugin");
146                 props.put("org.quartz.plugin.triggHistory.triggerFiredMessage",
147                             "Trigger {1}.{0} fired job {6}.{5} at: {4, date, HH:mm:ss MM/dd/yyyy}");
148                 props.put("org.quartz.plugin.triggHistory.triggerCompleteMessage",
149                             "Trigger {1}.{0} completed firing job {6}.{5} at {4, date, HH:mm:ss MM/dd/yyyy}.");
150             }
151             Quartz.LOG.info("Sheduling Quartz with properties {}", props);
152 
153             schedFact.initialize(props);
154             Quartz.QUARTZ.scheduler = schedFact.getScheduler("eFapsScheduler");
155             if (Quartz.QUARTZ.scheduler != null) {
156                 Quartz.QUARTZ.scheduler.shutdown();
157             }
158             Quartz.QUARTZ.scheduler =  schedFact.getScheduler();
159 
160             if (config.getAttributeValueAsBoolean(KernelSettings.MSGTRIGGERACTIVE)) {
161                 final int interval = config.getAttributeValueAsInteger(KernelSettings.MSGTRIGGERINTERVAL);
162                 final Trigger trigger = TriggerBuilder.newTrigger()
163                                 .withIdentity("SystemMessageTrigger")
164                                 .withSchedule(SimpleScheduleBuilder.repeatMinutelyForever(interval > 0 ? interval : 1))
165                                 .build();
166 
167                 JobDetail jobDetail = Quartz.QUARTZ.scheduler.getJobDetail(new JobKey("SystemMessage",
168                                 Quartz.QUARTZGROUP));
169                 if (jobDetail == null) {
170                     jobDetail = JobBuilder.newJob(MessageStatusHolder.class)
171                                     .withIdentity("SystemMessage", Quartz.QUARTZGROUP).build();
172                     Quartz.QUARTZ.scheduler.scheduleJob(jobDetail, trigger);
173                 } else {
174                     Quartz.QUARTZ.scheduler.rescheduleJob(new TriggerKey("SystemMessageTrigger", Quartz.QUARTZGROUP),
175                                     trigger);
176                 }
177             }
178             Quartz.QUARTZ.scheduler.start();
179         } catch (final SchedulerException e) {
180             throw new EFapsException(Quartz.class, "Quartz.SchedulerException", e);
181         }
182     }
183 
184     /**
185      * Getter method for the instance variable {@link #QUARTZ}.
186      *
187      * @return value of instance variable {@link #QUARTZ}
188      * @throws EFapsException on error
189      */
190     public static Quartz getQuartz()
191         throws EFapsException
192     {
193         if (Quartz.QUARTZ == null) {
194             Quartz.initialize();
195         }
196         return Quartz.QUARTZ;
197     }
198 
199 
200     /**
201      * ShutDown Quartz.
202      */
203     public static void shutDown()
204     {
205         if (Quartz.QUARTZ != null && Quartz.QUARTZ.scheduler != null) {
206             try {
207                 Quartz.QUARTZ.scheduler.shutdown();
208             } catch (final SchedulerException e) {
209                 Quartz.LOG.error("Problems on shutdown of QuartsSheduler", e);
210             }
211         }
212     }
213 
214 }