Example usage for org.hibernate.proxy HibernateProxy getHibernateLazyInitializer

List of usage examples for org.hibernate.proxy HibernateProxy getHibernateLazyInitializer

Introduction

In this page you can find the example usage for org.hibernate.proxy HibernateProxy getHibernateLazyInitializer.

Prototype

public LazyInitializer getHibernateLazyInitializer();

Source Link

Document

Get the underlying lazy initialization handler.

Usage

From source file:net.sf.beanlib.hibernate.UnEnhancer.java

License:Apache License

public static <T> Class<T> getActualClass(Object object) {
    Class<?> c = object.getClass();
    boolean enhanced = true;

    while (c != null && enhanced) {
        enhanced = isCheckCGLib() && Enhancer.isEnhanced(c) || isJavassistEnhanced(c);
        if (enhanced) {
            if (object instanceof HibernateProxy) {
                HibernateProxy hibernateProxy = (HibernateProxy) object;
                LazyInitializer lazyInitializer = hibernateProxy.getHibernateLazyInitializer();
                try {
                    // suggested by Chris (harris3@sourceforge.net)
                    Object impl = lazyInitializer.getImplementation();

                    if (impl != null) {
                        @SuppressWarnings("unchecked")
                        Class<T> ret = (Class<T>) impl.getClass();
                        return ret;
                    }//  ww  w.j  a va  2s. c o  m
                } catch (HibernateException ex) {
                    Logger.getLogger(UnEnhancer.class)
                            .warn("Unable to retrieve the underlying persistent object", ex);
                }
                @SuppressWarnings("unchecked")
                Class<T> ret = lazyInitializer.getPersistentClass();
                return ret;
            }
            c = c.getSuperclass();
        }
    }
    @SuppressWarnings("unchecked")
    Class<T> ret = (Class<T>) c;
    return ret;
}

From source file:net.sf.beanlib.hibernate.UnEnhancer.java

License:Apache License

public static <T> T unenhanceObject(T object) {
    if (object instanceof HibernateProxy) {
        HibernateProxy hibernateProxy = (HibernateProxy) object;
        LazyInitializer lazyInitializer = hibernateProxy.getHibernateLazyInitializer();

        @SuppressWarnings("unchecked")
        T ret = (T) lazyInitializer.getImplementation();
        return ret;
    }// w  w w  . jav  a  2 s  .  c  o  m
    return object;
}

From source file:ome.security.basic.BasicSecuritySystem.java

License:Open Source License

/**
 * /*from  w  w  w.  j  a  va  2  s.  c o m*/
 * It would be better to catch the
 * {@link SecureAction#updateObject(IObject)} method in a try/finally block,
 * but since flush can be so poorly controlled that's not possible. instead,
 * we use the one time token which is removed this Object is checked for
 * {@link #hasPrivilegedToken(IObject) privileges}.
 * 
 * @param obj
 *            A managed (non-detached) entity. Not null.
 * @param action
 *            A code-block that will be given the entity argument with a
 *            {@link #hasPrivilegedToken(IObject)} privileged token}.
 */
public <T extends IObject> T doAction(SecureAction action, T... objs) {
    Assert.notNull(objs);
    Assert.notEmpty(objs);
    Assert.notNull(action);

    final LocalQuery query = (LocalQuery) sf.getQueryService();
    final List<GraphHolder> ghs = new ArrayList<GraphHolder>();

    for (T obj : objs) {

        // TODO inject
        if (obj.getId() != null && !query.contains(obj)) {
            throw new SecurityViolation(
                    "Services are not allowed to call " + "doAction() on non-Session-managed entities.");
        }

        // ticket:1794 - use of IQuery.get along with doAction() creates
        // two objects (outer proxy and inner target) and only the outer
        // proxy has its graph holder modified without this block, leading
        // to security violations on flush since no token is present.
        if (obj instanceof HibernateProxy) {
            HibernateProxy hp = (HibernateProxy) obj;
            IObject obj2 = (IObject) hp.getHibernateLazyInitializer().getImplementation();
            ghs.add(obj2.getGraphHolder());
        }

        // FIXME
        // Token oneTimeToken = new Token();
        // oneTimeTokens.put(oneTimeToken);
        ghs.add(obj.getGraphHolder());

    }

    // Holding onto the graph holders since they protect the access
    // to their tokens
    for (GraphHolder graphHolder : ghs) {
        tokenHolder.setToken(graphHolder); // oneTimeToken
    }

    T retVal;
    try {
        retVal = action.updateObject(objs);
    } finally {
        for (GraphHolder graphHolder : ghs) {
            tokenHolder.clearToken(graphHolder);
        }
    }
    return retVal;
}

From source file:org.babyfish.hibernate.association.EntityCollection.java

License:Open Source License

@Override
protected boolean isAbandonableElement(E element) {
    //This endpoint is not inverse means the opposite end point is inverse.
    if (!this.isInverse() && element instanceof HibernateProxy) {
        HibernateProxy proxy = (HibernateProxy) element;
        SessionImplementor session = proxy.getHibernateLazyInitializer().getSession();
        return session == null || !session.isOpen() || !session.isConnected();
    }//from ww w. jav a2  s .  c o m
    return false;
}

From source file:org.babyfish.hibernate.association.EntityIndexedReference.java

License:Open Source License

@Override
public boolean isLoadable() {
    if (this.value instanceof HibernateProxy) {
        HibernateProxy proxy = (HibernateProxy) this.value;
        return proxy.getHibernateLazyInitializer().getSession().isConnected();
    }//from   ww  w.  j ava2s  . co  m
    return true;
}

From source file:org.babyfish.hibernate.association.EntityKeyedReference.java

License:Open Source License

@Override
protected boolean isAbandonableValue(T value) {
    //This endpoint is not inverse means the opposite end point is inverse.
    if (!this.isInverse() && value instanceof HibernateProxy) {
        HibernateProxy proxy = (HibernateProxy) value;
        SessionImplementor session = proxy.getHibernateLazyInitializer().getSession();
        return session == null || !session.isOpen() || !session.isConnected();
    }/*  ww  w. ja v a 2s. co m*/
    return false;
}

From source file:org.babyfish.hibernate.association.EntityNavigableMap.java

License:Open Source License

@Override
protected boolean isAbandonableValue(V value) {
    //This endpoint is not inverse means the opposite end point is inverse.
    if (!this.isInverse() && value instanceof HibernateProxy) {
        HibernateProxy proxy = (HibernateProxy) value;
        SessionImplementor session = proxy.getHibernateLazyInitializer().getSession();
        return session == null || !session.isOpen() || !session.isConnected();
    }/*from   w  w  w .j a v  a  2 s.  c  o m*/
    return false;
}

From source file:org.babyfish.hibernate.proxy.FrozenLazyInitializerImpl.java

License:Open Source License

protected FrozenLazyInitializerImpl(HibernateProxy owner) {
    ProxyObject proxyObject = (ProxyObject) owner;
    MethodHandler handler = proxyObject.getHandler();
    if (!(handler instanceof LazyInitializer)) {
        Arguments.mustBeInstanceOfValue("((" + ProxyObject.class.getName() + ")owner).getHandler()", handler,
                LazyInitializer.class);
    }/*w  ww  .j av  a  2 s .  c  o m*/
    Class<?> persistentClass = getPersistentClass((BasicLazyInitializer) handler);
    LazyInitializer lazyInitializer = owner.getHibernateLazyInitializer();
    if (lazyInitializer instanceof FrozenLazyInitializer) {
        throw new AssertionError();
    }
    this.owner = owner;
    this.lazyInitializer = lazyInitializer;
    this.objectModelMetadata = HibernateMetadatas.of(persistentClass);
    this.initTransient(persistentClass);
    proxyObject.setHandler(this);
}

From source file:org.brekka.commons.persistence.support.EntityUtils.java

License:Apache License

public static <T> T narrow(final Object object, final Class<T> expected) {
    if (object instanceof HibernateProxy) {
        HibernateProxy proxy = (HibernateProxy) object;
        return expected.cast(proxy.getHibernateLazyInitializer().getImplementation());
    }//from   ww w  . j av a  2 s  .  c o  m
    return expected.cast(object);
}

From source file:org.broadleafcommerce.common.util.HibernateUtils.java

License:Apache License

/**
 * <p>Ensure a domain object is an actual persisted object and not a Hibernate proxy object by getting its real implementation.
 *
 * <p>This is primarily useful when retrieving a lazy loaded object that has been subclassed and you have the intention of casting it.
 *
 * @param t the domain object to deproxy
 * @return the actual persisted object or the passed in object if it is not a Hibernate proxy
 *///from  w w w  .jav a 2s .  c  o m
public static <T> T deproxy(T t) {
    if (t instanceof HibernateProxy) {
        HibernateProxy proxy = (HibernateProxy) t;
        LazyInitializer lazyInitializer = proxy.getHibernateLazyInitializer();
        return (T) lazyInitializer.getImplementation();
    }
    return t;
}