Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/jellyfish/implementation/MappingManagerDefault.java


1   package org.jellyfish.implementation;
2   
3   import java.beans.PropertyDescriptor;
4   import java.lang.reflect.InvocationTargetException;
5   import java.util.Collection;
6   import java.util.HashMap;
7   import java.util.Iterator;
8   import java.util.Map;
9   
10  import javax.naming.NamingException;
11  import javax.naming.directory.Attribute;
12  import javax.naming.directory.Attributes;
13  import javax.naming.directory.BasicAttribute;
14  import javax.naming.directory.BasicAttributes;
15  
16  import org.apache.avalon.framework.logger.LogEnabled;
17  import org.apache.avalon.framework.service.ServiceException;
18  import org.apache.avalon.framework.service.ServiceManager;
19  import org.apache.avalon.framework.service.Serviceable;
20  import org.apache.commons.beanutils.BeanUtils;
21  import org.apache.commons.beanutils.ConvertUtils;
22  import org.apache.commons.beanutils.Converter;
23  import org.apache.commons.beanutils.PropertyUtils;
24  import org.jellyfish.ConfigurationManager;
25  import org.jellyfish.MappingManager;
26  import org.jellyfish.ObjectMapNotFoundException;
27  import org.jellyfish.config.AttributeMapping;
28  import org.jellyfish.config.ObjectMapping;
29  
30  public class MappingManagerDefault
31    implements MappingManager, Serviceable, LogEnabled {
32    private static MappingManagerDefault instance;
33  
34    private static final String START_DN_ATTRIBUTE = "${";
35    private static final String END_DN_ATTRIBUTE = "}";
36  
37    private Map objectMappingCache = new HashMap();
38  
39    private ServiceManager serviceManager;
40  
41    private org.apache.avalon.framework.logger.Logger logger;
42  
43    private ConfigurationManager configurationManager;
44  
45    public MappingManagerDefault() {
46    }
47  
48    public void service(ServiceManager serviceManager)
49      throws ServiceException {
50      this.serviceManager = serviceManager;
51      try {
52        configurationManager =
53          (ConfigurationManager) serviceManager.lookup(
54            ConfigurationManager.ROLE);
55      } catch (ServiceException e) {
56        e.printStackTrace();
57        throw new RuntimeException(e);
58      }
59    }
60  
61    public void enableLogging(
62      org.apache.avalon.framework.logger.Logger logger) {
63      this.logger = logger;
64    }
65  
66    private Converter createConverter() {
67      try {
68        return (Converter) serviceManager.lookup(Converter.class.getName());
69      } catch (ServiceException e) {
70        e.printStackTrace();
71        throw new RuntimeException(e);
72      }
73    }
74    public Attributes mapToAttributes(Object object) {
75      Attributes attributes = new BasicAttributes();
76      try {
77        for (Iterator iterator =
78          getObjectMapping(object).getAttributeMappings().iterator();
79          iterator.hasNext();
80          ) {
81          AttributeMapping attributeMapping =
82            (AttributeMapping) iterator.next();
83          Object value =
84            getObjectProperty(object, attributeMapping.getJavaName());
85          storeValueInAttributes(
86            attributes,
87            attributeMapping.getLdapName(),
88            value);
89        }
90      } catch (ObjectMapNotFoundException e) {
91        logException(object, e);
92      }
93      return attributes;
94    }
95  
96    private void storeValueInAttributes(
97      Attributes attributes,
98      String name,
99      Object value) {
100     if (value instanceof Collection) {
101       for (Iterator iterator = ((Collection) value).iterator();
102         iterator.hasNext();
103         ) {
104         Object subValue = iterator.next();
105         storeValueInAttributes(attributes, name, subValue);
106       }
107     } else if (value instanceof Object[]) {
108       for (int index = 0; index < ((Object[]) value).length; index++) {
109         storeValueInAttributes(
110           attributes,
111           name,
112           ((Object[]) value)[index]);
113       }
114     } else {
115       if (isMappedClass(value)) {
116         value = getObjectDn(value);
117       }
118 
119       if (value != null) {
120         Attribute attribute = attributes.get(name);
121         if (attribute == null) {
122           attribute = new BasicAttribute(name);
123           attributes.put(attribute);
124         }
125         attribute.add(value);
126       }
127     }
128   }
129 
130   private boolean isMappedClass(Object value) {
131     return value != null
132       && objectMappingCache.containsKey(value.getClass());
133   }
134 
135   public Object mapFromAttributes(Attributes attributes) {
136     Object object = null;
137     try {
138       ObjectMapping objectMapping = getObjectMapping(attributes);
139       object = Class.forName(objectMapping.getJavaName()).newInstance();
140       Map objectProperties = new HashMap();
141       for (Iterator iterator =
142         objectMapping.getAttributeMappings().iterator();
143         iterator.hasNext();
144         ) {
145         AttributeMapping attributeMapping =
146           (AttributeMapping) iterator.next();
147         Attribute attribute =
148           attributes.get(attributeMapping.getLdapName());
149 
150         objectProperties.put(
151           attributeMapping.getJavaName(),
152           getValueFromAttribute(
153             attribute,
154             object,
155             attributeMapping.getJavaName(),
156             attributeMapping.getType()));
157       }
158       setObjectProperties(object, objectProperties);
159     } catch (ObjectMapNotFoundException e) {
160       logger.fatalError(e.getMessage(), e);
161     } catch (InstantiationException e) {
162       logger.fatalError(e.getMessage(), e);
163     } catch (IllegalAccessException e) {
164       logger.fatalError(e.getMessage(), e);
165     } catch (ClassNotFoundException e) {
166       logger.fatalError(e.getMessage(), e);
167     } catch (NamingException e) {
168       logger.fatalError(e.getMessage(), e);
169     }
170     return object;
171   }
172 
173   private Object getValueFromAttribute(
174     Attribute attribute,
175     Object object,
176     String propertyName,
177     String propertyType)
178     throws
179       ClassNotFoundException,
180       NamingException,
181       InstantiationException,
182       IllegalAccessException {
183     if (attribute == null) {
184       return null;
185     }
186 
187     Object value = null;
188     Class propertyClass = null;
189     if (propertyType == null || propertyType.trim().length() == 0) {
190       propertyClass = getTypeOfProperty(object, propertyName);
191     } else {
192       propertyClass = Class.forName(propertyType);
193     }
194 
195     if (Collection.class.isAssignableFrom(propertyClass)) {
196       value =
197         getCollectionFromAttribute(
198           attribute,
199           value,
200           propertyName,
201           (Collection) propertyClass.newInstance());
202     } else if (
203       Object[].class.isAssignableFrom(
204         getTypeOfProperty(object, propertyName))) {
205       value = getArrayFromAttribute(attribute, value, propertyName);
206     } else {
207       value = attribute.get();
208     }
209     return value;
210   }
211 
212   private Object getInstanceOfProperty(Object object, String propertyName) {
213     try {
214       return getTypeOfProperty(object, propertyName).newInstance();
215     } catch (InstantiationException e) {
216       logException(object, e);
217     } catch (IllegalAccessException e) {
218       logException(object, e);
219     }
220     return null;
221   }
222 
223   private Class getTypeOfProperty(Object object, String propertyName) {
224     try {
225       PropertyDescriptor descriptor =
226         PropertyUtils.getPropertyDescriptor(object, propertyName);
227       return descriptor.getPropertyType();
228     } catch (IllegalAccessException e) {
229       logException(object, e);
230     } catch (InvocationTargetException e) {
231       logException(object, e);
232     } catch (NoSuchMethodException e) {
233       logException(object, e);
234     }
235     return null;
236   }
237 
238   private Collection getCollectionFromAttribute(
239     Attribute attribute,
240     Object object,
241     String propertyName,
242     Collection container)
243     throws NamingException {
244     if (attribute != null) {
245       for (int index = 0; index < attribute.size(); index++) {
246         container.add(attribute.get(index));
247       }
248     }
249     return container;
250   }
251 
252   private Object[] getArrayFromAttribute(
253     Attribute attribute,
254     Object object,
255     String propertyName)
256     throws NamingException {
257     String[] values = new String[attribute.size()];
258     for (int index = 0; index < values.length; index++) {
259       values[index] = (String) attribute.get(index);
260     }
261     return values;
262   }
263 
264   private void setObjectProperties(Object object, Map properties) {
265     try {
266       BeanUtils.populate(object, properties);
267     } catch (IllegalAccessException e) {
268       logException(object, e);
269     } catch (InvocationTargetException e) {
270       logException(object, e);
271     } catch (IllegalArgumentException e) {
272       logException(object, e);
273     }
274   }
275 
276   private Object getObjectProperty(Object object, String property) {
277     try {
278       return PropertyUtils.getProperty(object, property);
279     } catch (IllegalAccessException e) {
280       logException(object, e);
281     } catch (InvocationTargetException e) {
282       logException(object, e);
283     } catch (NoSuchMethodException e) {
284       logException(object, e);
285     }
286     return "";
287   }
288 
289   public ObjectMapping getObjectMapping(Object object)
290     throws ObjectMapNotFoundException {
291     if (objectMappingCache.containsKey(object)) {
292       return (ObjectMapping) objectMappingCache.get(object.getClass());
293     }
294 
295     for (Iterator iterator =
296       configurationManager
297         .getConfiguration()
298         .getObjectMappings()
299         .iterator();
300       iterator.hasNext();
301       ) {
302       ObjectMapping objectMapping = (ObjectMapping) iterator.next();
303       if (objectMapping
304         .getJavaName()
305         .equals(object.getClass().getName())) {
306         objectMappingCache.put(object.getClass(), objectMapping);
307         ConvertUtils.register(createConverter(), object.getClass());
308         return objectMapping;
309       }
310     }
311 
312     throw new ObjectMapNotFoundException(object);
313   }
314 
315   public ObjectMapping getObjectMapping(Attributes attributes)
316     throws ObjectMapNotFoundException {
317     String ldapName = null;
318     try {
319       if (attributes.get(OBJECTCLASS) != null) {
320         ldapName = (String) attributes.get(OBJECTCLASS).get();
321         for (Iterator iterator =
322           configurationManager
323             .getConfiguration()
324             .getObjectMappings()
325             .iterator();
326           iterator.hasNext();
327           ) {
328           ObjectMapping objectMapping =
329             (ObjectMapping) iterator.next();
330           if (objectMapping
331             .getLdapName()
332             .equals(attributes.get(OBJECTCLASS).get())) {
333             return objectMapping;
334           }
335         }
336       } else {
337         logger.fatalError("No objectclass attribute!");
338       }
339     } catch (NamingException exception) {
340       logger.fatalError(exception.getMessage(), exception);
341     }
342 
343     throw new ObjectMapNotFoundException(ldapName);
344   }
345 
346   public String getUserDn(String username) {
347 
348     String dnPattern = configurationManager.getConfiguration().getUserDn();
349     int dnAttributeStart = dnPattern.indexOf(START_DN_ATTRIBUTE);
350     int dnAttributeEnd = dnPattern.indexOf(END_DN_ATTRIBUTE);
351     String dnAttribute =
352       dnPattern.substring(
353         dnAttributeStart + START_DN_ATTRIBUTE.length(),
354         dnAttributeEnd);
355     StringBuffer dn = new StringBuffer();
356     dn.append(dnPattern.substring(0, dnAttributeStart));
357     dn.append(username);
358     dn.append(
359       dnPattern.substring(dnAttributeEnd + END_DN_ATTRIBUTE.length()));
360     if (configurationManager.getConfiguration().getBaseDn().length() > 0) {
361       dn.append(',');
362     }
363     dn.append(configurationManager.getConfiguration().getBaseDn());
364     return dn.toString();
365   }
366 
367   public String getObjectDn(Object object) {
368     try {
369       String dnPattern = getObjectMapping(object).getDn();
370       int dnAttributeStart = dnPattern.indexOf(START_DN_ATTRIBUTE);
371       int dnAttributeEnd = dnPattern.indexOf(END_DN_ATTRIBUTE);
372       String dnAttribute =
373         dnPattern.substring(
374           dnAttributeStart + START_DN_ATTRIBUTE.length(),
375           dnAttributeEnd);
376       StringBuffer dn = new StringBuffer();
377       dn.append(dnPattern.substring(0, dnAttributeStart));
378       dn.append(getObjectProperty(object, dnAttribute));
379       dn.append(
380         dnPattern.substring(
381           dnAttributeEnd + END_DN_ATTRIBUTE.length()));
382       if (configurationManager.getConfiguration().getBaseDn().length()
383         > 0) {
384         dn.append(',');
385       }
386       dn.append(configurationManager.getConfiguration().getBaseDn());
387       return dn.toString();
388     } catch (ObjectMapNotFoundException e) {
389       logException(object, e);
390     }
391     return "";
392   }
393 
394   private void logException(Object object, Exception exception) {
395     logger.fatalError("Mapping " + object + ": " + exception.getMessage());
396     exception.printStackTrace();
397   }
398 }