is it possible all beans access one common class

Discussions

EJB design: is it possible all beans access one common class

  1. hi all,
     is it possible to put one class in app server which should be accessed by all classes without creating again instance of that class.in my application i am handling all exception in beans through user defined exception.which will get error message from one common repository which will get all messages from back end put into hashtable.when user defined exception raised in bean the instance of exception is created by passing error code.this exception class will pass this error code to that common repository class which having hash table.it will get back the error message to exception class.this error message will thrown to front end.

    so my problem is if it's possible to have one common class which hold my hash table and serve all my beans.this common class will be created once by login bean class when application starts.from then on wards it should serve my user defined exception class for getting error message from hash.
    please,any suggestion is very much appriciatable.

    regards
    raju
    rajireddy at hexaware dot co.in
  2. u can do it using two ways. one way is to create a singleton class, thus making sure that only one instance of that class exists in one JVM.

    the other way to do in ur case is define a static method getErrorCode() in ur class. read and populate the hashtable in the static block of that class.
  3. hi kapil,
    same thing i am using static method wich gets all error messages from back end.where i have to place this class which has hashtable,which will be used by ueser difined exception class when i raise exception in my business method.please let me know.if you any code please send it accross it's very urgent for me.
  4. u have to place this class in a class path which is recognizable to ur app server.

    for eg in weblogic u can put it in weblogic_root\your_server\classes

    for websphere u can configure the classpath in one of the .cfg files. not sure. i think admin.cfg or startup.cfg.

    just one more thing, this class has to be accessible to ur user defined exception which is bein thrown by ur EJB method, make sure if the exception is accessed by ur client then ur client shud also be aware of the error code class. if the client is part of ur app server like jsp or servlet then just point the class to app server class path.
  5. Hi,

    I have a similar problem. I want to connect to a CORBA Object and I want to create the ORB only one time. So I thought to make the class static but this is not allowed by EJB. It is not allowed to have static variables.

    So is it possible to have a EJB as a Singleton???

    It would be very helpful if you could give me some hints.

    Thanx, Andy
  6. well u can definitely make sure that only one instance of ur EJB is created, by restricting it in the deployment file, with number of instance as 1.

    the ORB object can be defined as an object instance, which can be only be initialised once.

    so u cud have a

    ORBLookupBean implements SessionBean
    {
    ORBObject o = null;

    public void ejbCreate(){
    if (o == null){
     //create object}
    }

    }

    public ORBObject getORB(){
    return o;}

    }
  7. Thanx a lot for your help. That's a good solution and it should work in (hopefully) every Application Server.

    Andy
  8. hi kapil again,
    singleton is good idea,but who should hold the handle for this singleton after created(say by my login bean when application starts).i have to get handle for this singleton class from different beans when i throw my user defined exception.please let me know how it will possible.

    ragards
    raju
  9. In a singleton class, there should be a getInstace(), which is a static method that will return the handle.

    eg,
    public class Singleton {

      private Singleton() {

      }

      private static Singleton mySingleton= new Singleton();

      public static Singleton getInstance() {

        return mySingleton;
      }
    }
      
      

  10. hi koshy,
    where i have to put this singleton class after creating.it should be accesseble by all of my beans in app server.
    thanks
    raju
  11. this single ton class will be normal java vanilla object and not bean, so theres no question of a handle.

    about accessing it from other beans yes u can very well do it from anywhere within the same jvm.

    as koshy has already mentioned the code.

    wherever u wanna use it just call -

    SingleTon o = SingleTon.getInstance();

    and u will always get the same instance.


  12. thanks kapil,for your reply.
    raju