View Javadoc
1   /*
2    * Copyright 2004-2025 the original author or authors.
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    *    https://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 com.ibatis.sqlmap.engine.config;
17  
18  import com.ibatis.sqlmap.engine.cache.CacheController;
19  import com.ibatis.sqlmap.engine.cache.CacheModel;
20  import com.ibatis.sqlmap.engine.impl.SqlMapClientImpl;
21  import com.ibatis.sqlmap.engine.scope.ErrorContext;
22  
23  import java.util.Properties;
24  
25  /**
26   * The Class CacheModelConfig.
27   */
28  public class CacheModelConfig {
29  
30    /** The error context. */
31    private ErrorContext errorContext;
32  
33    /** The cache model. */
34    private CacheModel cacheModel;
35  
36    /**
37     * Instantiates a new cache model config.
38     *
39     * @param config
40     *          the config
41     * @param id
42     *          the id
43     * @param controller
44     *          the controller
45     * @param readOnly
46     *          the read only
47     * @param serialize
48     *          the serialize
49     */
50    CacheModelConfig(SqlMapConfiguration config, String id, CacheController controller, boolean readOnly,
51        boolean serialize) {
52      this.errorContext = config.getErrorContext();
53      this.cacheModel = new CacheModel();
54      SqlMapClientImpl client = config.getClient();
55      errorContext.setActivity("building a cache model");
56      cacheModel.setReadOnly(readOnly);
57      cacheModel.setSerialize(serialize);
58      errorContext.setObjectId(id + " cache model");
59      errorContext.setMoreInfo("Check the cache model type.");
60      cacheModel.setId(id);
61      cacheModel.setResource(errorContext.getResource());
62      try {
63        cacheModel.setCacheController(controller);
64      } catch (Exception e) {
65        throw new RuntimeException("Error setting Cache Controller Class.  Cause: " + e, e);
66      }
67      errorContext.setMoreInfo("Check the cache model configuration.");
68      if (client.getDelegate().isCacheModelsEnabled()) {
69        client.getDelegate().addCacheModel(cacheModel);
70      }
71      errorContext.setMoreInfo(null);
72      errorContext.setObjectId(null);
73    }
74  
75    /**
76     * Sets the flush interval.
77     *
78     * @param hours
79     *          the hours
80     * @param minutes
81     *          the minutes
82     * @param seconds
83     *          the seconds
84     * @param milliseconds
85     *          the milliseconds
86     */
87    public void setFlushInterval(long hours, long minutes, long seconds, long milliseconds) {
88      errorContext.setMoreInfo("Check the cache model flush interval.");
89      long t = 0L;
90      t += milliseconds;
91      t += seconds * 1000L;
92      t += minutes * 60L * 1000L;
93      t += hours * 60L * 60L * 1000L;
94      if (t < 1L) {
95        throw new RuntimeException(
96            "A flush interval must specify one or more of milliseconds, seconds, minutes or hours.");
97      }
98      cacheModel.setFlushInterval(t);
99    }
100 
101   /**
102    * Adds the flush trigger statement.
103    *
104    * @param statement
105    *          the statement
106    */
107   public void addFlushTriggerStatement(String statement) {
108     errorContext.setMoreInfo("Check the cache model flush on statement elements.");
109     cacheModel.addFlushTriggerStatement(statement);
110   }
111 
112   /**
113    * Gets the cache model.
114    *
115    * @return the cache model
116    */
117   public CacheModel getCacheModel() {
118     return cacheModel;
119   }
120 
121   /**
122    * Sets the controller properties.
123    *
124    * @param cacheProps
125    *          the new controller properties
126    */
127   public void setControllerProperties(Properties cacheProps) {
128     cacheModel.setControllerProperties(cacheProps);
129   }
130 }