ejb@glassfish.java.net

Re: Environment Entry Resource Injection

From: Cheng Fang <Cheng.Fang_at_Sun.COM>
Date: Wed, 05 Jul 2006 16:51:35 -0400

You need to declare a env-entry in ejb-jar.xml for this bean, and inject
this env-entry by its name into your bean class. Don't put env-entry in
sun-ejb-jar.xml. HTH.

<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="3.0"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd">
<enterprise-beans>
  <session>
    <ejb-name>GeocodeServiceBean</ejb-name>
      <env-entry>
        <description>xxx</description>
        <env-entry-name>foobar</env-entry-name>
        <env-entry-type>java.lang.String</env-entry-type>
        <env-entry-value>xxx</env-entry-value>
      </env-entry>

    @Resource(name="foobar")
    private String address;

Cheng

Daniel Cavalcanti wrote:

> Hello,
>
> I have an EJB 3.0 project that I'm having a little trouble with.
> I have a remote, stateless bean, and this bean has a property called
> address of type String.
> I annotate the property to have resource injected to it, but the
> container (glassfish) is not injecting the resource.
>
> here is the code:
>
> /*
> --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
> */
>
> package com.localmatters.mobile.services.geocode;
>
> import java.util.List;
> import javax.ejb.Remote;
>
> /**
> * The geocode service interface.
> */
> @Remote()
> public interface GeocodeService {
>
> /**
> * Geocodes a free-form location.
> *
> * @param location The free-form location.
> * @throws NullPointerException If location is null.
> * @throws GeocodeServiceException If geocode service fails.
> * @return The geocodes for the location.
> */
> public List<Geocode> geocode(String location)
> throws NullPointerException, GeocodeServiceException;
>
> /**
> * Geocodes a location defined by its distinct fields.
> *
> * @param address The address field.
> * @param city The city field.
> * @param state The state (or province) field.
> * @param postal The postal code field.
> * @throws GeocodeServiceException If geocode service fails.
> * @return The geocodes for the location.
> */
> public List<Geocode> geocode(String address, String city, String
> state, String postal)
> throws GeocodeServiceException;
>
> }
>
> /*
> --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
> */
>
> package com.localmatters.mobile.services.geocode;
>
> import java.util.ArrayList;
> import java.util.List;
> import javax.annotation.PostConstruct;
> import javax.annotation.Resource;
> import javax.ejb.Stateless ;
>
> /**
> * The hermes based geocode service implementation.
> *
> * @author Daniel Cavalcanti
> */
> @Stateless()
> public class GeocodeServiceBean implements GeocodeService {
>
> //
> **********************************************************************
> // Instance variables
> //
> **********************************************************************
>
> /**
> * The hermes web service WSDL URL.
> */
> @Resource()
> private String address;
>
> //
> **********************************************************************
> // Constructors
> //
> **********************************************************************
>
> /**
> * Creates a new instance of GeocodeServiceBean.
> */
> public GeocodeServiceBean() {
> }
>
> //
> **********************************************************************
> // EJB life-cycle (call-back) methods
> //
> **********************************************************************
>
> /**
> * Establishes connection to hermes web service client port.
> */
> @PostConstruct()
> private void postConstruct() {
> // TODO establish hermes web service client port connection
> }
>
> //
> **********************************************************************
> // Getter/Setter methods
> //
> **********************************************************************
>
> //
> **********************************************************************
> // Geocode service implementation
> //
> **********************************************************************
>
> public List<Geocode> geocode(String location)
> throws NullPointerException, GeocodeServiceException {
>
> if (location == null)
> throw new NullPointerException("Location cannot be null.");
>
> // TODO implement method
> return new ArrayList<Geocode>();
>
> }
>
> public List<Geocode> geocode(String address, String city, String
> state, String postal)
> throws GeocodeServiceException {
>
> // TODO implement method
> return new ArrayList<Geocode>();
>
> }
>
> }
>
> /*
> --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
> */
>
> sun-ejb-jar.xml file
>
>
> <?xml version="1.0" encoding="UTF-8"?>
> <!DOCTYPE sun-ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD
> Application Server 9.0 EJB 3.0//EN" "
> http://www.sun.com/software/appserver/dtds/sun-ejb-jar_3_0-0.dtd">
> <sun-ejb-jar>
> <enterprise-beans>
>
> <session>
>
> <!-- The resource injection target bean -->
> <ejb-name>GeocodeService</ejb-name>
>
> <ejb-class>com.localmatters.mobile.services.geocode.GeocodeServiceBean</ejb-class>
>
> <!-- The resource definitions -->
> <env-entry>
> <description>The hermes web service WSDL
> URL.</description>
> <env-entry-name>wsdl</env-entry-name>
> <env-entry-type> java.lang.String</env-entry-type>
>
> <env-entry-value>http://mapping-ws.localmatters.com/services5/mapping?wsdl</env-entry-value>
>
> </env-entry>
>
> </session>
>
> </enterprise-beans>
> </sun-ejb-jar>
>
> /*
> --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
> */
>
> Could someone tell me what I'm doing wrong?
>
> thanks,
> Daniel