You are viewing a plain text version of this content. The canonical link for it is here.
Posted to graffito-commits@incubator.apache.org by ap...@apache.org on 2006/02/07 10:40:39 UTC
svn commit: r375572 - in
/incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr:
persistence/atomictypeconverter/ persistence/atomictypeconverter/impl/
reflection/
Author: apopescu
Date: Tue Feb 7 02:40:36 2006
New Revision: 375572
URL: http://svn.apache.org/viewcvs?rev=375572&view=rev
Log:
Added AtomicTypeConverterProvider, implementation and default
Added ReflectionUtils (decoupled ObjectConverter from reflection usage)
Added:
incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/atomictypeconverter/AtomicTypeConverterProvider.java
incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/atomictypeconverter/impl/AtomicTypeConverterProviderImpl.java
incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/atomictypeconverter/impl/DefaultAtomicTypeConverterProvider.java
incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/reflection/ReflectionUtils.java
Added: incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/atomictypeconverter/AtomicTypeConverterProvider.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/atomictypeconverter/AtomicTypeConverterProvider.java?rev=375572&view=auto
==============================================================================
--- incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/atomictypeconverter/AtomicTypeConverterProvider.java (added)
+++ incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/atomictypeconverter/AtomicTypeConverterProvider.java Tue Feb 7 02:40:36 2006
@@ -0,0 +1,45 @@
+/*
+ * Copyright 2004-2005 The Apache Software Foundation or its licensors,
+ * as applicable.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.portals.graffito.jcr.persistence.atomictypeconverter;
+
+
+import java.util.Map;
+
+/**
+ * This interface defines a provider for accessing
+ * {@link org.apache.portals.graffito.jcr.persistence.atomictypeconverter.AtomicTypeConverter}
+ *
+ * @author <a href='mailto:the_mindstorm[at]evolva[dot]ro'>Alexandru Popescu</a>
+ */
+public interface AtomicTypeConverterProvider {
+ /**
+ * Retrieves the <code>AtomicTypeConverter</code> associated with a class.
+ *
+ * @param clazz a class
+ * @return the corresponding <code>AtomicTypeConverter</code> or <tt>null</tt>
+ * if the class has no <code>AtomicTypeConverter</code> associated
+ */
+ AtomicTypeConverter getAtomicTypeConverter(Class clazz);
+
+ /**
+ * Returns a map of all registered <code>AtomicTypeConverter<code>s.
+ *
+ * @return <code>Map<Class, AtomicTypeConverter></code>
+ */
+ Map getAtomicTypeConverters();
+
+}
\ No newline at end of file
Added: incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/atomictypeconverter/impl/AtomicTypeConverterProviderImpl.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/atomictypeconverter/impl/AtomicTypeConverterProviderImpl.java?rev=375572&view=auto
==============================================================================
--- incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/atomictypeconverter/impl/AtomicTypeConverterProviderImpl.java (added)
+++ incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/atomictypeconverter/impl/AtomicTypeConverterProviderImpl.java Tue Feb 7 02:40:36 2006
@@ -0,0 +1,101 @@
+/*
+ * Copyright 2004-2005 The Apache Software Foundation or its licensors,
+ * as applicable.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.portals.graffito.jcr.persistence.atomictypeconverter.impl;
+
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+import org.apache.portals.graffito.jcr.exception.IncorrectAtomicTypeException;
+import org.apache.portals.graffito.jcr.persistence.atomictypeconverter.AtomicTypeConverter;
+import org.apache.portals.graffito.jcr.persistence.atomictypeconverter.AtomicTypeConverterProvider;
+
+
+/**
+ * Implementation of {@link AtomicTypeConverterProvider}.
+ *
+ * @author <a href='mailto:the_mindstorm[at]evolva[dot]ro'>Alexandru Popescu</a>
+ */
+public class AtomicTypeConverterProviderImpl implements AtomicTypeConverterProvider {
+ protected Map m_converters;
+ protected Map m_converterInstances = new HashMap();
+
+ /**
+ * No-arg constructor.
+ */
+ public AtomicTypeConverterProviderImpl() {
+ }
+
+ /**
+ * Full constructor.
+ *
+ * @param converters a map of classes and their associated <code>AtomicTypeConverter</code>
+ * classes.
+ */
+ public AtomicTypeConverterProviderImpl(Map converters) {
+ m_converters= converters;
+ }
+
+ /**
+ * Sets the associations of classes and their <code>AtomicTypeConverter</code>
+ * classes.
+ *
+ * @param converters <code>Map<Class, Class></code>
+ */
+ public void setAtomicTypeConvertors(Map converters) {
+ m_converters= converters;
+ }
+
+ /**
+ * @see org.apache.portals.graffito.jcr.persistence.atomictypeconverter.AtomicTypeConverterProvider#getAtomicTypeConverter(java.lang.Class)
+ */
+ public AtomicTypeConverter getAtomicTypeConverter(Class clazz) {
+ AtomicTypeConverter converter= (AtomicTypeConverter) m_converterInstances.get(clazz);
+ if(null != converter) {
+ return converter;
+ }
+ Class converterClass= (Class) m_converters.get(clazz);
+ if(null == converterClass) {
+ throw new IncorrectAtomicTypeException("No registered converter for class '" + clazz + "'");
+ }
+
+ try {
+ converter= (AtomicTypeConverter) converterClass.newInstance();
+ m_converterInstances.put(clazz, converter);
+ }
+ catch(Exception ex) {
+ throw new IncorrectAtomicTypeException(
+ "Cannot create converter instance from class '" + clazz + "'", ex);
+
+ }
+
+ return converter;
+ }
+
+ /**
+ * @see org.apache.portals.graffito.jcr.persistence.atomictypeconverter.AtomicTypeConverterProvider#getAtomicTypeConverters()
+ */
+ public Map getAtomicTypeConverters() {
+ Map result= new HashMap();
+ for(Iterator it= m_converters.keySet().iterator(); it.hasNext(); ) {
+ Class clazz= (Class) it.next();
+ result.put(clazz, getAtomicTypeConverter(clazz));
+ }
+
+ return result;
+ }
+}
Added: incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/atomictypeconverter/impl/DefaultAtomicTypeConverterProvider.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/atomictypeconverter/impl/DefaultAtomicTypeConverterProvider.java?rev=375572&view=auto
==============================================================================
--- incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/atomictypeconverter/impl/DefaultAtomicTypeConverterProvider.java (added)
+++ incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/atomictypeconverter/impl/DefaultAtomicTypeConverterProvider.java Tue Feb 7 02:40:36 2006
@@ -0,0 +1,81 @@
+/*
+ * Copyright 2004-2005 The Apache Software Foundation or its licensors,
+ * as applicable.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.portals.graffito.jcr.persistence.atomictypeconverter.impl;
+
+import java.io.InputStream;
+import java.sql.Timestamp;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.GregorianCalendar;
+import java.util.HashMap;
+import java.util.Map;
+
+
+/**
+ * An <code>AtomicTypeConverterProvider</code> that registers by default the
+ * convertes available in Graffito.
+ *
+ * @author <a href='mailto:the_mindstorm[at]evolva[dot]ro'>Alexandru Popescu</a>
+ */
+public class DefaultAtomicTypeConverterProvider extends AtomicTypeConverterProviderImpl {
+ /**
+ * No-arg constructor.
+ */
+ public DefaultAtomicTypeConverterProvider() {
+ m_converters= registerDefaultAtomicTypeConverters();
+ }
+
+ /**
+ * Full constructor.
+ *
+ * @param converters a map of classes and their associated <code>AtomicTypeConverter</code>
+ * classes.
+ */
+ public DefaultAtomicTypeConverterProvider(Map converters) {
+ this();
+ m_converters.putAll(converters);
+ }
+
+ /**
+ * @see org.apache.portals.graffito.jcr.persistence.atomictypeconverter.impl.AtomicTypeConverterProviderImpl#setAtomicTypeConvertors(java.util.Map)
+ */
+ public void setAtomicTypeConvertors(Map converters) {
+ m_converters.putAll(converters);
+ }
+
+ protected Map registerDefaultAtomicTypeConverters() {
+ Map converters= new HashMap();
+
+ converters.put(String.class, StringTypeConverterImpl.class);
+ converters.put(InputStream.class, BinaryTypeConverterImpl.class);
+ converters.put(long.class, LongTypeConverterImpl.class);
+ converters.put(Long.class, LongTypeConverterImpl.class);
+ converters.put(int.class, IntTypeConverterImpl.class);
+ converters.put(Integer.class, IntTypeConverterImpl.class);
+ converters.put(double.class, DoubleTypeConverterImpl.class);
+ converters.put(Double.class, DoubleTypeConverterImpl.class);
+ converters.put(boolean.class, BooleanTypeConverterImpl.class);
+ converters.put(Boolean.class, BooleanTypeConverterImpl.class);
+ converters.put(Calendar.class, CalendarTypeConverterImpl.class);
+ converters.put(GregorianCalendar.class, CalendarTypeConverterImpl.class);
+ converters.put(Date.class, UtilDateTypeConverterImpl.class);
+ converters.put(byte[].class, ByteArrayTypeConverterImpl.class);
+ converters.put(Timestamp.class, TimestampTypeConverterImpl.class);
+
+ return converters;
+ }
+}
Added: incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/reflection/ReflectionUtils.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/reflection/ReflectionUtils.java?rev=375572&view=auto
==============================================================================
--- incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/reflection/ReflectionUtils.java (added)
+++ incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/reflection/ReflectionUtils.java Tue Feb 7 02:40:36 2006
@@ -0,0 +1,91 @@
+package org.apache.portals.graffito.jcr.reflection;
+
+import java.lang.reflect.InvocationTargetException;
+
+import org.apache.commons.beanutils.ConstructorUtils;
+import org.apache.commons.beanutils.PropertyUtils;
+import org.apache.portals.graffito.jcr.exception.JcrMappingException;
+import org.apache.portals.graffito.jcr.persistence.collectionconverter.CollectionConverter;
+
+
+/**
+ * This class/interface
+ */
+abstract public class ReflectionUtils {
+ public static Object getNestedProperty(Object object, String fieldName) {
+ try {
+ return PropertyUtils.getNestedProperty(object, fieldName);
+ }
+ catch(IllegalAccessException e) {
+ throw new JcrMappingException("Cannot access property "
+ + fieldName,
+ e);
+ }
+ catch(InvocationTargetException e) {
+ throw new JcrMappingException("Cannot access property "
+ + fieldName,
+ e);
+ }
+ catch(NoSuchMethodException e) {
+ throw new JcrMappingException("Cannot access property "
+ + fieldName,
+ e);
+ }
+ }
+
+ public static Class getPropertyType(Object object, String fieldName) {
+ try {
+ return PropertyUtils.getPropertyType(object, fieldName);
+ }
+ catch(Exception ex) {
+ throw new JcrMappingException("Cannot access property "
+ + fieldName,
+ ex);
+ }
+ }
+
+ public static Object newInstance(Class clazz) {
+ try {
+ return clazz.newInstance();
+ }
+ catch(Exception ex) {
+ throw new JcrMappingException("Cannot create instance for class "
+ + clazz,
+ ex);
+ }
+ }
+
+ /**
+ * @param className
+ * @param objects
+ * @return
+ */
+ public static CollectionConverter invokeConstructor(String className,
+ Object[] params) {
+ try {
+ Class converterClass= Class.forName(className);
+
+ return (CollectionConverter) ConstructorUtils.invokeConstructor(converterClass, params);
+ }
+ catch(Exception ex) {
+ throw new JcrMappingException("Cannot create instance for class "
+ + className,
+ ex);
+ }
+ }
+
+ /**
+ * @param object
+ * @param fieldName
+ * @param path
+ */
+ public static void setNestedProperty(Object object, String fieldName, Object value) {
+ try {
+ PropertyUtils.setNestedProperty(object, fieldName, value);
+ }
+ catch(Exception ex) {
+ throw new JcrMappingException("Cannot set the field " + fieldName,
+ ex);
+ }
+ }
+}