001: /*
002: * JFox - The most lightweight Java EE Application Server!
003: * more details please visit http://www.huihoo.org/jfox or http://www.jfox.org.cn.
004: *
005: * JFox is licenced and re-distributable under GNU LGPL.
006: */
007: package org.jfox.ejb3;
008:
009: import java.rmi.RemoteException;
010: import java.security.Identity;
011: import java.security.Principal;
012: import java.util.HashMap;
013: import java.util.List;
014: import java.util.Map;
015: import java.util.Properties;
016: import javax.ejb.EJBException;
017: import javax.ejb.EJBHome;
018: import javax.ejb.EJBLocalHome;
019: import javax.ejb.EJBLocalObject;
020: import javax.ejb.EJBObject;
021: import javax.ejb.Handle;
022: import javax.ejb.MessageDrivenContext;
023: import javax.ejb.RemoveException;
024: import javax.ejb.SessionContext;
025: import javax.ejb.TimerService;
026: import javax.naming.Context;
027: import javax.naming.NameAlreadyBoundException;
028: import javax.naming.NameNotFoundException;
029: import javax.naming.NamingException;
030: import javax.transaction.SystemException;
031: import javax.transaction.UserTransaction;
032: import javax.xml.rpc.handler.MessageContext;
033:
034: import org.jfox.ejb3.naming.ContextAdapter;
035:
036: /**
037: * @author <a href="mailto:jfox.young@gmail.com">Young Yang</a>
038: */
039: @SuppressWarnings({"deprecation"})
040: public abstract class ExtendEJBContext implements SessionContext,
041: MessageDrivenContext, EJBObject, EJBLocalObject {
042:
043: private EJBObjectId ejbObjectId;
044: private Object ejbInstance;
045:
046: /**
047: * Component env Map, ä¿?å˜ java:comp/env 对象,å?ªä¿?å˜ Class level 的注入
048: * Field Level ä¸?å?š env ä¿?å˜
049: */
050: private Map<String, Object> envMap = new HashMap<String, Object>();
051:
052: private ENContext envContext = new ENContext();
053:
054: public ExtendEJBContext(EJBObjectId ejbObjectId, Object ejbInstance) {
055: this .ejbObjectId = ejbObjectId;
056: this .ejbInstance = ejbInstance;
057: }
058:
059: public Context getENContext() {
060: return envContext;
061: }
062:
063: protected EJBObjectId getEJBObjectId() {
064: return ejbObjectId;
065: }
066:
067: public Object getEJBInstance() {
068: return ejbInstance;
069: }
070:
071: public Principal getCallerPrincipal() {
072: EJBInvocation invocation = EJBInvocation.current();
073: if (invocation != null) {
074: return invocation.getCallerGroup();
075: } else {
076: return null;
077: }
078:
079: }
080:
081: public EJBHome getEJBHome() {
082: return null;
083: }
084:
085: public EJBLocalHome getEJBLocalHome() {
086: return null;
087: }
088:
089: public UserTransaction getUserTransaction()
090: throws IllegalStateException {
091: EJBInvocation invocation = EJBInvocation.current();
092: if (invocation == null) {
093: throw new IllegalStateException(
094: "EJBContext.getUserTransactionCurrent() exception, thread not in a EJB invocation!");
095: } else {
096: try {
097: return (UserTransaction) invocation
098: .getTransactionManager().getTransaction();
099: } catch (SystemException e) {
100: throw new IllegalStateException(e);
101: }
102: }
103: }
104:
105: public boolean isCallerInRole(final String roleName) {
106: EJBInvocation invocation = EJBInvocation.current();
107: if (invocation == null) {
108: return false;
109: }
110: List<? extends Principal> principals = invocation
111: .getCallerRolesList();
112: for (Principal p : principals) {
113: if (p.getName().equals(roleName)) {
114: return true;
115: }
116: }
117: return false;
118: }
119:
120: @Deprecated
121: public Identity getCallerIdentity() {
122: throw new UnsupportedOperationException("getCallerIdentity");
123: }
124:
125: @Deprecated
126: public Properties getEnvironment() {
127: throw new UnsupportedOperationException("getEnvironment");
128: }
129:
130: @Deprecated
131: public boolean isCallerInRole(final Identity role) {
132: throw new UnsupportedOperationException("isCallerInRole");
133: }
134:
135: // SessionContext
136: public <T> T getBusinessObject(Class<T> businessInterface)
137: throws IllegalStateException {
138: throw new IllegalStateException(
139: "Can not invoke getBusinessObject!");
140: }
141:
142: public EJBLocalObject getEJBLocalObject()
143: throws IllegalStateException {
144: return this ;
145: }
146:
147: public EJBObject getEJBObject() throws IllegalStateException {
148: return this ;
149: }
150:
151: public Class getInvokedBusinessInterface()
152: throws IllegalStateException {
153: return EJBInvocation.current().getInterfaceMethod()
154: .getDeclaringClass();
155: }
156:
157: public MessageContext getMessageContext()
158: throws IllegalStateException {
159: return null;
160: }
161:
162: public TimerService getTimerService() throws IllegalStateException {
163: throw new IllegalStateException("Can not call getTimerService!");
164: }
165:
166: // EJBObject & EJBLocalObject
167:
168: public Handle getHandle() throws RemoteException {
169: return new EJBHandleImpl(getEJBObjectId());
170: }
171:
172: public Object getPrimaryKey() {
173: return getEJBObjectId();
174: }
175:
176: public boolean isIdentical(EJBObject obj) throws RemoteException {
177: return obj.getPrimaryKey().equals(getPrimaryKey());
178: }
179:
180: public void remove() throws RemoveException {
181: throw new RemoveException("Can not invoke remove()!");
182: }
183:
184: public boolean isIdentical(EJBLocalObject obj) throws EJBException {
185: return obj.getPrimaryKey().equals(getPrimaryKey());
186: }
187:
188: public boolean equals(Object obj) {
189: if (obj == null || !(obj instanceof EJBObjectId)) {
190: return false;
191: } else {
192: try {
193: return isIdentical((EJBObject) obj);
194: } catch (Exception e) {
195: return false;
196: }
197: }
198: }
199:
200: public int hashCode() {
201: return getEJBObjectId().hashCode();
202: }
203:
204: protected Object clone() throws CloneNotSupportedException {
205: throw new CloneNotSupportedException(getEJBObjectId()
206: .toString());
207: }
208:
209: /**
210: * 获å?– Http Session ä¸çš„æ•°æ?®ï¼Œéœ€è¦?ä¿?è¯? Web Server å’Œ EJB Container 在å?Œä¸€è™šæ‹Ÿæœºä¸
211: * @param attribute session key
212: */
213: public Object getSessionAttribute(String attribute) {
214: return EJBInvocation.current().getSessionAttribute(attribute);
215: }
216:
217: public class ENContext extends ContextAdapter {
218:
219: //--- java:comp/env naming container
220: public void bind(String name, Object obj)
221: throws NamingException {
222: if (envMap.containsKey(name)) {
223: throw new NameAlreadyBoundException(name);
224: }
225: envMap.put(name, obj);
226: }
227:
228: public void rebind(String name, Object obj)
229: throws NamingException {
230: envMap.put(name, obj);
231: }
232:
233: public void unbind(String name) throws NamingException {
234: if (!envMap.containsKey(name)) {
235: throw new NameNotFoundException(name);
236: }
237: envMap.remove(name);
238: }
239:
240: public Object lookup(String name) throws NamingException {
241: if (!envMap.containsKey(name)) {
242: throw new NameNotFoundException(name);
243: }
244: return envMap.get(name);
245: }
246: }
247: }
|