Search in sources :

Example 1 with ObjectNotFoundException

use of javax.ejb.ObjectNotFoundException in project tomee by apache.

the class CmpContainer method findEJBObject.

private Object findEJBObject(final Method callMethod, final Object[] args, final ThreadContext callContext, final InterfaceType interfaceType) throws OpenEJBException {
    final BeanContext beanContext = callContext.getBeanContext();
    final TransactionPolicy txPolicy = createTransactionPolicy(beanContext.getTransactionType(callMethod, interfaceType), callContext);
    try {
        final List<Object> results = cmpEngine.queryBeans(callContext, callMethod, args);
        final KeyGenerator kg = beanContext.getKeyGenerator();
        // single ProxyInfo object is returned.
        if (callMethod.getReturnType() == Collection.class || callMethod.getReturnType() == Enumeration.class) {
            final List<ProxyInfo> proxies = new ArrayList<ProxyInfo>();
            for (final Object value : results) {
                final EntityBean bean = (EntityBean) value;
                if (value == null) {
                    proxies.add(null);
                } else {
                    // get the primary key
                    final Object primaryKey = kg.getPrimaryKey(bean);
                    // create a new ProxyInfo based on the deployment info and primary key and add it to the vector
                    proxies.add(new ProxyInfo(beanContext, primaryKey));
                }
            }
            if (callMethod.getReturnType() == Enumeration.class) {
                return new Enumerator(proxies);
            } else {
                return proxies;
            }
        } else {
            if (results.size() != 1) {
                throw new ObjectNotFoundException("A Enteprise bean with deployment_id = " + beanContext.getDeploymentID() + (args != null && args.length >= 1 ? " and primarykey = " + args[0] : "") + " Does not exist");
            }
            // create a new ProxyInfo based on the deployment info and primary key
            final EntityBean bean = (EntityBean) results.get(0);
            if (bean == null) {
                return null;
            } else {
                final Object primaryKey = kg.getPrimaryKey(bean);
                return new ProxyInfo(beanContext, primaryKey);
            }
        }
    } catch (final FinderException fe) {
        handleApplicationException(txPolicy, fe, false);
    } catch (final Throwable e) {
        // handle reflection exception
        handleSystemException(txPolicy, e, callContext);
    } finally {
        afterInvoke(txPolicy, callContext);
    }
    throw new AssertionError("Should not get here");
}
Also used : Enumeration(java.util.Enumeration) ArrayList(java.util.ArrayList) TransactionPolicy(org.apache.openejb.core.transaction.TransactionPolicy) EjbTransactionUtil.createTransactionPolicy(org.apache.openejb.core.transaction.EjbTransactionUtil.createTransactionPolicy) BeanContext(org.apache.openejb.BeanContext) ProxyInfo(org.apache.openejb.ProxyInfo) FinderException(javax.ejb.FinderException) Enumerator(org.apache.openejb.util.Enumerator) EntityBean(javax.ejb.EntityBean) ObjectNotFoundException(javax.ejb.ObjectNotFoundException) Collection(java.util.Collection) EJBLocalObject(javax.ejb.EJBLocalObject) EJBObject(javax.ejb.EJBObject)

Example 2 with ObjectNotFoundException

use of javax.ejb.ObjectNotFoundException in project tomee by apache.

the class CmpContainer method select.

public Object select(final BeanContext beanContext, final String methodSignature, final String returnType, final Object... args) throws FinderException {
    final String signature = beanContext.getAbstractSchemaName() + "." + methodSignature;
    try {
        // execute the select query
        final Collection<Object> results = cmpEngine.queryBeans(beanContext, signature, args);
        // 
        // process the results
        // 
        // If we need to return a set...
        final Collection<Object> proxies;
        if (returnType.equals("java.util.Set")) {
            // we collect values into a LinkedHashSet to preserve ordering
            proxies = new LinkedHashSet<Object>();
        } else {
            // otherwise use a simple array list
            proxies = new ArrayList<Object>();
        }
        final boolean isSingleValued = !returnType.equals("java.util.Collection") && !returnType.equals("java.util.Set");
        ProxyFactory proxyFactory = null;
        for (Object value : results) {
            // if this is a single valued query and we already have results, throw FinderException
            if (isSingleValued && !proxies.isEmpty()) {
                throw new FinderException("The single valued query " + methodSignature + "returned more than one item");
            }
            // if we have an EntityBean, we need to proxy it
            if (value instanceof EntityBean) {
                final EntityBean entityBean = (EntityBean) value;
                if (proxyFactory == null) {
                    final BeanContext result = getBeanContextByClass(entityBean.getClass());
                    if (result != null) {
                        proxyFactory = new ProxyFactory(result);
                    }
                }
                if (proxyFactory != null) {
                    if (beanContext.isRemoteQueryResults(methodSignature)) {
                        value = proxyFactory.createRemoteProxy(entityBean, this);
                    } else {
                        value = proxyFactory.createLocalProxy(entityBean, this);
                    }
                }
            }
            proxies.add(value);
        }
        // if not single valued, return the set
        if (!isSingleValued) {
            return proxies;
        }
        // single valued query that returned no rows, is an exception
        if (proxies.isEmpty()) {
            throw new ObjectNotFoundException();
        }
        // return the single item.... multiple return values was handled in for loop above
        return proxies.iterator().next();
    } catch (final RuntimeException e) {
        throw new EJBException(e);
    }
}
Also used : FinderException(javax.ejb.FinderException) BeanContext(org.apache.openejb.BeanContext) EntityBean(javax.ejb.EntityBean) ObjectNotFoundException(javax.ejb.ObjectNotFoundException) EJBLocalObject(javax.ejb.EJBLocalObject) EJBObject(javax.ejb.EJBObject) OpenEJBException(org.apache.openejb.OpenEJBException) EJBException(javax.ejb.EJBException)

Example 3 with ObjectNotFoundException

use of javax.ejb.ObjectNotFoundException in project tomee by apache.

the class CmpContainer method findByPrimaryKey.

private Object findByPrimaryKey(final Method callMethod, final Object[] args, final ThreadContext callContext, final InterfaceType interfaceType) throws OpenEJBException {
    final BeanContext beanContext = callContext.getBeanContext();
    final TransactionPolicy txPolicy = createTransactionPolicy(beanContext.getTransactionType(callMethod, interfaceType), callContext);
    try {
        final EntityBean bean = (EntityBean) cmpEngine.loadBean(callContext, args[0]);
        if (bean == null) {
            throw new ObjectNotFoundException(beanContext.getDeploymentID() + " : " + args[0]);
        }
        // rebuild the primary key
        final KeyGenerator kg = beanContext.getKeyGenerator();
        final Object primaryKey = kg.getPrimaryKey(bean);
        // create a new ProxyInfo based on the deployment info and primary key
        return new ProxyInfo(beanContext, primaryKey);
    } catch (final FinderException fe) {
        handleApplicationException(txPolicy, fe, false);
    } catch (final Throwable e) {
        // handle reflection exception
        handleSystemException(txPolicy, e, callContext);
    } finally {
        afterInvoke(txPolicy, callContext);
    }
    throw new AssertionError("Should not get here");
}
Also used : BeanContext(org.apache.openejb.BeanContext) ProxyInfo(org.apache.openejb.ProxyInfo) FinderException(javax.ejb.FinderException) EntityBean(javax.ejb.EntityBean) ObjectNotFoundException(javax.ejb.ObjectNotFoundException) TransactionPolicy(org.apache.openejb.core.transaction.TransactionPolicy) EjbTransactionUtil.createTransactionPolicy(org.apache.openejb.core.transaction.EjbTransactionUtil.createTransactionPolicy) EJBLocalObject(javax.ejb.EJBLocalObject) EJBObject(javax.ejb.EJBObject)

Aggregations

EJBLocalObject (javax.ejb.EJBLocalObject)3 EJBObject (javax.ejb.EJBObject)3 EntityBean (javax.ejb.EntityBean)3 FinderException (javax.ejb.FinderException)3 ObjectNotFoundException (javax.ejb.ObjectNotFoundException)3 BeanContext (org.apache.openejb.BeanContext)3 ProxyInfo (org.apache.openejb.ProxyInfo)2 EjbTransactionUtil.createTransactionPolicy (org.apache.openejb.core.transaction.EjbTransactionUtil.createTransactionPolicy)2 TransactionPolicy (org.apache.openejb.core.transaction.TransactionPolicy)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Enumeration (java.util.Enumeration)1 EJBException (javax.ejb.EJBException)1 OpenEJBException (org.apache.openejb.OpenEJBException)1 Enumerator (org.apache.openejb.util.Enumerator)1