/** * Updates or adds an agent to the database. * * @param agent The Agent you wish to modify or add in the database. */ protected void updateAgentInDatabase(AgentImpl agent) { EntityManager em = null; EntityTransaction tx = null; try { em = emf.createEntityManager(); tx = em.getTransaction(); tx.begin(); AgentImpl existing = getAgent(agent.getName(), agent.getOrganization(), em); if (existing == null) { em.persist(agent); } else { existing.setConfiguration(agent.getConfiguration()); existing.setLastHeardFrom(agent.getLastHeardFrom()); existing.setState(agent.getState()); existing.setSchedulerRoles(agent.getSchedulerRoles()); existing.setUrl(agent.getUrl()); em.merge(existing); } tx.commit(); } catch (RollbackException e) { logger.warn("Unable to commit to DB in updateAgent."); throw e; } finally { if (em != null) em.close(); } }
/** * {@inheritDoc} * * @see org.opencastproject.capture.admin.api.CaptureAgentStateService#getKnownAgents() */ public Map<String, Agent> getKnownAgents() { EntityManager em = null; User user = securityService.getUser(); Organization org = securityService.getOrganization(); String orgAdmin = org.getAdminRole(); String[] roles = user.getRoles(); try { em = emf.createEntityManager(); Query q = em.createNamedQuery("Agent.byOrganization"); q.setParameter("org", securityService.getOrganization().getId()); // Filter the results in memory if this user is not an administrator List<AgentImpl> agents = q.getResultList(); if (!user.hasRole(SecurityConstants.GLOBAL_ADMIN_ROLE) && !user.hasRole(orgAdmin)) { for (Iterator<AgentImpl> iter = agents.iterator(); iter.hasNext(); ) { AgentImpl agent = iter.next(); Set<String> schedulerRoles = agent.getSchedulerRoles(); // If there are no roles associated with this capture agent, it is available to anyone who // can pass the // coarse-grained web layer security if (schedulerRoles == null || schedulerRoles.isEmpty()) { continue; } boolean hasSchedulerRole = false; for (String role : roles) { if (schedulerRoles.contains(role)) { hasSchedulerRole = true; break; } } if (!hasSchedulerRole) { iter.remove(); } } } // Build the map that the API defines as agent name->agent Map<String, Agent> map = new TreeMap<String, Agent>(); for (AgentImpl agent : agents) { map.put(agent.getName(), agent); } return map; } finally { if (em != null) em.close(); } }