1 13 14 package org.ejbca.core.ejb.services; 15 16 import java.io.Serializable ; 17 import java.util.Collection ; 18 import java.util.Date ; 19 import java.util.HashMap ; 20 import java.util.HashSet ; 21 import java.util.Iterator ; 22 23 import javax.ejb.CreateException ; 24 import javax.ejb.EJBException ; 25 import javax.ejb.Timer ; 26 27 import org.apache.commons.lang.StringUtils; 28 import org.ejbca.core.ejb.BaseSessionBean; 29 import org.ejbca.core.ejb.log.ILogSessionLocal; 30 import org.ejbca.core.ejb.log.ILogSessionLocalHome; 31 import org.ejbca.core.model.InternalResources; 32 import org.ejbca.core.model.log.Admin; 33 import org.ejbca.core.model.log.LogEntry; 34 import org.ejbca.core.model.services.IInterval; 35 import org.ejbca.core.model.services.IWorker; 36 import org.ejbca.core.model.services.ServiceConfiguration; 37 import org.ejbca.core.model.services.ServiceExecutionFailedException; 38 39 40 138 public class ServiceTimerSessionBean extends BaseSessionBean implements javax.ejb.TimedObject { 139 140 141 142 145 private transient ILogSessionLocal logsession = null; 146 147 148 private static final InternalResources intres = InternalResources.getInstance(); 149 150 151 154 Admin intAdmin = new Admin(Admin.TYPE_INTERNALUSER); 155 156 157 158 163 public void ejbCreate() throws CreateException { 164 165 } 166 167 172 private static final Integer SERVICELOADER_ID = Integer.valueOf(0); 173 174 private static final long SERVICELOADER_PERIOD = 5 * 60 * 1000; 175 176 182 public void ejbTimeout(Timer timer) { 183 debug(">ejbTimeout"); 184 Integer timerInfo = (Integer ) timer.getInfo(); 185 if(timerInfo.equals(SERVICELOADER_ID)){ 186 log.debug("Running the internal Service loader."); 187 load(); 188 }else{ 189 ServiceConfiguration serviceData = null; 190 IWorker worker = null; 191 String serviceName = null; 192 boolean run = false; 193 try{ 194 serviceData = getServiceSession().getServiceConfiguration(intAdmin, timerInfo.intValue()); 195 if(serviceData != null){ 196 serviceName = getServiceSession().getServiceName(intAdmin, timerInfo.intValue()); 197 worker = getWorker(serviceData,serviceName); 198 run = checkAndUpdateServiceTimeout(worker.getNextInterval(), timerInfo, serviceData, serviceName); 199 } 200 } catch (Exception e) { 201 log.error(e); 203 } 204 if(run){ 205 if(serviceData != null){ 206 try{ 207 if(serviceData.isActive() && worker.getNextInterval() != IInterval.DONT_EXECUTE){ 208 worker.work(); 209 getLogSession().log(intAdmin, intAdmin.getCaId(), LogEntry.MODULE_SERVICES, new java.util.Date (), null, null, LogEntry.EVENT_INFO_SERVICEEXECUTED, intres.getLocalizedMessage("services.serviceexecuted", serviceName)); 210 } 211 }catch (ServiceExecutionFailedException e) { 212 getLogSession().log(intAdmin, intAdmin.getCaId(), LogEntry.MODULE_SERVICES, new java.util.Date (), null, null, LogEntry.EVENT_ERROR_SERVICEEXECUTED, intres.getLocalizedMessage("services.serviceexecutionfailed", serviceName)); 213 } 214 } else { 215 getLogSession().log(intAdmin, intAdmin.getCaId(), LogEntry.MODULE_SERVICES, new java.util.Date (), null, null, LogEntry.EVENT_ERROR_SERVICEEXECUTED, intres.getLocalizedMessage("services.servicenotfound", timerInfo)); 216 } 217 }else{ 218 getLogSession().log(intAdmin, intAdmin.getCaId(), LogEntry.MODULE_SERVICES, new java.util.Date (), null, null, LogEntry.EVENT_INFO_SERVICEEXECUTED, intres.getLocalizedMessage("services.servicerunonothernode", timerInfo)); 219 } 220 } 221 debug("<ejbTimeout"); 222 } 223 231 public boolean checkAndUpdateServiceTimeout(long nextInterval, int timerInfo, ServiceConfiguration serviceData, String serviceName) { 232 boolean ret = false; 233 getSessionContext().getTimerService().createTimer(nextInterval*1000, timerInfo); 234 Date nextRunDate = serviceData.getNextRunTimestamp(); 235 Date currentDate = new Date (); 236 if(currentDate.after(nextRunDate)){ 237 nextRunDate = new Date (currentDate.getTime() + nextInterval); 238 serviceData.setNextRunTimestamp(nextRunDate); 239 getServiceSession().changeService(intAdmin, serviceName, serviceData); 240 ret=true; 241 } 242 return ret; 243 } 244 245 251 public void load(){ 252 254 Collection currentTimers = getSessionContext().getTimerService().getTimers(); 255 Iterator iter = currentTimers.iterator(); 256 HashSet existingTimers = new HashSet (); 257 while(iter.hasNext()){ 258 Timer timer = (Timer ) iter.next(); 259 try { 260 Serializable info = timer.getInfo(); 261 existingTimers.add(info); 262 } catch (Throwable e) { 263 log.debug("Error invoking timer.getInfo(): ", e); 265 } 266 } 267 268 HashMap idToNameMap = getServiceSession().getServiceIdToNameMap(intAdmin); 269 Collection allServices = idToNameMap.keySet(); 270 iter = allServices.iterator(); 271 while(iter.hasNext()){ 272 Integer id = (Integer ) iter.next(); 273 ServiceConfiguration serviceConfiguration = getServiceSession().getServiceConfiguration(intAdmin, id.intValue()); 274 if(!existingTimers.contains(id)){ 275 IWorker worker = getWorker(serviceConfiguration, (String ) idToNameMap.get(id)); 276 if(worker != null && serviceConfiguration.isActive() && worker.getNextInterval() != IInterval.DONT_EXECUTE){ 277 getSessionContext().getTimerService().createTimer((worker.getNextInterval()) *1000, id); 278 } 279 } 280 } 281 282 if(!existingTimers.contains(SERVICELOADER_ID)){ 283 getSessionContext().getTimerService().createTimer(SERVICELOADER_PERIOD, SERVICELOADER_ID); 285 } 286 } 287 288 294 public void unload(){ 295 Collection currentTimers = getSessionContext().getTimerService().getTimers(); 297 Iterator iter = currentTimers.iterator(); 298 while(iter.hasNext()){ 299 Timer timer = (Timer ) iter.next(); 300 timer.cancel(); 301 } 302 } 303 304 305 312 public void addTimer(long interval, Integer id){ 313 cancelTimer(id); 315 getSessionContext().getTimerService().createTimer(interval, id); 316 } 317 318 324 public void cancelTimer(Integer id){ 325 Collection timers = getSessionContext().getTimerService().getTimers(); 326 Iterator iter = timers.iterator(); 327 while(iter.hasNext()){ 328 Timer next = (Timer ) iter.next(); 329 if(id.equals(next.getInfo())){ 330 next.cancel(); 331 } 332 } 333 } 334 335 336 337 344 private IWorker getWorker(ServiceConfiguration serviceConfiguration, String serviceName) { 345 IWorker worker = null; 346 try { 347 String clazz = serviceConfiguration.getWorkerClassPath(); 348 if (StringUtils.isNotEmpty(clazz)) { 349 worker = (IWorker) this.getClass().getClassLoader().loadClass(clazz).newInstance(); 350 worker.init(intAdmin, serviceConfiguration, serviceName); 351 } else { 352 log.info("Worker has empty classpath for service "+serviceName); 353 } 354 } catch (Exception e) { 355 log.error("Worker is missconfigured, check the classpath",e); 356 } 357 358 return worker; 359 } 360 361 362 363 368 private ILogSessionLocal getLogSession() { 369 if (logsession == null) { 370 try { 371 ILogSessionLocalHome logsessionhome = (ILogSessionLocalHome) getLocator().getLocalHome(ILogSessionLocalHome.COMP_NAME); 372 logsession = logsessionhome.create(); 373 } catch (CreateException e) { 374 throw new EJBException (e); 375 } 376 } 377 return logsession; 378 } 380 381 382 383 388 private IServiceSessionLocal getServiceSession() { 389 IServiceSessionLocal servicesession = null; 390 try { 391 IServiceSessionLocalHome servicesessionhome = (IServiceSessionLocalHome) getLocator().getLocalHome(IServiceSessionLocalHome.COMP_NAME); 392 servicesession = servicesessionhome.create(); 393 } catch (CreateException e) { 394 throw new EJBException (e); 395 } 396 397 return servicesession ; 398 } 400 401 402 403 404 } | Popular Tags |