This discussion is archived
0 Replies Latest reply: Nov 2, 2011 1:44 PM by 897001 RSS

EJB Injection into JSR286 Portlet

897001 Newbie
Currently Being Moderated
I'm currently facing an issue accessing an Stateless EJB from my JSR286 Weblogic 10.3.2 Portal Server. I have 2 ears: 1) PreferencesPortalEAR and 2)PreferencesEJBEar

PreferencesPortalEAR - contains the web module with jsr286 portlet and the ejb client jar
PreferencesEJBEar - contains the j2ee module with the ejb and ejb client jar

When I attempt to access the EJB from the portlet doing a JNDI lookup it works fine but when I try to use DI to get a reference to the bean I get a NullPointerException.

Here's the code:

PreferenceBean EJB:
/**
* Session Bean implementation class PreferencesBean
*/
@Stateless(mappedName="ejb/Preference")
public class PreferenceBean implements PreferenceLocal, PreferenceRemote {

     @Override
     public void createPreference(List<PreferenceEntity> preferences) {
          System.out.println("Created Preference");
          
     }

     @Override
     public void deletePreference(List<PreferenceEntity> preferences) {
          System.out.println("Deleted Preference");
          
     }

     @Override
     public List<PreferenceEntity> getPreference(PreferenceType prefType) {
          System.out.println("Get Preference");
          
          ArrayList<PreferenceEntity> peAL = new ArrayList<PreferenceEntity>();
          
          for (int i=0;i<10;i++) {
               PreferenceEntity pe = new PreferenceEntity();
               pe.setId(i);
               pe.setCreateDate(new Date());
               pe.setPreferenceName("prefName" + i);
               pe.setPreferenceValue("prefValue"+i);
               pe.setUserName("weblogic"+i);
               peAL.add(pe);
          }
          
          return peAL;
     }

     @Override
     public void updatePreference(List<PreferenceEntity> preferences) {
          System.out.println("Update Preference");
     }
}

Here's the Portlet EJBPortlet:

public class EJBPortlet extends GenericPortlet {
     @EJB(mappedName="ejb/Preference")
     private Preference preference;
     
     
     public void doView(RenderRequest request, RenderResponse response)
               throws PortletException, IOException {
          response.setContentType("text/html");
//          Context ctx = null;
//          Preference preference = null;
//          
//          try {
//               /** not needed when running locally **/
//               Hashtable<String, String> env = new Hashtable<String, String>();
//               env.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
//               env.put(Context.SECURITY_PRINCIPAL,"weblogic");
//               env.put(Context.SECURITY_CREDENTIALS,"weblogic123");
//               env.put(Context.PROVIDER_URL,"t3://localhost:7001");
//               ctx = new InitialContext();
//               preference = (Preference) ctx.lookup("ejb/Preference#com.mycompany.preferences.PreferenceRemote");
//          } catch (Exception e) {
//               e.printStackTrace();
//          }
          
          
          preference.createPreference(null);
          preference.deletePreference(null);
          
          ArrayList<PreferenceEntity> peAL = (ArrayList<PreferenceEntity>)preference.getPreference(PreferenceType.CATEGORY);
          for (PreferenceEntity pe : peAL) {
               response.getWriter().write(pe.toString() + " ") ;
          }

          response.getWriter().write("<p>Hello World!</p>");
     }

}

As previously mentioned, using InitialContext and JNDI lookup in the portlet works fine. When that code is commented and I use @EJB, I get a NullPointerException.

Thanks for your help.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points