1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
|
<?xml version="1.0" encoding="UTF-8"?>
<chapter id="odm">
<title>Object-Directory Mapping (ODM)</title>
<sect1 id="odm-intro">
<title>Introduction</title>
<para>Relational mapping frameworks like Hibernate and JPA have offered
developers the ability to use annotations to map database tables to Java
objects for some time. The Spring Framework LDAP project now offers the
same ability with respect to directories through the use of the
<code>org.springframework.ldap.odm</code> package (sometimes abbreviated
as <code>o.s.l.odm</code>).</para>
</sect1>
<sect1 id="odm-odmmanager">
<title>OdmManager</title>
<para>The <code>org.springframework.ldap.odm.OdmManager</code> interface,
and its implementation, is the central class in the ODM package. The
<code>OdmManager</code> orchestrates the process of reading objects from
the directory and mapping the data to annotated Java object classes. This
interface provides access to the underlying directory instance through the
following methods:</para>
<itemizedlist>
<listitem>
<para><code><T> T read(Class<T> clazz, Name
dn)</code></para>
</listitem>
<listitem>
<para><code>void create(Object entry)</code></para>
</listitem>
<listitem>
<para><code>void update(Object entry)</code></para>
</listitem>
<listitem>
<para><code>void delete(Object entry)</code></para>
</listitem>
<listitem>
<para><code><T> List<T> findAll(Class<T> clazz, Name
base, SearchControls searchControls)</code></para>
</listitem>
<listitem>
<para><code><T> List<T> search(Class<T> clazz, Name
base, String filter, SearchControls searchControls)</code></para>
</listitem>
</itemizedlist>
<para>A reference to an implementation of this interface can be obtained
through the
<code>org.springframework.ldap.odm.core.impl.OdmManagerImplFactoryBean</code>.
A basic configuration of this factory would be as follows:</para>
<example>
<title>Configuring the OdmManager Factory</title>
<programlisting>
<beans>
...
<bean id="odmManager"
class="org.springframework.ldap.odm.core.impl.OdmManagerImplFactoryBean">
<property name="converterManager" ref="converterManager" />
<property name="contextSource" ref="contextSource" />
<property name="managedClasses">
<set>
<value>com.example.dao.SimplePerson</value>
</set>
</property>
</bean>
...
</beans>
</programlisting>
</example>
<para>The factory requires the list of entity classes to be managed by the
<code>OdmManager</code> to be explicitly declared. These classes should be
properly annotated as defined in the next section. The
<code>converterManager</code> referenced in the above definition is
described in <xref linkend="odm-typeconversion" />.</para>
</sect1>
<sect1 id="odm-annotations">
<title>Annotations</title>
<para>Entity classes managed by the <code>OdmManager</code> are required
to be annotated with the annotations in the
<code>org.springframework.ldap.odm.annotations</code> package. The
available annotations are:</para>
<itemizedlist>
<listitem>
<para><code>@Entry</code> - Class level annotation indicating the
<code>objectClass</code> definitions to which the entity
maps.<emphasis> (required)</emphasis></para>
</listitem>
<listitem>
<para><code>@Id</code> - Indicates the entity DN; the field declaring
this attribute must be a derivative of the
<code>javax.naming.Name</code> class.
<emphasis>(required)</emphasis></para>
</listitem>
<listitem>
<para><code>@Attribute</code> - Indicates the mapping of a directory
attribute to the object class field.</para>
</listitem>
<listitem>
<para><code>@Transient</code> - Indicates the field is not persistent
and should be ignored by the <code>OdmManager</code>.</para>
</listitem>
</itemizedlist>
<simpara>The <code>@Entry</code> and <code>@Id</code> attributes are
required to be declared on managed classes. <code>@Entry</code> is used to
specify which object classes the entity maps too. All object classes for
which fields are mapped are required to be declared. Also, in order for a
directory entry to be considered a match to the managed entity, all object
classes declared by the directory entry must match be declared by in the
<code>@Entry</code> annotation.</simpara>
<simpara>The <code>@Id</code> annotation is used to map the distinguished
name of the entry to a field. The field must be an instance of
<code>javax.naming.Name</code> or a subclass of it.</simpara>
<simpara>The <code>@Attribute</code> annotation is used to map object
class fields to entity fields. <code>@Attribute</code> is required to
declare the name of the object class property to which the field maps and
may optionally declare the syntax OID of the LDAP attribute, to guarantee
exact matching. <code>@Attribute</code> also provides the type declaration
which allows you to indicate whether the attribute is regarded as binary
based or string based by the LDAP JNDI provider.</simpara>
<simpara>The <code>@Transient</code> annotation is used to indicate the
field should be ignored by the <code>OdmManager</code> and not mapped to
an underlying LDAP property.</simpara>
</sect1>
<sect1 id="odm-typeconversion">
<title>Type Conversion</title>
<para>The <code>OdmManager</code> relies on the
<code>org.springframework.ldap.odm.typeconversion</code> package to
convert LDAP attributes to Java fields. The main interface in this class
is the
<code>org.springframework.ldap.odm.typeconversion.ConverterManager</code>.
The default <code>ConverterManager</code> implementation uses the
following algorithm when parsing objects to convert fields:<orderedlist>
<listitem>
<para>Try to find and use a <code>Converter</code> registered for
the <code>fromClass</code>, <code>syntax</code> and
<code>toClass</code> and use it.</para>
</listitem>
<listitem>
<para>If this fails, then if the <code>toClass</code>
<code>isAssignableFrom</code> the
<code>fromClass</code> then just assign it.</para>
</listitem>
<listitem>
<para>If this fails try to find and use a
<code>Converter</code> registered for the
<code>fromClass</code> and the <code>toClass</code> ignoring the
syntax.</para>
</listitem>
<listitem>
<para>If this fails then throw a
<exceptionname>ConverterException</exceptionname>.</para>
</listitem>
</orderedlist></para>
<para>Implementations of the <code>ConverterManager</code> interface can
be obtained from the
<code>o.s.l.odm.typeconversion.impl.ConvertManagerFactoryBean</code>.
The factory bean requires converter configurations to be declared in the
bean configuration.</para>
<para>The converterConfig property accepts a set of
<code>ConverterConfig</code> classes, each one defining some conversion
logic. A converter config is an instance of
<code>o.s.l.odm.typeconversion.impl.ConverterManagerFactoryBean.ConverterConfig</code>.
The config defines a set of source classes, the set of target classes, and
an implementation of the
<code>org.springframework.ldap.odm.typeconversion.impl.Converter</code>
interface which provides the logic to convert from the
<code>fromClass</code> to the <code>toClass</code>. A sample configuration
is provided in the following example:</para>
<example>
<title>Configuring the Converter Manager Factory</title>
<programlisting>
<bean id="fromStringConverter"
class="org.springframework.ldap.odm.typeconversion.impl.converters.FromStringConverter" />
<bean id="toStringConverter"
class="org.springframework.ldap.odm.typeconversion.impl.converters.ToStringConverter" />
<bean id="converterManager"
class="org.springframework.ldap.odm.typeconversion.impl.ConverterManagerFactoryBean">
<property name="converterConfig">
<set>
<bean class="org.springframework.ldap.odm.\
typeconversion.impl.ConverterManagerFactoryBean$ConverterConfig">
<property name="fromClasses">
<set>
<value>java.lang.String</value>
</set>
</property>
<property name="toClasses">
<set>
<value>java.lang.Byte</value>
<value>java.lang.Short</value>
<value>java.lang.Integer</value>
<value>java.lang.Long</value>
<value>java.lang.Float</value>
<value>java.lang.Double</value>
<value>java.lang.Boolean</value>
</set>
</property>
<property name="converter" ref="fromStringConverter" />
</bean>
<bean class="org.springframework.ldap.odm.\
typeconversion.impl.ConverterManagerFactoryBean$ConverterConfig">
<property name="fromClasses">
<set>
<value>java.lang.Byte</value>
<value>java.lang.Short</value>
<value>java.lang.Integer</value>
<value>java.lang.Long</value>
<value>java.lang.Float</value>
<value>java.lang.Double</value>
<value>java.lang.Boolean</value>
</set>
</property>
<property name="toClasses">
<set>
<value>java.lang.String</value>
</set>
</property>
<property name="converter" ref="toStringConverter" />
</bean>
</set>
</property>
</bean>
</programlisting>
</example>
</sect1>
<sect1 id="odm-execution">
<title>Execution</title>
<para>After all components are configured, directory interaction can be
achieved through a reference to the <code>OdmManager</code>, as shown in
this example:</para>
<example>
<title>Execution</title>
<programlisting>
public class App {
private static Log log = LogFactory.getLog(App.class);
private static final SearchControls searchControls =
new SearchControls(SearchControls.SUBTREE_SCOPE, 100, 10000, null, true, false);
public static void main( String[] args ) {
try {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
OdmManager manager = (OdmManager) context.getBean("odmManager");
List<SimplePerson> people = manager.search(SimplePerson.class,
new DistinguishedName("dc=example,dc=com"), "uid=*", searchControls);
log.info("People found: " + people.size());
for (SimplePerson person : people) {
log.info( person );
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
</programlisting>
</example>
</sect1>
</chapter>
|