/** * {@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; }
/** * {@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(); } }
/** * Gets an agent by name, using the current user's organizational context. * * @param name the unique agent name * @return the agent */ protected AgentImpl getAgent(String name) { EntityManager em = null; try { em = emf.createEntityManager(); return getAgent(name, securityService.getOrganization().getId(), em); } finally { if (em != null) em.close(); } }
@Before public void setUp() throws UserTrackingException { SecurityService security = EasyMock.createMock(SecurityService.class); EasyMock.expect(security.getUser()) .andReturn(new User(MOCK_USER, "mh_default", new String[] {"ROLE_USER"})) .anyTimes(); BundleContext bc = EasyMock.createMock(BundleContext.class); EasyMock.expect(bc.getProperty("org.opencastproject.server.url")) .andReturn("http://www.example.org:8080") .anyTimes(); @SuppressWarnings("rawtypes") Dictionary dict = EasyMock.createMock(Dictionary.class); EasyMock.expect(dict.get(RestConstants.SERVICE_PATH_PROPERTY)) .andReturn("/usertracking") .anyTimes(); ComponentContext context = EasyMock.createMock(ComponentContext.class); EasyMock.expect(context.getBundleContext()).andReturn(bc).anyTimes(); EasyMock.expect(context.getProperties()).andReturn(dict).anyTimes(); UserActionImpl ua = EasyMock.createMock(UserActionImpl.class); EasyMock.expect(ua.getId()).andReturn(4L).anyTimes(); UserTrackingService usertracking = EasyMock.createMock(UserTrackingService.class); EasyMock.expect(usertracking.addUserFootprint(EasyMock.isA(UserAction.class))) .andReturn(ua) .anyTimes(); EasyMock.replay(security, bc, dict, context, ua, usertracking); service = new UserTrackingRestService(); service.setSecurityService(security); service.setService(usertracking); service.activate(context); }
/** * Removes an agent from the database. * * @param agentName The name of the agent you wish to remove. */ private void deleteAgentFromDatabase(String agentName) { EntityManager em = null; EntityTransaction tx = null; try { em = emf.createEntityManager(); tx = em.getTransaction(); tx.begin(); Agent existing = getAgent(agentName, securityService.getOrganization().getId(), em); if (existing != null) { em.remove(existing); } tx.commit(); } catch (RollbackException e) { logger.warn("Unable to commit to DB in deleteAgent."); } 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; }