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: }
|