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 2014/12/22 14:41:43 UTC

[1/3] syncope git commit: [SYNCOPE-622] Extracted interface, provided defalt and disabled implementations

Repository: syncope
Updated Branches:
  refs/heads/1_2_X 386b86a79 -> 96015df1b
  refs/heads/master 9c8f5b9df -> f8e3da0f0


[SYNCOPE-622] Extracted interface, provided defalt and disabled implementations


Project: http://git-wip-us.apache.org/repos/asf/syncope/repo
Commit: http://git-wip-us.apache.org/repos/asf/syncope/commit/96015df1
Tree: http://git-wip-us.apache.org/repos/asf/syncope/tree/96015df1
Diff: http://git-wip-us.apache.org/repos/asf/syncope/diff/96015df1

Branch: refs/heads/1_2_X
Commit: 96015df1b93cf14384445422baafa63f75806d9c
Parents: 386b86a
Author: Francesco Chicchiriccò <il...@apache.org>
Authored: Mon Dec 22 14:41:25 2014 +0100
Committer: Francesco Chicchiriccò <il...@apache.org>
Committed: Mon Dec 22 14:41:25 2014 +0100

----------------------------------------------------------------------
 .../core/cache/DisabledVirAttrCache.java        |  50 +++++++
 .../syncope/core/cache/MemoryVirAttrCache.java  | 148 +++++++++++++++++++
 .../apache/syncope/core/cache/VirAttrCache.java |  65 ++++++++
 .../syncope/core/cache/VirAttrCacheKey.java     |  79 ++++++++++
 .../syncope/core/cache/VirAttrCacheValue.java   |  86 +++++++++++
 .../syncope/core/connid/ConnObjectUtil.java     |   4 +-
 .../apache/syncope/core/util/MappingUtil.java   |   7 +-
 .../apache/syncope/core/util/VirAttrCache.java  | 144 ------------------
 .../syncope/core/util/VirAttrCacheKey.java      |  79 ----------
 .../syncope/core/util/VirAttrCacheValue.java    |  86 -----------
 core/src/main/resources/coreContext.xml         |   2 +-
 11 files changed, 435 insertions(+), 315 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/syncope/blob/96015df1/core/src/main/java/org/apache/syncope/core/cache/DisabledVirAttrCache.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/syncope/core/cache/DisabledVirAttrCache.java b/core/src/main/java/org/apache/syncope/core/cache/DisabledVirAttrCache.java
new file mode 100644
index 0000000..4e343a3
--- /dev/null
+++ b/core/src/main/java/org/apache/syncope/core/cache/DisabledVirAttrCache.java
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.syncope.core.cache;
+
+import org.apache.syncope.common.types.AttributableType;
+
+/**
+ * Empty virtual attribute value cache implementation.
+ */
+public class DisabledVirAttrCache implements VirAttrCache {
+
+    @Override
+    public void expire(final AttributableType type, final Long id, final String schemaName) {
+        // nothing to do
+    }
+
+    @Override
+    public VirAttrCacheValue get(final AttributableType type, final Long id, final String schemaName) {
+        return null;
+    }
+
+    @Override
+    public boolean isValidEntry(VirAttrCacheValue value) {
+        return false;
+    }
+
+    @Override
+    public void put(
+            final AttributableType type, final Long id, final String schemaName, final VirAttrCacheValue value) {
+
+        // nothing to do
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/syncope/blob/96015df1/core/src/main/java/org/apache/syncope/core/cache/MemoryVirAttrCache.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/syncope/core/cache/MemoryVirAttrCache.java b/core/src/main/java/org/apache/syncope/core/cache/MemoryVirAttrCache.java
new file mode 100644
index 0000000..b48ca10
--- /dev/null
+++ b/core/src/main/java/org/apache/syncope/core/cache/MemoryVirAttrCache.java
@@ -0,0 +1,148 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.syncope.core.cache;
+
+import java.util.Date;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import org.apache.syncope.common.types.AttributableType;
+
+/**
+ * In-memory (HashMap) virtual attribute value cache implementation.
+ */
+public class MemoryVirAttrCache implements VirAttrCache {
+
+    /**
+     * Elapsed time in seconds.
+     */
+    protected int ttl;
+
+    /**
+     * Max cache size.
+     */
+    protected int maxCacheSize;
+
+    /**
+     * Cache entries.
+     */
+    protected final Map<VirAttrCacheKey, VirAttrCacheValue> cache = new HashMap<VirAttrCacheKey, VirAttrCacheValue>();
+
+    public MemoryVirAttrCache(final int ttl, final int maxCacheSize) {
+        this.ttl = ttl;
+        this.maxCacheSize = maxCacheSize;
+    }
+
+    /**
+     * Cache virtual attribute values.
+     *
+     * @param type user or role
+     * @param id user or role id
+     * @param schemaName virtual attribute name
+     * @param value virtual attribute values
+     */
+    @Override
+    public void put(
+            final AttributableType type,
+            final Long id,
+            final String schemaName,
+            final VirAttrCacheValue value) {
+
+        synchronized (cache) {
+            // this operations (retrieve cache space and put entry on) have to be thread safe.
+            if (this.cache.size() >= this.maxCacheSize) {
+                free();
+            }
+
+            cache.put(new VirAttrCacheKey(type, id, schemaName), value);
+        }
+    }
+
+    /**
+     * Retrieve cached value. Return null in case of virtual attribute not cached.
+     *
+     * @param type user or role
+     * @param id user or role id
+     * @param schemaName virtual attribute schema name.
+     * @return cached values or null if virtual attribute is not cached.
+     */
+    @Override
+    public VirAttrCacheValue get(final AttributableType type, final Long id, final String schemaName) {
+        return cache.get(new VirAttrCacheKey(type, id, schemaName));
+    }
+
+    /**
+     * Force entry expiring.
+     *
+     * @param type user or role
+     * @param id user or role id
+     * @param schemaName virtual attribute schema name
+     */
+    @Override
+    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();
+            }
+        }
+    }
+
+    /**
+     * 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.
+     */
+    private void free() {
+        final Set<VirAttrCacheKey> toBeRemoved = new HashSet<VirAttrCacheKey>();
+
+        Map.Entry<VirAttrCacheKey, VirAttrCacheValue> latest = null;
+
+        for (Map.Entry<VirAttrCacheKey, VirAttrCacheValue> entry : cache.entrySet()) {
+            if (isValidEntry(entry.getValue())) {
+                final Date date = entry.getValue().getLastAccessDate();
+                if (latest == null || latest.getValue().getLastAccessDate().after(date)) {
+                    latest = entry;
+                }
+            } else {
+                toBeRemoved.add(entry.getKey());
+            }
+        }
+
+        if (toBeRemoved.isEmpty() && latest != null) {
+            // remove the oldest entry
+            cache.remove(latest.getKey());
+        } else {
+            // remove expired entries
+            cache.keySet().removeAll(toBeRemoved);
+        }
+    }
+
+    /**
+     * Cache entry is valid if and only if value exist and it is not expired.
+     *
+     * @param value cache entry value.
+     * @return TRUE if the value is valid; FALSE otherwise.
+     */
+    @Override
+    public boolean isValidEntry(final VirAttrCacheValue value) {
+        final Date expiringDate = new Date(value == null ? 0 : value.getCreationDate().getTime() + ttl * 1000);
+        return expiringDate.after(new Date());
+    }
+}

http://git-wip-us.apache.org/repos/asf/syncope/blob/96015df1/core/src/main/java/org/apache/syncope/core/cache/VirAttrCache.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/syncope/core/cache/VirAttrCache.java b/core/src/main/java/org/apache/syncope/core/cache/VirAttrCache.java
new file mode 100644
index 0000000..723eb1d
--- /dev/null
+++ b/core/src/main/java/org/apache/syncope/core/cache/VirAttrCache.java
@@ -0,0 +1,65 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.syncope.core.cache;
+
+import org.apache.syncope.common.types.AttributableType;
+
+/**
+ * Virtual Attribute Value cache.
+ */
+public interface VirAttrCache {
+
+    /**
+     * Force entry expiring.
+     *
+     * @param type user or role
+     * @param id user or role id
+     * @param schemaName virtual attribute schema name
+     */
+    void expire(AttributableType type, Long id, String schemaName);
+
+    /**
+     * Retrieve cached value. Return null in case of virtual attribute not cached.
+     *
+     * @param type user or role
+     * @param id user or role id
+     * @param schemaName virtual attribute schema name.
+     * @return cached values or null if virtual attribute is not cached.
+     */
+    VirAttrCacheValue get(AttributableType type, Long id, String schemaName);
+
+    /**
+     * Cache entry is valid if and only if value exist and it is not expired.
+     *
+     * @param value cache entry value.
+     * @return TRUE if the value is valid; FALSE otherwise.
+     */
+    boolean isValidEntry(VirAttrCacheValue value);
+
+    /**
+     * Cache virtual attribute values.
+     *
+     * @param type user or role
+     * @param id user or role id
+     * @param schemaName virtual attribute name
+     * @param value virtual attribute values
+     */
+    void put(AttributableType type, Long id, String schemaName, VirAttrCacheValue value);
+
+}

http://git-wip-us.apache.org/repos/asf/syncope/blob/96015df1/core/src/main/java/org/apache/syncope/core/cache/VirAttrCacheKey.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/syncope/core/cache/VirAttrCacheKey.java b/core/src/main/java/org/apache/syncope/core/cache/VirAttrCacheKey.java
new file mode 100644
index 0000000..b22a278
--- /dev/null
+++ b/core/src/main/java/org/apache/syncope/core/cache/VirAttrCacheKey.java
@@ -0,0 +1,79 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.syncope.core.cache;
+
+import org.apache.commons.lang3.builder.EqualsBuilder;
+import org.apache.commons.lang3.builder.HashCodeBuilder;
+import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
+import org.apache.commons.lang3.builder.ToStringStyle;
+import org.apache.syncope.common.types.AttributableType;
+
+/**
+ * Ccahe entry key.
+ */
+public class VirAttrCacheKey {
+
+    /**
+     * Subject type.
+     */
+    private final AttributableType type;
+
+    /**
+     * Subject ID.
+     */
+    private final transient Long id;
+
+    /**
+     * Virtual attribute schema name.
+     */
+    private final transient String virSchema;
+
+    public VirAttrCacheKey(final AttributableType type, final Long id, final String virSchema) {
+        this.type = type;
+        this.id = id;
+        this.virSchema = virSchema;
+    }
+
+    public AttributableType getType() {
+        return type;
+    }
+
+    public Long getId() {
+        return id;
+    }
+
+    public String getVirSchema() {
+        return virSchema;
+    }
+
+    @Override
+    public boolean equals(final Object obj) {
+        return EqualsBuilder.reflectionEquals(this, obj, true);
+    }
+
+    @Override
+    public int hashCode() {
+        return HashCodeBuilder.reflectionHashCode(this, true);
+    }
+
+    @Override
+    public String toString() {
+        return ReflectionToStringBuilder.toString(this, ToStringStyle.MULTI_LINE_STYLE, true);
+    }
+}

http://git-wip-us.apache.org/repos/asf/syncope/blob/96015df1/core/src/main/java/org/apache/syncope/core/cache/VirAttrCacheValue.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/syncope/core/cache/VirAttrCacheValue.java b/core/src/main/java/org/apache/syncope/core/cache/VirAttrCacheValue.java
new file mode 100644
index 0000000..5a634f2
--- /dev/null
+++ b/core/src/main/java/org/apache/syncope/core/cache/VirAttrCacheValue.java
@@ -0,0 +1,86 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.syncope.core.cache;
+
+import java.util.Date;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * Cache entry value.
+ */
+public class VirAttrCacheValue {
+
+    /**
+     * Virtual attribute values.
+     */
+    private final Map<String, Set<String>> values;
+
+    /**
+     * Entry creation date.
+     */
+    private Date creationDate;
+
+    /**
+     * Entry access date.
+     */
+    private Date lastAccessDate;
+
+    public VirAttrCacheValue() {
+        this.creationDate = new Date();
+        this.lastAccessDate = new Date();
+        values = new HashMap<String, Set<String>>();
+    }
+
+    public void setResourceValues(final String resourceName, final Set<String> values) {
+        this.values.put(resourceName, values);
+    }
+
+    public Date getCreationDate() {
+        return creationDate;
+    }
+
+    public void forceExpiring() {
+        creationDate = new Date(0);
+    }
+
+    public Set<String> getValues(final String resourceName) {
+        return values.get(resourceName);
+    }
+
+    public Set<String> getValues() {
+        final Set<String> res = new HashSet<String>();
+
+        for (Set<String> value : values.values()) {
+            res.addAll(value);
+        }
+
+        return res;
+    }
+
+    public Date getLastAccessDate() {
+        return lastAccessDate;
+    }
+
+    void setLastAccessDate(final Date lastAccessDate) {
+        this.lastAccessDate = lastAccessDate;
+    }
+}

http://git-wip-us.apache.org/repos/asf/syncope/blob/96015df1/core/src/main/java/org/apache/syncope/core/connid/ConnObjectUtil.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/syncope/core/connid/ConnObjectUtil.java b/core/src/main/java/org/apache/syncope/core/connid/ConnObjectUtil.java
index 519c931..814e83b 100644
--- a/core/src/main/java/org/apache/syncope/core/connid/ConnObjectUtil.java
+++ b/core/src/main/java/org/apache/syncope/core/connid/ConnObjectUtil.java
@@ -69,8 +69,8 @@ import org.apache.syncope.core.util.AttributableUtil;
 import org.apache.syncope.core.util.InvalidPasswordPolicySpecException;
 import org.apache.syncope.core.util.MappingUtil;
 import org.apache.syncope.core.util.SecureRandomUtil;
-import org.apache.syncope.core.util.VirAttrCache;
-import org.apache.syncope.core.util.VirAttrCacheValue;
+import org.apache.syncope.core.cache.VirAttrCache;
+import org.apache.syncope.core.cache.VirAttrCacheValue;
 import org.apache.syncope.core.util.jexl.JexlUtil;
 import org.identityconnectors.common.Base64;
 import org.identityconnectors.common.security.GuardedByteArray;

http://git-wip-us.apache.org/repos/asf/syncope/blob/96015df1/core/src/main/java/org/apache/syncope/core/util/MappingUtil.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/syncope/core/util/MappingUtil.java b/core/src/main/java/org/apache/syncope/core/util/MappingUtil.java
index 77cacdf..f6dc331 100644
--- a/core/src/main/java/org/apache/syncope/core/util/MappingUtil.java
+++ b/core/src/main/java/org/apache/syncope/core/util/MappingUtil.java
@@ -35,6 +35,7 @@ import org.apache.syncope.common.types.AttributableType;
 import org.apache.syncope.common.types.IntMappingType;
 import org.apache.syncope.common.types.AttributeSchemaType;
 import org.apache.syncope.common.types.MappingPurpose;
+import org.apache.syncope.core.cache.VirAttrCache;
 import org.apache.syncope.core.connid.ConnObjectUtil;
 import org.apache.syncope.core.connid.PasswordGenerator;
 import org.apache.syncope.core.persistence.beans.AbstractAttr;
@@ -208,7 +209,7 @@ public final class MappingUtil {
             }
         }
 
-        final Attribute accountIdExtAttr = 
+        final Attribute accountIdExtAttr =
                 AttributeUtil.find(attrUtil.getAccountIdItem(resource).getExtAttrName(), attributes);
         if (accountIdExtAttr != null) {
             attributes.remove(accountIdExtAttr);
@@ -377,8 +378,8 @@ public final class MappingUtil {
                 } else {
                     result = new AbstractMap.SimpleEntry<String, Attribute>(
                             null, objValues.isEmpty()
-                            ? AttributeBuilder.build(extAttrName)
-                            : AttributeBuilder.build(extAttrName, objValues.iterator().next()));
+                                    ? AttributeBuilder.build(extAttrName)
+                                    : AttributeBuilder.build(extAttrName, objValues.iterator().next()));
                 }
             }
         }

http://git-wip-us.apache.org/repos/asf/syncope/blob/96015df1/core/src/main/java/org/apache/syncope/core/util/VirAttrCache.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/syncope/core/util/VirAttrCache.java b/core/src/main/java/org/apache/syncope/core/util/VirAttrCache.java
deleted file mode 100644
index 10b9a6c..0000000
--- a/core/src/main/java/org/apache/syncope/core/util/VirAttrCache.java
+++ /dev/null
@@ -1,144 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.syncope.core.util;
-
-import java.util.Date;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Map;
-import java.util.Set;
-import org.apache.syncope.common.types.AttributableType;
-
-/**
- * Virtual Attribute Value cache.
- */
-public final class VirAttrCache {
-
-    /**
-     * Elapsed time in seconds.
-     */
-    private final int ttl;
-
-    /**
-     * Max cache size.
-     */
-    private final int maxCacheSize;
-
-    /**
-     * Cache entries.
-     */
-    private final Map<VirAttrCacheKey, VirAttrCacheValue> cache = new HashMap<VirAttrCacheKey, VirAttrCacheValue>();
-
-    public VirAttrCache(final int ttl, final int maxCacheSize) {
-        this.ttl = ttl;
-        this.maxCacheSize = maxCacheSize;
-    }
-
-    /**
-     * Cache virtual attribute values.
-     *
-     * @param type user or role
-     * @param id user or role id
-     * @param schemaName virtual attribute name
-     * @param value virtual attribute values
-     */
-    public void put(
-            final AttributableType type,
-            final Long id,
-            final String schemaName,
-            final VirAttrCacheValue value) {
-
-        synchronized (cache) {
-            // this operations (retrieve cache space and put entry on) have to be thread safe.
-            if (this.cache.size() >= this.maxCacheSize) {
-                free();
-            }
-
-            cache.put(new VirAttrCacheKey(type, id, schemaName), value);
-        }
-    }
-
-    /**
-     * Retrieve cached value. Return null in case of virtual attribute not cached.
-     *
-     * @param type user or role
-     * @param id user or role id
-     * @param schemaName virtual attribute schema name.
-     * @return cached values or null if virtual attribute is not cached.
-     */
-    public VirAttrCacheValue get(final AttributableType type, final Long id, final String schemaName) {
-        return cache.get(new VirAttrCacheKey(type, id, schemaName));
-    }
-
-    /**
-     * Force entry expiring.
-     *
-     * @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 id, final String schemaName) {
-        final VirAttrCacheValue value = cache.get(new VirAttrCacheKey(type, id, schemaName));
-        if (isValidEntry(value)) {
-            synchronized (cache) {
-                value.forceExpiring();
-            }
-        }
-    }
-
-    /**
-     * 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.
-     */
-    private void free() {
-        final Set<VirAttrCacheKey> toBeRemoved = new HashSet<VirAttrCacheKey>();
-
-        Map.Entry<VirAttrCacheKey, VirAttrCacheValue> latest = null;
-
-        for (Map.Entry<VirAttrCacheKey, VirAttrCacheValue> entry : cache.entrySet()) {
-            if (isValidEntry(entry.getValue())) {
-                final Date date = entry.getValue().getLastAccessDate();
-                if (latest == null || latest.getValue().getLastAccessDate().after(date)) {
-                    latest = entry;
-                }
-            } else {
-                toBeRemoved.add(entry.getKey());
-            }
-        }
-
-        if (toBeRemoved.isEmpty() && latest != null) {
-            // remove the oldest entry
-            cache.remove(latest.getKey());
-        } else {
-            // remove expired entries
-            cache.keySet().removeAll(toBeRemoved);
-        }
-    }
-
-    /**
-     * Cache entry is valid if and only if value exist and it is not expired.
-     *
-     * @param value cache entry value.
-     * @return TRUE if the value is valid; FALSE otherwise.
-     */
-    public boolean isValidEntry(final VirAttrCacheValue value) {
-        final Date expiringDate = new Date(value == null ? 0 : value.getCreationDate().getTime() + ttl * 1000);
-        return expiringDate.after(new Date());
-    }
-}

http://git-wip-us.apache.org/repos/asf/syncope/blob/96015df1/core/src/main/java/org/apache/syncope/core/util/VirAttrCacheKey.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/syncope/core/util/VirAttrCacheKey.java b/core/src/main/java/org/apache/syncope/core/util/VirAttrCacheKey.java
deleted file mode 100644
index e68f4c0..0000000
--- a/core/src/main/java/org/apache/syncope/core/util/VirAttrCacheKey.java
+++ /dev/null
@@ -1,79 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.syncope.core.util;
-
-import org.apache.commons.lang3.builder.EqualsBuilder;
-import org.apache.commons.lang3.builder.HashCodeBuilder;
-import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
-import org.apache.commons.lang3.builder.ToStringStyle;
-import org.apache.syncope.common.types.AttributableType;
-
-/**
- * Ccahe entry key.
- */
-public class VirAttrCacheKey {
-
-    /**
-     * Subject type.
-     */
-    private final AttributableType type;
-
-    /**
-     * Subject ID.
-     */
-    private final transient Long id;
-
-    /**
-     * Virtual attribute schema name.
-     */
-    private final transient String virSchema;
-
-    public VirAttrCacheKey(final AttributableType type, final Long id, final String virSchema) {
-        this.type = type;
-        this.id = id;
-        this.virSchema = virSchema;
-    }
-
-    public AttributableType getType() {
-        return type;
-    }
-
-    public Long getId() {
-        return id;
-    }
-
-    public String getVirSchema() {
-        return virSchema;
-    }
-
-    @Override
-    public boolean equals(final Object obj) {
-        return EqualsBuilder.reflectionEquals(this, obj, true);
-    }
-
-    @Override
-    public int hashCode() {
-        return HashCodeBuilder.reflectionHashCode(this, true);
-    }
-
-    @Override
-    public String toString() {
-        return ReflectionToStringBuilder.toString(this, ToStringStyle.MULTI_LINE_STYLE, true);
-    }
-}

http://git-wip-us.apache.org/repos/asf/syncope/blob/96015df1/core/src/main/java/org/apache/syncope/core/util/VirAttrCacheValue.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/syncope/core/util/VirAttrCacheValue.java b/core/src/main/java/org/apache/syncope/core/util/VirAttrCacheValue.java
deleted file mode 100644
index bca5acf..0000000
--- a/core/src/main/java/org/apache/syncope/core/util/VirAttrCacheValue.java
+++ /dev/null
@@ -1,86 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.syncope.core.util;
-
-import java.util.Date;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Map;
-import java.util.Set;
-
-/**
- * Cache entry value.
- */
-public class VirAttrCacheValue {
-
-    /**
-     * Virtual attribute values.
-     */
-    private final Map<String, Set<String>> values;
-
-    /**
-     * Entry creation date.
-     */
-    private Date creationDate;
-
-    /**
-     * Entry access date.
-     */
-    private Date lastAccessDate;
-
-    public VirAttrCacheValue() {
-        this.creationDate = new Date();
-        this.lastAccessDate = new Date();
-        values = new HashMap<String, Set<String>>();
-    }
-
-    public void setResourceValues(final String resourceName, final Set<String> values) {
-        this.values.put(resourceName, values);
-    }
-
-    public Date getCreationDate() {
-        return creationDate;
-    }
-
-    public void forceExpiring() {
-        creationDate = new Date(0);
-    }
-
-    public Set<String> getValues(final String resourceName) {
-        return values.get(resourceName);
-    }
-
-    public Set<String> getValues() {
-        final Set<String> res = new HashSet<String>();
-
-        for (Set<String> value : values.values()) {
-            res.addAll(value);
-        }
-
-        return res;
-    }
-
-    public Date getLastAccessDate() {
-        return lastAccessDate;
-    }
-
-    void setLastAccessDate(Date lastAccessDate) {
-        this.lastAccessDate = lastAccessDate;
-    }
-}

http://git-wip-us.apache.org/repos/asf/syncope/blob/96015df1/core/src/main/resources/coreContext.xml
----------------------------------------------------------------------
diff --git a/core/src/main/resources/coreContext.xml b/core/src/main/resources/coreContext.xml
index 9b12622..9bb146b 100644
--- a/core/src/main/resources/coreContext.xml
+++ b/core/src/main/resources/coreContext.xml
@@ -141,7 +141,7 @@ under the License.
   <task:annotation-driven executor="connectorExecutor"/>
   <task:executor id="connectorExecutor" pool-size="10"/>
   
-  <bean id="virAttrCache" class="org.apache.syncope.core.util.VirAttrCache" scope="singleton">
+  <bean id="virAttrCache" class="org.apache.syncope.core.cache.MemoryVirAttrCache" scope="singleton">
     <constructor-arg value="60"/>
     <constructor-arg value="5000"/>
   </bean>


[2/3] syncope git commit: [SYNCOPE-622] Extracted interface, provided defalt and disabled implementations

Posted by il...@apache.org.
[SYNCOPE-622] Extracted interface, provided defalt and disabled implementations


Project: http://git-wip-us.apache.org/repos/asf/syncope/repo
Commit: http://git-wip-us.apache.org/repos/asf/syncope/commit/96015df1
Tree: http://git-wip-us.apache.org/repos/asf/syncope/tree/96015df1
Diff: http://git-wip-us.apache.org/repos/asf/syncope/diff/96015df1

Branch: refs/heads/master
Commit: 96015df1b93cf14384445422baafa63f75806d9c
Parents: 386b86a
Author: Francesco Chicchiriccò <il...@apache.org>
Authored: Mon Dec 22 14:41:25 2014 +0100
Committer: Francesco Chicchiriccò <il...@apache.org>
Committed: Mon Dec 22 14:41:25 2014 +0100

----------------------------------------------------------------------
 .../core/cache/DisabledVirAttrCache.java        |  50 +++++++
 .../syncope/core/cache/MemoryVirAttrCache.java  | 148 +++++++++++++++++++
 .../apache/syncope/core/cache/VirAttrCache.java |  65 ++++++++
 .../syncope/core/cache/VirAttrCacheKey.java     |  79 ++++++++++
 .../syncope/core/cache/VirAttrCacheValue.java   |  86 +++++++++++
 .../syncope/core/connid/ConnObjectUtil.java     |   4 +-
 .../apache/syncope/core/util/MappingUtil.java   |   7 +-
 .../apache/syncope/core/util/VirAttrCache.java  | 144 ------------------
 .../syncope/core/util/VirAttrCacheKey.java      |  79 ----------
 .../syncope/core/util/VirAttrCacheValue.java    |  86 -----------
 core/src/main/resources/coreContext.xml         |   2 +-
 11 files changed, 435 insertions(+), 315 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/syncope/blob/96015df1/core/src/main/java/org/apache/syncope/core/cache/DisabledVirAttrCache.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/syncope/core/cache/DisabledVirAttrCache.java b/core/src/main/java/org/apache/syncope/core/cache/DisabledVirAttrCache.java
new file mode 100644
index 0000000..4e343a3
--- /dev/null
+++ b/core/src/main/java/org/apache/syncope/core/cache/DisabledVirAttrCache.java
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.syncope.core.cache;
+
+import org.apache.syncope.common.types.AttributableType;
+
+/**
+ * Empty virtual attribute value cache implementation.
+ */
+public class DisabledVirAttrCache implements VirAttrCache {
+
+    @Override
+    public void expire(final AttributableType type, final Long id, final String schemaName) {
+        // nothing to do
+    }
+
+    @Override
+    public VirAttrCacheValue get(final AttributableType type, final Long id, final String schemaName) {
+        return null;
+    }
+
+    @Override
+    public boolean isValidEntry(VirAttrCacheValue value) {
+        return false;
+    }
+
+    @Override
+    public void put(
+            final AttributableType type, final Long id, final String schemaName, final VirAttrCacheValue value) {
+
+        // nothing to do
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/syncope/blob/96015df1/core/src/main/java/org/apache/syncope/core/cache/MemoryVirAttrCache.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/syncope/core/cache/MemoryVirAttrCache.java b/core/src/main/java/org/apache/syncope/core/cache/MemoryVirAttrCache.java
new file mode 100644
index 0000000..b48ca10
--- /dev/null
+++ b/core/src/main/java/org/apache/syncope/core/cache/MemoryVirAttrCache.java
@@ -0,0 +1,148 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.syncope.core.cache;
+
+import java.util.Date;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import org.apache.syncope.common.types.AttributableType;
+
+/**
+ * In-memory (HashMap) virtual attribute value cache implementation.
+ */
+public class MemoryVirAttrCache implements VirAttrCache {
+
+    /**
+     * Elapsed time in seconds.
+     */
+    protected int ttl;
+
+    /**
+     * Max cache size.
+     */
+    protected int maxCacheSize;
+
+    /**
+     * Cache entries.
+     */
+    protected final Map<VirAttrCacheKey, VirAttrCacheValue> cache = new HashMap<VirAttrCacheKey, VirAttrCacheValue>();
+
+    public MemoryVirAttrCache(final int ttl, final int maxCacheSize) {
+        this.ttl = ttl;
+        this.maxCacheSize = maxCacheSize;
+    }
+
+    /**
+     * Cache virtual attribute values.
+     *
+     * @param type user or role
+     * @param id user or role id
+     * @param schemaName virtual attribute name
+     * @param value virtual attribute values
+     */
+    @Override
+    public void put(
+            final AttributableType type,
+            final Long id,
+            final String schemaName,
+            final VirAttrCacheValue value) {
+
+        synchronized (cache) {
+            // this operations (retrieve cache space and put entry on) have to be thread safe.
+            if (this.cache.size() >= this.maxCacheSize) {
+                free();
+            }
+
+            cache.put(new VirAttrCacheKey(type, id, schemaName), value);
+        }
+    }
+
+    /**
+     * Retrieve cached value. Return null in case of virtual attribute not cached.
+     *
+     * @param type user or role
+     * @param id user or role id
+     * @param schemaName virtual attribute schema name.
+     * @return cached values or null if virtual attribute is not cached.
+     */
+    @Override
+    public VirAttrCacheValue get(final AttributableType type, final Long id, final String schemaName) {
+        return cache.get(new VirAttrCacheKey(type, id, schemaName));
+    }
+
+    /**
+     * Force entry expiring.
+     *
+     * @param type user or role
+     * @param id user or role id
+     * @param schemaName virtual attribute schema name
+     */
+    @Override
+    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();
+            }
+        }
+    }
+
+    /**
+     * 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.
+     */
+    private void free() {
+        final Set<VirAttrCacheKey> toBeRemoved = new HashSet<VirAttrCacheKey>();
+
+        Map.Entry<VirAttrCacheKey, VirAttrCacheValue> latest = null;
+
+        for (Map.Entry<VirAttrCacheKey, VirAttrCacheValue> entry : cache.entrySet()) {
+            if (isValidEntry(entry.getValue())) {
+                final Date date = entry.getValue().getLastAccessDate();
+                if (latest == null || latest.getValue().getLastAccessDate().after(date)) {
+                    latest = entry;
+                }
+            } else {
+                toBeRemoved.add(entry.getKey());
+            }
+        }
+
+        if (toBeRemoved.isEmpty() && latest != null) {
+            // remove the oldest entry
+            cache.remove(latest.getKey());
+        } else {
+            // remove expired entries
+            cache.keySet().removeAll(toBeRemoved);
+        }
+    }
+
+    /**
+     * Cache entry is valid if and only if value exist and it is not expired.
+     *
+     * @param value cache entry value.
+     * @return TRUE if the value is valid; FALSE otherwise.
+     */
+    @Override
+    public boolean isValidEntry(final VirAttrCacheValue value) {
+        final Date expiringDate = new Date(value == null ? 0 : value.getCreationDate().getTime() + ttl * 1000);
+        return expiringDate.after(new Date());
+    }
+}

http://git-wip-us.apache.org/repos/asf/syncope/blob/96015df1/core/src/main/java/org/apache/syncope/core/cache/VirAttrCache.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/syncope/core/cache/VirAttrCache.java b/core/src/main/java/org/apache/syncope/core/cache/VirAttrCache.java
new file mode 100644
index 0000000..723eb1d
--- /dev/null
+++ b/core/src/main/java/org/apache/syncope/core/cache/VirAttrCache.java
@@ -0,0 +1,65 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.syncope.core.cache;
+
+import org.apache.syncope.common.types.AttributableType;
+
+/**
+ * Virtual Attribute Value cache.
+ */
+public interface VirAttrCache {
+
+    /**
+     * Force entry expiring.
+     *
+     * @param type user or role
+     * @param id user or role id
+     * @param schemaName virtual attribute schema name
+     */
+    void expire(AttributableType type, Long id, String schemaName);
+
+    /**
+     * Retrieve cached value. Return null in case of virtual attribute not cached.
+     *
+     * @param type user or role
+     * @param id user or role id
+     * @param schemaName virtual attribute schema name.
+     * @return cached values or null if virtual attribute is not cached.
+     */
+    VirAttrCacheValue get(AttributableType type, Long id, String schemaName);
+
+    /**
+     * Cache entry is valid if and only if value exist and it is not expired.
+     *
+     * @param value cache entry value.
+     * @return TRUE if the value is valid; FALSE otherwise.
+     */
+    boolean isValidEntry(VirAttrCacheValue value);
+
+    /**
+     * Cache virtual attribute values.
+     *
+     * @param type user or role
+     * @param id user or role id
+     * @param schemaName virtual attribute name
+     * @param value virtual attribute values
+     */
+    void put(AttributableType type, Long id, String schemaName, VirAttrCacheValue value);
+
+}

http://git-wip-us.apache.org/repos/asf/syncope/blob/96015df1/core/src/main/java/org/apache/syncope/core/cache/VirAttrCacheKey.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/syncope/core/cache/VirAttrCacheKey.java b/core/src/main/java/org/apache/syncope/core/cache/VirAttrCacheKey.java
new file mode 100644
index 0000000..b22a278
--- /dev/null
+++ b/core/src/main/java/org/apache/syncope/core/cache/VirAttrCacheKey.java
@@ -0,0 +1,79 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.syncope.core.cache;
+
+import org.apache.commons.lang3.builder.EqualsBuilder;
+import org.apache.commons.lang3.builder.HashCodeBuilder;
+import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
+import org.apache.commons.lang3.builder.ToStringStyle;
+import org.apache.syncope.common.types.AttributableType;
+
+/**
+ * Ccahe entry key.
+ */
+public class VirAttrCacheKey {
+
+    /**
+     * Subject type.
+     */
+    private final AttributableType type;
+
+    /**
+     * Subject ID.
+     */
+    private final transient Long id;
+
+    /**
+     * Virtual attribute schema name.
+     */
+    private final transient String virSchema;
+
+    public VirAttrCacheKey(final AttributableType type, final Long id, final String virSchema) {
+        this.type = type;
+        this.id = id;
+        this.virSchema = virSchema;
+    }
+
+    public AttributableType getType() {
+        return type;
+    }
+
+    public Long getId() {
+        return id;
+    }
+
+    public String getVirSchema() {
+        return virSchema;
+    }
+
+    @Override
+    public boolean equals(final Object obj) {
+        return EqualsBuilder.reflectionEquals(this, obj, true);
+    }
+
+    @Override
+    public int hashCode() {
+        return HashCodeBuilder.reflectionHashCode(this, true);
+    }
+
+    @Override
+    public String toString() {
+        return ReflectionToStringBuilder.toString(this, ToStringStyle.MULTI_LINE_STYLE, true);
+    }
+}

http://git-wip-us.apache.org/repos/asf/syncope/blob/96015df1/core/src/main/java/org/apache/syncope/core/cache/VirAttrCacheValue.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/syncope/core/cache/VirAttrCacheValue.java b/core/src/main/java/org/apache/syncope/core/cache/VirAttrCacheValue.java
new file mode 100644
index 0000000..5a634f2
--- /dev/null
+++ b/core/src/main/java/org/apache/syncope/core/cache/VirAttrCacheValue.java
@@ -0,0 +1,86 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.syncope.core.cache;
+
+import java.util.Date;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * Cache entry value.
+ */
+public class VirAttrCacheValue {
+
+    /**
+     * Virtual attribute values.
+     */
+    private final Map<String, Set<String>> values;
+
+    /**
+     * Entry creation date.
+     */
+    private Date creationDate;
+
+    /**
+     * Entry access date.
+     */
+    private Date lastAccessDate;
+
+    public VirAttrCacheValue() {
+        this.creationDate = new Date();
+        this.lastAccessDate = new Date();
+        values = new HashMap<String, Set<String>>();
+    }
+
+    public void setResourceValues(final String resourceName, final Set<String> values) {
+        this.values.put(resourceName, values);
+    }
+
+    public Date getCreationDate() {
+        return creationDate;
+    }
+
+    public void forceExpiring() {
+        creationDate = new Date(0);
+    }
+
+    public Set<String> getValues(final String resourceName) {
+        return values.get(resourceName);
+    }
+
+    public Set<String> getValues() {
+        final Set<String> res = new HashSet<String>();
+
+        for (Set<String> value : values.values()) {
+            res.addAll(value);
+        }
+
+        return res;
+    }
+
+    public Date getLastAccessDate() {
+        return lastAccessDate;
+    }
+
+    void setLastAccessDate(final Date lastAccessDate) {
+        this.lastAccessDate = lastAccessDate;
+    }
+}

http://git-wip-us.apache.org/repos/asf/syncope/blob/96015df1/core/src/main/java/org/apache/syncope/core/connid/ConnObjectUtil.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/syncope/core/connid/ConnObjectUtil.java b/core/src/main/java/org/apache/syncope/core/connid/ConnObjectUtil.java
index 519c931..814e83b 100644
--- a/core/src/main/java/org/apache/syncope/core/connid/ConnObjectUtil.java
+++ b/core/src/main/java/org/apache/syncope/core/connid/ConnObjectUtil.java
@@ -69,8 +69,8 @@ import org.apache.syncope.core.util.AttributableUtil;
 import org.apache.syncope.core.util.InvalidPasswordPolicySpecException;
 import org.apache.syncope.core.util.MappingUtil;
 import org.apache.syncope.core.util.SecureRandomUtil;
-import org.apache.syncope.core.util.VirAttrCache;
-import org.apache.syncope.core.util.VirAttrCacheValue;
+import org.apache.syncope.core.cache.VirAttrCache;
+import org.apache.syncope.core.cache.VirAttrCacheValue;
 import org.apache.syncope.core.util.jexl.JexlUtil;
 import org.identityconnectors.common.Base64;
 import org.identityconnectors.common.security.GuardedByteArray;

http://git-wip-us.apache.org/repos/asf/syncope/blob/96015df1/core/src/main/java/org/apache/syncope/core/util/MappingUtil.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/syncope/core/util/MappingUtil.java b/core/src/main/java/org/apache/syncope/core/util/MappingUtil.java
index 77cacdf..f6dc331 100644
--- a/core/src/main/java/org/apache/syncope/core/util/MappingUtil.java
+++ b/core/src/main/java/org/apache/syncope/core/util/MappingUtil.java
@@ -35,6 +35,7 @@ import org.apache.syncope.common.types.AttributableType;
 import org.apache.syncope.common.types.IntMappingType;
 import org.apache.syncope.common.types.AttributeSchemaType;
 import org.apache.syncope.common.types.MappingPurpose;
+import org.apache.syncope.core.cache.VirAttrCache;
 import org.apache.syncope.core.connid.ConnObjectUtil;
 import org.apache.syncope.core.connid.PasswordGenerator;
 import org.apache.syncope.core.persistence.beans.AbstractAttr;
@@ -208,7 +209,7 @@ public final class MappingUtil {
             }
         }
 
-        final Attribute accountIdExtAttr = 
+        final Attribute accountIdExtAttr =
                 AttributeUtil.find(attrUtil.getAccountIdItem(resource).getExtAttrName(), attributes);
         if (accountIdExtAttr != null) {
             attributes.remove(accountIdExtAttr);
@@ -377,8 +378,8 @@ public final class MappingUtil {
                 } else {
                     result = new AbstractMap.SimpleEntry<String, Attribute>(
                             null, objValues.isEmpty()
-                            ? AttributeBuilder.build(extAttrName)
-                            : AttributeBuilder.build(extAttrName, objValues.iterator().next()));
+                                    ? AttributeBuilder.build(extAttrName)
+                                    : AttributeBuilder.build(extAttrName, objValues.iterator().next()));
                 }
             }
         }

http://git-wip-us.apache.org/repos/asf/syncope/blob/96015df1/core/src/main/java/org/apache/syncope/core/util/VirAttrCache.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/syncope/core/util/VirAttrCache.java b/core/src/main/java/org/apache/syncope/core/util/VirAttrCache.java
deleted file mode 100644
index 10b9a6c..0000000
--- a/core/src/main/java/org/apache/syncope/core/util/VirAttrCache.java
+++ /dev/null
@@ -1,144 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.syncope.core.util;
-
-import java.util.Date;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Map;
-import java.util.Set;
-import org.apache.syncope.common.types.AttributableType;
-
-/**
- * Virtual Attribute Value cache.
- */
-public final class VirAttrCache {
-
-    /**
-     * Elapsed time in seconds.
-     */
-    private final int ttl;
-
-    /**
-     * Max cache size.
-     */
-    private final int maxCacheSize;
-
-    /**
-     * Cache entries.
-     */
-    private final Map<VirAttrCacheKey, VirAttrCacheValue> cache = new HashMap<VirAttrCacheKey, VirAttrCacheValue>();
-
-    public VirAttrCache(final int ttl, final int maxCacheSize) {
-        this.ttl = ttl;
-        this.maxCacheSize = maxCacheSize;
-    }
-
-    /**
-     * Cache virtual attribute values.
-     *
-     * @param type user or role
-     * @param id user or role id
-     * @param schemaName virtual attribute name
-     * @param value virtual attribute values
-     */
-    public void put(
-            final AttributableType type,
-            final Long id,
-            final String schemaName,
-            final VirAttrCacheValue value) {
-
-        synchronized (cache) {
-            // this operations (retrieve cache space and put entry on) have to be thread safe.
-            if (this.cache.size() >= this.maxCacheSize) {
-                free();
-            }
-
-            cache.put(new VirAttrCacheKey(type, id, schemaName), value);
-        }
-    }
-
-    /**
-     * Retrieve cached value. Return null in case of virtual attribute not cached.
-     *
-     * @param type user or role
-     * @param id user or role id
-     * @param schemaName virtual attribute schema name.
-     * @return cached values or null if virtual attribute is not cached.
-     */
-    public VirAttrCacheValue get(final AttributableType type, final Long id, final String schemaName) {
-        return cache.get(new VirAttrCacheKey(type, id, schemaName));
-    }
-
-    /**
-     * Force entry expiring.
-     *
-     * @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 id, final String schemaName) {
-        final VirAttrCacheValue value = cache.get(new VirAttrCacheKey(type, id, schemaName));
-        if (isValidEntry(value)) {
-            synchronized (cache) {
-                value.forceExpiring();
-            }
-        }
-    }
-
-    /**
-     * 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.
-     */
-    private void free() {
-        final Set<VirAttrCacheKey> toBeRemoved = new HashSet<VirAttrCacheKey>();
-
-        Map.Entry<VirAttrCacheKey, VirAttrCacheValue> latest = null;
-
-        for (Map.Entry<VirAttrCacheKey, VirAttrCacheValue> entry : cache.entrySet()) {
-            if (isValidEntry(entry.getValue())) {
-                final Date date = entry.getValue().getLastAccessDate();
-                if (latest == null || latest.getValue().getLastAccessDate().after(date)) {
-                    latest = entry;
-                }
-            } else {
-                toBeRemoved.add(entry.getKey());
-            }
-        }
-
-        if (toBeRemoved.isEmpty() && latest != null) {
-            // remove the oldest entry
-            cache.remove(latest.getKey());
-        } else {
-            // remove expired entries
-            cache.keySet().removeAll(toBeRemoved);
-        }
-    }
-
-    /**
-     * Cache entry is valid if and only if value exist and it is not expired.
-     *
-     * @param value cache entry value.
-     * @return TRUE if the value is valid; FALSE otherwise.
-     */
-    public boolean isValidEntry(final VirAttrCacheValue value) {
-        final Date expiringDate = new Date(value == null ? 0 : value.getCreationDate().getTime() + ttl * 1000);
-        return expiringDate.after(new Date());
-    }
-}

http://git-wip-us.apache.org/repos/asf/syncope/blob/96015df1/core/src/main/java/org/apache/syncope/core/util/VirAttrCacheKey.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/syncope/core/util/VirAttrCacheKey.java b/core/src/main/java/org/apache/syncope/core/util/VirAttrCacheKey.java
deleted file mode 100644
index e68f4c0..0000000
--- a/core/src/main/java/org/apache/syncope/core/util/VirAttrCacheKey.java
+++ /dev/null
@@ -1,79 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.syncope.core.util;
-
-import org.apache.commons.lang3.builder.EqualsBuilder;
-import org.apache.commons.lang3.builder.HashCodeBuilder;
-import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
-import org.apache.commons.lang3.builder.ToStringStyle;
-import org.apache.syncope.common.types.AttributableType;
-
-/**
- * Ccahe entry key.
- */
-public class VirAttrCacheKey {
-
-    /**
-     * Subject type.
-     */
-    private final AttributableType type;
-
-    /**
-     * Subject ID.
-     */
-    private final transient Long id;
-
-    /**
-     * Virtual attribute schema name.
-     */
-    private final transient String virSchema;
-
-    public VirAttrCacheKey(final AttributableType type, final Long id, final String virSchema) {
-        this.type = type;
-        this.id = id;
-        this.virSchema = virSchema;
-    }
-
-    public AttributableType getType() {
-        return type;
-    }
-
-    public Long getId() {
-        return id;
-    }
-
-    public String getVirSchema() {
-        return virSchema;
-    }
-
-    @Override
-    public boolean equals(final Object obj) {
-        return EqualsBuilder.reflectionEquals(this, obj, true);
-    }
-
-    @Override
-    public int hashCode() {
-        return HashCodeBuilder.reflectionHashCode(this, true);
-    }
-
-    @Override
-    public String toString() {
-        return ReflectionToStringBuilder.toString(this, ToStringStyle.MULTI_LINE_STYLE, true);
-    }
-}

http://git-wip-us.apache.org/repos/asf/syncope/blob/96015df1/core/src/main/java/org/apache/syncope/core/util/VirAttrCacheValue.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/syncope/core/util/VirAttrCacheValue.java b/core/src/main/java/org/apache/syncope/core/util/VirAttrCacheValue.java
deleted file mode 100644
index bca5acf..0000000
--- a/core/src/main/java/org/apache/syncope/core/util/VirAttrCacheValue.java
+++ /dev/null
@@ -1,86 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.syncope.core.util;
-
-import java.util.Date;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Map;
-import java.util.Set;
-
-/**
- * Cache entry value.
- */
-public class VirAttrCacheValue {
-
-    /**
-     * Virtual attribute values.
-     */
-    private final Map<String, Set<String>> values;
-
-    /**
-     * Entry creation date.
-     */
-    private Date creationDate;
-
-    /**
-     * Entry access date.
-     */
-    private Date lastAccessDate;
-
-    public VirAttrCacheValue() {
-        this.creationDate = new Date();
-        this.lastAccessDate = new Date();
-        values = new HashMap<String, Set<String>>();
-    }
-
-    public void setResourceValues(final String resourceName, final Set<String> values) {
-        this.values.put(resourceName, values);
-    }
-
-    public Date getCreationDate() {
-        return creationDate;
-    }
-
-    public void forceExpiring() {
-        creationDate = new Date(0);
-    }
-
-    public Set<String> getValues(final String resourceName) {
-        return values.get(resourceName);
-    }
-
-    public Set<String> getValues() {
-        final Set<String> res = new HashSet<String>();
-
-        for (Set<String> value : values.values()) {
-            res.addAll(value);
-        }
-
-        return res;
-    }
-
-    public Date getLastAccessDate() {
-        return lastAccessDate;
-    }
-
-    void setLastAccessDate(Date lastAccessDate) {
-        this.lastAccessDate = lastAccessDate;
-    }
-}

http://git-wip-us.apache.org/repos/asf/syncope/blob/96015df1/core/src/main/resources/coreContext.xml
----------------------------------------------------------------------
diff --git a/core/src/main/resources/coreContext.xml b/core/src/main/resources/coreContext.xml
index 9b12622..9bb146b 100644
--- a/core/src/main/resources/coreContext.xml
+++ b/core/src/main/resources/coreContext.xml
@@ -141,7 +141,7 @@ under the License.
   <task:annotation-driven executor="connectorExecutor"/>
   <task:executor id="connectorExecutor" pool-size="10"/>
   
-  <bean id="virAttrCache" class="org.apache.syncope.core.util.VirAttrCache" scope="singleton">
+  <bean id="virAttrCache" class="org.apache.syncope.core.cache.MemoryVirAttrCache" scope="singleton">
     <constructor-arg value="60"/>
     <constructor-arg value="5000"/>
   </bean>


[3/3] syncope git commit: Merge branch '1_2_X'

Posted by il...@apache.org.
Merge branch '1_2_X'


Project: http://git-wip-us.apache.org/repos/asf/syncope/repo
Commit: http://git-wip-us.apache.org/repos/asf/syncope/commit/f8e3da0f
Tree: http://git-wip-us.apache.org/repos/asf/syncope/tree/f8e3da0f
Diff: http://git-wip-us.apache.org/repos/asf/syncope/diff/f8e3da0f

Branch: refs/heads/master
Commit: f8e3da0f018083fa49384ac7bf196279713f3cd4
Parents: 9c8f5b9 96015df
Author: Francesco Chicchiriccò <il...@apache.org>
Authored: Mon Dec 22 14:41:33 2014 +0100
Committer: Francesco Chicchiriccò <il...@apache.org>
Committed: Mon Dec 22 14:41:33 2014 +0100

----------------------------------------------------------------------
 .../core/cache/DisabledVirAttrCache.java        |  50 +++++++
 .../syncope/core/cache/MemoryVirAttrCache.java  | 148 +++++++++++++++++++
 .../apache/syncope/core/cache/VirAttrCache.java |  65 ++++++++
 .../syncope/core/cache/VirAttrCacheKey.java     |  79 ++++++++++
 .../syncope/core/cache/VirAttrCacheValue.java   |  86 +++++++++++
 .../syncope/core/connid/ConnObjectUtil.java     |   4 +-
 .../apache/syncope/core/util/MappingUtil.java   |   7 +-
 .../apache/syncope/core/util/VirAttrCache.java  | 144 ------------------
 .../syncope/core/util/VirAttrCacheKey.java      |  79 ----------
 .../syncope/core/util/VirAttrCacheValue.java    |  86 -----------
 core/src/main/resources/coreContext.xml         |   2 +-
 11 files changed, 435 insertions(+), 315 deletions(-)
----------------------------------------------------------------------