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.jcs.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 private final Class<K> keyType; 052 private final Class<V> valueType; 053 private final boolean storeByValue; 054 private final boolean readThrough; 055 private final boolean writeThrough; 056 private final Factory<CacheLoader<K, V>> cacheLoaderFactory; 057 private final Factory<CacheWriter<? super K, ? super V>> cacheWristerFactory; 058 private final Factory<ExpiryPolicy> expiryPolicyFactory; 059 private final Set<CacheEntryListenerConfiguration<K, V>> cacheEntryListenerConfigurations; 060 061 private volatile boolean statisticsEnabled; 062 private volatile boolean managementEnabled; 063 064 public JCSConfiguration(final Configuration<K, V> configuration, final Class<K> keyType, final Class<V> valueType) 065 { 066 this.keyType = keyType; 067 this.valueType = valueType; 068 if (configuration instanceof CompleteConfiguration) 069 { 070 final CompleteConfiguration<K, V> cConfiguration = (CompleteConfiguration<K, V>) configuration; 071 storeByValue = configuration.isStoreByValue(); 072 readThrough = cConfiguration.isReadThrough(); 073 writeThrough = cConfiguration.isWriteThrough(); 074 statisticsEnabled = cConfiguration.isStatisticsEnabled(); 075 managementEnabled = cConfiguration.isManagementEnabled(); 076 cacheLoaderFactory = cConfiguration.getCacheLoaderFactory(); 077 cacheWristerFactory = cConfiguration.getCacheWriterFactory(); 078 this.expiryPolicyFactory = cConfiguration.getExpiryPolicyFactory(); 079 cacheEntryListenerConfigurations = new HashSet<CacheEntryListenerConfiguration<K, V>>(); 080 081 final Iterable<CacheEntryListenerConfiguration<K, V>> entryListenerConfigurations = cConfiguration 082 .getCacheEntryListenerConfigurations(); 083 if (entryListenerConfigurations != null) 084 { 085 for (final CacheEntryListenerConfiguration<K, V> kvCacheEntryListenerConfiguration : entryListenerConfigurations) 086 { 087 cacheEntryListenerConfigurations.add(kvCacheEntryListenerConfiguration); 088 } 089 } 090 } 091 else 092 { 093 expiryPolicyFactory = EternalExpiryPolicy.factoryOf(); 094 storeByValue = true; 095 readThrough = false; 096 writeThrough = false; 097 statisticsEnabled = false; 098 managementEnabled = false; 099 cacheLoaderFactory = null; 100 cacheWristerFactory = null; 101 cacheEntryListenerConfigurations = new HashSet<CacheEntryListenerConfiguration<K, V>>(); 102 } 103 } 104 105 @Override 106 public Class<K> getKeyType() 107 { 108 return keyType == null ? (Class<K>) Object.class : keyType; 109 } 110 111 @Override 112 public Class<V> getValueType() 113 { 114 return valueType == null ? (Class<V>) Object.class : valueType; 115 } 116 117 @Override 118 public boolean isStoreByValue() 119 { 120 return storeByValue; 121 } 122 123 @Override 124 public boolean isReadThrough() 125 { 126 return readThrough; 127 } 128 129 @Override 130 public boolean isWriteThrough() 131 { 132 return writeThrough; 133 } 134 135 @Override 136 public boolean isStatisticsEnabled() 137 { 138 return statisticsEnabled; 139 } 140 141 @Override 142 public boolean isManagementEnabled() 143 { 144 return managementEnabled; 145 } 146 147 @Override 148 public Iterable<CacheEntryListenerConfiguration<K, V>> getCacheEntryListenerConfigurations() 149 { 150 return Collections.unmodifiableSet(cacheEntryListenerConfigurations); 151 } 152 153 @Override 154 public Factory<CacheLoader<K, V>> getCacheLoaderFactory() 155 { 156 return cacheLoaderFactory; 157 } 158 159 @Override 160 public Factory<CacheWriter<? super K, ? super V>> getCacheWriterFactory() 161 { 162 return cacheWristerFactory; 163 } 164 165 @Override 166 public Factory<ExpiryPolicy> getExpiryPolicyFactory() 167 { 168 return expiryPolicyFactory; 169 } 170 171 public synchronized void addListener(final CacheEntryListenerConfiguration<K, V> cacheEntryListenerConfiguration) 172 { 173 cacheEntryListenerConfigurations.add(cacheEntryListenerConfiguration); 174 } 175 176 public synchronized void removeListener(final CacheEntryListenerConfiguration<K, V> cacheEntryListenerConfiguration) 177 { 178 cacheEntryListenerConfigurations.remove(cacheEntryListenerConfiguration); 179 } 180 181 public void statisticsEnabled() 182 { 183 statisticsEnabled = true; 184 } 185 186 public void managementEnabled() 187 { 188 managementEnabled = true; 189 } 190 191 public void statisticsDisabled() 192 { 193 statisticsEnabled = false; 194 } 195 196 public void managementDisabled() 197 { 198 managementEnabled = false; 199 } 200}