Example usage for org.hibernate SessionFactory getCurrentSession

List of usage examples for org.hibernate SessionFactory getCurrentSession

Introduction

In this page you can find the example usage for org.hibernate SessionFactory getCurrentSession.

Prototype

Session getCurrentSession() throws HibernateException;

Source Link

Document

Obtains the current session.

Usage

From source file:org.xerela.provider.netman.NetworksProvider.java

License:Mozilla Public License

/** {@inheritDoc} */
public ManagedNetwork getManagedNetwork(String name) {
    if (name == null || name.trim().length() == 0) {
        return null;
    }/* w w  w  .  j ava  2  s .c o m*/

    try {
        boolean ownTransaction = TransactionElf.beginOrJoinTransaction();

        SessionFactory sessionFactory = NetworksActivator.getSessionFactory();
        Session session = sessionFactory.getCurrentSession();

        Criteria criteria = session.createCriteria(ManagedNetwork.class).add(Restrictions.eq(NAME, name));
        ManagedNetwork network = (ManagedNetwork) criteria.uniqueResult();

        if (ownTransaction) {
            TransactionElf.commit();
        }

        return network;
    } catch (RuntimeException e) {
        TransactionElf.rollback();
        throw e;
    }
}

From source file:org.xerela.provider.netman.NetworksProvider.java

License:Mozilla Public License

/** {@inheritDoc} */
@SuppressWarnings("unchecked")
public List<String> getManagedNetworkNames() {
    try {/*from  w w w . j  a v a 2 s . com*/
        boolean ownTransaction = TransactionElf.beginOrJoinTransaction();

        SessionFactory sessionFactory = NetworksActivator.getSessionFactory();
        Session session = sessionFactory.getCurrentSession();

        SQLQuery query = session.createSQLQuery("SELECT name FROM managed_network ORDER BY name"); //$NON-NLS-1$
        List<?> list = query.list();

        if (ownTransaction) {
            TransactionElf.commit();
        }

        if (list != null) {
            return (List<String>) list;
        }

        return new ArrayList<String>();
    } catch (RuntimeException e) {
        TransactionElf.rollback();
        throw e;
    }
}

From source file:org.xerela.provider.netman.NetworksProvider.java

License:Mozilla Public License

/** {@inheritDoc} */
public ManagedNetwork getDefaultManagedNetwork() {
    rwLock.readLock().lock();// w ww . j a  va2  s  .com

    try {
        if (defaultNetwork != null) {
            return defaultNetwork;
        }

        boolean ownTransaction = TransactionElf.beginOrJoinTransaction();

        SessionFactory sessionFactory = NetworksActivator.getSessionFactory();
        Session session = sessionFactory.getCurrentSession();

        Criteria criteria = session.createCriteria(ManagedNetwork.class).add(Restrictions.eq(IS_DEFAULT, true));
        defaultNetwork = (ManagedNetwork) criteria.uniqueResult();

        if (ownTransaction) {
            TransactionElf.commit();
        }

        return defaultNetwork;
    } catch (HibernateException he) {
        TransactionElf.rollback();
        throw he;
    } finally {
        rwLock.readLock().unlock();
    }
}

From source file:org.xerela.provider.netman.NetworksProvider.java

License:Mozilla Public License

/** {@inheritDoc} */
public void setDefaultManagedNetwork(String name) {
    if (name == null || name.length() == 0) {
        return;//  w  w w  . ja  v a  2  s  .c o m
    }

    rwLock.writeLock().lock();
    try {
        boolean ownTransaction = TransactionElf.beginOrJoinTransaction();

        ManagedNetwork newDefault = getManagedNetwork(name);
        if (newDefault == null) {
            return;
        }
        ManagedNetwork currentDefault = getDefaultManagedNetwork();

        SessionFactory sessionFactory = NetworksActivator.getSessionFactory();
        Session session = sessionFactory.getCurrentSession();

        if (currentDefault != null) {
            currentDefault.setDefault(false);
            session.update(currentDefault);
            defaultNetwork = null;
        }

        newDefault.setDefault(true);
        session.update(newDefault);

        if (ownTransaction) {
            TransactionElf.commit();
        }
    } catch (Exception e) {
        TransactionElf.rollback();
        throw new RuntimeException(e);
    } finally {
        rwLock.writeLock().unlock();
    }
}

From source file:org.xerela.provider.security.SecurityProvider.java

License:Mozilla Public License

/** {@inheritDoc} */
public void changePassword(String username, String password) {
    SessionFactory sessionFactory = SecurityProviderActivator.getSessionFactory();
    Session session = sessionFactory.getCurrentSession();

    ZPrincipal zprincipal = (ZPrincipal) session.get(ZPrincipal.class, username);
    zprincipal.setMD5Password(SecurityElf.calcMD5(username, password));
    session.update(zprincipal);/*from  ww w . j av a  2 s  .  c om*/
}

From source file:org.xerela.provider.security.SecurityProvider.java

License:Mozilla Public License

/** {@inheritDoc} */
public void changeMyPassword(String password) {
    // Get the invoking user
    Principal principal = SecurityProviderActivator.getSecurityService().getUserSession().getPrincipal();

    SessionFactory sessionFactory = SecurityProviderActivator.getSessionFactory();
    Session session = sessionFactory.getCurrentSession();

    ZPrincipal zprincipal = (ZPrincipal) session.get(ZPrincipal.class, principal.getName());
    zprincipal.setMD5Password(SecurityElf.calcMD5(principal.getName(), password));
    session.update(zprincipal);/*from   ww  w .j  av a  2s  .  c  o m*/
}

From source file:org.xerela.provider.security.SecurityProvider.java

License:Mozilla Public License

/** {@inheritDoc} */
public ZRole getRole(String role) {
    SessionFactory sessionFactory = SecurityProviderActivator.getSessionFactory();
    Session session = sessionFactory.getCurrentSession();

    ZRole zrole = (ZRole) session.get(ZRole.class, role);

    if (zrole != null) {

        // Expand ORG_XERELA_ACCESS_ALL permission into all available permissions.
        if (zrole.hasPermission(ORG_XERELA_ACCESS_ALL)) {
            // We're about to change the ZRole object at runtime, we don't want these
            // changes persisted
            session.setReadOnly(zrole, true);

            Set<String> permissions = new HashSet<String>();
            for (String perm : getAvailablePermissions()) {
                String[] split = perm.split("="); //$NON-NLS-1$
                permissions.add(split[0]);
            }/*from   w ww  .j av  a  2  s  . co  m*/
            zrole.setPermissionSet(permissions);
        }
    }

    return zrole;
}

From source file:org.xerela.provider.security.SecurityProvider.java

License:Mozilla Public License

/** {@inheritDoc} */
public void createRole(String role, List<String> permissions) {
    SessionFactory sessionFactory = SecurityProviderActivator.getSessionFactory();
    Session session = sessionFactory.getCurrentSession();

    ZRole zrole = new ZRole(role);
    Set<String> permSet = new HashSet<String>();
    permSet.addAll(permissions);//from  w  w  w . ja v  a  2s .  com

    zrole.setPermissionSet(permSet);
    session.save(zrole);
}

From source file:org.xerela.provider.security.SecurityProvider.java

License:Mozilla Public License

/** {@inheritDoc} */
public void updateRole(ZRole zrole) {
    if (zrole.getName().equals(ADMINISTRATOR_ROLE)) {
        throw new SecurityException("Updating the Administrator role is not allowed."); //$NON-NLS-1$
    }//from   w  ww.j ava  2 s  .co  m

    SessionFactory sessionFactory = SecurityProviderActivator.getSessionFactory();
    Session session = sessionFactory.getCurrentSession();

    session.update(zrole);
}

From source file:org.xerela.provider.security.SecurityProvider.java

License:Mozilla Public License

/** {@inheritDoc} */
public void deleteRole(String role) {
    if (role.equals(ADMINISTRATOR_ROLE)) {
        throw new SecurityException("Deleting the Administrator role is not allowed."); //$NON-NLS-1$
    }//w w w .  ja  va2  s  .  c om

    SessionFactory sessionFactory = SecurityProviderActivator.getSessionFactory();
    Session session = sessionFactory.getCurrentSession();

    session.delete(new ZRole(role));
}