Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] eclipselink bug with weld proxy

Current workaround, ugly as hell but will fix it for me until this is corrected:

package be.valuya.gestemps.server;

import be.valuya.gestemps.util.DomainObject;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.interceptor.AroundInvoke;
import javax.interceptor.InvocationContext;

/**
 *
 * @author Yannick
 */
public class ProxiedEntityInterceptor {

    @AroundInvoke
    public Object removeProxy(InvocationContext invocationContext) throws Exception {
        Object[] parameters = invocationContext.getParameters();

        if (parameters != null) {
            Object[] filteredParameters = Stream.of(parameters)
                    .map(this::getRealDomainObject)
                    .collect(Collectors.toList())
                    .toArray(new Object[0]);
            invocationContext.setParameters(filteredParameters);
        }

        return invocationContext.proceed();
    }

    private Object getRealDomainObject(Object sourceObject) {
        Map<Object, Object> filteredObjectMap = new HashMap<>();
        return getRealDomainObject(filteredObjectMap, sourceObject);
    }

    private Object getRealDomainObject(Map<Object, Object> filteredObjectMap, Object sourceObject) {
        if (!(sourceObject instanceof DomainObject)) { // some shortcut that works in my case, remove or adapt if you don't want to filter every parameter
            return sourceObject;
        }
        if (filteredObjectMap.containsKey(sourceObject)) {
            return filteredObjectMap.get(sourceObject);
        }
        try {
            Class<?> sourceClass = sourceObject.getClass();
            String sourceClassName = sourceClass.getName();
            Pattern proxyPattern = Pattern.compile("^(.*)\\$Proxy.*$");
            Matcher matcher = proxyPattern.matcher(sourceClassName);

            Class targetClass;
            if (matcher.matches()) {
                String unproxiedClassName = matcher.group(1);
                try {
                    targetClass = Class.forName(unproxiedClassName);
                } catch (ClassNotFoundException classNotFoundException) {
                    throw new IllegalStateException(classNotFoundException);
                }
            } else {
                targetClass = sourceClass;
            }
            Object targetObject = targetClass.newInstance();
            filteredObjectMap.put(sourceObject, targetObject);

            BeanInfo beanInfo = Introspector.getBeanInfo(targetClass);
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
                Method readMethod = propertyDescriptor.getReadMethod();
                Method writeMethod = propertyDescriptor.getWriteMethod();
                if (writeMethod == null) {
                    // e.g. getClass() has no setter...
                    continue;
                }
                Object value = readMethod.invoke(sourceObject);
                Object filteredValue = getRealDomainObject(filteredObjectMap, value);
                writeMethod.invoke(targetObject, filteredValue);
            }

            return targetObject;
        } catch (IllegalArgumentException | IntrospectionException | InvocationTargetException | IllegalAccessException | InstantiationException exception) {
            throw new RuntimeException(exception);
        }
    }

}


Le lun. 21 déc. 2015 à 09:42, Yannick Majoros <yannick.majoros@xxxxxxxxx> a écrit :
Hello,

I just hit this specific bug once again in EL 2.6.1.

It comes from MergeManager@1046:

ClassDescriptor descriptor = unitOfWork.getDescriptor(clone.getClass());

But clone.getClass() is my.package.MyEntity$Proxy$_$$_WeldClientProxy . If I force to the right descriptor, it works fine.

What could we do about this?

Cheers

Yannick

Le lun. 15 juin 2015 à 13:40, Yannick Majoros <yannick.majoros@xxxxxxxxx> a écrit :
Hi Tomas,

Sorry, I don't have a reproducer (big app), but I can help if someone tries to write one. Merging any Weld-proxied entity should do.

Best regards,

Yannick

Le dim. 14 juin 2015 à 11:29, Tomas Kraus <tomas.kraus@xxxxxxxxxx> a écrit :
Hello,

NPE is a bug that should be fixed. Please file a bug in EclipseLink
bugzilla (RT/Eclipselink/Foundation) and provide reproduction scenario
for it (best is to attach some sample application).

Thanks

Tomas

Dne 6/12/15 v 7:58 AM Yannick Majoros napsal(a):
> Hello,
>
> Part of an object graph I'm trying to merge is returned by a CDI
> @Producer method, which gives me a proxied object.
>
> It seems that merging this graph gives below NPE.
>
> While debugging, I noticed I have an object A with a relation to B and
> B is proxied, B.id == null but B.getId() != null. Guess that's the
> problem?
>
> What can I do?
>
> I'm using Glassfish 4.1 and EL 2.5.2.
>
> java.lang.NullPointerException
>     at
> org.eclipse.persistence.internal.sessions.MergeManager.registerObjectForMergeCloneIntoWorkingCopy(MergeManager.java:1047)
>     at
> org.eclipse.persistence.internal.sessions.MergeManager.getTargetVersionOfSourceObject(MergeManager.java:228)
>     at
> org.eclipse.persistence.mappings.ObjectReferenceMapping.mergeIntoObject(ObjectReferenceMapping.java:513)
>     at
> org.eclipse.persistence.internal.descriptors.ObjectBuilder.mergeIntoObject(ObjectBuilder.java:4132)
>     at
> org.eclipse.persistence.internal.sessions.MergeManager.mergeChangesOfCloneIntoWorkingCopy(MergeManager.java:594)
>     at
> org.eclipse.persistence.internal.sessions.MergeManager.mergeChanges(MergeManager.java:313)
>     at
> org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.mergeCloneWithReferences(UnitOfWorkImpl.java:3521)
>     at
> org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork.mergeCloneWithReferences(RepeatableWriteUnitOfWork.java:384)
>     at
> org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.mergeCloneWithReferences(UnitOfWorkImpl.java:3481)
>     at
> org.eclipse.persistence.internal.jpa.EntityManagerImpl.mergeInternal(EntityManagerImpl.java:542)
>     at
> org.eclipse.persistence.internal.jpa.EntityManagerImpl.merge(EntityManagerImpl.java:519)
>     at
> com.sun.enterprise.container.common.impl.EntityManagerWrapper.merge(EntityManagerWrapper.java:305)
>     at
> be.valuya.gestemps.server.TaskService.saveTask(TaskService.java:234)
>     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>     at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
>     at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
>     at java.lang.reflect.Method.invoke(Method.java:483)
>     at
> org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1081)
>     at
> org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1153)
>     at
> com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:4786)
>     at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:656)
>     at
> com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
>     at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:608)
>     at
> org.jboss.weld.ejb.AbstractEJBRequestScopeActivationInterceptor.aroundInvoke(AbstractEJBRequestScopeActivationInterceptor.java:46)
>     at
> org.jboss.weld.ejb.SessionBeanInterceptor.aroundInvoke(SessionBeanInterceptor.java:52)
>     at sun.reflect.GeneratedMethodAccessor131.invoke(Unknown Source)
>     at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
>     at java.lang.reflect.Method.invoke(Method.java:483)
>     at
> com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:883)
>     at
> com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
>     at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:608)
>     at
> com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doCall(SystemInterceptorProxy.java:163)
>     at
> com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorProxy.java:140)
>     at sun.reflect.GeneratedMethodAccessor132.invoke(Unknown Source)
>     at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
>     at java.lang.reflect.Method.invoke(Method.java:483)
>     at
> com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:883)
>     at
> com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
>     at
> com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:369)
>     at
> com.sun.ejb.containers.BaseContainer.__intercept(BaseContainer.java:4758)
>     at
> com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:4746)
>     at
> com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:212)
>     at
> com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:88)
>     at com.sun.proxy.$Proxy239.saveTask(Unknown Source)
>     at
> be.valuya.gestemps.server.__EJB31_Generated__TaskService__Intf____Bean__.saveTask(Unknown
> Source)
>     at
> be.valuya.gestemps.web.control.TaskController.actionSave(TaskController.java:713)
>     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>     at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
>     at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
>     at java.lang.reflect.Method.invoke(Method.java:483)
>     at com.sun.el.parser.AstValue.invoke(AstValue.java:289)
>     at
> com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:304)
>     at
> org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40)
>     at
> org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
>     at
> com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
>     at
> javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
>     at
> com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
>     at javax.faces.component.UICommand.broadcast(UICommand.java:315)
>     at
> javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
>     at
> javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
>     at
> com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
>     at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
>     at
> com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
>     at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
>     at
> org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
>     at
> org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:344)
>     at
> org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
>     at
> be.valuya.gestemps.web.control.InitFilter.doFilter(InitFilter.java:53)
>     at
> org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
>     at
> org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
>     at
> org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:316)
>     at
> org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
>     at
> org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
>     at
> org.apache.catalina.core.StandardPipeline.doChainInvoke(StandardPipeline.java:678)
>     at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97)
>     at
> org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
>     at
> org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:415)
>     at
> org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:282)
>     at
> com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
>     at
> com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
>     at
> org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201)
>     at
> org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175)
>     at
> org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
>     at
> org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
>     at
> org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
>     at
> org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
>     at
> org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
>     at
> org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
>     at
> org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
>     at
> org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561)
>     at
> org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
>     at
> org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
>     at
> org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
>     at
> org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
>     at
> org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
>     at
> org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
>     at java.lang.Thread.run(Thread.java:744)
>
> Best regards,
>
> Yannick
> _______________________________________________
> eclipselink-users mailing list
> eclipselink-users@xxxxxxxxxxx
> To change your delivery options, retrieve your password, or
> unsubscribe from this list, visit
> https://dev.eclipse.org/mailman/listinfo/eclipselink-users

_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/eclipselink-users
--
Yannick Majoros
--
Yannick Majoros

Back to the top