Source Code Cross Referenced for PersistentFileNamespace.java in  » Web-Framework » anvil » anvil » server » file » 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 » Web Framework » anvil » anvil.server.file 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * $Id: PersistentFileNamespace.java,v 1.7 2002/09/16 08:05:06 jkl Exp $
003:         *
004:         * Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
005:         *
006:         * Use is subject to license terms, as defined in
007:         * Anvil Sofware License, Version 1.1. See LICENSE 
008:         * file, or http://njet.org/license-1.1.txt
009:         */
010:        package anvil.server.file;
011:
012:        import java.io.File;
013:        import java.io.InputStream;
014:        import java.io.FileInputStream;
015:        import java.io.FileOutputStream;
016:        import anvil.java.util.BindingEnumeration;
017:        import anvil.core.Any;
018:        import anvil.core.Array;
019:        import anvil.core.Serialization;
020:        import anvil.server.PersistentNamespace;
021:        import anvil.server.NamespacePreferences;
022:        import anvil.server.Zone;
023:        import anvil.server.ConfigurationError;
024:        import anvil.script.Context;
025:
026:        /**
027:         * class PersistentFileNamespace
028:         *
029:         * @author: Jani Lehtimäki
030:         */
031:        public class PersistentFileNamespace implements  PersistentNamespace {
032:
033:            protected Zone _zone;
034:            protected File _file;
035:            protected boolean _copyonget = false;
036:            protected boolean _copyonset = false;
037:            protected boolean _loaded = false;
038:            protected int _modcount = 0;
039:            protected int _maxmodcount = 100;
040:            protected long _lastmodified = -1;
041:            protected String _name = "";
042:            protected Array _namespace = new Array();
043:
044:            public PersistentFileNamespace() {
045:            }
046:
047:            public PersistentFileNamespace(Zone zone, String name, File file,
048:                    boolean copyonget, boolean copyonset, int maxmodcount) {
049:
050:                _name = name;
051:                _file = file;
052:                _zone = zone;
053:                _copyonget = copyonget;
054:                _copyonset = copyonset;
055:                _maxmodcount = maxmodcount;
056:            }
057:
058:            public String toString() {
059:                return "PersistentFileNamespace(" + _name + "@" + _file + ")";
060:            }
061:
062:            public String getName() {
063:                return _name;
064:            }
065:
066:            public void initialize(Zone zone, NamespacePreferences prefs) {
067:                String filename = (String) prefs.getPreference("file");
068:                if (filename == null) {
069:                    throw new ConfigurationError(
070:                            "PersistentFileNamespace requires existends of property 'file'");
071:                }
072:                File file = new File(filename);
073:                _name = prefs.getName();
074:                _file = file;
075:                _zone = zone;
076:                _copyonget = prefs.getBooleanPreference("copyonget", false);
077:                _copyonset = prefs.getBooleanPreference("copyonset", false);
078:                _maxmodcount = prefs.getIntPreference("maxmodifications", 1);
079:                if (_maxmodcount < 1) {
080:                    _maxmodcount = 1;
081:                }
082:                _zone.log().info("Namespace " + this  + " initialized");
083:            }
084:
085:            public synchronized void remove() {
086:                _namespace.clear();
087:                _file.delete();
088:            }
089:
090:            public synchronized void commit() {
091:                save(true);
092:            }
093:
094:            public void stop() {
095:                save(true);
096:                _zone.log().info("Namespace " + this  + " stopped");
097:            }
098:
099:            private void load() {
100:                if (_file.exists() && _file.canRead()) {
101:                    if (!_loaded || (_file.lastModified() > _lastmodified)) {
102:                        FileInputStream in = null;
103:                        try {
104:                            if (_file.length() > 0) {
105:                                in = new FileInputStream(_file);
106:                                Any data = Serialization.unserialize(null, in);
107:                                if (data.isArray()) {
108:                                    _namespace = data.toArray();
109:                                }
110:                            } else {
111:                                _namespace.clear();
112:                            }
113:                        } catch (Throwable t) {
114:                            _zone.log().error(t);
115:                        } finally {
116:                            if (in != null) {
117:                                try {
118:                                    in.close();
119:                                } catch (Throwable t) {
120:                                }
121:                            }
122:                        }
123:                        _lastmodified = _file.lastModified();
124:                        _loaded = true;
125:                    }
126:                }
127:            }
128:
129:            private void save(boolean forcesave) {
130:                _modcount++;
131:                if (((_modcount % _maxmodcount) == 0) || forcesave) {
132:                    FileOutputStream out = null;
133:                    try {
134:                        out = new FileOutputStream(_file);
135:                        Serialization.serialize(null, _namespace, out);
136:                    } catch (Throwable t) {
137:                        _zone.log().error(t);
138:                    } finally {
139:                        if (out != null) {
140:                            try {
141:                                out.close();
142:                            } catch (Throwable t) {
143:                            }
144:                        }
145:                    }
146:                    _lastmodified = _file.lastModified();
147:                }
148:            }
149:
150:            public synchronized BindingEnumeration getVariables() {
151:                load();
152:                return _namespace.keysAndElements();
153:            }
154:
155:            public synchronized Any getVariable(String name) {
156:                load();
157:                Any value = _namespace.get(Any.create(name));
158:                if (value == null) {
159:                    value = Any.UNDEFINED;
160:                }
161:                if (_copyonget) {
162:                    value = value.copy();
163:                }
164:                return value;
165:            }
166:
167:            public synchronized Any setVariable(String name, Any value) {
168:                load();
169:                if (_copyonset) {
170:                    value = value.copy();
171:                }
172:                _namespace.append(name, value);
173:                save(false);
174:                return value;
175:            }
176:
177:            public synchronized Any checkVariable(String name) {
178:                load();
179:                Any value = _namespace.get(Any.create(name));
180:                if (value != null) {
181:                    return value;
182:                } else {
183:                    return Any.UNDEFINED;
184:                }
185:            }
186:
187:            public synchronized boolean deleteVariable(String name) {
188:                load();
189:                boolean deleted = (_namespace.remove(Any.create(name)) != null);
190:                if (deleted) {
191:                    save(false);
192:                }
193:                return deleted;
194:            }
195:
196:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.