Example usage for org.hibernate StatelessSession close

List of usage examples for org.hibernate StatelessSession close

Introduction

In this page you can find the example usage for org.hibernate StatelessSession close.

Prototype

void close();

Source Link

Document

Close the stateless session and release the JDBC connection.

Usage

From source file:org.jahia.modules.external.id.ExternalProviderInitializerServiceImpl.java

License:Open Source License

@Override
public String getExternalIdentifier(String internalId) throws RepositoryException {
    String externalId = null;/*from  ww  w. j  a v  a  2 s.c o m*/
    StatelessSession session = null;
    try {
        session = getHibernateSessionFactory().openStatelessSession();
        session.beginTransaction();
        UuidMapping mapping = (UuidMapping) session.get(UuidMapping.class, internalId);
        if (mapping != null) {
            externalId = mapping.getExternalId();
        }
        session.getTransaction().commit();
    } catch (Exception e) {
        if (session != null) {
            session.getTransaction().rollback();
        }
        throw new RepositoryException(e);
    } finally {
        if (session != null) {
            session.close();
        }
    }

    return externalId;
}

From source file:org.jahia.modules.external.id.ExternalProviderInitializerServiceImpl.java

License:Open Source License

@Override
public String getInternalIdentifier(String externalId, String providerKey) throws RepositoryException {
    int hash = externalId.hashCode();
    String cacheKey = getCacheKey(hash, providerKey);
    String uuid = getIdentifierCache().get(cacheKey) != null
            ? (String) getIdentifierCache().get(cacheKey).getObjectValue()
            : null;/* w  w w.j av  a 2  s . co  m*/
    if (uuid == null) {
        StatelessSession session = null;
        try {
            session = getHibernateSessionFactory().openStatelessSession();
            session.beginTransaction();
            List<?> list = session
                    .createQuery("from UuidMapping where providerKey=:providerKey and externalIdHash=:idHash")
                    .setString("providerKey", providerKey).setLong("idHash", hash).setReadOnly(true).list();
            if (list.size() > 0) {
                uuid = ((UuidMapping) list.get(0)).getInternalUuid();
                getIdentifierCache().put(new Element(cacheKey, uuid, true));
            }
            session.getTransaction().commit();
        } catch (Exception e) {
            if (session != null) {
                session.getTransaction().rollback();
            }
            throw new RepositoryException(e);
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

    return uuid;
}

From source file:org.jahia.modules.external.id.ExternalProviderInitializerServiceImpl.java

License:Open Source License

@Override
public void removeProvider(String providerKey) throws RepositoryException {
    SessionFactory hibernateSession = getHibernateSessionFactory();
    StatelessSession session = null;
    try {//from   w ww.j ava 2 s.  c  om
        session = hibernateSession.openStatelessSession();
        session.beginTransaction();
        int deletedCount = session.createQuery("delete from ExternalProviderID where providerKey=:providerKey")
                .setString("providerKey", providerKey).executeUpdate();
        if (deletedCount > 0) {
            logger.info("Deleted external provider entry for key {}", providerKey);
            deletedCount = session.createQuery("delete from UuidMapping where providerKey=:providerKey")
                    .setString("providerKey", providerKey).executeUpdate();
            logger.info("Deleted {} identifier mapping entries for external provider with key {}", deletedCount,
                    providerKey);
        } else {
            logger.info("No external provider entry found for key {}", providerKey);
        }
        session.getTransaction().commit();
    } catch (Exception e) {
        if (session != null) {
            session.getTransaction().rollback();
        }
        throw new RepositoryException(
                "Issue when removing external provider entry and identifier mappings for provider key "
                        + providerKey,
                e);
    } finally {
        if (session != null) {
            session.close();
        }
    }
}

From source file:org.jahia.modules.ugp.showcase.DbUserGroupProvider.java

License:Open Source License

@Override
public JahiaGroup getGroup(String name) throws GroupNotFoundException {
    JahiaGroup foundGroup = null;/*from   www.j  a va2 s.com*/
    StatelessSession hib = sessionFactoryBean.openStatelessSession();
    hib.beginTransaction();

    try {
        @SuppressWarnings("unchecked")
        List<Group> groups = hib.createCriteria(Group.class).add(Restrictions.idEq(name)).list();

        foundGroup = !groups.isEmpty() ? toGroup(groups.iterator().next()) : null;
        hib.getTransaction().commit();
    } catch (HibernateException e) {
        logger.error(e.getMessage(), e);
        hib.getTransaction().rollback();
    } finally {
        hib.close();
    }

    if (foundGroup == null) {
        throw new GroupNotFoundException("Unable to find group " + name + " for the provider " + getKey());
    }

    return foundGroup;
}

From source file:org.jahia.modules.ugp.showcase.DbUserGroupProvider.java

License:Open Source License

@Override
public List<Member> getGroupMembers(String groupName) {
    List<Member> members = null;
    StatelessSession hib = sessionFactoryBean.openStatelessSession();
    hib.beginTransaction();//from www.  j  a va 2s .c o m

    try {

        @SuppressWarnings("unchecked")
        Set<Group> groups = new LinkedHashSet<>(hib.createCriteria(Group.class)
                .add(Restrictions.idEq(groupName)).setFetchMode("members", FetchMode.JOIN).list());
        if (groups.size() > 0) {
            members = new LinkedList<>();
            for (GroupMember m : groups.iterator().next().getMembers()) {
                members.add(new Member(m.getName(), MemberType.USER));
            }
        }
        hib.getTransaction().commit();
    } catch (HibernateException e) {
        logger.error(e.getMessage(), e);
        hib.getTransaction().rollback();
    } finally {
        hib.close();
    }

    return members != null ? members : Collections.<Member>emptyList();
}

From source file:org.jahia.modules.ugp.showcase.DbUserGroupProvider.java

License:Open Source License

@Override
public List<String> getMembership(Member member) {
    List<String> owners = null;
    StatelessSession hib = sessionFactoryBean.openStatelessSession();
    hib.beginTransaction();//from  w  ww.  j  a  va  2  s .c o  m

    try {

        @SuppressWarnings("unchecked")
        List<Group> groups = hib.createCriteria(Group.class).createCriteria("members")
                .add(Restrictions.eq("name", member.getName())).list();
        if (groups.size() > 0) {
            owners = new LinkedList<>();
            for (Group g : groups) {
                owners.add(g.getGroupname());
            }
        }
        hib.getTransaction().commit();
    } catch (HibernateException e) {
        logger.error(e.getMessage(), e);
        hib.getTransaction().rollback();
    } finally {
        hib.close();
    }

    return owners != null ? owners : Collections.<String>emptyList();
}

From source file:org.jahia.modules.ugp.showcase.DbUserGroupProvider.java

License:Open Source License

@Override
public List<String> searchGroups(Properties searchCriteria, long offset, long limit) {

    if (hasUnknownCriteria(searchCriteria, Collections.singleton("groupname"))) {
        return Collections.emptyList();
    }/*from   w  w w . jav a 2  s.  c  o  m*/

    List<String> groups = null;
    StatelessSession hib = sessionFactoryBean.openStatelessSession();
    hib.beginTransaction();

    try {
        Criteria query = hib.createCriteria(Group.class);
        if (offset > 0) {
            query.setFirstResult((int) offset);
        }
        if (limit > 0) {
            query.setMaxResults((int) limit);
        }

        filterQuery(query, searchCriteria);

        logger.info("Executing query {}", query);

        @SuppressWarnings("unchecked")
        List<Group> list = query.list();
        if (list.size() > 0) {
            groups = new LinkedList<>();
            for (Group g : list) {
                groups.add(g.getGroupname());
            }
        }

        hib.getTransaction().commit();
    } catch (HibernateException e) {
        logger.error(e.getMessage(), e);
        hib.getTransaction().rollback();
    } finally {
        hib.close();
    }

    logger.info("Found {} groups matching the search criteria {}", groups != null ? groups.size() : 0,
            searchCriteria);

    return groups != null ? groups : Collections.<String>emptyList();
}

From source file:org.jahia.modules.ugp.showcase.DbUserProvider.java

License:Open Source License

@Override
public JahiaUser getUser(String name) throws UserNotFoundException {
    JahiaUser foundUser = null;/*from  ww w.j  av a2  s.  c  om*/
    StatelessSession hib = sessionFactoryBean.openStatelessSession();
    hib.beginTransaction();

    try {
        @SuppressWarnings("unchecked")
        Set<User> users = new LinkedHashSet<>(hib.createCriteria(User.class).add(Restrictions.idEq(name))
                .setFetchMode("properties", FetchMode.JOIN).list());

        foundUser = !users.isEmpty() ? toUser(users.iterator().next()) : null;
        hib.getTransaction().commit();
    } catch (HibernateException e) {
        logger.error(e.getMessage(), e);
        hib.getTransaction().rollback();
    } finally {
        hib.close();
    }

    if (foundUser == null) {
        throw new UserNotFoundException("Unable to find user " + name + " for the provider " + getKey());
    }

    return foundUser;
}

From source file:org.jahia.modules.ugp.showcase.DbUserProvider.java

License:Open Source License

@Override
public List<String> searchUsers(Properties searchCriteria, long offset, long limit) {

    if (hasUnknownCriteria(searchCriteria,
            Arrays.asList(new String[] { "username", "j:firstName", "j:lastName", "j:email" }))) {
        return Collections.emptyList();
    }/*from  w  w  w  .ja v  a 2  s. c o  m*/

    List<String> users = null;
    StatelessSession hib = sessionFactoryBean.openStatelessSession();
    hib.beginTransaction();

    try {
        Criteria query = hib.createCriteria(User.class);
        if (offset > 0) {
            query.setFirstResult((int) offset);
        }
        if (limit > 0) {
            query.setMaxResults((int) limit);
        }

        Criteria rootQuery = query;

        query = filterQuery(query, searchCriteria);

        logger.info("Executing query {}", rootQuery);

        @SuppressWarnings("unchecked")
        List<User> list = query.list();
        if (list.size() > 0) {
            users = new LinkedList<>();
            for (User u : list) {
                users.add(u.getUsername());
            }
        }

        hib.getTransaction().commit();
    } catch (HibernateException e) {
        logger.error(e.getMessage(), e);
        hib.getTransaction().rollback();
    } finally {
        hib.close();
    }

    logger.info("Found {} users matching the search criteria {}", users != null ? users.size() : 0,
            searchCriteria);

    return users != null ? users : Collections.<String>emptyList();
}

From source file:org.jahia.modules.ugp.showcase.persistence.DataInitializer.java

License:Open Source License

private boolean checkData() {
    boolean dataPresent = false;
    StatelessSession hib = sessionFactoryBean.openStatelessSession();
    hib.beginTransaction();/*from  w  ww .j a  v a 2s .  c om*/

    try {
        long count = ((Number) hib.createCriteria(User.class).setProjection(Projections.rowCount())
                .uniqueResult()).longValue();
        dataPresent = count > 0;
        hib.getTransaction().commit();
    } catch (HibernateException e) {
        hib.getTransaction().rollback();
    } finally {
        hib.close();
    }
    return dataPresent;
}