On Sat, Apr 13, 2013 at 1:00 PM, José Luis Cetina <maxtorz...@gmail.com>wrote:

> When i dont want to use codi I use jndi lookup, it is to easy to use and
> portable. I create a simple method for retrive my ejbs without using codi
> instead i use jndi using only the name of the ejb class.
> El 13/04/2013 11:45, "Howard W. Smith, Jr." <smithh032...@gmail.com>
> escribió:
>
>
José,

JNDI works, thank you!  This was the first time I 'ever' had to use JNDI
lookup. I had to research it (search google, look at TomEE examples - ejb
reference[1][2][3], surely did not want to create an 'interface', kept
looking, looked at tomee log, as i know it lists all @EJBs at startup in
the log, didn't see what i was looking for, so i looked at RI/Glassfish
log, since i 'remembered' that RI/Glassfish mentions the JNDI lookup path
for all my @EJB's in the glassfish log at 'startup' of my app)...

So, per an oracle blog[4][5] about JNDI lookup and what i saw in glassfish
log (12/9/2012 was last time i used glassfish to start my app...smile), I
modified the @Singleton @Lock(READ) bean, accordingly (see code below),
tested it, and voila, it works!!!! :)


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package converter;

import java.util.concurrent.TimeUnit;

import javax.ejb.AccessTimeout;
import javax.ejb.Lock;
import javax.ejb.LockType;
import javax.ejb.Singleton;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import javax.naming.InitialContext;

import jpa.entities.Customer;
import jpa.session.CustomerFacade;


@Singleton
@Lock(LockType.READ)
@AccessTimeout(value = 1, unit = TimeUnit.MINUTES)
@FacesConverter(forClass = Customer.class)
public class CustomerConverter implements Converter {

    public CustomerConverter() {

    }

    public Object getAsObject(FacesContext facesContext, UIComponent
component, String value) {
        if (value == null || value.length() == 0) {
            return null;
        }
        /*
         * 2012-07-10 when user enters invalid/incomplete value (e.g.
"irene", see below) in AutoComplete
         *
        WARNING: For input string: "irene"
        java.lang.NumberFormatException: For input string: "irene"
                at
java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
                at java.lang.Integer.parseInt(Integer.java:492)
                at java.lang.Integer.valueOf(Integer.java:582)
                ...
                ...
                at
org.primefaces.component.autocomplete.AutoCompleteRenderer.getConvertedValue(AutoCompleteRenderer.java:529)
                at
javax.faces.component.UIInput.getConvertedValue(UIInput.java:1030)
                at javax.faces.component.UIInput.validate(UIInput.java:960)
         *
         */
        try {
            Integer test = getKey(value);
        } catch (java.lang.NumberFormatException e) {
            return null;
        }
        Object object = null;
        CustomerFacade ejbFacade;
        try {
            InitialContext ic = new InitialContext();
            ejbFacade = (CustomerFacade)
ic.lookup("java:global/mcmsweb/CustomerFacade");
            if (ejbFacade == null) {
                System.err.println("CustomerConverter.getAsObject():
ejbFacade = null)");
                return null;
            }
        } catch (Exception e) {
            System.err.println("CustomerConverter.getAsObject(): error on
JNDI lookup of CustomerFacade");
            e.printStackTrace();
            return null;
        }
        try {
            object = ejbFacade.find(getKey(value));
        } catch (Exception e) {
            System.err.println("CustomerConverter.getAsObject(): error on
ejbFacade.find(getKey(value))");
            e.printStackTrace();
            return null;
        }
        return object;
    }

    java.lang.Integer getKey(String value) {
        java.lang.Integer key;
        key = Integer.valueOf(value);
        return key;
    }

    String getStringKey(java.lang.Integer value) {
        StringBuffer sb = new StringBuffer();
        sb.append(value);
        return sb.toString();
    }

    public String getAsString(FacesContext facesContext, UIComponent
component, Object object) {
        if (object == null) {
            return null;
        }
        if (object instanceof Customer) {
            Customer o = (Customer) object;
            return getStringKey(o.getCustomerId());
        } else {
            throw new IllegalArgumentException("object " + object + " is of
type " + object.getClass().getName() + "; expected type: " +
Customer.class.getName());
        }
    }
}


tomee examples - ejb reference
[1] http://tomee.apache.org/examples-trunk/injection-of-ejbs/README.html
[2]
http://tomee.apache.org/examples-trunk/lookup-of-ejbs-with-descriptor/README.html
[3] http://tomee.apache.org/examples-trunk/lookup-of-ejbs/README.html

oracle blogs
[4]
https://blogs.oracle.com/kensaks/entry/application_specified_portable_jndi_names

which references the following, but needed to replace 'sun' with 'oracle'
in the URL

[5] https://blogs.oracle.com/kensaks/entry/portable_global_jndi_names

Reply via email to