View Javadoc

1   /*
2    * Copyright 2004-2005 The Apache Software Foundation or its licensors,
3    *                     as applicable.
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.portals.graffito.jcr.persistence.atomictypeconverter.impl;
18  
19  import java.util.HashMap;
20  import java.util.Iterator;
21  import java.util.Map;
22  
23  import org.apache.portals.graffito.jcr.exception.IncorrectAtomicTypeException;
24  import org.apache.portals.graffito.jcr.persistence.atomictypeconverter.AtomicTypeConverter;
25  import org.apache.portals.graffito.jcr.persistence.atomictypeconverter.AtomicTypeConverterProvider;
26  
27  
28  /***
29   * Implementation of {@link AtomicTypeConverterProvider}.
30   * 
31   * @author <a href='mailto:the_mindstorm[at]evolva[dot]ro'>Alexandru Popescu</a>
32   */
33  public class AtomicTypeConverterProviderImpl implements AtomicTypeConverterProvider {
34      protected Map m_converters;
35      protected Map m_converterInstances = new HashMap();
36      
37      /***
38       * No-arg constructor.
39       */
40      public AtomicTypeConverterProviderImpl() {
41      }
42      
43      /***
44       * Full constructor.
45       * 
46       * @param converters a map of classes and their associated <code>AtomicTypeConverter</code>
47       * classes.
48       */
49      public AtomicTypeConverterProviderImpl(Map converters) {
50          m_converters= converters;
51      }
52      
53      /***
54       * Sets the associations of classes and their <code>AtomicTypeConverter</code>
55       * classes.
56       * 
57       * @param converters <code>Map<Class, Class></code>
58       */
59      public void setAtomicTypeConvertors(Map converters) {
60          m_converters= converters;
61      }
62      
63      /***
64       * @see org.apache.portals.graffito.jcr.persistence.atomictypeconverter.AtomicTypeConverterProvider#getAtomicTypeConverter(java.lang.Class)
65       */
66      public AtomicTypeConverter getAtomicTypeConverter(Class clazz) {
67          AtomicTypeConverter converter= (AtomicTypeConverter) m_converterInstances.get(clazz);
68          if(null != converter) {
69              return converter;
70          }
71          Class converterClass= (Class) m_converters.get(clazz);
72          if(null == converterClass) {
73              throw new IncorrectAtomicTypeException("No registered converter for class '" + clazz + "'");
74          }
75          
76          try {
77              converter= (AtomicTypeConverter) converterClass.newInstance();
78              m_converterInstances.put(clazz, converter);
79          }
80          catch(Exception ex) {
81              throw new IncorrectAtomicTypeException(
82                      "Cannot create converter instance from class '" + clazz + "'", ex);
83              
84          }
85          
86          return converter;
87      }
88      
89      /***
90       * @see org.apache.portals.graffito.jcr.persistence.atomictypeconverter.AtomicTypeConverterProvider#getAtomicTypeConverters()
91       */
92      public Map getAtomicTypeConverters() {
93          Map result= new HashMap();
94          for(Iterator it= m_converters.keySet().iterator(); it.hasNext(); ) {
95              Class clazz= (Class) it.next();
96              result.put(clazz, getAtomicTypeConverter(clazz));
97          }
98          
99          return result;
100     }
101 }