Source Code Cross Referenced for SimpleLogger.java in  » Database-JDBC-Connection-Pool » Space4J » org » space4j » implementation » 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 » Database JDBC Connection Pool » Space4J » org.space4j.implementation 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        // Space4J(TM) - Object Persistence in RAM
002:        // Copyright (C) 2003 Sergio Oliveira Junior
003:        // This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
004:
005:        package org.space4j.implementation;
006:
007:        import org.space4j.*;
008:
009:        import java.util.*;
010:        import java.io.*;
011:
012:        public class SimpleLogger implements  Logger {
013:
014:            // each log file will have up to 10k of size
015:            private static long MAX_LOG_SIZE = 1024 * 10; // 10k 
016:
017:            // 18 should be enough for a very long number
018:            // Ex: 000000000000000001.log
019:            private static int NUMBER_OF_DIGITS = 18;
020:
021:            // the main dir of all client dirs
022:            private static String DIR = "space4j_db";
023:
024:            private static String LOG_PREFIX = ".log";
025:            private static String SNAPSHOT_PREFIX = ".snap";
026:
027:            private long size = 0;
028:            private SizedFileOutputStream sfos = null;
029:            private ObjectOutputStream out = null;
030:            private long lognumber = 0;
031:            private String dirname;
032:
033:            /**
034:             * Initializes this SimpleLogger.<br>
035:             * SimpleLogger uses a special directory named space4j_db to store all other directories.
036:             * Each application (client) has its own subdirectory below this one.
037:             * You pass the name of this subdirectory through the dirname parameter.
038:             * @param dirname The dir where to store the files.
039:             */
040:            public SimpleLogger(String dirname) {
041:                this .dirname = dirname;
042:                lognumber = getLastLog() + 1;
043:
044:                // check and make DIR..        
045:                try {
046:                    File dir = new File(DIR);
047:                    if (!dir.exists())
048:                        dir.mkdir();
049:                } catch (Exception e) {
050:                    e.printStackTrace();
051:                }
052:            }
053:
054:            public long getLogNumber() {
055:                return lognumber;
056:            }
057:
058:            private void openLog() throws IOException {
059:                File dir = new File(DIR + "/" + dirname);
060:                if (!dir.exists())
061:                    dir.mkdir();
062:                lognumber = getLastLog() + 1;
063:                File log = new File(DIR + "/" + dirname,
064:                        getNumberString(lognumber) + LOG_PREFIX);
065:                sfos = new SizedFileOutputStream(log);
066:                out = new ObjectOutputStream(sfos);
067:            }
068:
069:            private void closeLog() throws IOException {
070:                if (out != null)
071:                    out.close();
072:                out = null;
073:            }
074:
075:            public synchronized void logCommand(Command cmd)
076:                    throws LoggerException {
077:                try {
078:
079:                    if (out == null || sfos.getSize() >= MAX_LOG_SIZE) {
080:                        closeLog();
081:                        openLog();
082:                    }
083:
084:                    // save the log number where this command is stored
085:                    cmd.setLogNumber(getLogNumber());
086:
087:                    out.writeObject(cmd);
088:                    out.reset();
089:                    out.flush();
090:
091:                } catch (IOException e) {
092:                    e.printStackTrace();
093:                    throw new LoggerException(e.getMessage());
094:                }
095:            }
096:
097:            public void takeSnapshot(Space space) throws LoggerException {
098:                takeSnapshot(space, lognumber);
099:            }
100:
101:            public synchronized void takeSnapshot(Space space, long snapnumber)
102:                    throws LoggerException {
103:                try {
104:
105:                    closeLog();
106:                    File file = new File(DIR + "/" + dirname,
107:                            getNumberString(snapnumber) + SNAPSHOT_PREFIX);
108:                    FileOutputStream fos = new FileOutputStream(file);
109:                    ObjectOutputStream oos = new ObjectOutputStream(fos);
110:                    oos.writeObject(space);
111:                    oos.flush();
112:                    oos.close();
113:
114:                } catch (IOException e) {
115:                    e.printStackTrace();
116:                    throw new LoggerException(e.getMessage());
117:                }
118:            }
119:
120:            public synchronized Space readSnapshot() throws LoggerException {
121:                try {
122:
123:                    long lastsnap = getLastSnapshot();
124:                    if (lastsnap == 0)
125:                        return null;
126:
127:                    File file = new File(DIR + "/" + dirname,
128:                            getNumberString(lastsnap) + SNAPSHOT_PREFIX);
129:                    FileInputStream fis = new FileInputStream(file);
130:                    ObjectInputStream ois = new ObjectInputStream(fis);
131:                    Space space = (Space) ois.readObject();
132:                    ois.close();
133:
134:                    return space;
135:
136:                } catch (IOException e) {
137:                    e.printStackTrace();
138:                    throw new LoggerException(e.getMessage());
139:                } catch (ClassNotFoundException e) {
140:                    e.printStackTrace();
141:                    throw new LoggerException(e.getMessage());
142:                }
143:            }
144:
145:            // If last snapshot is X, than I must apply all command beginning with X + 1.
146:            public synchronized void reapplyCommandsFromLog(Space4J space4j)
147:                    throws LoggerException, CommandException {
148:
149:                long lastsnap = getLastSnapshot();
150:                long start = lastsnap + 1;
151:
152:                File file = new File(DIR + "/" + dirname,
153:                        getNumberString(start) + LOG_PREFIX);
154:
155:                while (file.exists()) {
156:
157:                    try {
158:
159:                        FileInputStream fis = new FileInputStream(file);
160:                        ObjectInputStream ois = new ObjectInputStream(fis);
161:
162:                        try {
163:                            while (true) {
164:                                Command cmd = (Command) ois.readObject();
165:                                space4j.executeCommand(cmd, false); // of course, no need to log here
166:                            }
167:                        } catch (EOFException e) {
168:                            // normal... no more objects to read...
169:                        } catch (CommandException e) {
170:                            throw e;
171:                        } catch (IOException e) {
172:                            e.printStackTrace();
173:                            throw new LoggerException(e.getMessage());
174:                        } catch (ClassNotFoundException e) {
175:                            e.printStackTrace();
176:                            throw new LoggerException(e.getMessage());
177:                        } finally {
178:                            try {
179:                                ois.close();
180:                            } catch (Exception e) {
181:                            }
182:                        }
183:
184:                        start++;
185:
186:                        file = new File(DIR + "/" + dirname,
187:                                getNumberString(start) + LOG_PREFIX);
188:                    } catch (IOException e) {
189:                        e.printStackTrace();
190:                        throw new LoggerException(e.getMessage());
191:                    }
192:                }
193:            }
194:
195:            // construct a big number with NUMBER_OF_DIGITS digits    
196:            private String getNumberString(long d) {
197:                String s = String.valueOf(d);
198:                int x = NUMBER_OF_DIGITS - s.length();
199:                StringBuffer sb = new StringBuffer();
200:                for (int i = 0; i < x; i++) {
201:                    sb.append("0");
202:                }
203:                sb.append(s);
204:
205:                return sb.toString();
206:            }
207:
208:            // find the number of the last snapshot in disk
209:            private long getLastSnapshot() {
210:                File dir = new File(DIR + "/" + dirname);
211:
212:                File[] files = dir.listFiles(new FileFilter() {
213:                    public boolean accept(File file) {
214:                        String filename = file.getName();
215:                        if (filename.endsWith(SNAPSHOT_PREFIX)) {
216:                            return true;
217:                        }
218:                        return false;
219:                    }
220:                });
221:
222:                if (files == null)
223:                    return 0;
224:
225:                Arrays.sort(files);
226:
227:                if (files.length == 0) {
228:                    return 0;
229:                } else {
230:                    String filename = files[files.length - 1].getName();
231:                    return Long.parseLong(filename.substring(0, filename
232:                            .indexOf(".")));
233:                }
234:            }
235:
236:            // find the last log file number saved to disk
237:            private long getLastLog() {
238:
239:                long lastsnap = getLastSnapshot();
240:
241:                long lastlog = lastsnap + 1;
242:
243:                File file = new File(DIR + "/" + dirname,
244:                        getNumberString(lastlog) + LOG_PREFIX);
245:
246:                while (file.exists()) {
247:                    lastlog++;
248:                    file = new File(DIR + "/" + dirname,
249:                            getNumberString(lastlog) + LOG_PREFIX);
250:                }
251:
252:                lastlog--;
253:
254:                return lastlog;
255:
256:            }
257:
258:            /* A FileOutputStream that keeps track of its size, so we can rotate the log when it is too big. */
259:            private class SizedFileOutputStream extends FileOutputStream {
260:
261:                private long size = 0;
262:
263:                public SizedFileOutputStream(File file) throws IOException {
264:                    super (file);
265:                }
266:
267:                public long getSize() {
268:                    return size;
269:                }
270:
271:                public void flush() throws IOException {
272:                    super .flush();
273:                    getFD().sync(); // make sure if flushes !!!
274:                }
275:
276:                public void write(byte[] b) throws IOException {
277:                    super .write(b);
278:                    size += b.length;
279:                }
280:
281:                public void write(byte[] b, int off, int len)
282:                        throws IOException {
283:                    super .write(b, off, len);
284:                    size += len;
285:                }
286:
287:                public void write(int b) throws IOException {
288:                    super.write(b);
289:                    size++;
290:                }
291:
292:            }
293:
294:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.