Source Code Cross Referenced for CoreContext.java in  » J2EE » openejb3 » org » apache » openejb » core » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » openejb3 » org.apache.openejb.core 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         *
009:         *     http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         */package org.apache.openejb.core;
017:
018:        import javax.ejb.EJBHome;
019:        import javax.ejb.EJBLocalHome;
020:        import javax.ejb.EJBLocalObject;
021:        import javax.ejb.TimerService;
022:        import javax.naming.Context;
023:        import javax.naming.InitialContext;
024:        import javax.naming.NamingException;
025:        import javax.transaction.Status;
026:        import javax.transaction.TransactionManager;
027:        import javax.transaction.UserTransaction;
028:
029:        import org.apache.openejb.DeploymentInfo;
030:        import org.apache.openejb.InterfaceType;
031:        import org.apache.openejb.RpcContainer;
032:        import org.apache.openejb.InternalErrorException;
033:        import org.apache.openejb.core.ivm.EjbObjectProxyHandler;
034:        import org.apache.openejb.core.ivm.IntraVmProxy;
035:        import org.apache.openejb.spi.SecurityService;
036:        import org.apache.openejb.util.proxy.ProxyManager;
037:
038:        public abstract class CoreContext implements  java.io.Serializable {
039:
040:            public final static byte SECURITY_METHOD = (byte) 1;
041:
042:            public final static byte USER_TRANSACTION_METHOD = (byte) 2;
043:
044:            public final static byte ROLLBACK_METHOD = (byte) 3;
045:
046:            public final static byte EJBOBJECT_METHOD = (byte) 4;
047:
048:            public final static byte EJBHOME_METHOD = (byte) 5;
049:
050:            private final UserTransaction userTransaction;
051:            private final SecurityService securityService;
052:            private final TransactionManager transactionManager;
053:
054:            public CoreContext(TransactionManager transactionManager,
055:                    SecurityService securityService) {
056:                this .transactionManager = transactionManager;
057:                this .securityService = securityService;
058:                this .userTransaction = new CoreUserTransaction(
059:                        transactionManager);
060:            }
061:
062:            protected CoreContext(TransactionManager transactionManager,
063:                    SecurityService securityService,
064:                    UserTransaction userTransaction) {
065:                this .transactionManager = transactionManager;
066:                this .securityService = securityService;
067:                this .userTransaction = userTransaction;
068:            }
069:
070:            private TransactionManager getTransactionManager() {
071:                return transactionManager;
072:            }
073:
074:            public abstract void checkBeanState(byte methodCategory)
075:                    throws IllegalStateException;
076:
077:            public java.security.Principal getCallerPrincipal() {
078:                checkBeanState(SECURITY_METHOD);
079:                return getSecurityService().getCallerPrincipal();
080:            }
081:
082:            private SecurityService getSecurityService() {
083:                return securityService;
084:            }
085:
086:            public boolean isCallerInRole(java.lang.String roleName) {
087:                checkBeanState(SECURITY_METHOD);
088:                return securityService.isCallerInRole(roleName);
089:            }
090:
091:            public EJBHome getEJBHome() {
092:                checkBeanState(EJBHOME_METHOD);
093:
094:                ThreadContext threadContext = ThreadContext.getThreadContext();
095:                org.apache.openejb.core.CoreDeploymentInfo di = (org.apache.openejb.core.CoreDeploymentInfo) threadContext
096:                        .getDeploymentInfo();
097:
098:                return di.getEJBHome();
099:            }
100:
101:            public javax.ejb.EJBObject getEJBObject() {
102:                checkBeanState(EJBOBJECT_METHOD);
103:
104:                ThreadContext threadContext = ThreadContext.getThreadContext();
105:                org.apache.openejb.DeploymentInfo di = threadContext
106:                        .getDeploymentInfo();
107:
108:                EjbObjectProxyHandler handler = newEjbObjectHandler(
109:                        (RpcContainer) di.getContainer(), threadContext
110:                                .getPrimaryKey(), di.getDeploymentID(),
111:                        InterfaceType.EJB_OBJECT);
112:                Object newProxy = null;
113:                try {
114:                    Class[] interfaces = new Class[] { di.getRemoteInterface(),
115:                            org.apache.openejb.core.ivm.IntraVmProxy.class };
116:                    newProxy = ProxyManager.newProxyInstance(interfaces,
117:                            handler);
118:                } catch (IllegalAccessException iae) {
119:                    throw new RuntimeException(
120:                            "Could not create IVM proxy for "
121:                                    + di.getRemoteInterface() + " interface",
122:                            iae);
123:                }
124:                return (javax.ejb.EJBObject) newProxy;
125:            }
126:
127:            public EJBLocalObject getEJBLocalObject() {
128:                ThreadContext threadContext = ThreadContext.getThreadContext();
129:                org.apache.openejb.DeploymentInfo di = threadContext
130:                        .getDeploymentInfo();
131:
132:                EjbObjectProxyHandler handler = newEjbObjectHandler(
133:                        (RpcContainer) di.getContainer(), threadContext
134:                                .getPrimaryKey(), di.getDeploymentID(),
135:                        InterfaceType.EJB_LOCAL);
136:
137:                Object newProxy = null;
138:                try {
139:                    Class[] interfaces = new Class[] { di.getLocalInterface(),
140:                            org.apache.openejb.core.ivm.IntraVmProxy.class };
141:                    newProxy = ProxyManager.newProxyInstance(interfaces,
142:                            handler);
143:                } catch (IllegalAccessException iae) {
144:                    throw new RuntimeException(
145:                            "Could not create IVM proxy for "
146:                                    + di.getLocalInterface() + " interface",
147:                            iae);
148:                }
149:                return (EJBLocalObject) newProxy;
150:            }
151:
152:            public Object getBusinessObject(Class interfce) {
153:                ThreadContext threadContext = ThreadContext.getThreadContext();
154:                DeploymentInfo di = threadContext.getDeploymentInfo();
155:
156:                InterfaceType interfaceType;
157:                if (di.getBusinessLocalInterface() != null
158:                        && di.getBusinessLocalInterface().getName().equals(
159:                                interfce.getName())) {
160:                    interfaceType = InterfaceType.BUSINESS_LOCAL;
161:                } else if (di.getBusinessRemoteInterface() != null
162:                        && di.getBusinessRemoteInterface().getName().equals(
163:                                interfce.getName())) {
164:                    interfaceType = InterfaceType.BUSINESS_REMOTE;
165:                } else {
166:                    throw new IllegalArgumentException(
167:                            "Component has no such interface "
168:                                    + interfce.getName());
169:                }
170:
171:                Object newProxy;
172:                try {
173:                    EjbObjectProxyHandler handler = newEjbObjectHandler(
174:                            (RpcContainer) di.getContainer(), threadContext
175:                                    .getPrimaryKey(), di.getDeploymentID(),
176:                            interfaceType);
177:                    Class[] interfaces = new Class[] { interfce,
178:                            IntraVmProxy.class };
179:                    newProxy = ProxyManager.newProxyInstance(interfaces,
180:                            handler);
181:                } catch (IllegalAccessException iae) {
182:                    throw new InternalErrorException(
183:                            "Could not create IVM proxy for "
184:                                    + interfce.getName() + " interface", iae);
185:                }
186:                return newProxy;
187:            }
188:
189:            public EJBLocalHome getEJBLocalHome() {
190:                ThreadContext threadContext = ThreadContext.getThreadContext();
191:                org.apache.openejb.core.CoreDeploymentInfo di = threadContext
192:                        .getDeploymentInfo();
193:
194:                return di.getEJBLocalHome();
195:            }
196:
197:            public TimerService getTimerService() {
198:                return null;
199:            }
200:
201:            public Object getPrimaryKey() {
202:                /*
203:                 * This method is only declared in the EntityContext interface and is therefor
204:                 * unavailable in the SessionContext and doesn't not require a check for bean kind (Entity vs Session).
205:                 */
206:                checkBeanState(EJBOBJECT_METHOD);
207:
208:                ThreadContext threadContext = ThreadContext.getThreadContext();
209:                return threadContext.getPrimaryKey();
210:            }
211:
212:            public boolean getRollbackOnly() {
213:
214:                ThreadContext threadContext = ThreadContext.getThreadContext();
215:                org.apache.openejb.DeploymentInfo di = threadContext
216:                        .getDeploymentInfo();
217:                if (di.isBeanManagedTransaction()) {
218:                    throw new IllegalStateException(
219:                            "bean-managed transaction beans can not access the getRollbackOnly( ) method");
220:                }
221:
222:                checkBeanState(ROLLBACK_METHOD);
223:                try {
224:                    int status = getTransactionManager().getStatus();
225:                    if (status == Status.STATUS_MARKED_ROLLBACK
226:                            || status == Status.STATUS_ROLLEDBACK) {
227:                        return true;
228:                    } else if (status == Status.STATUS_NO_TRANSACTION) {
229:                        // this would be true for Supports tx attribute where no tx was propagated
230:                        throw new IllegalStateException(
231:                                "No current transaction");
232:                    } else {
233:                        return false;
234:                    }
235:                } catch (javax.transaction.SystemException se) {
236:                    throw new RuntimeException(
237:                            "Transaction service has thrown a SystemException",
238:                            se);
239:                }
240:            }
241:
242:            public void setRollbackOnly() {
243:                ThreadContext threadContext = ThreadContext.getThreadContext();
244:                org.apache.openejb.DeploymentInfo di = threadContext
245:                        .getDeploymentInfo();
246:                if (di.isBeanManagedTransaction()) {
247:                    throw new IllegalStateException(
248:                            "bean-managed transaction beans can not access the setRollbackOnly( ) method");
249:                }
250:
251:                checkBeanState(ROLLBACK_METHOD);
252:
253:                try {
254:                    getTransactionManager().setRollbackOnly();
255:                } catch (javax.transaction.SystemException se) {
256:                    throw new RuntimeException(
257:                            "Transaction service has thrown a SystemException",
258:                            se);
259:                }
260:
261:            }
262:
263:            public javax.transaction.UserTransaction getUserTransaction() {
264:
265:                ThreadContext threadContext = ThreadContext.getThreadContext();
266:                org.apache.openejb.DeploymentInfo di = threadContext
267:                        .getDeploymentInfo();
268:                if (di.isBeanManagedTransaction()) {
269:                    checkBeanState(USER_TRANSACTION_METHOD);
270:                    return userTransaction;
271:                } else {
272:                    throw new java.lang.IllegalStateException(
273:                            "container-managed transaction beans can not access the UserTransaction");
274:                }
275:            }
276:
277:            /**
278:             * Lookup a resource within the component's private naming context.
279:             *
280:             * @param name - Name of the entry (relative to java:comp/env).
281:             * @return The looked-up object.
282:             * @see http://java.sun.com/javaee/5/docs/api/javax/ejb/EJBContext.html#lookup(java.lang.String)
283:             * @see EJB3.0 "Core Contracts and Requirements", section 4.5.2, table 2.
284:             */
285:            public Object lookup(String name) {
286:                Context initialContext = null;
287:                Object object = null;
288:
289:                try {
290:                    initialContext = new InitialContext();
291:                    object = initialContext.lookup("java:comp/env/" + name);
292:                } catch (NamingException nex) {
293:                    throw new IllegalArgumentException(nex);
294:                }
295:                return object;
296:            }
297:
298:            /*----------------------------------------------------*/
299:            /* UNSUPPORTED DEPRICATED METHODS                     */
300:            /*----------------------------------------------------*/
301:
302:            public boolean isCallerInRole(java.security.Identity role) {
303:                throw new java.lang.UnsupportedOperationException();
304:            }
305:
306:            public java.security.Identity getCallerIdentity() {
307:                throw new java.lang.UnsupportedOperationException();
308:            }
309:
310:            public java.util.Properties getEnvironment() {
311:                throw new java.lang.UnsupportedOperationException();
312:            }
313:
314:            protected abstract EjbObjectProxyHandler newEjbObjectHandler(
315:                    RpcContainer container, Object pk, Object depID,
316:                    InterfaceType interfaceType);
317:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.