001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019/** 020 * Copyright 2003-2010 Terracotta, Inc. 021 * 022 * Licensed under the Apache License, Version 2.0 (the "License"); 023 * you may not use this file except in compliance with the License. 024 * You may obtain a copy of the License at 025 * 026 * http://www.apache.org/licenses/LICENSE-2.0 027 * 028 * Unless required by applicable law or agreed to in writing, software 029 * distributed under the License is distributed on an "AS IS" BASIS, 030 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 031 * See the License for the specific language governing permissions and 032 * limitations under the License. 033 */ 034package org.apache.commons.jcs3.jcache; 035 036import javax.cache.configuration.CacheEntryListenerConfiguration; 037import javax.cache.configuration.CompleteConfiguration; 038import javax.cache.configuration.Configuration; 039import javax.cache.configuration.Factory; 040import javax.cache.expiry.EternalExpiryPolicy; 041import javax.cache.expiry.ExpiryPolicy; 042import javax.cache.integration.CacheLoader; 043import javax.cache.integration.CacheWriter; 044import java.util.Collections; 045import java.util.HashSet; 046import java.util.Set; 047 048public class JCSConfiguration<K, V> implements CompleteConfiguration<K, V> 049{ 050 051 /** 052 * 053 */ 054 private static final long serialVersionUID = 3322514800838658711L; 055 private final Class<K> keyType; 056 private final Class<V> valueType; 057 private final boolean storeByValue; 058 private final boolean readThrough; 059 private final boolean writeThrough; 060 private final Factory<CacheLoader<K, V>> cacheLoaderFactory; 061 private final Factory<CacheWriter<? super K, ? super V>> cacheWristerFactory; 062 private final Factory<ExpiryPolicy> expiryPolicyFactory; 063 private final Set<CacheEntryListenerConfiguration<K, V>> cacheEntryListenerConfigurations; 064 065 private volatile boolean statisticsEnabled; 066 private volatile boolean managementEnabled; 067 068 public JCSConfiguration(final Configuration<K, V> configuration, final Class<K> keyType, final Class<V> valueType) 069 { 070 this.keyType = keyType; 071 this.valueType = valueType; 072 if (configuration instanceof CompleteConfiguration) 073 { 074 final CompleteConfiguration<K, V> cConfiguration = (CompleteConfiguration<K, V>) configuration; 075 storeByValue = configuration.isStoreByValue(); 076 readThrough = cConfiguration.isReadThrough(); 077 writeThrough = cConfiguration.isWriteThrough(); 078 statisticsEnabled = cConfiguration.isStatisticsEnabled(); 079 managementEnabled = cConfiguration.isManagementEnabled(); 080 cacheLoaderFactory = cConfiguration.getCacheLoaderFactory(); 081 cacheWristerFactory = cConfiguration.getCacheWriterFactory(); 082 this.expiryPolicyFactory = cConfiguration.getExpiryPolicyFactory(); 083 cacheEntryListenerConfigurations = new HashSet<>(); 084 085 final Iterable<CacheEntryListenerConfiguration<K, V>> entryListenerConfigurations = cConfiguration 086 .getCacheEntryListenerConfigurations(); 087 if (entryListenerConfigurations != null) 088 { 089 for (final CacheEntryListenerConfiguration<K, V> kvCacheEntryListenerConfiguration : entryListenerConfigurations) 090 { 091 cacheEntryListenerConfigurations.add(kvCacheEntryListenerConfiguration); 092 } 093 } 094 } 095 else 096 { 097 expiryPolicyFactory = EternalExpiryPolicy.factoryOf(); 098 storeByValue = true; 099 readThrough = false; 100 writeThrough = false; 101 statisticsEnabled = false; 102 managementEnabled = false; 103 cacheLoaderFactory = null; 104 cacheWristerFactory = null; 105 cacheEntryListenerConfigurations = new HashSet<>(); 106 } 107 } 108 109 @Override 110 public Class<K> getKeyType() 111 { 112 return keyType == null ? (Class<K>) Object.class : keyType; 113 } 114 115 @Override 116 public Class<V> getValueType() 117 { 118 return valueType == null ? (Class<V>) Object.class : valueType; 119 } 120 121 @Override 122 public boolean isStoreByValue() 123 { 124 return storeByValue; 125 } 126 127 @Override 128 public boolean isReadThrough() 129 { 130 return readThrough; 131 } 132 133 @Override 134 public boolean isWriteThrough() 135 { 136 return writeThrough; 137 } 138 139 @Override 140 public boolean isStatisticsEnabled() 141 { 142 return statisticsEnabled; 143 } 144 145 @Override 146 public boolean isManagementEnabled() 147 { 148 return managementEnabled; 149 } 150 151 @Override 152 public Iterable<CacheEntryListenerConfiguration<K, V>> getCacheEntryListenerConfigurations() 153 { 154 return Collections.unmodifiableSet(cacheEntryListenerConfigurations); 155 } 156 157 @Override 158 public Factory<CacheLoader<K, V>> getCacheLoaderFactory() 159 { 160 return cacheLoaderFactory; 161 } 162 163 @Override 164 public Factory<CacheWriter<? super K, ? super V>> getCacheWriterFactory() 165 { 166 return cacheWristerFactory; 167 } 168 169 @Override 170 public Factory<ExpiryPolicy> getExpiryPolicyFactory() 171 { 172 return expiryPolicyFactory; 173 } 174 175 public synchronized void addListener(final CacheEntryListenerConfiguration<K, V> cacheEntryListenerConfiguration) 176 { 177 cacheEntryListenerConfigurations.add(cacheEntryListenerConfiguration); 178 } 179 180 public synchronized void removeListener(final CacheEntryListenerConfiguration<K, V> cacheEntryListenerConfiguration) 181 { 182 cacheEntryListenerConfigurations.remove(cacheEntryListenerConfiguration); 183 } 184 185 public void statisticsEnabled() 186 { 187 statisticsEnabled = true; 188 } 189 190 public void managementEnabled() 191 { 192 managementEnabled = true; 193 } 194 195 public void statisticsDisabled() 196 { 197 statisticsEnabled = false; 198 } 199 200 public void managementDisabled() 201 { 202 managementEnabled = false; 203 } 204}