Source Code Cross Referenced for ServiceLocator.java in  » Authentication-Authorization » ejbca » org » ejbca » core » ejb » 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 » Authentication Authorization » ejbca » org.ejbca.core.ejb 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.ejbca.core.ejb;
002:
003:        import java.net.URL;
004:        import java.util.Map;
005:        import java.util.Collections;
006:        import java.util.HashMap;
007:        import javax.naming.InitialContext;
008:        import javax.naming.NameNotFoundException;
009:        import javax.naming.NamingException;
010:        import javax.naming.Context;
011:        import javax.rmi.PortableRemoteObject;
012:        import javax.sql.DataSource;
013:        import javax.ejb.EJBHome;
014:        import javax.ejb.EJBLocalHome;
015:        import javax.mail.Session;
016:
017:        /**
018:         * A simple implementation of the ServiceLocator/HomeFactory J2EE Pattern.
019:         * {@link http://developer.java.sun.com/developer/restricted/patterns/ServiceLocator.html}
020:         *
021:         * It is used to look up JNDI related resources such as EJB homes, datasources, ...
022:         * @version $Id: ServiceLocator.java,v 1.2 2006/10/02 07:54:37 anatom Exp $
023:         */
024:        public class ServiceLocator {
025:
026:            /** ejb home cache */
027:            private transient Map ejbHomes = Collections
028:                    .synchronizedMap(new HashMap());
029:
030:            /** the jndi context */
031:            private transient Context ctx;
032:
033:            /** the singleton instance */
034:            private static transient ServiceLocator instance;
035:
036:            /**
037:             * Create a new service locator object.
038:             * @throws ServiceLocatorException if the context failed to be initialized
039:             */
040:            private ServiceLocator() throws ServiceLocatorException {
041:                try {
042:                    this .ctx = new InitialContext();
043:                } catch (NamingException e) {
044:                    throw new ServiceLocatorException(e);
045:                }
046:            }
047:
048:            /**
049:             * return the singleton instance
050:             * @return the singleton instance
051:             * @throws ServiceLocatorException if the instance could not be initialized the first time
052:             */
053:            public static final ServiceLocator getInstance()
054:                    throws ServiceLocatorException {
055:                // synchronization is intentionally left out. It 'should' not have dramatic
056:                // consequences as it is not that destructive.
057:                if (instance == null) {
058:                    instance = new ServiceLocator();
059:                }
060:                return instance;
061:            }
062:
063:            /**
064:             * return the ejb local home.
065:             * clients need to cast to the type of EJBHome they desire
066:             * @param jndiHomeName the jndi home name matching the requested local home.
067:             * @return the Local EJB Home corresponding to the home name
068:             */
069:            public EJBLocalHome getLocalHome(String jndiHomeName)
070:                    throws ServiceLocatorException {
071:                EJBLocalHome home = (EJBLocalHome) ejbHomes.get(jndiHomeName);
072:                if (home == null) {
073:                    home = (EJBLocalHome) getObject(jndiHomeName);
074:                    ejbHomes.put(jndiHomeName, home);
075:                }
076:                return home;
077:            }
078:
079:            /**
080:             * return the ejb remote home.
081:             * clients need to cast to the type of EJBHome they desire
082:             * @param jndiHomeName the jndi home name matching the requested remote home.
083:             * @return the Local EJB Home corresponding to the home name
084:             */
085:            public EJBHome getRemoteHome(String jndiHomeName, Class className)
086:                    throws ServiceLocatorException {
087:                EJBHome home = (EJBHome) ejbHomes.get(className);
088:                if (home == null) {
089:                    Object objref = getObject(jndiHomeName);
090:                    home = (EJBHome) PortableRemoteObject.narrow(objref,
091:                            className);
092:                    ejbHomes.put(className, home);
093:                }
094:                return home;
095:            }
096:
097:            /**
098:             * return the datasource object corresponding the the env entry name
099:             * @return the DataSource corresponding to the env entry name parameter
100:             * @throws ServiceLocatorException if the lookup fails
101:             */
102:            public DataSource getDataSource(String dataSourceName)
103:                    throws ServiceLocatorException {
104:                return (DataSource) getObject(dataSourceName);
105:            }
106:
107:            /**
108:             * return the URL object corresponding to the env entry name
109:             * @param envName the env entry name
110:             * @return the URL value corresponding to the env entry name.
111:             * @throws ServiceLocatorException if the lookup fails
112:             */
113:            public URL getUrl(String envName) throws ServiceLocatorException {
114:                return (URL) getObject(envName);
115:            }
116:
117:            /**
118:             * return a boolean value corresponding to the env entry
119:             * @param envName the env entry name
120:             * @return the boolean value corresponding to the env entry.
121:             * @throws ServiceLocatorException if the lookup fails
122:             */
123:            public boolean getBoolean(String envName)
124:                    throws ServiceLocatorException {
125:                return ((Boolean) getObject(envName)).booleanValue();
126:            }
127:
128:            /**
129:             * return a string value corresponding to the env entry
130:             * @param envName the env entry name
131:             * @return the boolean value corresponding to the env entry.
132:             * @throws ServiceLocatorException if the lookup fails
133:             */
134:            public String getString(String envName)
135:                    throws ServiceLocatorException {
136:                String ret = null;
137:                try {
138:                    ret = (String) getObject(envName);
139:                } catch (ServiceLocatorException e) {
140:                    if (e.getCause() instanceof  NameNotFoundException) {
141:                        // ignore this and return null, otherwise we can not have empty values in Glassfish
142:                        ret = null;
143:                    }
144:                }
145:                return ret;
146:            }
147:
148:            /**
149:             * return a mail session corresponding to the env entry
150:             * @param envName the env entry name
151:             * @return the mail session corresponding to the env entry.
152:             * @throws ServiceLocatorException if the lookup fails
153:             */
154:            public Session getMailSession(String envName)
155:                    throws ServiceLocatorException {
156:                return (Session) getObject(envName);
157:            }
158:
159:            /**
160:             * return a known java object corresponding to the env entry
161:             * @param envName the env entry name
162:             * @return the java object corresponding to the env entry
163:             * @throws ServiceLocatorException if the lookup fails
164:             */
165:            public Object getObject(String envName)
166:                    throws ServiceLocatorException {
167:                try {
168:                    return ctx.lookup(envName);
169:                } catch (NamingException e) {
170:                    throw new ServiceLocatorException(e);
171:                }
172:            }
173:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.