Source Code Cross Referenced for DBPageManager.java in  » Web-Framework » jpublish » org » jpublish » page » db » 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 » jpublish » org.jpublish.page.db 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2004-2007 the original author or authors.
003:         *
004:         * Licensed under the Apache License, Version 2.0 (the "License");
005:         * you may not use this file except in compliance with the License.
006:         * You may obtain a copy of the License at
007:         *
008:         *      http://www.apache.org/licenses/LICENSE-2.0
009:         *
010:         * Unless required by applicable law or agreed to in writing, software
011:         * distributed under the License is distributed on an "AS IS" BASIS,
012:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         * See the License for the specific language governing permissions and
014:         * limitations under the License.
015:         *
016:         */
017:
018:        package org.jpublish.page.db;
019:
020:        import java.io.StringReader;
021:        import java.sql.ResultSet;
022:        import java.sql.Connection;
023:        import java.sql.SQLException;
024:        import java.sql.DriverManager;
025:        import java.sql.PreparedStatement;
026:
027:        import java.util.Map;
028:        import java.util.HashMap;
029:        import java.util.Iterator;
030:
031:        import org.apache.commons.logging.Log;
032:        import org.apache.commons.logging.LogFactory;
033:
034:        import com.anthonyeden.lib.util.SQLUtilities;
035:        import com.anthonyeden.lib.config.Configuration;
036:        import com.anthonyeden.lib.config.ConfigurationException;
037:
038:        import org.jpublish.page.PageInstance;
039:        import org.jpublish.page.PageDefinition;
040:        import org.jpublish.page.AbstractPageManager;
041:        import org.jpublish.page.PageNotFoundException;
042:        import org.jpublish.util.PathUtilities;
043:        import org.jpublish.util.vfs.VFSFile;
044:
045:        /** Implementation of the PageManager interface which pulls all page 
046:         definitions from a database.  The DBPageManager class currently does
047:         not cache any Page objects.
048:
049:         @author Anthony Eden
050:         @since 1.1
051:         */
052:
053:        public class DBPageManager extends AbstractPageManager {
054:
055:            private static Log log = LogFactory.getLog(DBPageManager.class);
056:
057:            private static final String SELECT_PAGE_STMT = "select * from page where path = ?";
058:            private static final String SELECT_PAGES_STMT = "select * from page where path like ?";
059:            private static final String INSERT_PAGE_STMT = "insert into page set (path, data) values (?, ?)";
060:            private static final String UPDATE_PAGE_STMT = "update page set data = ? where path = ?";
061:            private static final String DELETE_PAGE_STMT = "delete from page where path = ?";
062:
063:            protected String driver;
064:            protected String url;
065:            protected String username;
066:            protected String password;
067:
068:            protected Map pageDefinitions;
069:
070:            /** Construct a new DBPageManager. */
071:
072:            public DBPageManager() {
073:                pageDefinitions = new HashMap();
074:            }
075:
076:            /** Get the SQL driver.
077:            
078:                @return The driver class name
079:             */
080:
081:            public String getDriver() {
082:                return driver;
083:            }
084:
085:            /** Set the SQL driver.
086:            
087:                @param driver The driver class name
088:             */
089:
090:            public void setDriver(String driver) {
091:                this .driver = driver;
092:            }
093:
094:            /** Get the database URL.
095:            
096:                @return The database URL
097:             */
098:
099:            public String getURL() {
100:                return url;
101:            }
102:
103:            /** Set the database URL.
104:            
105:                @param url The URL
106:             */
107:
108:            public void setURL(String url) {
109:                this .url = url;
110:            }
111:
112:            /** Get the database username.
113:            
114:                @return The database username
115:             */
116:
117:            public String getUsername() {
118:                return username;
119:            }
120:
121:            /** Set the database username.
122:            
123:                @param username The username
124:             */
125:
126:            public void setUsername(String username) {
127:                this .username = username;
128:            }
129:
130:            /** Set the database password.
131:            
132:                @param password The password
133:             */
134:
135:            public void setPassword(String password) {
136:                this .password = password;
137:            }
138:
139:            /** Get a PageInstance from the given path.  If no page can be found
140:                then this method will throw a PageFoundException.
141:            
142:                @param path The page path
143:                @return The Page
144:                @throws Exception Any Exception
145:             */
146:
147:            public synchronized PageInstance getPage(String path)
148:                    throws Exception {
149:                String pagePath = PathUtilities.extractPagePath(path);
150:                if (log.isDebugEnabled())
151:                    log.debug("Page path: " + pagePath);
152:
153:                PageInstance page = null;
154:                PageDefinition pageDefinition = null;
155:
156:                Connection c = null;
157:                try {
158:
159:                    log.debug("Loading page definition configuration from DB");
160:
161:                    // select the data from the database
162:                    c = getConnection();
163:                    PreparedStatement ps = c.prepareStatement(SELECT_PAGE_STMT);
164:                    ps.setString(1, pagePath);
165:                    ResultSet resultSet = ps.executeQuery();
166:
167:                    if (resultSet.next()) {
168:                        String pageXML = resultSet.getString("data");
169:
170:                        pageDefinition = new PageDefinition(siteContext,
171:                                pagePath);
172:                        pageDefinition.loadConfiguration(new StringReader(
173:                                pageXML));
174:                    }
175:                } catch (Exception e) {
176:                    log.error("Error loading page definition: "
177:                            + e.getMessage());
178:                    throw e;
179:                } finally {
180:                    SQLUtilities.close(c);
181:                }
182:
183:                if (pageDefinition != null) {
184:                    if (log.isDebugEnabled())
185:                        log.debug("Getting page instance for " + path);
186:                    page = pageDefinition.getPageInstance(path);
187:                } else {
188:                    throw new PageNotFoundException("Page not found: " + path);
189:                }
190:
191:                return page;
192:            }
193:
194:            /** Put the page instance into the location specified by the given
195:                path.
196:                
197:                @param path The page path
198:                @param page The Page object
199:                @throws Exception
200:             */
201:
202:            public void putPage(String path, PageInstance page)
203:                    throws Exception {
204:                // NYI
205:            }
206:
207:            /** Remove the page at the specified path.
208:            
209:                @param path The page path
210:             */
211:
212:            public void removePage(String path) throws Exception {
213:                // NYI
214:            }
215:
216:            /** Make the directory for the specified path.  Parent directories
217:                will also be created if they do not exist.
218:                
219:                @param path The directory path
220:             */
221:
222:            public void makeDirectory(String path) {
223:
224:            }
225:
226:            /** Remove the directory for the specified path.  The directory
227:                must be empty.
228:            
229:                @param path The path
230:                @throws Exception
231:             */
232:
233:            public void removeDirectory(String path) throws Exception {
234:
235:            }
236:
237:            /** Get an iterator which can be used to iterate through all pages
238:                known to the PageManager.
239:                
240:                @return An Iterator of Pages
241:                @throws Exception Any Exception
242:             */
243:
244:            public Iterator getPages() throws Exception {
245:                return getPages("");
246:            }
247:
248:            /** Get an iterator which can be used to iterate through all the
249:                pages at or below the specified path.  If the path refers
250:                to a file, then the file's parent directory is used.
251:                
252:                @param path The base path
253:                @return An Iterator of Pages
254:                @throws Exception Any Exception
255:             */
256:
257:            public Iterator getPages(String path) throws Exception {
258:                Connection c = null;
259:
260:                try {
261:                    c = getConnection();
262:                    PreparedStatement ps = c
263:                            .prepareStatement(SELECT_PAGES_STMT);
264:                    ps.setString(1, path + "%");
265:                    ResultSet resultSet = ps.executeQuery();
266:
267:                    return new DBPageIterator(this , resultSet);
268:                } finally {
269:                    SQLUtilities.close(c);
270:                }
271:            }
272:
273:            /** Get the Virtual File System root file.  The Virtual File System
274:                provides a datasource-independent way of navigating through all
275:                items known to the PageManager.
276:                
277:                <p>The DBPageManager does not currently support a virtual file
278:                system.  This method will through an UnsupportedOperationException.</p>
279:                
280:                @return The root VFSFile
281:                @throws Exception
282:             */
283:
284:            public synchronized VFSFile getVFSRoot() throws Exception {
285:                // NYI
286:                throw new UnsupportedOperationException();
287:            }
288:
289:            /** Load the DBPageManager configuration.
290:            
291:                @param configuration The Configuration object
292:                @throws ConfigurationException
293:             */
294:
295:            public void loadConfiguration(Configuration configuration)
296:                    throws ConfigurationException {
297:                // load database configuration
298:                Configuration dbConfiguration = configuration
299:                        .getChild("database");
300:                setURL(dbConfiguration.getChildValue("url"));
301:                setDriver(dbConfiguration.getChildValue("driver"));
302:                setUsername(dbConfiguration.getChildValue("username"));
303:                setPassword(dbConfiguration.getChildValue("password"));
304:            }
305:
306:            protected Connection getConnection() throws SQLException {
307:                return DriverManager.getConnection(getURL(), getUsername(),
308:                        getPassword());
309:            }
310:
311:            protected String getPassword() {
312:                return password;
313:            }
314:
315:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.