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 2012/09/21 17:33:04 UTC

svn commit: r1388553 - in /incubator/syncope/trunk/core/src/main/java/org/apache/syncope/core: persistence/beans/AbstractDerAttr.java propagation/PropagationManager.java util/JexlUtil.java workflow/NoOpUserWorkflowAdapter.java

Author: ilgrosso
Date: Fri Sep 21 15:33:03 2012
New Revision: 1388553

URL: http://svn.apache.org/viewvc?rev=1388553&view=rev
Log:
Cleaning up and refactoring JexlUtil

Modified:
    incubator/syncope/trunk/core/src/main/java/org/apache/syncope/core/persistence/beans/AbstractDerAttr.java
    incubator/syncope/trunk/core/src/main/java/org/apache/syncope/core/propagation/PropagationManager.java
    incubator/syncope/trunk/core/src/main/java/org/apache/syncope/core/util/JexlUtil.java
    incubator/syncope/trunk/core/src/main/java/org/apache/syncope/core/workflow/NoOpUserWorkflowAdapter.java

Modified: incubator/syncope/trunk/core/src/main/java/org/apache/syncope/core/persistence/beans/AbstractDerAttr.java
URL: http://svn.apache.org/viewvc/incubator/syncope/trunk/core/src/main/java/org/apache/syncope/core/persistence/beans/AbstractDerAttr.java?rev=1388553&r1=1388552&r2=1388553&view=diff
==============================================================================
--- incubator/syncope/trunk/core/src/main/java/org/apache/syncope/core/persistence/beans/AbstractDerAttr.java (original)
+++ incubator/syncope/trunk/core/src/main/java/org/apache/syncope/core/persistence/beans/AbstractDerAttr.java Fri Sep 21 15:33:03 2012
@@ -18,17 +18,13 @@
  */
 package org.apache.syncope.core.persistence.beans;
 
-import java.lang.reflect.Field;
-import java.util.Arrays;
 import java.util.Collection;
-import java.util.Date;
-import java.util.List;
 import javax.persistence.GeneratedValue;
 import javax.persistence.GenerationType;
 import javax.persistence.Id;
 import javax.persistence.MappedSuperclass;
 import org.apache.commons.jexl2.JexlContext;
-import org.apache.commons.lang.ArrayUtils;
+import org.apache.commons.jexl2.MapContext;
 import org.apache.syncope.core.util.ApplicationContextProvider;
 import org.apache.syncope.core.util.JexlUtil;
 import org.springframework.context.ConfigurableApplicationContext;
@@ -38,9 +34,6 @@ public abstract class AbstractDerAttr ex
 
     private static final long serialVersionUID = 4740924251090424771L;
 
-    private final List<String> ignoredFields = Arrays.asList(
-            new String[]{"password", "clearPassword", "serialVersionUID"});
-
     @Id
     @GeneratedValue(strategy = GenerationType.AUTO)
     protected Long id;
@@ -60,39 +53,14 @@ public abstract class AbstractDerAttr ex
         final JexlUtil jexlUtil = context.getBean(JexlUtil.class);
 
         // Prepare context using user attributes
-        final JexlContext jexlContext = jexlUtil.addAttrsToContext(attributes, null);
-
-        createJexlContext(jexlContext);
+        final JexlContext jexlContext = new MapContext();
+        jexlUtil.addAttrsToContext(attributes, jexlContext);
+        jexlUtil.addFieldsToContext(getOwner(), jexlContext);
 
         // Evaluate expression using the context prepared before
         return jexlUtil.evaluate(getDerivedSchema().getExpression(), jexlContext);
     }
 
-    private void createJexlContext(final JexlContext jexlContext) {
-        AbstractAttributable instance = getOwner();
-        Field[] fields = instance.getClass().getDeclaredFields();
-        for (int i = 0; i < fields.length; i++) {
-            try {
-                Field field = fields[i];
-                field.setAccessible(true);
-                if ((!field.isSynthetic()) && (!field.getName().startsWith("pc"))
-                        && (!ArrayUtils.contains(ignoredFields.toArray(), field.getName()))
-                        && (!Iterable.class.isAssignableFrom(field.getType()))
-                        && (!field.getType().isArray())) {
-                    if (field.getType().equals(Date.class)) {
-                        jexlContext.set(field.getName(), field.get(instance) != null
-                                ? ((AbstractBaseBean) instance).getDateFormatter().format(field.get(instance)) : "");
-                    } else {
-                        jexlContext.set(field.getName(), field.get(instance) != null ? field.get(instance) : "");
-                    }
-                }
-
-            } catch (Exception ex) {
-                LOG.error("Reading class attributes error", ex);
-            }
-        }
-    }
-
     public abstract <T extends AbstractAttributable> T getOwner();
 
     public abstract <T extends AbstractAttributable> void setOwner(T owner);

Modified: incubator/syncope/trunk/core/src/main/java/org/apache/syncope/core/propagation/PropagationManager.java
URL: http://svn.apache.org/viewvc/incubator/syncope/trunk/core/src/main/java/org/apache/syncope/core/propagation/PropagationManager.java?rev=1388553&r1=1388552&r2=1388553&view=diff
==============================================================================
--- incubator/syncope/trunk/core/src/main/java/org/apache/syncope/core/propagation/PropagationManager.java (original)
+++ incubator/syncope/trunk/core/src/main/java/org/apache/syncope/core/propagation/PropagationManager.java Fri Sep 21 15:33:03 2012
@@ -29,6 +29,8 @@ import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Set;
 import org.apache.commons.collections.keyvalue.DefaultMapEntry;
+import org.apache.commons.jexl2.JexlContext;
+import org.apache.commons.jexl2.MapContext;
 import org.apache.syncope.client.mod.AttributeMod;
 import org.apache.syncope.client.to.AttributeTO;
 import org.apache.syncope.core.init.ConnInstanceLoader;
@@ -464,7 +466,11 @@ public class PropagationManager {
         }
 
         // Evaluate AccountLink expression
-        String evalAccountLink = jexlUtil.evaluate(resource.getAccountLink(), user);
+        final JexlContext jexlContext = new MapContext();
+        jexlUtil.addFieldsToContext(user, jexlContext);
+        jexlUtil.addAttrsToContext(user.getAttributes(), jexlContext);
+        jexlUtil.addDerAttrsToContext(user.getDerivedAttributes(), user.getAttributes(), jexlContext);
+        String evalAccountLink = jexlUtil.evaluate(resource.getAccountLink(), jexlContext);
 
         // AccountId must be propagated. It could be a simple attribute for
         // the target resource or the key (depending on the accountLink)

Modified: incubator/syncope/trunk/core/src/main/java/org/apache/syncope/core/util/JexlUtil.java
URL: http://svn.apache.org/viewvc/incubator/syncope/trunk/core/src/main/java/org/apache/syncope/core/util/JexlUtil.java?rev=1388553&r1=1388552&r2=1388553&view=diff
==============================================================================
--- incubator/syncope/trunk/core/src/main/java/org/apache/syncope/core/util/JexlUtil.java (original)
+++ incubator/syncope/trunk/core/src/main/java/org/apache/syncope/core/util/JexlUtil.java Fri Sep 21 15:33:03 2012
@@ -18,23 +18,25 @@
  */
 package org.apache.syncope.core.util;
 
+import java.lang.reflect.Field;
 import java.util.Collection;
+import java.util.Date;
 import java.util.List;
 import org.apache.commons.jexl2.Expression;
 import org.apache.commons.jexl2.JexlContext;
 import org.apache.commons.jexl2.JexlEngine;
 import org.apache.commons.jexl2.JexlException;
 import org.apache.commons.jexl2.MapContext;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.beans.factory.annotation.Autowired;
+import org.apache.commons.lang.ArrayUtils;
+import org.apache.commons.lang.StringUtils;
 import org.apache.syncope.client.to.AbstractAttributableTO;
 import org.apache.syncope.client.to.AttributeTO;
-import org.apache.syncope.client.to.UserTO;
 import org.apache.syncope.core.persistence.beans.AbstractAttr;
-import org.apache.syncope.core.persistence.beans.AbstractAttributable;
+import org.apache.syncope.core.persistence.beans.AbstractBaseBean;
 import org.apache.syncope.core.persistence.beans.AbstractDerAttr;
-import org.apache.syncope.core.persistence.beans.user.SyncopeUser;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
 
 /**
  * @see http://commons.apache.org/jexl/reference/index.html
@@ -43,9 +45,12 @@ public class JexlUtil {
 
     /**
      * Logger.
+     *
      */
     private static final Logger LOG = LoggerFactory.getLogger(JexlUtil.class);
 
+    private static final String[] IGNORE_FIELDS = {"password", "clearPassword", "serialVersionUID"};
+
     @Autowired
     private JexlEngine jexlEngine;
 
@@ -63,11 +68,9 @@ public class JexlUtil {
     }
 
     public String evaluate(final String expression, final JexlContext jexlContext) {
-
         String result = "";
 
-        if (expression != null && !expression.isEmpty() && jexlContext != null) {
-
+        if (StringUtils.isNotBlank(expression) && jexlContext != null) {
             try {
                 Expression jexlExpression = jexlEngine.createExpression(expression);
                 Object evaluated = jexlExpression.evaluate(jexlContext);
@@ -76,7 +79,6 @@ public class JexlUtil {
                 }
             } catch (JexlException e) {
                 LOG.error("Invalid jexl expression: " + expression, e);
-                result = "";
             }
         } else {
             LOG.debug("Expression not provided or invalid context");
@@ -85,36 +87,38 @@ public class JexlUtil {
         return result;
     }
 
-    public String evaluate(final String expression, final AbstractAttributable attributable) {
-
-        final JexlContext jexlContext = new MapContext();
+    public JexlContext addFieldsToContext(final Object attributable, final JexlContext jexlContext) {
+        JexlContext context = jexlContext == null
+                ? new MapContext()
+                : jexlContext;
 
-        if (attributable instanceof SyncopeUser) {
-            SyncopeUser user = (SyncopeUser) attributable;
+        final Field[] fields = attributable.getClass().getDeclaredFields();
+        for (int i = 0; i < fields.length; i++) {
+            try {
+                Field field = fields[i];
+                field.setAccessible(true);
+                final String fieldName = field.getName();
+                if ((!field.isSynthetic()) && (!fieldName.startsWith("pc"))
+                        && (!ArrayUtils.contains(IGNORE_FIELDS, fieldName))
+                        && (!Iterable.class.isAssignableFrom(field.getType()))
+                        && (!field.getType().isArray())) {
+
+                    final Object fieldValue = field.get(attributable);
+
+                    context.set(fieldName, fieldValue == null
+                            ? ""
+                            : (field.getType().equals(Date.class)
+                            ? ((AbstractBaseBean) attributable).getDateFormatter().format(fieldValue)
+                            : fieldValue));
 
-            jexlContext.set("username", user.getUsername() != null
-                    ? user.getUsername()
-                    : "");
-            jexlContext.set("creationDate", user.getCreationDate() != null
-                    ? user.getDateFormatter().format(user.getCreationDate())
-                    : "");
-            jexlContext.set("lastLoginDate", user.getLastLoginDate() != null
-                    ? user.getDateFormatter().format(user.getLastLoginDate())
-                    : "");
-            jexlContext.set("failedLogins", user.getFailedLogins() != null
-                    ? user.getFailedLogins()
-                    : "");
-            jexlContext.set("changePwdDate", user.getChangePwdDate() != null
-                    ? user.getDateFormatter().format(user.getChangePwdDate())
-                    : "");
+                    LOG.debug("Add field {} with value {}", fieldName, fieldValue);
+                }
+            } catch (Exception e) {
+                LOG.error("Reading class attributes error", e);
+            }
         }
 
-        addAttrsToContext(attributable.getAttributes(), jexlContext);
-
-        addDerAttrsToContext(attributable.getDerivedAttributes(), attributable.getAttributes(), jexlContext);
-
-        // Evaluate expression using the context prepared before
-        return evaluate(expression, jexlContext);
+        return context;
     }
 
     public JexlContext addAttrsToContext(final Collection<? extends AbstractAttr> attributes,
@@ -130,8 +134,7 @@ public class JexlUtil {
                     ? ""
                     : attributeValues.get(0);
 
-            LOG.debug("Add attribute {} with value {}",
-                    new Object[] { attribute.getSchema().getName(), expressionValue });
+            LOG.debug("Add attribute {} with value {}", attribute.getSchema().getName(), expressionValue);
 
             context.set(attribute.getSchema().getName(), expressionValue);
         }
@@ -139,42 +142,31 @@ public class JexlUtil {
         return context;
     }
 
-    public JexlContext addDerAttrsToContext(final Collection<? extends AbstractDerAttr> derAttributes,
-            final Collection<? extends AbstractAttr> attributes, final JexlContext jexlContext) {
+    public JexlContext addDerAttrsToContext(final Collection<? extends AbstractDerAttr> derAttrs,
+            final Collection<? extends AbstractAttr> attrs, final JexlContext jexlContext) {
 
         JexlContext context = jexlContext == null
                 ? new MapContext()
                 : jexlContext;
 
-        for (AbstractDerAttr attribute : derAttributes) {
-            String expressionValue = attribute.getValue(attributes);
+        for (AbstractDerAttr derAttr : derAttrs) {
+            String expressionValue = derAttr.getValue(attrs);
             if (expressionValue == null) {
                 expressionValue = "";
             }
 
-            LOG.debug("Add derived attribute {} with value {}", new Object[] { attribute.getDerivedSchema().getName(),
-                    expressionValue });
+            LOG.debug("Add derived attribute {} with value {}", derAttr.getDerivedSchema().getName(), expressionValue);
 
-            context.set(attribute.getDerivedSchema().getName(), expressionValue);
+            context.set(derAttr.getDerivedSchema().getName(), expressionValue);
         }
 
         return context;
     }
 
     public String evaluate(final String expression, final AbstractAttributableTO attributableTO) {
-
         final JexlContext context = new MapContext();
 
-        if (attributableTO instanceof UserTO) {
-            UserTO user = (UserTO) attributableTO;
-
-            context.set("username", user.getUsername() != null
-                    ? user.getUsername()
-                    : "");
-            context.set("password", user.getPassword() != null
-                    ? user.getPassword()
-                    : "");
-        }
+        addFieldsToContext(attributableTO, context);
 
         for (AttributeTO attribute : attributableTO.getAttributes()) {
             List<String> attributeValues = attribute.getValues();
@@ -182,7 +174,7 @@ public class JexlUtil {
                     ? ""
                     : attributeValues.get(0);
 
-            LOG.debug("Add attribute {} with value {}", new Object[] { attribute.getSchema(), expressionValue });
+            LOG.debug("Add attribute {} with value {}", attribute.getSchema(), expressionValue);
 
             context.set(attribute.getSchema(), expressionValue);
         }
@@ -192,7 +184,7 @@ public class JexlUtil {
                     ? ""
                     : attributeValues.get(0);
 
-            LOG.debug("Add attribute {} with value {}", new Object[] { attribute.getSchema(), expressionValue });
+            LOG.debug("Add derived attribute {} with value {}", attribute.getSchema(), expressionValue);
 
             context.set(attribute.getSchema(), expressionValue);
         }
@@ -202,7 +194,7 @@ public class JexlUtil {
                     ? ""
                     : attributeValues.get(0);
 
-            LOG.debug("Add attribute {} with value {}", new Object[] { attribute.getSchema(), expressionValue });
+            LOG.debug("Add virtual attribute {} with value {}", attribute.getSchema(), expressionValue);
 
             context.set(attribute.getSchema(), expressionValue);
         }

Modified: incubator/syncope/trunk/core/src/main/java/org/apache/syncope/core/workflow/NoOpUserWorkflowAdapter.java
URL: http://svn.apache.org/viewvc/incubator/syncope/trunk/core/src/main/java/org/apache/syncope/core/workflow/NoOpUserWorkflowAdapter.java?rev=1388553&r1=1388552&r2=1388553&view=diff
==============================================================================
--- incubator/syncope/trunk/core/src/main/java/org/apache/syncope/core/workflow/NoOpUserWorkflowAdapter.java (original)
+++ incubator/syncope/trunk/core/src/main/java/org/apache/syncope/core/workflow/NoOpUserWorkflowAdapter.java Fri Sep 21 15:33:03 2012
@@ -40,8 +40,8 @@ import org.springframework.transaction.a
 @Transactional(rollbackFor = {Throwable.class})
 public class NoOpUserWorkflowAdapter extends AbstractUserWorkflowAdapter {
 
-    private static final List<String> TASKS = Arrays.asList(new String[]{"create", "activate", "update", "suspend",
-                "reactivate", "delete"});
+    private static final List<String> TASKS = Arrays.asList(new String[]{
+                "create", "activate", "update", "suspend", "reactivate", "delete"});
 
     public static final String ENABLED = "enabled";