KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > ipanema > base > BaseSessionBeanImpl


1 /*
2  * Created on 02.03.2004
3  */

4 package com.nightlabs.ipanema.base;
5
6 import java.rmi.RemoteException JavaDoc;
7 import java.security.Principal JavaDoc;
8 import java.util.ArrayList JavaDoc;
9 import java.util.Collection JavaDoc;
10 import java.util.Hashtable JavaDoc;
11
12 import javax.ejb.EJBException JavaDoc;
13 import javax.ejb.SessionContext JavaDoc;
14 import javax.jdo.PersistenceManager;
15 import javax.jdo.PersistenceManagerFactory;
16 import javax.naming.InitialContext JavaDoc;
17
18 import com.nightlabs.ModuleException;
19 import com.nightlabs.ipanema.servermanager.IpanemaServerManager;
20 import com.nightlabs.ipanema.servermanager.IpanemaServerManagerFactory;
21
22 /**
23  * This class should be used as anchestor of your session beans. Note, that
24  * some methods will probably not work with stateless session beans. This is,
25  * because it seems a stateless session bean has no principal stored in its
26  * session context.
27  *
28  * @author nick@nightlabs.de
29  * @author marco@nightlabs.de
30  */

31 public class BaseSessionBeanImpl
32 {
33     protected SessionContext JavaDoc sessionContext;
34
35     public void setSessionContext(SessionContext JavaDoc sessionContext)
36         throws EJBException JavaDoc, RemoteException JavaDoc
37     {
38         this.sessionContext = sessionContext;
39     }
40     public void unsetSessionContext()
41     {
42         this.sessionContext = null;
43     }
44
45     /**
46      * This method returns the IpanemaPrincipal representing the current user. Note, that
47      * this method probably only works with stateful session beans!
48      *
49      * @return Returns the principal representing the current user.
50      */

51     public IpanemaPrincipal getPrincipal()
52     {
53         Principal JavaDoc pr = sessionContext.getCallerPrincipal();
54 // System.out.println("*********************************************************");
55
// System.out.println("Principal Class: "+pr.getClass().getName());
56
// System.out.println("Principal: "+pr);
57
// System.out.println("*********************************************************");
58
return (IpanemaPrincipal)pr;
59     }
60
61     /**
62      * This method is a shortcut to <code>getPrincipal().getLookup()</code>. It might
63      * not work with stateless session beans!
64      *
65      * @return The Lookup instance assigned to the current user.
66      *
67      * @see getPrincipal()
68      */

69     protected Lookup getLookup()
70     {
71         return getPrincipal().getLookup();
72     }
73     
74     protected PersistenceManagerFactory getPersistenceManagerFactory()
75     throws ModuleException
76     {
77         return getPrincipal().getLookup().getPersistenceManagerFactory();
78     }
79
80     protected void cache_addDirtyObjectIDs(Collection JavaDoc objectIDs)
81     throws ModuleException
82     {
83         getPrincipal().getLookup().getCacheManager().addDirtyObjectIDs(objectIDs);
84     }
85
86     protected void cache_addDirtyObjectID(Object JavaDoc objectID)
87     throws ModuleException
88     {
89         ArrayList JavaDoc l = new ArrayList JavaDoc(1);
90         l.add(objectID);
91         getPrincipal().getLookup().getCacheManager().addDirtyObjectIDs(l);
92     }
93
94     /**
95      * This method is a shortcut to <code>getPrincipal().getLookup().getPersistenceManager()</code>.
96      * It might fail with stateless session beans!
97      *
98      * @return Returns the PersistenceManager assigned to the current user.
99      * @throws ModuleException
100      *
101      * @see getPrincipal()
102      */

103     protected PersistenceManager getPersistenceManager()
104         throws ModuleException
105     {
106         return getPrincipal().getLookup().getPersistenceManager();
107     }
108     
109     protected InitialContext JavaDoc getInitialContext()
110         throws ModuleException
111     {
112         return getPrincipal().getLookup().getInitialContext();
113     }
114
115     /**
116      * Use this method whenever you want to communicate with another
117      * organisation. This method configures an InitialContext to connect
118      * to the right server and to authenticate correctly according
119      * to the Ipanema way of organisation@organisation-authentication.
120      * <br/><br/>
121      * This method is a shortcut to
122      * <code>getPrincipal().getLookup().getInitialContext(organisationID)</code>.
123      *
124      * @param organisationID The organisationID with wich to communicate.
125      * @return Returns an InitialContext that is configured properly to authenticate at and communicate with another organisation (wherever it may be - e.g. on another server).
126      * @throws ModuleException
127      */

128     protected InitialContext JavaDoc getInitialContext(String JavaDoc organisationID)
129         throws ModuleException
130     {
131         return getPrincipal().getLookup().getInitialContext(organisationID);
132     }
133
134     /**
135      * This method returns the properties that are used by
136      * <code>getInitialContext(String organisationID)</code>. It is meant
137      * to be used with *Util classes that expect a Hashtable/Properties instance
138      * to create a home interface.
139      * <br/><br/>
140      * This method is a shortcut to
141      * <code>getPrincipal().getLookup().getInitialContextProps(organisationID)</code>
142      *
143      * @param organisationID
144      * @return Returns an instance of Properties to be used in <code>new InitialContext(Properties)</code>.
145      * @throws ModuleException
146      *
147      * @see getInitialContext(String organisationID)
148      */

149     protected Hashtable JavaDoc getInitialContextProps(String JavaDoc organisationID)
150         throws ModuleException
151     {
152         return getPrincipal().getLookup().getInitialContextProps(organisationID);
153     }
154
155     protected IpanemaServerManagerFactory getIpanemaServerManagerFactory()
156         throws ModuleException
157     {
158         return getPrincipal().getLookup().getIpanemaServerManagerFactory();
159     }
160     
161     protected IpanemaServerManager getIpanemaServerManager()
162     throws ModuleException
163     {
164         return getPrincipal().getLookup().getIpanemaServerManager();
165     }
166
167     protected String JavaDoc getOrganisationID()
168     throws ModuleException
169     {
170         return getPrincipal().getOrganisationID();
171     }
172
173     protected String JavaDoc getUserID()
174     throws ModuleException
175     {
176         return new String JavaDoc(getPrincipal().getUserID());
177     }
178     
179     protected boolean userIsOrganisation()
180     throws ModuleException
181     {
182         return getPrincipal().userIsOrganisation();
183     }
184     
185     protected String JavaDoc getPrincipalString()
186         throws ModuleException
187     {
188         return getPrincipal().toString();
189     }
190 }
191
Popular Tags