1 13 14 package org.ejbca.core.ejb.services; 15 16 import java.util.ArrayList ; 17 import java.util.Collection ; 18 import java.util.Date ; 19 import java.util.HashMap ; 20 import java.util.Iterator ; 21 import java.util.Random ; 22 23 import javax.ejb.CreateException ; 24 import javax.ejb.EJBException ; 25 import javax.ejb.FinderException ; 26 27 import org.ejbca.core.ejb.BaseSessionBean; 28 import org.ejbca.core.ejb.authorization.IAuthorizationSessionLocal; 29 import org.ejbca.core.ejb.authorization.IAuthorizationSessionLocalHome; 30 import org.ejbca.core.ejb.log.ILogSessionLocal; 31 import org.ejbca.core.ejb.log.ILogSessionLocalHome; 32 import org.ejbca.core.model.InternalResources; 33 import org.ejbca.core.model.authorization.AuthorizationDeniedException; 34 import org.ejbca.core.model.authorization.AvailableAccessRules; 35 import org.ejbca.core.model.log.Admin; 36 import org.ejbca.core.model.log.LogEntry; 37 import org.ejbca.core.model.services.IInterval; 38 import org.ejbca.core.model.services.IWorker; 39 import org.ejbca.core.model.services.ServiceConfiguration; 40 import org.ejbca.core.model.services.ServiceExistsException; 41 42 43 125 public class LocalServiceSessionBean extends BaseSessionBean { 126 127 130 private transient ServiceDataLocalHome servicehome = null; 131 132 135 private transient IServiceTimerSessionLocal serviceTimerSession = null; 136 137 138 141 private transient IAuthorizationSessionLocal authorizationsession = null; 142 143 146 private transient ILogSessionLocal logsession = null; 147 148 149 private static final InternalResources intres = InternalResources.getInstance(); 150 151 152 155 Admin intAdmin = new Admin(Admin.TYPE_INTERNALUSER); 156 157 162 public void ejbCreate() throws CreateException { 163 164 } 165 166 167 168 169 170 177 private IWorker getWorker(ServiceConfiguration serviceConfiguration, String serviceName) { 178 IWorker worker = null; 179 try { 180 worker = (IWorker) this.getClass().getClassLoader().loadClass(serviceConfiguration.getWorkerClassPath()).newInstance(); 181 worker.init(intAdmin, serviceConfiguration, serviceName); 182 } catch (Exception e) { 183 String msg = intres.getLocalizedMessage("services.errorworkerconfig", serviceConfiguration.getWorkerClassPath(), serviceName); 184 log.error(msg,e); 185 } 186 187 return worker; 188 } 189 190 191 192 197 private ILogSessionLocal getLogSession() { 198 if (logsession == null) { 199 try { 200 ILogSessionLocalHome logsessionhome = (ILogSessionLocalHome) getLocator().getLocalHome(ILogSessionLocalHome.COMP_NAME); 201 logsession = logsessionhome.create(); 202 } catch (CreateException e) { 203 throw new EJBException (e); 204 } 205 } 206 return logsession; 207 } 209 210 215 private ServiceDataLocalHome getServiceDataHome() { 216 if (servicehome == null) { 217 servicehome = (ServiceDataLocalHome) getLocator().getLocalHome(ServiceDataLocalHome.COMP_NAME); 218 } 219 return servicehome; 220 } 222 227 private IAuthorizationSessionLocal getAuthorizationSession() { 228 if (authorizationsession == null) { 229 try { 230 IAuthorizationSessionLocalHome authorizationsessionhome = (IAuthorizationSessionLocalHome) getLocator().getLocalHome(IAuthorizationSessionLocalHome.COMP_NAME); 231 authorizationsession = authorizationsessionhome.create(); 232 } catch (CreateException e) { 233 throw new EJBException (e); 234 } 235 } 236 return authorizationsession; 237 } 239 244 private IServiceTimerSessionLocal getServiceTimerSession() { 245 if (serviceTimerSession == null) { 246 try { 247 IServiceTimerSessionLocalHome servicetimersessionhome = (IServiceTimerSessionLocalHome) getLocator().getLocalHome(IServiceTimerSessionLocalHome.COMP_NAME); 248 serviceTimerSession = servicetimersessionhome.create(); 249 } catch (CreateException e) { 250 throw new EJBException (e); 251 } 252 } 253 return serviceTimerSession; 254 } 256 263 264 public void addService(Admin admin, String name, ServiceConfiguration serviceConfiguration) throws ServiceExistsException { 265 debug(">addService(name: " + name + ")"); 266 addService(admin,findFreeServiceId().intValue(),name,serviceConfiguration); 267 debug("<addService()"); 268 } 270 271 279 280 public void addService(Admin admin, int id, String name, ServiceConfiguration serviceConfiguration) throws ServiceExistsException { 281 debug(">addService(name: " + name + ", id: " + id + ")"); 282 boolean success = false; 283 if(isAuthorizedToEditService(admin,serviceConfiguration)){ 284 try { 285 getServiceDataHome().findByName(name); 286 } catch (FinderException e) { 287 try { 288 getServiceDataHome().findByPrimaryKey(new Integer (id)); 289 } catch (FinderException f) { 290 try { 291 getServiceDataHome().create(new Integer (id), name, serviceConfiguration); 292 success = true; 293 294 } catch (CreateException g) { 295 error("Unexpected error creating new service: ", g); 296 } 297 } 298 } 299 if (success){ 300 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_SERVICES, new java.util.Date (), null, null, LogEntry.EVENT_INFO_SERVICESEDITED, intres.getLocalizedMessage("services.serviceadded", name)); 301 }else{ 302 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_SERVICES, new java.util.Date (), null, null, LogEntry.EVENT_ERROR_SERVICESEDITED, intres.getLocalizedMessage("services.erroraddingservice", name)); 303 } 304 if (!success) 305 throw new ServiceExistsException(); 306 }else{ 307 getLogSession().log(admin, admin.getCaId(),LogEntry.MODULE_SERVICES,new Date (),null,null,LogEntry.EVENT_ERROR_NOTAUTHORIZEDTORESOURCE,intres.getLocalizedMessage("services.notauthorizedtoadd", name)); 308 } 309 debug("<addService()"); 310 } 312 318 319 public void changeService(Admin admin, String name, ServiceConfiguration serviceConfiguration) { 320 debug(">changeService(name: " + name + ")"); 321 boolean success = false; 322 if(isAuthorizedToEditService(admin,serviceConfiguration)){ 323 try { 324 ServiceDataLocal htp = getServiceDataHome().findByName(name); 325 htp.setServiceConfiguration(serviceConfiguration); 326 success = true; 327 } catch (FinderException e) { 328 error("Can not find service to change: "+name); 329 } 330 331 if (success){ 332 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_SERVICES, new java.util.Date (), null, null, LogEntry.EVENT_INFO_SERVICESEDITED, intres.getLocalizedMessage("services.serviceedited", name)); 333 }else{ 334 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_SERVICES, new java.util.Date (), null, null, LogEntry.EVENT_ERROR_SERVICESEDITED, intres.getLocalizedMessage("services.erroreditingservice", name)); 335 } 336 }else{ 337 getLogSession().log(admin, admin.getCaId(),LogEntry.MODULE_SERVICES,new Date (),null,null,LogEntry.EVENT_ERROR_NOTAUTHORIZEDTORESOURCE,intres.getLocalizedMessage("services.notauthorizedtoedit", name)); 338 } 339 340 debug("<changeService()"); 341 } 343 350 public void cloneService(Admin admin, String oldname, String newname) throws ServiceExistsException { 351 debug(">cloneService(name: " + oldname + ")"); 352 ServiceConfiguration servicedata = null; 353 try { 354 ServiceDataLocal htp = getServiceDataHome().findByName(oldname); 355 servicedata = (ServiceConfiguration) htp.getServiceConfiguration().clone(); 356 if(isAuthorizedToEditService(admin,servicedata)){ 357 try { 358 addService(admin, newname, servicedata); 359 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_SERVICES, new java.util.Date (), null, null, LogEntry.EVENT_INFO_SERVICESEDITED, intres.getLocalizedMessage("services.servicecloned", newname,oldname)); 360 } catch (ServiceExistsException f) { 361 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_SERVICES, new java.util.Date (), null, null, LogEntry.EVENT_ERROR_SERVICESEDITED, intres.getLocalizedMessage("services.errorcloningservice", newname, oldname)); 362 throw f; 363 } 364 }else{ 365 getLogSession().log(admin, admin.getCaId(),LogEntry.MODULE_SERVICES,new Date (),null,null,LogEntry.EVENT_ERROR_NOTAUTHORIZEDTORESOURCE, intres.getLocalizedMessage("services.notauthorizedtoedit", oldname)); 366 } 367 } catch (FinderException e) { 368 error("Error cloning service: ", e); 369 throw new EJBException (e); 370 } catch (CloneNotSupportedException e) { 371 error("Error cloning service: ", e); 372 throw new EJBException (e); 373 } 374 375 debug("<cloneService()"); 376 } 378 384 public boolean removeService(Admin admin, String name) { 385 debug(">removeService(name: " + name + ")"); 386 boolean retval = false; 387 try { 388 ServiceDataLocal htp = getServiceDataHome().findByName(name); 389 ServiceConfiguration serviceConfiguration = htp.getServiceConfiguration(); 390 if(isAuthorizedToEditService(admin,serviceConfiguration)){ 391 IWorker worker = getWorker(serviceConfiguration, name); 392 if(worker != null){ 393 getServiceTimerSession().cancelTimer(htp.getId()); 394 } 395 htp.remove(); 396 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_SERVICES, new java.util.Date (), null, null, LogEntry.EVENT_INFO_SERVICESEDITED, intres.getLocalizedMessage("services.serviceremoved", name)); 397 retval = true; 398 }else{ 399 getLogSession().log(admin, admin.getCaId(),LogEntry.MODULE_SERVICES,new Date (),null,null,LogEntry.EVENT_ERROR_NOTAUTHORIZEDTORESOURCE, intres.getLocalizedMessage("services.notauthorizedtoedit", name)); 400 } 401 } catch (Exception e) { 402 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_SERVICES, new java.util.Date (), null, null, LogEntry.EVENT_ERROR_SERVICESEDITED, intres.getLocalizedMessage("services.errorremovingservice", name), e); 403 } 404 debug("<removeService)"); 405 406 return retval; 407 } 409 416 public void renameService(Admin admin, String oldname, String newname) throws ServiceExistsException { 417 debug(">renameService(from " + oldname + " to " + newname + ")"); 418 boolean success = false; 419 try { 420 getServiceDataHome().findByName(newname); 421 } catch (FinderException e) { 422 try { 423 ServiceDataLocal htp = getServiceDataHome().findByName(oldname); 424 if(isAuthorizedToEditService(admin,htp.getServiceConfiguration())){ 425 htp.setName(newname); 426 success = true; 427 }else{ 428 getLogSession().log(admin, admin.getCaId(),LogEntry.MODULE_SERVICES,new Date (),null,null,LogEntry.EVENT_ERROR_NOTAUTHORIZEDTORESOURCE, intres.getLocalizedMessage("services.notauthorizedtoedit", oldname)); 429 } 430 } catch (FinderException g) { 431 } 432 } 433 434 if (success){ 435 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_SERVICES, new java.util.Date (), null, null, LogEntry.EVENT_INFO_SERVICESEDITED, intres.getLocalizedMessage("services.servicerenamed", oldname, newname)); 436 }else{ 437 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_SERVICES, new java.util.Date (), null, null, LogEntry.EVENT_ERROR_SERVICESEDITED, intres.getLocalizedMessage("services.errorrenamingservice", oldname, newname)); 438 } 439 if (!success) 440 throw new ServiceExistsException(); 441 debug("<renameService()"); 442 } 444 451 public Collection getAuthorizedServiceIds(Admin admin) { 452 Collection returnval = new ArrayList (); 453 454 try{ 456 getAuthorizationSession().isAuthorizedNoLog(admin, AvailableAccessRules.ROLE_SUPERADMINISTRATOR); 457 returnval = getServiceIdToNameMap(admin).keySet(); 458 }catch (AuthorizationDeniedException e1) { 459 log.debug("AuthorizationDeniedException: ", e1); 460 } 461 462 return returnval; 463 } 465 471 public HashMap getServiceIdToNameMap(Admin admin) { 472 HashMap returnval = new HashMap (); 473 Collection result = null; 474 475 try { 476 result = getServiceDataHome().findAll(); 477 Iterator i = result.iterator(); 478 while (i.hasNext()) { 479 ServiceDataLocal next = (ServiceDataLocal) i.next(); 480 returnval.put(next.getId(), next.getName()); 481 } 482 } catch (FinderException e) { 483 } 484 return returnval; 485 } 487 488 495 public ServiceConfiguration getService(Admin admin, String name) { 496 ServiceConfiguration returnval = null; 497 498 try { 499 returnval = (getServiceDataHome().findByName(name)).getServiceConfiguration(); 500 } catch (FinderException e) { 501 } 503 return returnval; 504 } 506 513 public ServiceConfiguration getServiceConfiguration(Admin admin, int id) { 514 ServiceConfiguration returnval = null; 515 516 try { 517 returnval = (getServiceDataHome().findByPrimaryKey(new Integer (id))).getServiceConfiguration(); 518 } catch (FinderException e) { 519 } 521 return returnval; 522 } 524 525 526 533 public int getServiceId(Admin admin, String name) { 534 int returnval = 0; 535 536 try { 537 Integer id = (getServiceDataHome().findByName(name)).getId(); 538 returnval = id.intValue(); 539 } catch (FinderException e) { 540 } 541 542 return returnval; 543 } 545 553 public String getServiceName(Admin admin, int id) { 554 debug(">getServiceName(id: " + id + ")"); 555 String returnval = null; 556 ServiceDataLocal htp = null; 557 try { 558 htp = getServiceDataHome().findByPrimaryKey(new Integer (id)); 559 if (htp != null) { 560 returnval = htp.getName(); 561 } 562 } catch (FinderException e) { 563 } 564 565 debug("<getServiceName()"); 566 return returnval; 567 } 569 570 579 public void activateServiceTimer(Admin admin, String name) { 580 debug(">activateServiceTimer(name: " + name + ")"); 581 try { 582 ServiceDataLocal htp = getServiceDataHome().findByName(name); 583 ServiceConfiguration serviceConfiguration = htp.getServiceConfiguration(); 584 if(isAuthorizedToEditService(admin,serviceConfiguration)){ 585 IWorker worker = getWorker(serviceConfiguration, name); 586 if(worker != null){ 587 getServiceTimerSession().cancelTimer(htp.getId()); 588 if(serviceConfiguration.isActive() && worker.getNextInterval() != IInterval.DONT_EXECUTE){ 589 getServiceTimerSession().addTimer(worker.getNextInterval() *1000, htp.getId()); 590 } 591 } 592 }else{ 593 getLogSession().log(admin, admin.getCaId(),LogEntry.MODULE_SERVICES,new Date (),null,null,LogEntry.EVENT_ERROR_NOTAUTHORIZEDTORESOURCE,intres.getLocalizedMessage("services.notauthorizedtoedit", name)); 594 } 595 } catch (FinderException e) { 596 log.error("Can not find service: "+name); 597 } 598 debug("<activateServiceTimer()"); 599 } 601 602 610 private boolean isAuthorizedToEditService(Admin admin, ServiceConfiguration serviceConfiguraion) { 611 try { 612 if(getAuthorizationSession().isAuthorizedNoLog(admin,AvailableAccessRules.ROLE_SUPERADMINISTRATOR)){ 613 return true; 614 } 615 } catch (AuthorizationDeniedException e) {} 616 617 return false; 618 } 619 620 621 private Integer findFreeServiceId() { 622 Random ran = (new Random ((new Date ()).getTime())); 623 int id = ran.nextInt(); 624 boolean foundfree = false; 625 626 while (!foundfree) { 627 try { 628 if (id > 1) 629 getServiceDataHome().findByPrimaryKey(new Integer (id)); 630 id = ran.nextInt(); 631 } catch (FinderException e) { 632 foundfree = true; 633 } 634 } 635 return new Integer (id); 636 } 638 639 } | Popular Tags |