Source Code Cross Referenced for CastorSQLPreferencesPersistence.java in  » Web-Mail » Jwma » dtw » webmail » plugin » std » 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 Mail » Jwma » dtw.webmail.plugin.std 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /***
002:         * jwma Java WebMail
003:         * Copyright (c) 2000-2003 jwma team
004:         *
005:         * jwma is free software; you can distribute and use this source
006:         * under the terms of the BSD-style license received along with
007:         * the distribution.
008:         ***/package dtw.webmail.plugin.std;
009:
010:        import dtw.webmail.JwmaKernel;
011:        import dtw.webmail.JwmaSession;
012:        import dtw.webmail.model.JwmaException;
013:        import dtw.webmail.model.JwmaPreferencesImpl;
014:        import dtw.webmail.model.JwmaMailIdentityImpl;
015:        import dtw.webmail.plugin.PreferencesPersistencePlugin;
016:        import dtw.webmail.util.CastorDatabase;
017:        import dtw.webmail.util.CastorDatabasePool;
018:        import dtw.webmail.util.JwmaSettings;
019:        import dtw.webmail.util.config.JwmaConfiguration;
020:        import dtw.webmail.util.config.PostOffice;
021:        import org.apache.log4j.Logger;
022:        import org.exolab.castor.jdo.Query;
023:
024:        import java.io.FileReader;
025:        import java.io.FileWriter;
026:
027:        /**
028:         * A <tt>PreferencesPersistancePlugin</tt> implementation
029:         * based on Castor JDO.
030:         * <p>
031:         * Stores the preferences in a database.
032:         *
033:         * @author Dieter Wimberger
034:         * @version 0.9.7 07/02/2003
035:         */
036:        public class CastorSQLPreferencesPersistence implements 
037:                PreferencesPersistencePlugin {
038:
039:            //logging
040:            private static Logger log = Logger
041:                    .getLogger(CastorSQLPreferencesPersistence.class);
042:
043:            private String m_PreferencesTemplateFile;
044:            private CastorPreferences m_PreferencesTemplate;
045:            private CastorDatabasePool m_DBPool;
046:
047:            public JwmaPreferencesImpl loadPreferences(String identity)
048:                    throws JwmaException {
049:
050:                CastorPreferences prefs = null;
051:                CastorDatabase db = null;
052:                try {
053:                    db = m_DBPool.leaseDatabase();
054:                    //Begin transaction
055:                    db.begin();
056:                    //get cached query
057:                    Query oql = db.getQuery(JwmaDatabase.PREFINSTANCE_BYUSERID);
058:                    //bind param
059:                    oql.bind(identity);
060:                    //retrieve prefs instance
061:                    prefs = (CastorPreferences) oql.execute().next();
062:                    //end transaction
063:                    db.commit();
064:
065:                } catch (Exception ex) {
066:                    throw new JwmaException("").setException(ex);
067:                } finally {
068:                    //release the database
069:                    m_DBPool.releaseDatabase(db);
070:                }
071:                return prefs;
072:            }//loadPreferences
073:
074:            public void savePreferences(JwmaPreferencesImpl prefs)
075:                    throws JwmaException {
076:
077:                CastorDatabase db = null;
078:                CastorPreferences cprefs = (CastorPreferences) prefs;
079:                try {
080:                    db = m_DBPool.leaseDatabase();
081:                    // Begin a transaction
082:                    db.begin();
083:
084:                    //check if the preferences are not persistent yet.
085:                    //simplest is if the timestamp is not zero.
086:                    if (cprefs.jdoGetTimeStamp() != 0) {
087:                        cprefs.updatePreferences(db.getDatabase());
088:                    } else {
089:                        cprefs.persistPreferences(db.getDatabase());
090:                    }
091:                    // Commit the transaction
092:                    db.commit();
093:                } catch (Exception ex) {
094:                    throw new JwmaException("").setException(ex);
095:                } finally {
096:                    m_DBPool.releaseDatabase(db);
097:                }
098:            }//savePreferences
099:
100:            public boolean isPersistent(String identity) throws JwmaException {
101:
102:                //FIXME:This is definately not very efficient, but
103:                //if ok, and castor caches..hmm
104:                if (identity == null || identity.length() == 0) {
105:                    return false;
106:                }
107:                CastorDatabase db = null;
108:                boolean exists = false;
109:                try {
110:                    db = m_DBPool.leaseDatabase();
111:                    //Begin transaction
112:                    db.begin();
113:
114:                    //get cached query
115:                    Query oql = db.getQuery(JwmaDatabase.PREFINSTANCE_BYUSERID);
116:                    //bind param
117:                    oql.bind(identity);
118:
119:                    //retrieve prefs instance
120:                    if (oql.execute().hasMore()) {
121:                        exists = true;
122:                    }
123:
124:                    //end transaction
125:                    db.commit();
126:
127:                } catch (Exception ex) {
128:                    String msg = JwmaKernel.getReference().getLogMessage(
129:                            "jwma.plugin.castor.objcheck");
130:                    log.error(msg, ex);
131:                    throw new JwmaException(msg).setException(ex);
132:                } finally {
133:                    //release the database
134:                    m_DBPool.releaseDatabase(db);
135:                }
136:                return exists;
137:            }//isPersistent
138:
139:            public JwmaPreferencesImpl getPreferencesTemplate(
140:                    JwmaSession session) {
141:
142:                //1. get clone
143:                JwmaPreferencesImpl prefs = m_PreferencesTemplate.getClone();
144:
145:                //2. make post office config dependent adjustments:
146:                PostOffice po = session.getPostOffice();
147:
148:                //set root folder
149:                prefs.setRootFolder(po.getRootFolder());
150:
151:                //set replyto domain from postoffice
152:                String replyto = po.getReplyToDomain();
153:                if (replyto == null || replyto.length() == 0) {
154:                    replyto = po.getAddress();
155:                }
156:
157:                ((JwmaMailIdentityImpl) prefs.getMailIdentity())
158:                        .setReplyTo(session.getUsername() + "@" + replyto);
159:
160:                return prefs;
161:            }//getPreferencesTemplate
162:
163:            public void activate() throws JwmaException {
164:                JwmaKernel kernel = JwmaKernel.getReference();
165:                try {
166:                    String etc = kernel.getDirectoryPath(JwmaKernel.ETC_DIR);
167:
168:                    //String m_MappingFile = etc + settings.getMappingFilename();
169:                    m_PreferencesTemplateFile = etc
170:                            + JwmaConfiguration.TEMPLATE_FILENAME;
171:
172:                    //Setup castor helper
173:                    CastorHelper helper = CastorHelper.getReference();
174:
175:                    //load mapping
176:                    //helper.loadMapping(m_MappingFile);
177:
178:                    //prepare jdo
179:                    helper.prepareJDO(etc + "database.xml");
180:
181:                    //set the pool
182:                    m_DBPool = helper.getDatabasePool();
183:
184:                    //load the template
185:                    m_PreferencesTemplate = (CastorPreferences) helper
186:                            .unmarshal(new FileReader(m_PreferencesTemplateFile));
187:
188:                    log.info(JwmaKernel.getReference().getLogMessage(
189:                            "jwma.plugin.activation"));
190:                } catch (Exception ex) {
191:                    log.info(JwmaKernel.getReference().getLogMessage(
192:                            "jwma.plugin.failedactivation"));
193:                    throw new JwmaException("").setException(ex);
194:                }
195:            }//activate
196:
197:            public void deactivate() {
198:                //FIXME: shut down pool, close down connections?
199:            }//deactivate
200:
201:            public void setPreferencesTemplate(JwmaPreferencesImpl template)
202:                    throws JwmaException {
203:
204:                //set the reference immediately
205:                m_PreferencesTemplate = (CastorPreferences) template;
206:                //store
207:                try {
208:                    FileWriter writer = new FileWriter(
209:                            m_PreferencesTemplateFile);
210:                    CastorHelper.getReference().marshal(template, writer);
211:                    writer.close();
212:                } catch (Exception ex) {
213:                    throw new JwmaException("").setException(ex);
214:                }
215:            }//setPreferencesTemplate
216:
217:        }//class CastorSQLPreferencesPersistence
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.