An implementation of the
IPersonAttributeDao that is able to resolve attributes
based on an external Groovy script, Groovy object, or Java object. Changes to the groovy script can be auto-detected
in certain use cases.
There are several ways to use this Dao.
Approach 1: Groovy file pre-compiled to Java class file
Spring configuration:
<bean id="duplicateUsernameAttributeScript" class="org.jasig.portal.persondir.AttributeDuplicatingPersonAttributesScript"/>
<bean id="duplicateUsernameAttributeSource" class="org.jasig.services.persondir.support.GroovyPersonAttributeDao"
c:groovyObject-ref="duplicateUsernameAttributeScript"/>
Groovy file:
class SampleGroovyPersonAttributeDao implements org.jasig.services.persondir.IPersonAttributeScriptDao {
@Override
Map<String, Object> getAttributesForUser(String uid, Log log) {
return[name:[uid], likes:["cheese", "food"], id:[1234,2,3,4,5], another:"attribute"]
}
@Override
Map<String, List<Object>> getPersonAttributesFromMultivaluedAttributes(Map<String, List<Object>> attributes, Log log) {
Map<String, List<Object>> newMap = new HashMap<>(attributes)
newMap.put("foo", Arrays.asList(["value1", "value2"]))
return newMap
}
}
Notes:
- Use maven-antrun-plugin, gmavenplus-plugin, or similar to pre-compile groovy classes in maven build process
- Separate groovy source file, so can create unit test of groovy code
- Does not accommodate groovy source code changes
Approach 2: Groovy script file referenced by change-detecting configuration
Spring configuration:
<bean id="duplicateUsernameAttributeSource2" class="org.jasig.services.persondir.support.GroovyPersonAttributeDao"/>
c:groovyObject-ref="duplicateUsernameAttributeScript2"/>
<lang:groovy id="duplicateUsernameAttributeScript2" refresh-check-delay="5000"
script-source="classpath:AttributeDuplicatingPersonAttributesScript.groovy"/>
Groovy file:
Same as Approach 1
Notes:
- Separate groovy source file, so can create unit test of groovy code
- Will detect groovy source code changes
Approach 3: Inline Groovy script
Spring configuration:
<bean id="duplicateUsernameAttributeSource3" class="org.jasig.services.persondir.support.GroovyPersonAttributeDao"
c:groovyObject-ref="duplicateUsernameAttributeScript3"/>
<lang:groovy id="duplicateUsernameAttributeScript3">
<lang:inline-script><![CDATA[
class AttributeDuplicatingPersonAttributesScript extends org.jasig.services.persondir.support.BaseGroovyScriptDaoImpl {
@Override
Map<String, Object> getAttributesForUser(String uid, Log log) {
return[name:[uid], likes:["cheese", "food"], id:[1234,2,3,4,5], another:"attribute"]
}
]]></lang:inline-script>
</lang:groovy>
Notes:
- Cannot create unit test of groovy source file, will not detect changes
- Useful for embedded configuration