Source Code Cross Referenced for JavaContextHelper.java in  » J2EE » ow2-easybeans » org » ow2 » easybeans » deployment » helper » 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 » ow2 easybeans » org.ow2.easybeans.deployment.helper 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * EasyBeans
003:         * Copyright (C) 2006 Bull S.A.S.
004:         * Contact: easybeans@ow2.org
005:         *
006:         * This library is free software; you can redistribute it and/or
007:         * modify it under the terms of the GNU Lesser General Public
008:         * License as published by the Free Software Foundation; either
009:         * version 2.1 of the License, or any later version.
010:         *
011:         * This library is distributed in the hope that it will be useful,
012:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
013:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014:         * Lesser General Public License for more details.
015:         *
016:         * You should have received a copy of the GNU Lesser General Public
017:         * License along with this library; if not, write to the Free Software
018:         * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
019:         * USA
020:         *
021:         * --------------------------------------------------------------------------
022:         * $Id: JavaContextHelper.java 1970 2007-10-16 11:49:25Z benoitf $
023:         * --------------------------------------------------------------------------
024:         */package org.ow2.easybeans.deployment.helper;
025:
026:        import static javax.ejb.TransactionManagementType.CONTAINER;
027:
028:        import java.util.List;
029:
030:        import javax.naming.Context;
031:        import javax.naming.NamingException;
032:
033:        import org.ow2.easybeans.api.Factory;
034:        import org.ow2.easybeans.container.EasyBeansEJBContext;
035:        import org.ow2.easybeans.deployment.annotations.metadata.ClassAnnotationMetadata;
036:        import org.ow2.easybeans.deployment.xml.struct.EJB3;
037:        import org.ow2.easybeans.deployment.xml.struct.EnterpriseBeans;
038:        import org.ow2.easybeans.deployment.xml.struct.Session;
039:        import org.ow2.easybeans.deployment.xml.struct.common.EnvEntry;
040:        import org.ow2.easybeans.naming.NamingManager;
041:        import org.ow2.util.log.Log;
042:        import org.ow2.util.log.LogFactory;
043:
044:        /**
045:         * Builds a Context (java: context).
046:         * @author Florent Benoit
047:         */
048:        public final class JavaContextHelper {
049:
050:            /**
051:             * Logger.
052:             */
053:            private static Log logger = LogFactory
054:                    .getLog(JavaContextHelper.class);
055:
056:            /**
057:             * Utility class.
058:             */
059:            private JavaContextHelper() {
060:
061:            }
062:
063:            /**
064:             * Build a new context for the given bean.
065:             * @param bean the bean for which there is a need to build a context.
066:             * @param easyBeansFactory the factory to build EJBContext
067:             * @return a java: context.
068:             * @throws JavaContextHelperException if environment cannot be built
069:             */
070:            public static Context build(final ClassAnnotationMetadata bean,
071:                    final Factory easyBeansFactory)
072:                    throws JavaContextHelperException {
073:                Context javaCtx = null;
074:                try {
075:                    javaCtx = NamingManager.getInstance()
076:                            .createEnvironmentContext(bean.getClassName());
077:                } catch (NamingException e) {
078:                    throw new IllegalStateException(
079:                            "Cannot build a new environment", e);
080:                }
081:
082:                String beanClassName = bean.getClassName().replace("/", ".");
083:
084:                // Gets java:comp
085:                Context compCtx;
086:                try {
087:                    compCtx = (Context) javaCtx.lookup("comp");
088:                } catch (NamingException e) {
089:                    throw new JavaContextHelperException(
090:                            "Cannot create subcontext", e);
091:                }
092:
093:                // Unbind UserTransaction context if it is not a BMT bean
094:                // as specified in chapter 16.12 of EJB 3 spec.
095:                if (easyBeansFactory.getBeanInfo()
096:                        .getTransactionManagementType() == CONTAINER) {
097:                    logger
098:                            .debug("Bean is container managed so remove availability of java:comp/UserTransaction object");
099:                    try {
100:                        compCtx.unbind("UserTransaction");
101:                    } catch (NamingException e) {
102:                        throw new IllegalStateException(
103:                                "Cannot remove java:comp/UserTransaction object",
104:                                e);
105:                    }
106:                }
107:
108:                // bind EJBContext
109:                EasyBeansEJBContext<?> context = new EasyBeansEJBContext(
110:                        easyBeansFactory);
111:                try {
112:                    compCtx.bind("EJBContext", context);
113:                } catch (NamingException e) {
114:                    throw new JavaContextHelperException(
115:                            "Cannot bind EJBContext", e);
116:                }
117:
118:                // bind TimerService
119:                try {
120:                    compCtx.bind("TimerService", context.getTimerService());
121:                } catch (NamingException e) {
122:                    throw new JavaContextHelperException(
123:                            "Cannot bind EJBContext", e);
124:                }
125:
126:                Context envCtx;
127:                try {
128:                    envCtx = compCtx.createSubcontext("env");
129:                } catch (NamingException e) {
130:                    throw new JavaContextHelperException(
131:                            "Cannot create subcontext", e);
132:                }
133:
134:                // get DD of the bean.
135:                EJB3 ejb3 = bean.getEjbJarAnnotationMetadata().getEjb3();
136:
137:                String beanName = bean.getJCommonBean().getName();
138:                if (ejb3 != null) {
139:                    // Get env-entry of the given bean.
140:                    EnterpriseBeans enterpriseBeans = ejb3.getEnterpriseBeans();
141:                    if (enterpriseBeans != null) {
142:                        List<Session> sessionList = enterpriseBeans
143:                                .getSessionList();
144:                        for (Session session : sessionList) {
145:                            // Not the bean, continue
146:                            if (!beanClassName.equals(session.getEjbClass())
147:                                    && (beanName != null && !beanName
148:                                            .equals(session.getEjbName()))) {
149:                                continue;
150:                            }
151:
152:                            // bean is found, get env-entry
153:                            List<EnvEntry> envEntryList = session
154:                                    .getEnvEntryList();
155:                            for (EnvEntry envEntry : envEntryList) {
156:                                String name = envEntry.getEnvEntryName();
157:                                Object value = getEnvEntryValue(envEntry);
158:                                // if null, no entry in java:comp/env as specified chapter 15.4.1
159:                                if (value != null) {
160:                                    try {
161:                                        envCtx.rebind(name, value);
162:                                    } catch (NamingException e) {
163:                                        throw new JavaContextHelperException(
164:                                                "Cannot bind element '" + name
165:                                                        + "'.", e);
166:                                    }
167:                                }
168:                            }
169:
170:                        }
171:
172:                    }
173:
174:                }
175:
176:                return javaCtx;
177:            }
178:
179:            /**
180:             * Gets the value for a given env-entry.
181:             * @param envEntry the element representing env-entry.
182:             * @return the value associated to the given element.
183:             */
184:            private static Object getEnvEntryValue(final EnvEntry envEntry) {
185:                final String type = envEntry.getEnvEntryType();
186:                final String value = envEntry.getEnvEntryValue();
187:
188:                Object returnedValue = null;
189:
190:                if (type.equals(Boolean.class.getName())) {
191:                    if ("true".equalsIgnoreCase(value)) {
192:                        returnedValue = Boolean.TRUE;
193:                    } else if ("false".equalsIgnoreCase(value)) {
194:                        returnedValue = Boolean.FALSE;
195:                    }
196:                } else if (type.equals(String.class.getName())) {
197:                    returnedValue = value;
198:                } else if (type.equals(Integer.class.getName())) {
199:                    if (value != null) {
200:                        returnedValue = new Integer(value);
201:                    }
202:                } else if (type.equals(Character.class.getName())) {
203:                    if (value != null) {
204:                        if (value.length() != 1) {
205:                            throw new IllegalStateException(
206:                                    "The value '"
207:                                            + value
208:                                            + "' is not a valid value for env-entry of type java.lang.Character.");
209:                        }
210:                        returnedValue = new Character(value.charAt(0));
211:                    }
212:                } else if (type.equals(Double.class.getName())) {
213:                    if (value != null) {
214:                        returnedValue = new Double(value);
215:                    }
216:                } else if (type.equals(Byte.class.getName())) {
217:                    if (value != null) {
218:                        returnedValue = new Byte(value);
219:                    }
220:                } else if (type.equals(Short.class.getName())) {
221:                    if (value != null) {
222:                        returnedValue = new Short(value);
223:                    }
224:                } else if (type.equals(Long.class.getName())) {
225:                    if (value != null) {
226:                        returnedValue = new Long(value);
227:                    }
228:                } else if (type.equals(Float.class.getName())) {
229:                    if (value != null) {
230:                        returnedValue = new Float(value);
231:                    }
232:                } else {
233:                    throw new IllegalStateException(type
234:                            + " is not a valid type for env-entry.");
235:                }
236:                return returnedValue;
237:            }
238:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.