Debian Codegraph @

src/org/apache/commons/jcs3/auxiliary/disk/jdbc/hsql/HSQLDiskCacheFactory.java

raw symbols SCIP
1 package org.apache.commons.jcs3.auxiliary.disk.jdbc.hsql;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.sql.Connection;
23 import java.sql.DatabaseMetaData;
24 import java.sql.ResultSet;
25 import java.sql.SQLException;
26 import java.sql.Statement;
27
28 import javax.sql.DataSource;
29
30 import org.apache.commons.jcs3.auxiliary.AuxiliaryCacheAttributes;
31 import org.apache.commons.jcs3.auxiliary.disk.jdbc.JDBCDiskCache;
32 import org.apache.commons.jcs3.auxiliary.disk.jdbc.JDBCDiskCacheAttributes;
33 import org.apache.commons.jcs3.auxiliary.disk.jdbc.JDBCDiskCacheFactory;
34 import org.apache.commons.jcs3.engine.behavior.ICompositeCacheManager;
35 import org.apache.commons.jcs3.engine.behavior.IElementSerializer;
36 import org.apache.commons.jcs3.engine.logging.behavior.ICacheEventLogger;
37 import org.apache.commons.jcs3.log.Log;
38 import org.apache.commons.jcs3.log.LogManager;
39
40 /**
41 * This factory should create hsql disk caches.
42 */
43 public class HSQLDiskCacheFactory
44 extends JDBCDiskCacheFactory
45 {
46 /** The logger */
47 private static final Log log = LogManager.getLog( HSQLDiskCacheFactory.class );
48
49 /**
50 * This factory method should create an instance of the hsqlcache.
51 * <p>
52 * @param rawAttr
53 * @param compositeCacheManager
54 * @param cacheEventLogger
55 * @param elementSerializer
56 * @return JDBCDiskCache
57 * @throws SQLException if the creation of the cache instance fails
58 */
59 @Override
60 public <K, V> JDBCDiskCache<K, V> createCache( final AuxiliaryCacheAttributes rawAttr,
61 final ICompositeCacheManager compositeCacheManager,
62 final ICacheEventLogger cacheEventLogger,
63 final IElementSerializer elementSerializer )
64 throws SQLException
65 {
66 // TODO get this from the attributes.
67 System.setProperty( "hsqldb.cache_scale", "8" );
68
69 final JDBCDiskCache<K, V> cache = super.createCache(rawAttr, compositeCacheManager,
70 cacheEventLogger, elementSerializer);
71 setupDatabase( cache.getDataSource(), (JDBCDiskCacheAttributes) rawAttr );
72
73 return cache;
74 }
75
76 /**
77 * Creates the table if it doesn't exist
78 * <p>
79 * @param ds Data Source
80 * @param attributes Cache region configuration
81 * @throws SQLException
82 */
83 protected void setupDatabase( final DataSource ds, final JDBCDiskCacheAttributes attributes )
84 throws SQLException
85 {
86 try (Connection cConn = ds.getConnection())
87 {
88 setupTable( cConn, attributes.getTableName() );
89 log.info( "Finished setting up table [{0}]", attributes.getTableName());
90 }
91 }
92
93 /**
94 * SETUP TABLE FOR CACHE
95 * <p>
96 * @param cConn
97 * @param tableName
98 */
99 protected synchronized void setupTable( final Connection cConn, final String tableName ) throws SQLException
100 {
101 final DatabaseMetaData dmd = cConn.getMetaData();
102 final ResultSet result = dmd.getTables(null, null, tableName, null);
103
104 if (!result.next())
105 {
106 // TODO make the cached nature of the table configurable
107 final StringBuilder createSql = new StringBuilder();
108 createSql.append( "CREATE CACHED TABLE ").append( tableName );
109 createSql.append( "( " );
110 createSql.append( "CACHE_KEY VARCHAR(250) NOT NULL, " );
111 createSql.append( "REGION VARCHAR(250) NOT NULL, " );
112 createSql.append( "ELEMENT BINARY, " );
113 createSql.append( "CREATE_TIME TIMESTAMP, " );
114 createSql.append( "UPDATE_TIME_SECONDS BIGINT, " );
115 createSql.append( "MAX_LIFE_SECONDS BIGINT, " );
116 createSql.append( "SYSTEM_EXPIRE_TIME_SECONDS BIGINT, " );
117 createSql.append( "IS_ETERNAL CHAR(1), " );
118 createSql.append( "PRIMARY KEY (CACHE_KEY, REGION) " );
119 createSql.append( ");" );
120
121 try (Statement sStatement = cConn.createStatement())
122 {
123 sStatement.execute( createSql.toString() );
124 }
125 }
126 }
127 }