Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use reflection to realize ejb dynamic delegation

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you "how to use reflection to achieve ejb dynamic delegation", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use reflection to achieve ejb dynamic delegation" this article.

There may be many methods for each bean. Generally, we call the methods in sessionbean through a delegate, instead of directly calling the public method in sessionbean,delegate, which simply encapsulates the public method of each corresponding sessionbean, omitting every search for home and the create of EJB objects, but there may be many methods in our bean. If we write such a delegate for each bean, the workload will be great. And it is not convenient to transplant the system in the future, for example, the original implementation using ejb, now we have to use jdo to directly operate the database, and through the use of Java's reflect technology, we can better achieve these requirements. First of all, an abstract class of FacadeDelegate is defined to find the home of sessionbean. The code is as follows: XML:namespace prefix = o ns = "urn:schemas-microsoft-com:Office:office" / >

Import javax.ejb.*

Import testejb.util.common.*

Import testejb.util.resource.*

Public abstract class FacadeDelegate {

Private static String type = Resource.RemoteType

Public FacadeDelegate () {

}

Public EJBHome getHome (String jindiName,Class className)

{

EJBHome home = null

ServerLocatorAdapter adapter = ServerLocatorAdapter.getInstance ()

Try

{

Home = (EJBHome) adapter.getHome (type, jindiName, className)

}

Catch (Exception e)

{

System.err.println (e.getMessage () + jindiName + className.toString ())

}

Return home

}

}

ServerLocatorAdapter is a class used to find home through different methods according to whether local or remote calls the ejb object. If type is local, the method in LocalServerLocate is called; if type is remote, the method in RemoteServerLocate is called to get home. The code is as follows:

Import java.util.*

Import java.lang.reflect.*

Import testejb.util.resource.*

Public class ServerLocatorAdapter {

Private Map cache;// is used to cache home

Private static ServerLocatorAdapter me

Public static ServerLocatorAdapter getInstance ()

{

If (me = = null)

Me = new ServerLocatorAdapter ()

Return me

}

/ / obtain home

Public object getHome (String type,String jndiHomeName,Class className) throws Exception

{

Object home = null

If (cache.containsKey (jndiHomeName))

Return cache.get (jndiHomeName)

If (Resource.LocalType.equals (type))

{

Home = getLocalHome (jndiHomeName,className)

Cache.put (jndiHomeName,home)

Return home

}

If (Resource.RemoteType.equals (type))

{

Home = getRemoteHome (jndiHomeName,className)

Cache.put (jndiHomeName,home)

Return home

}

Return home

}

/ / obtain local home

Private Object getLocalHome (String jndiHomeName,Class className) throws Exception

{

Class myClass = Class.forName (Resource.LocalClass)

/ / Resource. LocalClass = "testejb.util.common. LocalServerLocator

Method method = myClass.getMethod (Resource.LocalConstractMethod,null)

/ / Resource. LocalConstractMethod = "getInstance"

LocalServerLocator local = null

Local = (LocalServerLocator) method.invoke (myClass,null)

Return local.getLocalHome (jndiHomeName,className)

}

/ / obtain remote home

Private Object getRemoteHome (String jndiHomeName,Class className) throws Exception

{

Class myClass = Class.forName (Resource.RemoteClass)

/ / Resource.RemoteClass = "testejb.util.common.RemoteServerLocator"

Method method = myClass.getMethod (Resource.RemoteConstractMethod,null)

/ / Resource.RemoteConstractMethod= "getInstance"

RemoteServerLocator remote = null

Remote = (RemoteServerLocator) method.invoke (myClass,null)

Return remote.getHome (jndiHomeName,className)

}

Private ServerLocatorAdapter () {

/ / provide thread safety guarantee for cache

Cache = Collections.synchronizedMap (new HashMap ())

}

}

Resource is a resource class, in which some specified configuration information is obtained by reading the configuration file.

RemoteServerLocator and LocalServerLocator are two concrete implementation classes that obtain home excuses according to different invocation methods. The code is as follows:

LocalServerLocator:

Import javax.naming.*

Import javax.Rmi.PortableRemoteObject

Import java.util.*

Import javax.ejb.*

Public class LocalServerLocator {

Private Context ic

Private Map cache;// cache home

Private static LocalServerLocator me

Public static LocalServerLocator getInstance ()

{

If (me = = null)

{

Try

{

Me = new LocalServerLocator ()

}

Catch (Exception e)

{

System.err.println (e.getCause ())

System.err.println (e.getMessage ())

}

}

Return me

}

Public EJBLocalHome getLocalHome (String jndiHomeName, Class className) throws Exception {

EJBLocalHome home = null

Try {

If (cache.containsKey (jndiHomeName)) {

Home = (EJBLocalHome) cache.get (jndiHomeName)

} else {

Object objref = ic.lookup (jndiHomeName)

Home = (EJBLocalHome) objref

Cache.put (jndiHomeName, home)

}

} catch (NamingException ne) {

System.err.println (jndiHomeName)

Throw ne

} catch (Exception e) {

Throw e

}

Return home

}

Private LocalServerLocator () throws Exception {

Try

{

Ic = new InitialContext ()

/ / provide thread safety guarantee for cache

Cache = Collections.synchronizedMap (new HashMap ())

}

Catch (NamingException ne)

{

Throw ne

}

Catch (Exception e)

{

Throw e

}

}

}

RemoteServerLocator

Import javax.naming.*

Import javax.rmi.PortableRemoteObject

Import java.util.*

Import javax.ejb.*

Public class RemoteServerLocator {

Private Context ic

Private Map cache

Private static RemoteServerLocator me

Public static RemoteServerLocator getInstance ()

{

If (me = = null)

{

Try

{

Me = new RemoteServerLocator ()

}

Catch (Exception e)

{

System.err.println (e.getMessage ())

}

}

Return me

}

Public EJBHome getHome (String jndiHomeName, Class className) throws Exception {

EJBHome home = null

Try {

If (cache.containsKey (jndiHomeName)) {

Home = (EJBHome) cache.get (jndiHomeName)

} else {

Object objref = ic.lookup (jndiHomeName)

Object obj = PortableRemoteObject.narrow (objref, className)

Home = (EJBHome) obj

Cache.put (jndiHomeName, home)

}

} catch (NamingException ne) {

System.err.println (jndiHomeName)

Throw ne

} catch (Exception e) {

Throw e

}

Return home

}

Private RemoteServerLocator () throws Exception {

Try {

Ic = getInitialContext ()

/ / provide thread safety guarantee for cache

Cache = Collections.synchronizedMap (new HashMap ())

} catch (NamingException ne) {

Throw ne

} catch (Exception e) {

Throw e

}

}

Private javax.naming.Context getInitialContext () throws NamingException {

Java.util.Hashtable JNDIPaRM = new java.util.Hashtable ()

JNDIParm.put (Context.PROVideR_URL, "your server address")

JNDIParm.put (Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory")

Return new InitialContext (JNDIParm)

}

}

With an understanding of the above invocation mechanisms, here is the concrete implementation of dynamic delegation, which defines a FacadeDelegateImp class that inherits the FacadeDelegate class. Take a look at the code first, and then explain it, so it's clearer.

Import testejb.delegate.common.*

Import javax.ejb.*

Import java.lang.reflect.*

Import java.util.*

Public class FacadeDelegateImp extends FacadeDelegate {

Private static FacadeDelegateImp me

Private Map cache

Private HashMap methodMap

Private Object object

Public static FacadeDelegateImp getInstance ()

{

If (me = = null)

Me = new FacadeDelegateImp ()

Return me

}

/ / the init method initializes the sessionbean to be called before calling invoke

Public void init (String jindiName, String className) {

EJBHome home = null

If (cache.containsKey (jindiName))

Home = (EJBHome) cache.get (jindiName)

Else

{

Try

{

Home = super.getHome (jindiName, Class.forName (className)); / / call the method of the parent class to get home

}

Catch (Exception e)

{

System.err.println (e.getMessage ())

}

Cache.put (jindiName,className)

}

Try

{

Object = home.getClass () .getMethod ("create", null) .invoke (home, null); / / call the / / create method of home to get ejbObject

MethodMap = new HashMap ()

/ / store all methods in ejbObject into methodMap

Method [] aryMethod = object.getClass () .getMethods

If (aryMethod! = null & & aryMethod.length > 0)

{

For (int I = 0; I

< aryMethod.length; i++) { methodMap.put(aryMethod[i].getName(), aryMethod[i]); } } } catch(Exception e) { System.err.println(e.getMessage()); } } //此init方法是对一般java类初始化 public void init(String className,Object[] args) { boolean flage = false; if(cache.get(className) != null) object = cache.get(className); else { try { Class myClass = Class.forName(className); if (args != null && args.length >

0) {

Class [] type = new Class [args.length]

For (int I = 0; I < type.length; iTunes +) {

Type [I] = args [I] .getClass ()

}

Constructor constructor = myClass.getConstructor (type)

Object = constructor.newInstance (args)

Cache.put (className, object)

}

Else {

Object = myClass.newInstance ()

Cache.put (className, object)

}

}

Catch (Exception e) {

System.err.println (e.getMessage ())

}

}

Method [] methods = object.getClass () .getMethods

MethodMap = new HashMap ()

For (int I = 0; I < methods.length; iTunes +)

MethodMap.put (methods [I] .getName (), methods [I])

}

Public Object invoke (String method, Object [] args,String jindiName, String className)

{

If ("init" .equals (method))

{

This.init (jindiName, className)

Return null

}

If (methodMap = = null)

This.init (jindiName, className)

Method tmpMethod = (Method) methodMap.get (method)

If (tmpMethod! = null)

{

Try

{

Return tmpMethod.invoke (object, args)

}

Catch (Exception e)

{

System.err.println (e.getMessage ())

}

}

Return null

}

Public Object invoke (String method, Object [] args, String className)

{

If ("init" .equals (method))

{

This.init (className,args)

Return null

}

If (methodMap = = null)

System.err.println ("not init")

Method tmpMethod = (Method) methodMap.get (method)

If (tmpMethod! = null)

{

Try

{

Return tmpMethod.invoke (object, args)

}

Catch (Exception e)

{

System.err.println (e.getMessage ())

}

}

Return null

}

Private FacadeDelegateImp () {

Cache = Collections.synchronizedMap (new HashMap ())

}

}

The above is all the content of the article "how to use reflection to achieve ejb dynamic delegation". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report