Source Code Cross Referenced for PersistenceFactory.java in  » Net » JGroups-2.4.1-sp3 » org » jgroups » persistence » 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 » Net » JGroups 2.4.1 sp3 » org.jgroups.persistence 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.jgroups.persistence;
002:
003:        /**
004:         * @author Mandar Shinde
005:         * This class is the factory to get access to any DB based or file based
006:         * implementation. None of the implementations should expose directly
007:         * to user for migration purposes
008:         */
009:
010:        import org.apache.commons.logging.Log;
011:        import org.apache.commons.logging.LogFactory;
012:        import org.jgroups.util.Util;
013:
014:        import java.io.FileInputStream;
015:        import java.io.IOException;
016:        import java.lang.reflect.Constructor;
017:        import java.util.Properties;
018:
019:        public class PersistenceFactory {
020:
021:            protected final static Log log = LogFactory
022:                    .getLog(PersistenceFactory.class);
023:
024:            /**
025:             * Default private constructor// does nothing
026:             */
027:            private PersistenceFactory() {
028:            }
029:
030:            /**
031:             * Singular public method to get access to any of the Persistence
032:             * Manager implementations. It is important to known at this point
033:             * that properties determine the implementation of the Persistence
034:             * Manager, there is no direct interface which gives access to 
035:             * either DB implemented ot FILE implemented storage API.
036:             * @return PersistenceFactory;
037:             */
038:            public static PersistenceFactory getInstance() {
039:                log.error(" getting factory instance ");
040:                if (_factory == null)
041:                    _factory = new PersistenceFactory();
042:                return _factory;
043:            }
044:
045:            /**
046:             * Register a custom persistence manager as opposed to the
047:             * {@link FilePersistenceManager} or {@link DBPersistenceManager}.
048:             */
049:            public synchronized void registerManager(PersistenceManager manager) {
050:                _manager = manager;
051:            }
052:
053:            /**
054:             * Reads the default properties and creates a persistencemanager
055:             * The default properties are picked up from the $USER_HOME or 
056:             * from the classpath. Default properties are represented by
057:             * "persist.properties"
058:             * @return PersistenceManager
059:             * @exception Exception;
060:             */
061:            public synchronized PersistenceManager createManager()
062:                    throws Exception {
063:                // will return null if not initialized
064:                // uses default properties
065:                if (_manager == null) {
066:                    if (checkDB())
067:                        _manager = createManagerDB(propPath);
068:                    else
069:                        _manager = createManagerFile(propPath);
070:                }
071:                return _manager;
072:            }
073:
074:            /**
075:             * Duplicated signature to create PersistenceManager to allow user to
076:             * provide property path. 
077:             * @param filePath complete pathname to get the properties
078:             * @return PersistenceManager;
079:             * @exception Exception;
080:             */
081:            public synchronized PersistenceManager createManager(String filePath)
082:                    throws Exception {
083:                if (_manager == null) {
084:                    if (checkDB(filePath))
085:                        _manager = createManagerDB(filePath);
086:                    else
087:                        _manager = createManagerFile(filePath);
088:                }
089:                return _manager;
090:            }
091:
092:            /**
093:             * Internal creator of DB persistence manager, the outside user accesses only
094:             * the PersistenceManager interface API
095:             */
096:            private PersistenceManager createManagerDB(String filePath)
097:                    throws Exception {
098:
099:                if (log.isInfoEnabled())
100:                    log.info("Calling db persist from factory: " + filePath);
101:                if (_manager == null)
102:                    _manager = new DBPersistenceManager(filePath);
103:                return _manager;
104:            }// end of DB persistence
105:
106:            /**
107:             * creates instance of file based persistency manager
108:             * @return PersistenceManager
109:             */
110:            private PersistenceManager createManagerFile(String filePath) {
111:
112:                if (log.isInfoEnabled())
113:                    log.info("Creating file manager: " + filePath);
114:                Properties props;
115:
116:                try {
117:                    if (_manager == null) {
118:                        props = readProps(filePath);
119:                        String classname = props.getProperty(filePersistMgr);
120:                        if (classname != null) {
121:                            Class cl = Util.loadClass(classname, this 
122:                                    .getClass());
123:                            Constructor ctor = cl
124:                                    .getConstructor(new Class[] { String.class });
125:                            _manager = (PersistenceManager) ctor
126:                                    .newInstance(new Object[] { filePath });
127:                        } else {
128:                            _manager = new FilePersistenceManager(filePath);
129:                        }
130:                    }
131:                    return _manager;
132:                } catch (Throwable t) {
133:                    t.printStackTrace();
134:                    return null;
135:                }
136:            }// end of file persistence
137:
138:            /**
139:             * checks the default properties for DB/File flag
140:             * @return boolean;
141:             * @exception Exception;
142:             */
143:            private boolean checkDB() throws Exception {
144:                Properties props = readProps(propPath);
145:                String persist = props.getProperty(persistProp);
146:                if ("DB".equals(persist))
147:                    return true;
148:                return false;
149:            }
150:
151:            /**
152:             * checks the provided properties for DB/File flag
153:             * @return boolean;
154:             */
155:            private boolean checkDB(String filePath) throws Exception {
156:                Properties props = readProps(filePath);
157:                String persist = props.getProperty(persistProp);
158:                if ("DB".equals(persist))
159:                    return true;
160:                return false;
161:            }
162:
163:            Properties readProps(String fileName) throws IOException {
164:                Properties props;
165:                FileInputStream _stream = new FileInputStream(fileName);
166:                props = new Properties();
167:                props.load(_stream);
168:                return props;
169:            }
170:
171:            private static volatile PersistenceManager _manager = null;
172:            private static volatile PersistenceFactory _factory = null;
173:
174:            /* Please set this according to configuration */
175:            final static String propPath = "persist.properties";
176:            final static String persistProp = "persist";
177:
178:            /** The class that implements a file-based PersistenceManager */
179:            final static String filePersistMgr = "filePersistMgr";
180:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.