/** * {@inheritDoc} * * @see * org.opencastproject.capture.admin.api.CaptureAgentStateService#setAgentState(java.lang.String, * java.lang.String) */ public int setAgentState(String agentName, String state) { // Checks the state is not null nor empty if (StringUtils.isBlank(state)) { logger.debug("Unable to set agent state, state is blank or null."); return BAD_PARAMETER; } else if (StringUtils.isBlank(agentName)) { logger.debug("Unable to set agent state, agent name is blank or null."); return BAD_PARAMETER; } else if (!KNOWN_STATES.contains(state)) { logger.warn("can not set agent to an invalid state: ", state); return BAD_PARAMETER; } else { logger.debug("Agent '{}' state set to '{}'", agentName, state); } AgentImpl agent = getAgent(agentName); if (agent == null) { // If the agent doesn't exists, but the name is not null nor empty, create a new one. logger.debug("Creating Agent {} with state {}.", agentName, state); Organization org = securityService.getOrganization(); AgentImpl a = new AgentImpl(agentName, org.getId(), state, "", new Properties()); updateAgentInDatabase(a); } else if (!agent.getState().equals(state)) { // the agent is known, so set the state logger.debug("Setting Agent {} to state {}.", agentName, state); agent.setState(state); updateAgentInDatabase(agent); } return OK; }
public boolean setAgentUrl(String agentName, String agentUrl) { AgentImpl agent = getAgent(agentName); if (agent == null) { return false; } else { agent.setUrl(agentUrl); updateAgentInDatabase(agent); } return true; }
/** * {@inheritDoc} * * @see org.osgi.service.cm.ManagedServiceFactory#updated(java.lang.String, java.util.Dictionary) */ @Override public void updated(String pid, Dictionary properties) throws ConfigurationException { // Get the agent properties String nameConfig = (String) properties.get("id"); if (isEmpty(nameConfig)) { throw new ConfigurationException("id", "must be specified"); } nameConfig = nameConfig.trim(); String urlConfig = (String) properties.get("url"); if (isEmpty(urlConfig)) { throw new ConfigurationException("url", "must be specified"); } urlConfig = urlConfig.trim(); String orgConfig = (String) properties.get("organization"); if (isEmpty(orgConfig)) { throw new ConfigurationException("organization", "must be specified"); } orgConfig = orgConfig.trim(); String schedulerRolesConfig = (String) properties.get("schedulerRoles"); if (isEmpty(schedulerRolesConfig)) { throw new ConfigurationException("schedulerRoles", "must be specified"); } schedulerRolesConfig = schedulerRolesConfig.trim(); // If we don't already have a mapping for this PID, create one if (!pidMap.containsKey(pid)) { pidMap.put(pid, nameConfig); } AgentImpl agent = getAgent(nameConfig, orgConfig); if (agent == null) { agent = new AgentImpl(nameConfig, orgConfig, UNKNOWN, urlConfig, new Properties()); } else { agent.url = urlConfig.trim(); agent.organization = orgConfig.trim(); agent.state = UNKNOWN; String[] schedulerRoles = schedulerRolesConfig.split(","); for (String role : schedulerRoles) { agent.schedulerRoles.add(role.trim()); } } // Update the database logger.info("Roles '{}' may schedule '{}'", schedulerRolesConfig, agent.name); updateAgentInDatabase(agent); }
/** * {@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(); } }
/** * {@inheritDoc} * * @see org.opencastproject.capture.admin.api.CaptureAgentStateService#setAgentConfiguration */ public int setAgentConfiguration(String agentName, Properties configuration) { AgentImpl agent = getAgent(agentName); if (agent != null) { logger.debug("Setting Agent {}'s capabilities", agentName); agent.setConfiguration(configuration); updateAgentInDatabase(agent); } else { // If the agent doesn't exists, but the name is not null nor empty, create a new one. if (StringUtils.isBlank(agentName)) { logger.debug("Unable to set agent state, agent name is blank or null."); return BAD_PARAMETER; } logger.debug("Creating Agent {} with state {}.", agentName, UNKNOWN); Organization org = securityService.getOrganization(); AgentImpl a = new AgentImpl(agentName, org.getId(), UNKNOWN, "", configuration); updateAgentInDatabase(a); } return OK; }
/** * 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(); } }