You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jetspeed-user@portals.apache.org by tr...@excite.it on 2003/11/19 12:11:32 UTC

How to use an EJB into jetspeed portlet?

I am trying to use Entity Container Managed Persistence (CMP) EJB in a portlet
and I had success with this portlet (client to test my ejb): 

public class EjbConnectionAction extends JspPortletAction {

  /** JNDI Context Provider */
  public static String CONTEXT_PROVIDER_URL = "localhost:1099";
  /** JNDI Context class Factory Name */
  public static String CONTEXT_FACTORY_NAME = "org.jnp.interfaces.NamingContextFactory";
  public static String CONTEXT_FACTORY_PKGS = "org.jboss.naming:org.jnp.interfaces";
  /** JNDI Properties */

  protected void buildNormalContext(Portlet portlet, RunData rundata) {
  } //buildNormalContext

  public void doPerform(RunData data) throws Exception {
    Properties Properties = new Properties();
    InitialContext mioContesto = null;
    Properties.put("Context.PROVIDER_URL", CONTEXT_PROVIDER_URL);
    Properties.put("java.naming.provider.url", CONTEXT_PROVIDER_URL);
    Properties.put("java.naming.factory.initial", CONTEXT_FACTORY_NAME);
    Properties.put("java.naming.factory.url.pkgs", CONTEXT_FACTORY_PKGS);
    Object client = null;
    try {
      mioContesto = new InitialContext(Properties);
      client = mioContesto.lookup("Client");
    }
    catch (NamingException ne) {
      System.out.println("(test)Errore1 " + ne);
    } 
    ClientRemoteHome xx = (ClientRemoteHome) client;
    ClientRemote c = null;
    try {
      c = xx.create();
    }
    catch (RemoteException re) {
      System.out.println("(test)Errore2" + re);
    }
    catch (CreateException ce) {
      System.out.println("(test)Errore3" + ce);
    }
    Collection col = null;
    try {
      c.insRecord(new Integer(1), "Carlo", "Carlo", new Integer(28));
      col = c.cercaPersoneEtaMax();
      c.delRecord(new Integer(3));
    }
    catch (RemoteException ex) {
    }
    if (col == null) {
      System.out.println("error!");
    }
    else {
      System.out.println("Result: " + col.size());
    } 
  } 
} 

How you can see, I call my ejb from an ACTION (EjbConnectionAction)
In the doPerform method I establish also the connection,but it seems to
me that this is not a correct method because, every time I call doPerform,
i establish the connection.
I want to be able to establish the connection only at the moment of the
activation of the portlet for example in the init() method.

public class MyPortlet extends GenericMVCPortlet
{

    /** JNDI Context Provider */
    public static String CONTEXT_PROVIDER_URL = "localhost:1099";
    /** JNDI Context class Factory Name */
    public static String CONTEXT_FACTORY_NAME =
        "org.jnp.interfaces.NamingContextFactory";
    public static String CONTEXT_FACTORY_PKGS =
        "org.jboss.naming:org.jnp.interfaces";
    /** JNDI Properties */

    public static final String TEMPLATE = "template";

    public void init() throws PortletException {
    // Can I estabilish the connection here????????????
    } 
} 

I have this doubt because after i have created ClientRemote, into the "init()".How
I can make it visible (usable) into the action ? [in the init() i have not
Rundata to set an Attribute].
In the action,i want to be able to make 
----------
    Collection col = null;
    try {
      c.insRecord(new Integer(1), "Carlo", "Carlo", new Integer(28));
      col = c.cercaPersoneEtaMax();
      c.delRecord(new Integer(3));
    }
    catch (RemoteException ex) {
    }
    if (col == null) {
      System.out.println("error!");
    }
    else {
      System.out.println("Result: " + col.size());
    } 
----------
Where "c" is the ClientRemote object created in the init() of the portlet.Some
suggestion in order to resolve this problem? It is possible or there is
an error in my logic of use ejb in the portlet? 
Can you publish some example source code of use ejb in a Jetspeed portlet
or publish a link to a site with examples.
thanks in advance.
Orfeo

www.orfeo.tk

__________________________________________________________________
Accesso Internet Gratis per utenti Excite! Attivalo subito!
http://www.excite.it/hitech/accesso

Il Mio Excite. Personalizza la tua Home page Excite come vuoi tu!
http://www.excite.it

AAA/Relazioni. Sfoglia gli annunci e trova la tua anima gemella
http://www.excite.it/relazioni




---------------------------------------------------------------------
To unsubscribe, e-mail: jetspeed-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jetspeed-user-help@jakarta.apache.org