Link to home
Start Free TrialLog in
Avatar of deshani
deshaniFlag for United States of America

asked on

Absolutely new to EJB.. Regarding implementing methods in interfaces..

Hi all.. Please answer this asap..
I have ".java" files in a package like this:

---------------------------------------------------------------------------
Authentication.java:-

package com.it.foundation.signon;
import javax.ejb.*;
import java.rmi.Remote;
import java.rmi.RemoteException;
import com.it.foundation.*;
public interface Authentication extends EJBObject, BeanInterface{
}
---------------------------------------------------------------------------
AuthenticationHome.java:-

package com.it.foundation.signon;
import javax.ejb.*;
import java.rmi.RemoteException;
public interface AuthenticationHome extends EJBHome {
      public Authentication create() throws RemoteException, CreateException;
}
---------------------------------------------------------------------------
The "BeanInterface.java" which is used in the "Authentication.java" is as given below:

package com.it.foundation;
import java.rmi.RemoteException;
import com.it.foundation.action.CommandRequest;
import com.it.foundation.action.CommandResponse;
public interface BeanInterface
{
          public CommandResponse execute(CommandRequest request) throws RemoteException;
}
---------------------------------------------------------------------------
**********************************************
Note that both the files above contain interfaces.
**********************************************

Now I have a file by the name "EJBGetter.java":

package com.it.foundation.utils;

import javax.rmi.PortableRemoteObject;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import com.it.foundation.signon.AuthenticationHome;

public final class EJBGetter {
    private static InitialContext initial = null;

    public static InitialContext getContext() throws NamingException {
        if (initial == null) {
            initial = new InitialContext();
        }

        return initial;
    }

    public static AuthenticationHome getAuthenticationHome() throws NamingException {
        InitialContext context = getContext();
        Object objref = context.lookup(CConst.AUTHENTICATION_EJBHOME);

        return (AuthenticationHome) PortableRemoteObject.narrow(objref, AuthenticationHome.class);
    }

}
---------------------------------------------------------------------------
**********************************************
QUESTION : How is it possible that the EJBGetter class is creating a function "getAuthenticationHome()" of the interface "AuthenticationHome"?  
**********************************************
---------------------------------------------------------------------------

It is even being used in another servlet as follows :

static {
        try {
            AuthenticationHome home = EJBGetter.getAuthenticationHome();
            authenticationBean = home.create();
        } catch (RemoteException rex) {
            rex.printStackTrace();
        } catch (CreateException cex) {
            cex.printStackTrace();
        } catch (NamingException nex) {
            nex.printStackTrace();
        }
    }
-----------------------------------------------------------------------------------------------
************************************************************
How is this possible? Could someone please explain. I have this code working correctly. I think I am missing somerthing here. But isn't an interface NOT allowed to implement code in its method. Moreover how is another class implementing the body of the method of an interface that it does implement?
Avatar of Mick Barry
Mick Barry
Flag of Australia image

> But isn't an interface NOT allowed to implement code in its method.

EJBGetter is not an interface, it is a class

> Moreover how is another class implementing the body of the method of an interface that it does implement?

A class can create methods of any sig, the fact that the sig is used by an interface is irrelevant.
The signature of that method is actually a littl different, in that it is a static class method.
Avatar of deshani

ASKER

Hi objects,
"The signature of that method is actually a littl different, in that it is a static class method."
---Does this mean that you are talking about this particular case or in a general sense?
---Does a method with a signature of another class/interface mean that when we use that method we can instantiate the object of that class, as is done in this case? See the EJBGetter class, where we say:
             ------------------------------------------------------
       AuthenticationHome home = EJBGetter.getAuthenticationHome();
             ------------------------------------------------------
Does this not amount to actually creating an object "home" of the interface "AuthenticationHome"?

Thanks,
Anup
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of deshani

ASKER

thanks a million...this clarifies it a lot. Still have some doubts, but I believe they are due to lack of knowledge in EJBs. One last request: Could you tell me a few resources that can point to the concept of "creating methods with signature of a different class" and on any reference guide to EJB?
Thanks again.
Regards,
Anup