deshani
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.C ommandRequ est;
import com.it.foundation.action.C ommandResp onse;
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.PortableRemoteOb ject;
import javax.naming.InitialContex t;
import javax.naming.NamingExcepti on;
import com.it.foundation.signon.A uthenticat ionHome;
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.AUTH ENTICATION _EJBHOME);
return (AuthenticationHome) PortableRemoteObject.narro w(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.getAuthenticatio nHome();
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?
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.C
import com.it.foundation.action.C
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.PortableRemoteOb
import javax.naming.InitialContex
import javax.naming.NamingExcepti
import com.it.foundation.signon.A
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.AUTH
return (AuthenticationHome) PortableRemoteObject.narro
}
}
--------------------------
**************************
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.getAuthenticatio
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?
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.getAuthenticatio nHome();
-------------------------- ---------- ---------- --------
Does this not amount to actually creating an object "home" of the interface "AuthenticationHome"?
Thanks,
Anup
"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.getAuthenticatio
--------------------------
Does this not amount to actually creating an object "home" of the interface "AuthenticationHome"?
Thanks,
Anup
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
Thanks again.
Regards,
Anup
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.