The inserted Java code looks up the remote or local home interface of an EJB reference and calls the "create" method to create an instance of the enterprise bean.
By using the Snippets view to quickly insert this code, you can avoid manually coding the appropriate business logic to create an instance of a bean. The Java code is generated and inserted in the current spot of the Java file that you are editing.
Example snippet insertion for calling an EJB create method
public class Test { public void createMyBean() { // insert snippet here } }
import com.ibm.etools.service.locator.ServiceLocatorManager; import java.rmi.RemoteException; import sample.RegistrationFacadeHome; import sample.RegistrationFacadeRemote; import javax.ejb.CreateException; public class Test { private final static String STATIC_RegistrationFacadeHome_REF_NAME = "ejb/RegistrationFacade"; private final static Class STATIC_RegistrationFacadeHome_CLASS = RegistrationFacadeHome.class; public void createMyBean() { // insert snippet here RegistrationFacadeRemote aRegistrationFacadeRemote = createRegistrationFacadeRemote(); } protected RegistrationFacadeRemote createRegistrationFacadeRemote() { RegistrationFacadeHome aRegistrationFacadeHome = (RegistrationFacadeHome) ServiceLocatorManager .getRemoteHome(STATIC_RegistrationFacadeHome_REF_NAME, STATIC_RegistrationFacadeHome_CLASS); try { if (aRegistrationFacadeHome != null) return aRegistrationFacadeHome.create(); } catch (CreateException ce) { // TODO Auto-generated catch block ce.printStackTrace(); } catch (RemoteException re) { // TODO Auto-generated catch block re.printStackTrace(); } return null; } }
To insert Java code for creating an instance of an enterprise bean:
The ServiceLocatorManager class has a static method called setErrorHandler(ServiceLocatorErrorHandler handler) that you can use to specify a specific error handler for error conditions that occur when looking up the home interface. The default handler simply calls printStackTrace() on the exception that is handled.