1    package com.instantbank.lettertemplate.control.util;
2    
3    import java.rmi.RemoteException;
4    import javax.rmi.PortableRemoteObject;
5    import javax.naming.InitialContext;
6    import javax.naming.NamingException;
7    import javax.ejb.CreateException;
8    import javax.ejb.SessionContext;
9    
10   import com.instantbank.common.utilcomponents.JNDINames;
11   import com.instantbank.component.lettertemplate.ejb.LetterTemplateHome;
12   import com.instantbank.component.job.ejb.JobHome;
13   import com.instantbank.lettertemplate.control.ejb.LetterTemplateControllerHome;
14   
15   /**
16    *  This is a utility class for obtaining EJB references.
17    *
18    * @author Instant-bank (Consuelo Franky)
19    * @created September 2002
20    */
21   public final class EJBUtil {
22   
23     /**
24      *  get Home reference to a LetterTemplateController EJB
25      *
26      * @return The LetterTemplateControllerHome value
27      * @exception Exception
28      */
29     public static LetterTemplateControllerHome getLetterTemplateControllerHome() throws Exception {
30       try {
31         InitialContext initial = new InitialContext();
32         Object objref = initial.lookup(JNDINames.LETTERTEMPLATE_SCC_EJBHOME);
33         return (LetterTemplateControllerHome)
34           PortableRemoteObject.narrow(objref, LetterTemplateControllerHome.class);
35       }
36       catch(NamingException ne) {
37         throw new Exception(ne.getMessage());
38       }
39     }
40   
41   
42     /**
43      *  get Home reference to a LetterTemplate EJB
44      *
45      * @return The LetterTemplateHome value
46      * @exception Exception
47      */
48     public static LetterTemplateHome getLetterTemplateHome() throws Exception {
49       try {
50         InitialContext initial = new InitialContext();
51         Object objref = initial.lookup(JNDINames.LETTERTEMPLATE_EJBHOME);
52         return (LetterTemplateHome)
53           PortableRemoteObject.narrow(objref, LetterTemplateHome.class);
54       }
55       catch(NamingException ne) {
56         throw new Exception(ne.getMessage());
57       }
58     }
59   
60   
61     /**
62      *  get Home reference to a Job EJB
63      *
64      * @return The JobHome value
65      * @exception Exception
66      */
67     public static JobHome getJobHome() throws Exception {
68       try {
69         InitialContext initial = new InitialContext();
70         Object objref = initial.lookup(JNDINames.JOB_EJBHOME);
71         return (JobHome)
72           PortableRemoteObject.narrow(objref, JobHome.class);
73       }
74       catch(NamingException ne) {
75         throw new Exception(ne.getMessage());
76       }
77     }
78   
79   
80     /**
81      *  get the current user from the context
82      *
83      * @param context is the context
84      * @return The userId value
85      * @exception Exception
86      */
87     public static String getUserId(SessionContext context)
88        throws Exception {
89       String userId = context.getCallerPrincipal().getName();
90       return userId;
91     }
92   
93   
94     /**
95      *  determine if the current user belongs to a role
96      *
97      * @param context is the context
98      * @param rol role of sistem
99      * @return The userInRole value
100     * @exception Exception
101     */
102    public static boolean isUserInRole(SessionContext context, String rol)
103       throws Exception {
104      return (context.isCallerInRole(rol));
105    }
106  
107  }
108