📄 localservicesessionbean.java
字号:
boolean success = false; if(isAuthorizedToEditService(admin,serviceConfiguration)){ try { ServiceDataLocal htp = getServiceDataHome().findByName(name); htp.setServiceConfiguration(serviceConfiguration); success = true; } catch (FinderException e) { error("Can not find service to change: "+name); } if (success){ getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_SERVICES, new java.util.Date(), null, null, LogEntry.EVENT_INFO_SERVICESEDITED, intres.getLocalizedMessage("services.serviceedited", name)); }else{ getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_SERVICES, new java.util.Date(), null, null, LogEntry.EVENT_ERROR_SERVICESEDITED, intres.getLocalizedMessage("services.erroreditingservice", name)); } }else{ getLogSession().log(admin, admin.getCaId(),LogEntry.MODULE_SERVICES,new Date(),null,null,LogEntry.EVENT_ERROR_NOTAUTHORIZEDTORESOURCE,intres.getLocalizedMessage("services.notauthorizedtoedit", name)); } debug("<changeService()"); } // changeService /** * Adds a service with the same content as the original. * * @throws ServiceExistsException if service already exists. * @throws EJBException if a communication or other error occurs. * @ejb.interface-method view-type="both" */ public void cloneService(Admin admin, String oldname, String newname) throws ServiceExistsException { debug(">cloneService(name: " + oldname + ")"); ServiceConfiguration servicedata = null; try { ServiceDataLocal htp = getServiceDataHome().findByName(oldname); servicedata = (ServiceConfiguration) htp.getServiceConfiguration().clone(); if(isAuthorizedToEditService(admin,servicedata)){ try { addService(admin, newname, servicedata); getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_SERVICES, new java.util.Date(), null, null, LogEntry.EVENT_INFO_SERVICESEDITED, intres.getLocalizedMessage("services.servicecloned", newname,oldname)); } catch (ServiceExistsException f) { getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_SERVICES, new java.util.Date(), null, null, LogEntry.EVENT_ERROR_SERVICESEDITED, intres.getLocalizedMessage("services.errorcloningservice", newname, oldname)); throw f; } }else{ getLogSession().log(admin, admin.getCaId(),LogEntry.MODULE_SERVICES,new Date(),null,null,LogEntry.EVENT_ERROR_NOTAUTHORIZEDTORESOURCE, intres.getLocalizedMessage("services.notauthorizedtoedit", oldname)); } } catch (FinderException e) { error("Error cloning service: ", e); throw new EJBException(e); } catch (CloneNotSupportedException e) { error("Error cloning service: ", e); throw new EJBException(e); } debug("<cloneService()"); } // cloneService /** * Removes a service from the database. * * @throws EJBException if a communication or other error occurs. * @ejb.interface-method view-type="both" */ public boolean removeService(Admin admin, String name) { debug(">removeService(name: " + name + ")"); boolean retval = false; try { ServiceDataLocal htp = getServiceDataHome().findByName(name); ServiceConfiguration serviceConfiguration = htp.getServiceConfiguration(); if(isAuthorizedToEditService(admin,serviceConfiguration)){ IWorker worker = getWorker(serviceConfiguration, name); if(worker != null){ getServiceTimerSession().cancelTimer(htp.getId()); } htp.remove(); getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_SERVICES, new java.util.Date(), null, null, LogEntry.EVENT_INFO_SERVICESEDITED, intres.getLocalizedMessage("services.serviceremoved", name)); retval = true; }else{ getLogSession().log(admin, admin.getCaId(),LogEntry.MODULE_SERVICES,new Date(),null,null,LogEntry.EVENT_ERROR_NOTAUTHORIZEDTORESOURCE, intres.getLocalizedMessage("services.notauthorizedtoedit", name)); } } catch (Exception e) { getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_SERVICES, new java.util.Date(), null, null, LogEntry.EVENT_ERROR_SERVICESEDITED, intres.getLocalizedMessage("services.errorremovingservice", name), e); } debug("<removeService)"); return retval; } // removeService /** * Renames a service * * @throws ServiceExistsException if service already exists. * @throws EJBException if a communication or other error occurs. * @ejb.interface-method view-type="both" */ public void renameService(Admin admin, String oldname, String newname) throws ServiceExistsException { debug(">renameService(from " + oldname + " to " + newname + ")"); boolean success = false; try { getServiceDataHome().findByName(newname); } catch (FinderException e) { try { ServiceDataLocal htp = getServiceDataHome().findByName(oldname); if(isAuthorizedToEditService(admin,htp.getServiceConfiguration())){ htp.setName(newname); success = true; }else{ getLogSession().log(admin, admin.getCaId(),LogEntry.MODULE_SERVICES,new Date(),null,null,LogEntry.EVENT_ERROR_NOTAUTHORIZEDTORESOURCE, intres.getLocalizedMessage("services.notauthorizedtoedit", oldname)); } } catch (FinderException g) { } } if (success){ getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_SERVICES, new java.util.Date(), null, null, LogEntry.EVENT_INFO_SERVICESEDITED, intres.getLocalizedMessage("services.servicerenamed", oldname, newname)); }else{ getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_SERVICES, new java.util.Date(), null, null, LogEntry.EVENT_ERROR_SERVICESEDITED, intres.getLocalizedMessage("services.errorrenamingservice", oldname, newname)); } if (!success) throw new ServiceExistsException(); debug("<renameService()"); } // renameService /** * Retrives a Collection of id:s (Integer) to authorized services. * Currently is the only check if the superadmin can see them all * * @return Collection of id:s (Integer) * @ejb.interface-method view-type="both" */ public Collection getAuthorizedServiceIds(Admin admin) { Collection returnval = new ArrayList(); // If superadmin return all service try{ getAuthorizationSession().isAuthorizedNoLog(admin, AvailableAccessRules.ROLE_SUPERADMINISTRATOR); returnval = getServiceIdToNameMap(admin).keySet(); }catch (AuthorizationDeniedException e1) { log.debug("AuthorizationDeniedException: ", e1); } return returnval; } // getAuthorizedServiceIds /** * Method creating a hashmap mapping service id (Integer) to service name (String). * * @ejb.transaction type="Supports" * @ejb.interface-method view-type="both" */ public HashMap getServiceIdToNameMap(Admin admin) { HashMap returnval = new HashMap(); Collection result = null; try { result = getServiceDataHome().findAll(); Iterator i = result.iterator(); while (i.hasNext()) { ServiceDataLocal next = (ServiceDataLocal) i.next(); returnval.put(next.getId(), next.getName()); } } catch (FinderException e) { } return returnval; } // getServiceIdToNameMap /** * Retrives a named service. * * @returns the service configuration or null if it doesn't exist. * @ejb.transaction type="Supports" * @ejb.interface-method view-type="both" */ public ServiceConfiguration getService(Admin admin, String name) { ServiceConfiguration returnval = null; try { returnval = (getServiceDataHome().findByName(name)).getServiceConfiguration(); } catch (FinderException e) { // return null if we cant find it } return returnval; } // getService /** * Finds a service configuration by id. * * @returns the service configuration or null if it doesn't exist. * @ejb.transaction type="Supports" * @ejb.interface-method view-type="both" */ public ServiceConfiguration getServiceConfiguration(Admin admin, int id) { ServiceConfiguration returnval = null; try { returnval = (getServiceDataHome().findByPrimaryKey(new Integer(id))).getServiceConfiguration(); } catch (FinderException e) { // return null if we cant find it } return returnval; } // getServiceConfiguration /** * Returns a service id, given it's service name * * @return the id or 0 if the service cannot be found. * @ejb.transaction type="Supports" * @ejb.interface-method view-type="both" */ public int getServiceId(Admin admin, String name) { int returnval = 0; try { Integer id = (getServiceDataHome().findByName(name)).getId(); returnval = id.intValue(); } catch (FinderException e) { } return returnval; } // getServiceId /** * Returns a Service name given its id. * * @return the name or null if id doesnt exists * @throws EJBException if a communication or other error occurs. * @ejb.transaction type="Supports" * @ejb.interface-method view-type="both" */ public String getServiceName(Admin admin, int id) { debug(">getServiceName(id: " + id + ")"); String returnval = null; ServiceDataLocal htp = null; try { htp = getServiceDataHome().findByPrimaryKey(new Integer(id)); if (htp != null) { returnval = htp.getName(); } } catch (FinderException e) { } debug("<getServiceName()"); return returnval; } // getServiceName /** * Activates the timer for a named service. The service must alrteady be previously added. * * @param admin The administrator performing the action * @param name the name of the service for which to activate the timer * @throws EJBException if a communication or other error occurs. * @ejb.transaction type="Supports" * @ejb.interface-method view-type="both" */ public void activateServiceTimer(Admin admin, String name) { debug(">activateServiceTimer(name: " + name + ")"); try { ServiceDataLocal htp = getServiceDataHome().findByName(name); ServiceConfiguration serviceConfiguration = htp.getServiceConfiguration(); if(isAuthorizedToEditService(admin,serviceConfiguration)){ IWorker worker = getWorker(serviceConfiguration, name); if(worker != null){ getServiceTimerSession().cancelTimer(htp.getId()); if(serviceConfiguration.isActive() && worker.getNextInterval() != IInterval.DONT_EXECUTE){ getServiceTimerSession().addTimer(worker.getNextInterval() *1000, htp.getId()); } } }else{ getLogSession().log(admin, admin.getCaId(),LogEntry.MODULE_SERVICES,new Date(),null,null,LogEntry.EVENT_ERROR_NOTAUTHORIZEDTORESOURCE,intres.getLocalizedMessage("services.notauthorizedtoedit", name)); } } catch (FinderException e) { log.error("Can not find service: "+name); } debug("<activateServiceTimer()"); } // getServiceName /** * Method to check if an admin is authorized to edit a service * The following checks are performed. * * 1. If the admin is an super administrator * * @return true if the administrator is authorized */ private boolean isAuthorizedToEditService(Admin admin, ServiceConfiguration serviceConfiguraion) { try { if(getAuthorizationSession().isAuthorizedNoLog(admin,AvailableAccessRules.ROLE_SUPERADMINISTRATOR)){ return true; } } catch (AuthorizationDeniedException e) {} return false; } private Integer findFreeServiceId() { Random ran = (new Random((new Date()).getTime())); int id = ran.nextInt(); boolean foundfree = false; while (!foundfree) { try { if (id > 1) getServiceDataHome().findByPrimaryKey(new Integer(id)); id = ran.nextInt(); } catch (FinderException e) { foundfree = true; } } return new Integer(id); } // findFreeServiceId} // LocalServiceSessionBean
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -