KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > store > jdbc > DataSourceSupport


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.store.jdbc;
19
20 import org.apache.derby.jdbc.EmbeddedDataSource;
21 import org.apache.activemq.util.IOHelper;
22
23 import javax.sql.DataSource JavaDoc;
24
25 import java.io.File JavaDoc;
26 import java.io.IOException JavaDoc;
27
28 /**
29  * A helper class which provides a factory method to create a default
30  * {@link DataSource) if one is not provided.
31  *
32  * @version $Revision: 511947 $
33  */

34 public class DataSourceSupport {
35
36     private String JavaDoc dataDirectory = IOHelper.getDefaultDataDirectory();
37     private File JavaDoc dataDirectoryFile;
38     private DataSource dataSource;
39
40     public DataSourceSupport() {
41     }
42
43     public DataSourceSupport(DataSource dataSource) {
44         this.dataSource = dataSource;
45     }
46
47     public File JavaDoc getDataDirectoryFile() {
48         if (dataDirectoryFile == null) {
49             dataDirectoryFile = new File JavaDoc(getDataDirectory());
50         }
51         return dataDirectoryFile;
52     }
53
54     public void setDataDirectoryFile(File JavaDoc dataDirectory) {
55         this.dataDirectoryFile = dataDirectory;
56     }
57
58     public String JavaDoc getDataDirectory() {
59         return dataDirectory;
60     }
61
62     public void setDataDirectory(String JavaDoc dataDirectory) {
63         this.dataDirectory = dataDirectory;
64     }
65
66     public DataSource getDataSource() throws IOException JavaDoc {
67         if (dataSource == null) {
68             dataSource = createDataSource();
69             if (dataSource == null) {
70                 throw new IllegalArgumentException JavaDoc("No dataSource property has been configured");
71             }
72         }
73         return dataSource;
74     }
75
76     public void setDataSource(DataSource dataSource) {
77         this.dataSource = dataSource;
78     }
79
80     protected DataSource createDataSource() throws IOException JavaDoc {
81
82         // Setup the Derby datasource.
83
System.setProperty("derby.system.home", getDataDirectoryFile().getCanonicalPath());
84         System.setProperty("derby.storage.fileSyncTransactionLog", "true");
85         System.setProperty("derby.storage.pageCacheSize", "100");
86
87         final EmbeddedDataSource ds = new EmbeddedDataSource();
88         ds.setDatabaseName("derbydb");
89         ds.setCreateDatabase("create");
90         return ds;
91     }
92     
93     public String JavaDoc toString(){
94         return ""+dataSource;
95     }
96
97 }
98
Popular Tags