Search in sources :

Example 6 with HostRegistrationJpaImpl

use of org.opencastproject.serviceregistry.impl.jpa.HostRegistrationJpaImpl in project opencast by opencast.

the class ServiceRegistryJpaImpl method enableHost.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.serviceregistry.api.ServiceRegistry#enableHost(String)
 */
@Override
public void enableHost(String host) throws ServiceRegistryException, NotFoundException {
    EntityManager em = null;
    EntityTransaction tx = null;
    try {
        em = emf.createEntityManager();
        tx = em.getTransaction();
        tx.begin();
        // Find the existing registrations for this host and if it exists, update it
        HostRegistrationJpaImpl hostRegistration = fetchHostRegistration(em, host);
        if (hostRegistration == null) {
            throw new NotFoundException("Host '" + host + "' is currently not registered, so it can not be enabled");
        } else {
            hostRegistration.setActive(true);
            em.merge(hostRegistration);
        }
        logger.info("Enabling {}", host);
        tx.commit();
        tx.begin();
        for (ServiceRegistration serviceRegistration : getServiceRegistrationsByHost(host)) {
            ServiceRegistrationJpaImpl registration = (ServiceRegistrationJpaImpl) serviceRegistration;
            registration.setActive(true);
            em.merge(registration);
            servicesStatistics.updateService(registration);
        }
        tx.commit();
        hostsStatistics.updateHost(hostRegistration);
    } catch (NotFoundException e) {
        throw e;
    } catch (Exception e) {
        if (tx != null && tx.isActive()) {
            tx.rollback();
        }
        throw new ServiceRegistryException(e);
    } finally {
        if (em != null)
            em.close();
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) NotFoundException(org.opencastproject.util.NotFoundException) ServiceRegistrationJpaImpl(org.opencastproject.serviceregistry.impl.jpa.ServiceRegistrationJpaImpl) HostRegistrationJpaImpl(org.opencastproject.serviceregistry.impl.jpa.HostRegistrationJpaImpl) URISyntaxException(java.net.URISyntaxException) NoResultException(javax.persistence.NoResultException) ConfigurationException(org.osgi.service.cm.ConfigurationException) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) TrustedHttpClientException(org.opencastproject.security.api.TrustedHttpClientException) PersistenceException(javax.persistence.PersistenceException) RollbackException(javax.persistence.RollbackException) NotFoundException(org.opencastproject.util.NotFoundException) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) ServiceRegistration(org.opencastproject.serviceregistry.api.ServiceRegistration)

Example 7 with HostRegistrationJpaImpl

use of org.opencastproject.serviceregistry.impl.jpa.HostRegistrationJpaImpl in project opencast by opencast.

the class ServiceRegistryJpaImpl method setMaintenanceStatus.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.serviceregistry.api.ServiceRegistry#setMaintenanceStatus(java.lang.String, boolean)
 */
@Override
public void setMaintenanceStatus(String baseUrl, boolean maintenance) throws NotFoundException {
    EntityManager em = null;
    EntityTransaction tx = null;
    try {
        em = emf.createEntityManager();
        tx = em.getTransaction();
        tx.begin();
        HostRegistrationJpaImpl reg = fetchHostRegistration(em, baseUrl);
        if (reg == null) {
            throw new NotFoundException("Can not set maintenance mode on a host that has not been registered");
        }
        reg.setMaintenanceMode(maintenance);
        em.merge(reg);
        tx.commit();
        hostsStatistics.updateHost(reg);
    } catch (RollbackException e) {
        if (tx != null && tx.isActive()) {
            tx.rollback();
        }
        throw e;
    } finally {
        if (em != null)
            em.close();
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) NotFoundException(org.opencastproject.util.NotFoundException) RollbackException(javax.persistence.RollbackException) HostRegistrationJpaImpl(org.opencastproject.serviceregistry.impl.jpa.HostRegistrationJpaImpl)

Aggregations

HostRegistrationJpaImpl (org.opencastproject.serviceregistry.impl.jpa.HostRegistrationJpaImpl)7 EntityManager (javax.persistence.EntityManager)6 EntityTransaction (javax.persistence.EntityTransaction)6 NoResultException (javax.persistence.NoResultException)6 RollbackException (javax.persistence.RollbackException)6 NotFoundException (org.opencastproject.util.NotFoundException)6 URISyntaxException (java.net.URISyntaxException)5 PersistenceException (javax.persistence.PersistenceException)5 TrustedHttpClientException (org.opencastproject.security.api.TrustedHttpClientException)5 ServiceRegistryException (org.opencastproject.serviceregistry.api.ServiceRegistryException)5 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)5 ConfigurationException (org.osgi.service.cm.ConfigurationException)5 ServiceRegistration (org.opencastproject.serviceregistry.api.ServiceRegistration)3 ServiceRegistrationJpaImpl (org.opencastproject.serviceregistry.impl.jpa.ServiceRegistrationJpaImpl)3 Query (javax.persistence.Query)1 TypedQuery (javax.persistence.TypedQuery)1