Source Code Cross Referenced for FilePersistenceManager.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:         * The class implements the PersistenceManager interface and provides users
006:         * a file based implementation when required.
007:         * The state of this class is current NOOP. Implementation will be in place 
008:         * once a better structure for file based properties will be designed.
009:         */
010:
011:        import java.io.*;
012:        import java.util.*;
013:
014:        public class FilePersistenceManager implements  PersistenceManager {
015:            private final File file;
016:
017:            /**
018:             * Default constructor
019:             */
020:            public FilePersistenceManager(String propertiesFilename)
021:                    throws Exception {
022:                Properties properties = new Properties();
023:                properties.load(new FileInputStream(propertiesFilename));
024:                String path = properties
025:                        .getProperty(PersistenceFactory.persistProp);
026:                file = new File(path);
027:                file.createNewFile();
028:            }
029:
030:            /**
031:             * Save new NV pair as serializable objects or if already exist; store 
032:             * new state 
033:             */
034:            public void save(Serializable key, Serializable val)
035:                    throws CannotPersistException {
036:                try {
037:                    Map map = retrieveAll();
038:                    map.put(key, val);
039:                    saveAll(map);
040:                } catch (CannotRetrieveException e) {
041:                    throw new CannotPersistException(e,
042:                            "Unable to pre-load existing store.");
043:                }
044:            }
045:
046:            /**
047:             * Remove existing NV from being persisted
048:             */
049:            public Serializable remove(Serializable key)
050:                    throws CannotRemoveException {
051:                Object o;
052:                try {
053:                    Map map = retrieveAll();
054:                    o = map.remove(key);
055:                    saveAll(map);
056:                } catch (CannotRetrieveException e) {
057:                    throw new CannotRemoveException(e,
058:                            "Unable to pre-load existing store.");
059:                } catch (CannotPersistException e) {
060:                    throw new CannotRemoveException(e,
061:                            "Unable to pre-load existing store.");
062:                }
063:                return (Serializable) o;
064:            }
065:
066:            /**
067:             * Use to store a complete map into persistent state
068:             * @exception CannotPersistException;
069:             */
070:            public void saveAll(Map map) throws CannotPersistException {
071:                try {
072:                    OutputStream fos = new FileOutputStream(file);
073:                    Properties prop = new Properties();
074:                    // NB: For some reason Properties.putAll(map) doesn't seem to work - dimc@users.sourceforge.net
075:                    for (Iterator iterator = map.entrySet().iterator(); iterator
076:                            .hasNext();) {
077:                        Map.Entry entry = (Map.Entry) iterator.next();
078:                        prop.setProperty(entry.getKey().toString(), entry
079:                                .getValue().toString());
080:                    }
081:                    prop.store(fos, null);
082:                    fos.flush();
083:                    fos.close();
084:                } catch (IOException e) {
085:                    throw new CannotPersistException(e, "Cannot save to: "
086:                            + file.getAbsolutePath());
087:                }
088:            }
089:
090:            /**
091:             * Gives back the Map in last known state
092:             * @return Map;
093:             * @exception CannotRetrieveException;
094:             */
095:            public Map retrieveAll() throws CannotRetrieveException {
096:                try {
097:                    Properties prop = new Properties();
098:                    FileInputStream fis = new FileInputStream(file);
099:                    prop.load(fis);
100:                    fis.close();
101:                    return filterLoadedValues(prop);
102:                } catch (IOException e) {
103:                    throw new CannotRetrieveException(e,
104:                            "Unable to load from file: "
105:                                    + file.getAbsolutePath());
106:                }
107:            }
108:
109:            /**
110:             * Turns the values into Floats to enable
111:             * {@link org.jgroups.demos.DistributedHashtableDemo} to work. 
112:             * Subclasses should override this method to convert the incoming map
113:             * of string/string key/value pairs into the types they want.  
114:             * @param in
115:             * @return Map
116:             */
117:            protected Map filterLoadedValues(Map in) {
118:                Map out = new HashMap();
119:                for (Iterator iterator = in.entrySet().iterator(); iterator
120:                        .hasNext();) {
121:                    Map.Entry entry = (Map.Entry) iterator.next();
122:                    out.put(entry.getKey().toString(), Float.valueOf(entry
123:                            .getValue().toString()));
124:                }
125:                return out;
126:            }
127:
128:            /**
129:             * Clears the complete NV state from the DB
130:             * @exception CannotRemoveException;
131:             x*/
132:            public void clear() throws CannotRemoveException {
133:                try {
134:                    saveAll(Collections.EMPTY_MAP);
135:                } catch (CannotPersistException e) {
136:                    throw new CannotRemoveException(e, "Unable to clear map.");
137:                }
138:            }
139:
140:            /**
141:             * Used to handle shutdown call the PersistenceManager implementation. 
142:             * Persistent engines can leave this implementation empty.
143:             */
144:            public void shutDown() {
145:                return;
146:            }
147:        }// end of class
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.