View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.jdiagnose.library.db;
17  
18  import javax.servlet.ServletContext;
19  
20  import org.springframework.web.context.ServletContextAware;
21  
22  
23  /***
24   * @author JMcCrindle
25   */
26  public class HsqlDbUrlFactory implements ServletContextAware {
27      
28      private String filename = null;
29      private String path = null;
30      private ServletContext context = null;
31      
32      public String getUrl() {
33          String filePath = path;
34          if(path.startsWith("context:")) {
35              filePath = context.getRealPath(path.substring("context:".length()));
36          }
37          return "jdbc:hsqldb:file:" + safeAppendSlash(filePath) + filename;
38      }
39  
40      protected String safeAppendSlash(String path) {
41          String fileSeparator = System.getProperty("file.separator");
42          return path.endsWith(fileSeparator) ? path : path + fileSeparator;
43      }
44  
45      public void setFilename(String filename) {
46          this.filename = filename;
47      }
48  
49      public void setPath(String path) {
50          this.path = path;
51      }
52  
53      /* (non-Javadoc)
54       * @see org.springframework.web.context.ServletContextAware#setServletContext(javax.servlet.ServletContext)
55       */
56      public void setServletContext(ServletContext context) {
57          this.context = context;
58      }
59  }