2017-03-27 4 views
0

Je fais des recherches sur Oracle ADF et EJB. J'utilise les faces ADF comme View et Controller, EJB comme service. Je ne veux pas utiliser le modèle ADF pour lier EJB Session pour interagir avec les faces ADF. Donc, je créer un haricot réussi à interagir avec ADF FacesComment injecter une session EJB à ADF ManagedBean

ManagedBean

public class EmployeeManagementController { 
    private List<Jobs> jobList; 
    private RichTable jobTable; 
    private RichPanelGroupLayout panelGroup; 

    //@EJB(mappedName = "HRSysDemo.JobBean",name = "jobBean") 
    @EJB 
    private JobBeanLocal jobBean; 
    /*...*/ 

    public void initPage() { 
     System.out.println("TESTING . . ."); 
     jobList = jobBean.getJobsFindAll(); 
    } 
} 

Session Bean

@Stateless(mappedName = "HRSysDemo/JobBean") 
public class JobBean implements JobBeanRemote, JobBeanLocal { 
    @Resource 
    SessionContext sessionContext; 
    @PersistenceContext(unitName = "Model") 
    private EntityManager em; 
    /*...*/ 
    public List<Jobs> getJobsFindAll() { 
     return em.createNamedQuery("Jobs.findAll").getResultList(); 
    } 
} 

Mon problème est "jobBean" toujours obtenir "null", cela signifie que EJB Session ne peut pas injecter dans ManagedBean. J'ai essayé quelques manières, telles que l'injection d'interface de changement (interface à distance), spécifiez le nom et mappedName, mais cela ne fonctionne toujours pas. Donc, comment puis-je injecter un EJB SessionBean dans un ADF ManagedBean?

Merci d'avance!

+0

Est-ce que les deux modèles dans un fichier EAR et le module EJB droit enregistré dans le module WAR? –

+1

Je ne suis pas sûr. Je n'utilise pas Eclipse, j'utilise Jdeveloper:/ –

+0

Ce n'est pas un format de fichier spécifique à l'IDE. C'est un format d'application J2EE packagé (prêt à déployer). –

Répondre

0

Je l'ai fait en suivant une étape dans here, ça ne fonctionne pas bien mais je les ai corrigés. J'espère que cela sera utile pour quelqu'un d'autre:

Voici mon code:

ELResolverWithInjectEJB.java

import java.lang.reflect.AccessibleObject; 
import java.lang.reflect.Field; 
import java.lang.reflect.InvocationTargetException; 
import java.lang.reflect.Method; 

import java.util.Hashtable; 
import java.util.List; 
import java.util.Map; 

import com.google.common.collect.Maps; 
import com.google.common.collect.Lists; 

import java.util.concurrent.ConcurrentHashMap; 
import javax.el.ELContext; 
import javax.el.MapELResolver; 

import javax.naming.Context; 
import javax.naming.InitialContext; 
import javax.naming.NamingException; 

import oracle.adf.controller.metadata.model.beans.ManagedBean; 
import oracle.adf.controller.metadata.model.beans.ManagedBeanScopeType; 

/* Illegal internal package import. Please use public API */ 
import oracle.adfinternal.controller.beans.AnnotationUtils; 
import oracle.adfinternal.controller.beans.ManagedBeanFactory; 
import oracle.adfinternal.controller.state.ScopeMap; 

public class ELResolverWithInjectEJB extends MapELResolver { 
    private static Method getManagedBeanScopeType = null; 
    private static Method shouldIncludeHigherScopes = null; 
    private static Method newInstance = null; 
    static { 
     try { 
      getManagedBeanScopeType = ScopeMap.class.getDeclaredMethod("getManagedBeanScopeType", null); 
      getManagedBeanScopeType.setAccessible(true); 
      shouldIncludeHigherScopes = ScopeMap.class.getDeclaredMethod("shouldIncludeHigherScopes", null); 
      shouldIncludeHigherScopes.setAccessible(true); 
      newInstance = ManagedBeanFactory.class.getDeclaredMethod("newInstance", ManagedBean.class); 
      newInstance.setAccessible(true); 
     } catch (Exception e) { 
      throw new RuntimeException(e); 
     } 
    } 

    public ELResolverWithInjectEJB() { 
     super(); 
    } 

    @SuppressWarnings("oracle.jdeveloper.java.semantic-warning") 
    public Object getValue(ELContext elContext, Object base, Object var) { 
     ScopeMap scope = (ScopeMap) (base instanceof ScopeMap ? base : null); 
     if (scope == null || scope.containsKey(var)) 
      return super.getValue(elContext, base, var); 
     Object bean = null; 
     try { 
      ManagedBeanFactory mbf = ManagedBeanFactory.getInstance(); 
      ManagedBean managedBean = mbf.getManagedBeanInCurrentPageFlow((String) var); 
      ManagedBeanScopeType expectedScope = (ManagedBeanScopeType) getManagedBeanScopeType.invoke(scope, null); 
      if (((Boolean) shouldIncludeHigherScopes.invoke(scope, null)) && 
       (managedBean == null || !managedBean.getScope().equals(expectedScope))) { 
       managedBean = mbf.getManagedBeanInAdfPageFlow((String) var); 
       } 
      if (managedBean != null && managedBean.getScope().equals(expectedScope)) { 
       bean = newInstance.invoke(mbf, managedBean); 
       if (bean != null) { 
        scope.put((String) var, bean); 
        EJBInjector.inject(bean); 
        AnnotationUtils.runPostConstructIfSpecified(bean, managedBean.getName(), managedBean.getScope()); 
        elContext.setPropertyResolved(true); 
       } 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return bean; 
    } 

    static class EJBInjector { 
     private static Map<String, List> processed = Maps.newConcurrentMap(); 
     private static Map<String, String> mappedBean = Maps.newConcurrentMap(); 
     private static Context ctx = null; 
     static { 
      try { 
       ctx = getInitialContext(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 

     @SuppressWarnings("unchecked") 
     public static void inject(Object o) throws IllegalAccessException, InvocationTargetException, NamingException { 
      List<AccessibleObject> inj = null; 
      if (!processed.containsKey(o.getClass().getName())) { 
       inj = Lists.newArrayList(); 
       for (Field field : o.getClass().getDeclaredFields()) { 
        if (field.getAnnotation(Autoinjector.class) != null) { 
         field.setAccessible(true); 
         inj.add(field); 
         Autoinjector rm = field.getAnnotation(Autoinjector.class); 
         mappedBean.put(convertKey(o, field), rm.ejbMappedName()); 
        } 
       } 
       processed.put(o.getClass().getName(), inj.isEmpty() ? null : inj); 
      } 
      inj = inj != null ? inj : processed.get(o.getClass().getName()); 
      if (inj != null) { 
       for (AccessibleObject i : inj) { 
        ((Field) i).set(o,ctx.lookup(mappedBean.get(convertKey(o, (Field) i)) + "#" + 
                ((Field) i).getType().getName())); 
       } 
      } 
     } 

     private static String convertKey(Object o, Field f) { 
      return o != null && f != null ? o.getClass().getName() + "_" + f.getType().getName() + "_" + f.getName() : 
         ""; 
     } 

     @SuppressWarnings("unchecked") 
     public static Context getInitialContext() throws NamingException { 
      Hashtable env = new Hashtable(); 
      env.put(Context.INITIAL_CONTEXT_FACTORY,[your_provide]); 
      env.put(Context.PROVIDER_URL, [your_url_server]); 
      return new InitialContext(env); 
      } 
     } 
    } 

Autoinjector.java

import java.lang.annotation.ElementType; 
import java.lang.annotation.Retention; 
import java.lang.annotation.RetentionPolicy; 
import java.lang.annotation.Target; 

@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD}) 
@Retention(RetentionPolicy.RUNTIME) 
public @interface Autoinjector { 
    String ejbMappedName() default ""; 
} 

faces-config .xml

<application> 
    <default-render-kit-id>oracle.adf.rich</default-render-kit-id> 
    <el-resolver>[your_package].ELResolverWithInjectEJB</el-resolver> 
</application> 

Dans contrôleur

public class DicOrgDetailPageController extends BaseController { 

    private static final long serialVersionUID = 2332665929048532518L; 

    /* -- Services -- */ 
    @Autoinjector(ejbMappedName = "mytest") 
    private SB_DicOrgService sbDicOrgService; 
} 
0

Définissez-vous vos beans dans l'adfc-config? Si cela ne fonctionne pas, essayez de les définir dans un fichier faces-config normal à la place.

+0

Je ne les définis pas dans le fichier adfc-config :(. Je les ai définis dans faces-config. –