You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@syncope.apache.org by il...@apache.org on 2013/03/01 17:22:16 UTC
svn commit: r1451634 - in /syncope/trunk/core/src/main:
java/org/apache/syncope/core/util/VirAttrCache.java
java/org/apache/syncope/core/util/VirAttrCacheKey.java
resources/syncopeContext.xml
Author: ilgrosso
Date: Fri Mar 1 16:22:16 2013
New Revision: 1451634
URL: http://svn.apache.org/r1451634
Log:
[SYNCOPE-327] Removing cleanup thread for VirAttrCache
Modified:
syncope/trunk/core/src/main/java/org/apache/syncope/core/util/VirAttrCache.java
syncope/trunk/core/src/main/java/org/apache/syncope/core/util/VirAttrCacheKey.java
syncope/trunk/core/src/main/resources/syncopeContext.xml
Modified: syncope/trunk/core/src/main/java/org/apache/syncope/core/util/VirAttrCache.java
URL: http://svn.apache.org/viewvc/syncope/trunk/core/src/main/java/org/apache/syncope/core/util/VirAttrCache.java?rev=1451634&r1=1451633&r2=1451634&view=diff
==============================================================================
--- syncope/trunk/core/src/main/java/org/apache/syncope/core/util/VirAttrCache.java (original)
+++ syncope/trunk/core/src/main/java/org/apache/syncope/core/util/VirAttrCache.java Fri Mar 1 16:22:16 2013
@@ -24,8 +24,6 @@ import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
-import java.util.concurrent.Executors;
-import java.util.concurrent.TimeUnit;
import org.apache.syncope.common.types.AttributableType;
/**
@@ -44,72 +42,56 @@ public final class VirAttrCache {
private final int maxCacheSize;
/**
- * Clean period.
- */
- private final int cleanPeriod;
-
- /**
* Cache entries.
*/
private final Map<VirAttrCacheKey, VirAttrCacheValue> cache = new HashMap<VirAttrCacheKey, VirAttrCacheValue>();
- public VirAttrCache(final int ttl, final int maxCacheSize, final int cleanPeriod) {
+ public VirAttrCache(final int ttl, final int maxCacheSize) {
this.ttl = ttl;
this.maxCacheSize = maxCacheSize;
- this.cleanPeriod = cleanPeriod;
-
- Executors.newScheduledThreadPool(1).scheduleWithFixedDelay(
- new Runnable() {
-
- @Override
- public void run() {
- synchronized (cache) {
- freeCacheSpace(false);
- }
- }
- }, cleanPeriod, cleanPeriod, TimeUnit.MINUTES);
}
/**
* Cache virtual attribute values.
*
- * @param userId user id.
- * @param schemaName virtual attribute name.
- * @param values virtual attribute values.
+ * @param type user or role
+ * @param id user or role id
+ * @param schemaName virtual attribute name
+ * @param values virtual attribute values
*/
- public void put(
- final AttributableType type, final Long userId, final String schemaName, final List<String> values) {
+ public void put(final AttributableType type, final Long id, final String schemaName, final List<String> values) {
synchronized (cache) {
// this operations (retrieve cache space and put entry on) have to be thread safe.
-
- if (cache.size() >= maxCacheSize) {
- freeCacheSpace(true);
+ if (this.cache.size() >= this.maxCacheSize) {
+ free();
}
- cache.put(new VirAttrCacheKey(type, userId, schemaName), new VirAttrCacheValue(values));
+ cache.put(new VirAttrCacheKey(type, id, schemaName), new VirAttrCacheValue(values));
}
}
/**
* Retrieve cached value. Return null in case of virtual attribute not cached.
*
- * @param userId user id.
+ * @param type user or role
+ * @param id user or role id
* @param schemaName virtual attribute schema name.
* @return cached values or null in case of virtual attribute not found.
*/
- public List<String> get(final AttributableType type, final Long userId, final String schemaName) {
- final VirAttrCacheValue value = cache.get(new VirAttrCacheKey(type, userId, schemaName));
+ public List<String> get(final AttributableType type, final Long id, final String schemaName) {
+ final VirAttrCacheValue value = cache.get(new VirAttrCacheKey(type, id, schemaName));
return isValidEntry(value) ? value.getValues() : null;
}
/**
* Force entry expiring.
*
- * @param userId user id.
- * @param schemaName virtual attribute schema name.
+ * @param type user or role
+ * @param id user or role id
+ * @param schemaName virtual attribute schema name
*/
- public void expire(final AttributableType type, final Long userId, final String schemaName) {
- final VirAttrCacheValue value = cache.get(new VirAttrCacheKey(type, userId, schemaName));
+ public void expire(final AttributableType type, final Long id, final String schemaName) {
+ final VirAttrCacheValue value = cache.get(new VirAttrCacheKey(type, id, schemaName));
if (isValidEntry(value)) {
synchronized (cache) {
value.forceExpiring();
@@ -120,10 +102,8 @@ public final class VirAttrCache {
/**
* Remove expired entries if exist. If required, one entry at least (the latest recently used) will be taken off.
* This method is not thread safe: the caller have to take care to synchronize the call.
- *
- * @param forceEscape if TRUE the latest recently used entry at least will be taken off.
*/
- private void freeCacheSpace(final boolean forceEscape) {
+ private void free() {
final Set<VirAttrCacheKey> toBeRemoved = new HashSet<VirAttrCacheKey>();
Map.Entry<VirAttrCacheKey, VirAttrCacheValue> latest = null;
@@ -139,11 +119,11 @@ public final class VirAttrCache {
}
}
- if (toBeRemoved.isEmpty() && forceEscape) {
- // remove the oldest entry.
+ if (toBeRemoved.isEmpty() && latest != null) {
+ // remove the oldest entry
cache.remove(latest.getKey());
} else {
- // remove expired entries.
+ // remove expired entries
cache.keySet().removeAll(toBeRemoved);
}
}
Modified: syncope/trunk/core/src/main/java/org/apache/syncope/core/util/VirAttrCacheKey.java
URL: http://svn.apache.org/viewvc/syncope/trunk/core/src/main/java/org/apache/syncope/core/util/VirAttrCacheKey.java?rev=1451634&r1=1451633&r2=1451634&view=diff
==============================================================================
--- syncope/trunk/core/src/main/java/org/apache/syncope/core/util/VirAttrCacheKey.java (original)
+++ syncope/trunk/core/src/main/java/org/apache/syncope/core/util/VirAttrCacheKey.java Fri Mar 1 16:22:16 2013
@@ -18,6 +18,10 @@
*/
package org.apache.syncope.core.util;
+import org.apache.commons.lang.builder.EqualsBuilder;
+import org.apache.commons.lang.builder.HashCodeBuilder;
+import org.apache.commons.lang.builder.ReflectionToStringBuilder;
+import org.apache.commons.lang.builder.ToStringStyle;
import org.apache.syncope.common.types.AttributableType;
/**
@@ -38,36 +42,38 @@ public class VirAttrCacheKey {
/**
* Virtual attribute schema name.
*/
- private final transient String virAttrSchema;
+ private final transient String virSchema;
- public VirAttrCacheKey(final AttributableType type, final Long id, final String virAttrSchema) {
+ public VirAttrCacheKey(final AttributableType type, final Long id, final String virSchema) {
this.type = type;
this.id = id;
- this.virAttrSchema = virAttrSchema;
+ this.virSchema = virSchema;
+ }
+
+ public AttributableType getType() {
+ return type;
}
public Long getId() {
return id;
}
- public String getVirAttrSchema() {
- return virAttrSchema;
+ public String getVirSchema() {
+ return virSchema;
+ }
+
+ @Override
+ public boolean equals(final Object obj) {
+ return EqualsBuilder.reflectionEquals(this, obj);
}
@Override
public int hashCode() {
- return 31 * (31 * (31
- + (id == null ? 0 : id.hashCode()))
- + (virAttrSchema == null ? 0 : virAttrSchema.hashCode()))
- + (type == null ? 0 : type.hashCode());
+ return HashCodeBuilder.reflectionHashCode(this);
}
@Override
- public boolean equals(Object o) {
- return o != null && o instanceof VirAttrCacheKey
- && ((id == null && ((VirAttrCacheKey) o).getId() == null)
- || id.equals(((VirAttrCacheKey) o).getId()))
- && ((virAttrSchema == null && ((VirAttrCacheKey) o).getVirAttrSchema() == null)
- || virAttrSchema.equals(((VirAttrCacheKey) o).getVirAttrSchema()));
+ public String toString() {
+ return ReflectionToStringBuilder.toString(this, ToStringStyle.MULTI_LINE_STYLE);
}
}
Modified: syncope/trunk/core/src/main/resources/syncopeContext.xml
URL: http://svn.apache.org/viewvc/syncope/trunk/core/src/main/resources/syncopeContext.xml?rev=1451634&r1=1451633&r2=1451634&view=diff
==============================================================================
--- syncope/trunk/core/src/main/resources/syncopeContext.xml (original)
+++ syncope/trunk/core/src/main/resources/syncopeContext.xml Fri Mar 1 16:22:16 2013
@@ -94,6 +94,5 @@ under the License.
<bean id="virAttrCache" class="org.apache.syncope.core.util.VirAttrCache" scope="singleton">
<constructor-arg value="60"/>
<constructor-arg value="5000"/>
- <constructor-arg value="5"/>
</bean>
</beans>