diff options
author | dims <dims@13f79535-47bb-0310-9956-ffa450edef68> | 2008-06-17 00:23:01 +0000 |
---|---|---|
committer | dims <dims@13f79535-47bb-0310-9956-ffa450edef68> | 2008-06-17 00:23:01 +0000 |
commit | bdd0a41aed7edf21ec2a65cfa17a86af2ef8c48a (patch) | |
tree | 38a92061c0793434c4be189f1d70c3458b6bc41d /sandbox/kgoodson/mappingFramework/sdo-snapshot/src/main/java/com/agfa/hap/sdo/mapper |
Move Tuscany from Incubator to top level.
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@668359 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sandbox/kgoodson/mappingFramework/sdo-snapshot/src/main/java/com/agfa/hap/sdo/mapper')
15 files changed, 1108 insertions, 0 deletions
diff --git a/sandbox/kgoodson/mappingFramework/sdo-snapshot/src/main/java/com/agfa/hap/sdo/mapper/AbstractPropertyAccessor.java b/sandbox/kgoodson/mappingFramework/sdo-snapshot/src/main/java/com/agfa/hap/sdo/mapper/AbstractPropertyAccessor.java new file mode 100644 index 0000000000..34eaa3ec5f --- /dev/null +++ b/sandbox/kgoodson/mappingFramework/sdo-snapshot/src/main/java/com/agfa/hap/sdo/mapper/AbstractPropertyAccessor.java @@ -0,0 +1,54 @@ +/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 com.agfa.hap.sdo.mapper;
+
+import java.util.Collection;
+
+import sun.reflect.generics.reflectiveObjects.NotImplementedException;
+
+import com.agfa.hap.sdo.DataMapper;
+import com.agfa.hap.sdo.Property;
+import com.agfa.hap.sdo.SnapshotDefinition;
+
+/**
+ * Abstract shell implementation for {@link PropertyAccessor}.
+ * @author AMOCZ
+ */
+public abstract class AbstractPropertyAccessor implements PropertyAccessor {
+
+ public Object getValue(Object instance, Property property,
+ DataMapper dataMapper) {
+ return null;
+ }
+
+ public Collection<?> getValues(Collection<?> instances, Property property,
+ SnapshotDefinition def, DataMapper dataMapper) {
+ throw new NotImplementedException();
+ }
+
+ public boolean isBulkAccessor() {
+ return false;
+ }
+
+ public void setValue(Object instance, Property property, Object value,
+ DataMapper dataMapper) {
+ }
+
+}
diff --git a/sandbox/kgoodson/mappingFramework/sdo-snapshot/src/main/java/com/agfa/hap/sdo/mapper/BeanPropertyAccessor.java b/sandbox/kgoodson/mappingFramework/sdo-snapshot/src/main/java/com/agfa/hap/sdo/mapper/BeanPropertyAccessor.java new file mode 100644 index 0000000000..010d9aea11 --- /dev/null +++ b/sandbox/kgoodson/mappingFramework/sdo-snapshot/src/main/java/com/agfa/hap/sdo/mapper/BeanPropertyAccessor.java @@ -0,0 +1,98 @@ +/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 com.agfa.hap.sdo.mapper;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.Collection;
+
+import sun.reflect.generics.reflectiveObjects.NotImplementedException;
+
+import com.agfa.hap.sdo.DataMapper;
+import com.agfa.hap.sdo.Property;
+import com.agfa.hap.sdo.SnapshotDefinition;
+
+/**
+ * PropertyAccessor that follows bean conventions (i.e. getX() methods) to get values.
+ * @author AMOCZ
+ */
+public class BeanPropertyAccessor implements PropertyAccessor {
+
+ private static final Object[] EMPTY_ARGS = new Object[0];
+
+ public Object getValue(Object instance, Property property, DataMapper dataMapper) {
+ if (!isReadable()) {
+ throw new IllegalArgumentException(instance.getClass() + " doesn't have a getter for property " + property.getName());
+ }
+ try {
+ return getter.invoke(instance, EMPTY_ARGS);
+ } catch (InvocationTargetException e) {
+ throw new IllegalArgumentException("Couldn't access property " + property.toString(), e.getTargetException());
+ } catch (IllegalAccessException e) {
+ throw new IllegalArgumentException("Couldn't access property " + property.toString(), e);
+ }
+ }
+
+ /**
+ * Sets the value of this property for the specified Object.
+ * @throws IllegalArgumentException
+ */
+ public void setValue(Object instance, Property property, Object newValue, DataMapper dataMapper) {
+ if (!isWritable()) {
+ throw new IllegalArgumentException(instance.getClass() + " doesn't have a setter for property " + property.getName());
+ }
+ try {
+ setter.invoke(instance, new Object[] { newValue });
+ } catch (InvocationTargetException e) {
+ throw new IllegalArgumentException("Couldn't access property " + property.toString(), e.getTargetException());
+ } catch (IllegalAccessException e) {
+ throw new IllegalArgumentException("Couldn't access property " + property.toString(), e);
+ }
+ }
+
+ public void setGetter(Method method) {
+ this.getter = method;
+ }
+
+ public void setSetter(Method method) {
+ this.setter = method;
+ }
+
+ public boolean isReadable() {
+ return getter != null;
+ }
+
+ public boolean isWritable() {
+ return setter != null;
+ }
+
+
+ public Collection<?> getValues(Collection<?> instances, Property property, SnapshotDefinition def, DataMapper dataMapper) {
+ throw new NotImplementedException();
+ }
+
+ public boolean isBulkAccessor() {
+ return false;
+ }
+
+
+ private Method setter;
+ private Method getter;
+}
diff --git a/sandbox/kgoodson/mappingFramework/sdo-snapshot/src/main/java/com/agfa/hap/sdo/mapper/BeanPropertyAccessorBuilder.java b/sandbox/kgoodson/mappingFramework/sdo-snapshot/src/main/java/com/agfa/hap/sdo/mapper/BeanPropertyAccessorBuilder.java new file mode 100644 index 0000000000..0f8add6e7e --- /dev/null +++ b/sandbox/kgoodson/mappingFramework/sdo-snapshot/src/main/java/com/agfa/hap/sdo/mapper/BeanPropertyAccessorBuilder.java @@ -0,0 +1,81 @@ +/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 com.agfa.hap.sdo.mapper;
+
+import java.lang.reflect.Method;
+
+import com.agfa.hap.sdo.Property;
+import com.agfa.hap.sdo.Type;
+
+/**
+ * PropertyAccessorBuilder that uses Java Bean conventions to extract properties from an object.
+ * @author AMOCZ
+ */
+public class BeanPropertyAccessorBuilder extends PropertyAccessorBuilder {
+
+ public BeanPropertyAccessorBuilder() {
+ super();
+ }
+
+ public BeanPropertyAccessorBuilder(PropertyAccessorBuilder next) {
+ super(next);
+ }
+
+ @Override
+ protected boolean accepts(Class cls, Type type) {
+ return true;
+ }
+
+ @Override
+ protected PropertyAccessor createPropertyAccessor(Class cls, Property prop) {
+ String propertyName = Character.toUpperCase(prop.getName().charAt(0)) + prop.getName().substring(1);
+ String methodName;
+ if (prop.getType().getInstanceClass() == Boolean.class) {
+ if (propertyName.startsWith("Is")){
+ methodName = prop.getName();
+ } else {
+ methodName = "is" + propertyName;
+ }
+ } else {
+ methodName = "get" + propertyName;
+ }
+ BeanPropertyAccessor property = prop.isMany() ? new ManyValuedBeanPropertyAccessor() : new BeanPropertyAccessor();
+ try {
+ Method method = cls.getMethod(methodName, (Class[]) null);
+ property.setGetter(method);
+ method = getSetMethod(cls, propertyName);
+ property.setSetter(method);
+ return property;
+ } catch (NoSuchMethodException e) {
+ throw new IllegalArgumentException("Can't find property " + prop.getName() + " on class " + cls.getName(), e);
+ }
+ }
+
+ // we assume there is no type overloading
+ private Method getSetMethod(Class cls, String propertyName) {
+ String methodName = "set" + propertyName;
+ for (Method m : cls.getMethods()) {
+ if (m.getName().equals(methodName)) {
+ return m;
+ }
+ }
+ return null;
+ }
+}
diff --git a/sandbox/kgoodson/mappingFramework/sdo-snapshot/src/main/java/com/agfa/hap/sdo/mapper/DelegatingDataMapper.java b/sandbox/kgoodson/mappingFramework/sdo-snapshot/src/main/java/com/agfa/hap/sdo/mapper/DelegatingDataMapper.java new file mode 100644 index 0000000000..53b297dfcc --- /dev/null +++ b/sandbox/kgoodson/mappingFramework/sdo-snapshot/src/main/java/com/agfa/hap/sdo/mapper/DelegatingDataMapper.java @@ -0,0 +1,90 @@ +/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 com.agfa.hap.sdo.mapper;
+
+import java.util.Collection;
+import java.util.Iterator;
+
+import com.agfa.hap.sdo.DataMapper;
+import com.agfa.hap.sdo.Property;
+import com.agfa.hap.sdo.SnapshotDefinition;
+import com.agfa.hap.sdo.Type;
+
+public class DelegatingDataMapper<T> implements DataMapper<T> {
+
+ private DataMapper<T> principalMapper;
+ private DataMapper<T> secondaryMapper;
+
+ public DelegatingDataMapper(DataMapper<T> principalMapper, DataMapper<T> secondaryMapper){
+ this.principalMapper = principalMapper;
+ this.secondaryMapper = secondaryMapper;
+ }
+
+ public T create(Type type) {
+ T newlyCreated = principalMapper.create(type);
+ if (newlyCreated == null){
+ return secondaryMapper.create(type);
+ }
+ return newlyCreated;
+ }
+
+ public Type getCorrespondingType(Class clazz) {
+ return principalMapper.getCorrespondingType(clazz);
+ }
+
+ public Iterator<? extends T> getObjects(T object, Property property) {
+ return principalMapper.getObjects(object, property);
+ }
+
+ public Object getProperty(T object, Property property) {
+ return principalMapper.getProperty(object, property);
+ }
+
+ public Type getType(T object) {
+ return principalMapper.getType(object);
+ }
+
+ public boolean isProxy(T object) {
+ return principalMapper.isProxy(object);
+ }
+
+ public T newProxy(Type type, Object identity) {
+ return principalMapper.newProxy(type, identity);
+ }
+
+ public void setProperty(T object, Property property, Object value) {
+ principalMapper.setProperty(object, property, value);
+
+ }
+
+ public void setUnavailable(T object, Property property) {
+ principalMapper.setUnavailable(object, property);
+
+ }
+
+ public Collection<T> getProperties(Collection<T> object, Property bulkProperty, SnapshotDefinition def) {
+ return principalMapper.getProperties(object, bulkProperty, def);
+ }
+
+ public boolean isBulkProperty(Class clazz, Property property) {
+ return principalMapper.isBulkProperty(clazz, property);
+ }
+
+}
diff --git a/sandbox/kgoodson/mappingFramework/sdo-snapshot/src/main/java/com/agfa/hap/sdo/mapper/DelegatingPartialDataObjectMapper.java b/sandbox/kgoodson/mappingFramework/sdo-snapshot/src/main/java/com/agfa/hap/sdo/mapper/DelegatingPartialDataObjectMapper.java new file mode 100644 index 0000000000..5976aa2b57 --- /dev/null +++ b/sandbox/kgoodson/mappingFramework/sdo-snapshot/src/main/java/com/agfa/hap/sdo/mapper/DelegatingPartialDataObjectMapper.java @@ -0,0 +1,138 @@ +/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 com.agfa.hap.sdo.mapper;
+
+import java.util.Collection;
+import java.util.Iterator;
+
+import com.agfa.hap.sdo.DataMapper;
+import com.agfa.hap.sdo.PartialDataObject;
+import com.agfa.hap.sdo.Property;
+import com.agfa.hap.sdo.SnapshotDefinition;
+import com.agfa.hap.sdo.Type;
+
+/**
+ * @author awvjz
+ *
+ * this datamapper wraps another datamapper and delegates to this second mapper in case the object it has to work with are not partialdataobjects
+ *
+ */
+public class DelegatingPartialDataObjectMapper<T> implements DataMapper<T> {
+ private DataMapper<T> delegate;
+ private PartialDataObjectMapper defaultMapper = new PartialDataObjectMapper();
+
+ public DelegatingPartialDataObjectMapper(DataMapper<T> delegateDataMapper){
+ delegate = delegateDataMapper;
+ }
+
+ public T create(Type type) {
+ T instance = delegate.create(type);
+ if (instance == null){
+ return (T) defaultMapper.create(type);
+ }
+ return instance;
+ }
+
+ public Type getCorrespondingType(Class clazz) {
+ Type type = null;
+ try {
+ type = delegate.getCorrespondingType(clazz);
+ } catch (IllegalArgumentException e){
+ //no type was found
+ }
+ if (type == null){
+ return defaultMapper.getCorrespondingType(clazz);
+ }
+ return type;
+ }
+
+
+ public Object getProperty(T object, Property property) {
+ if (object instanceof PartialDataObject){
+ return defaultMapper.getProperty((PartialDataObject) object, property);
+ }
+ return delegate.getProperty(object, property);
+ }
+
+
+ public T newProxy(Type type, Object identity) {
+ T proxy = delegate.newProxy(type, identity);
+ if (proxy == null){
+ return (T) defaultMapper.newProxy(type, identity);
+ }
+ return proxy;
+ }
+
+
+ public Iterator<? extends T> getObjects(T object, Property property) {
+ if (object instanceof PartialDataObject){
+ return (Iterator<? extends T>) defaultMapper.getObjects((PartialDataObject) object, property);
+ }
+ return delegate.getObjects(object, property);
+ }
+
+
+ public Type getType(T object) {
+ if (object instanceof PartialDataObject){
+ return defaultMapper.getType((PartialDataObject) object);
+ }
+ return delegate.getType(object);
+ }
+
+
+ public boolean isProxy(T object) {
+ if (object instanceof PartialDataObject){
+ return defaultMapper.isProxy((PartialDataObject) object);
+ }
+ return delegate.isProxy(object);
+ }
+
+
+ public void setProperty(T object, Property property, Object value) {
+ if (object instanceof PartialDataObject){
+ defaultMapper.setProperty((PartialDataObject) object, property, value);
+ } else {
+ delegate.setProperty(object, property, value);
+ }
+ }
+
+
+ public void setUnavailable(T object, Property property) {
+ if (object instanceof PartialDataObject){
+ defaultMapper.setUnavailable((PartialDataObject) object, property);
+ } else {
+ delegate.setUnavailable(object, property);
+ }
+ }
+
+ public Collection<T> getProperties(Collection<T> object, Property bulkProperty, SnapshotDefinition def) {
+ return delegate.getProperties(object, bulkProperty, def);
+ }
+
+ public boolean isBulkProperty(Class clazz, Property property) {
+ if (PartialDataObject.class.isAssignableFrom(clazz)) {
+ return false;
+ }
+ return delegate.isBulkProperty(clazz, property);
+ }
+
+
+
+}
diff --git a/sandbox/kgoodson/mappingFramework/sdo-snapshot/src/main/java/com/agfa/hap/sdo/mapper/FalsePropertyAccessor.java b/sandbox/kgoodson/mappingFramework/sdo-snapshot/src/main/java/com/agfa/hap/sdo/mapper/FalsePropertyAccessor.java new file mode 100644 index 0000000000..f66d6899ca --- /dev/null +++ b/sandbox/kgoodson/mappingFramework/sdo-snapshot/src/main/java/com/agfa/hap/sdo/mapper/FalsePropertyAccessor.java @@ -0,0 +1,32 @@ +/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 com.agfa.hap.sdo.mapper;
+
+import com.agfa.hap.sdo.DataMapper;
+import com.agfa.hap.sdo.Property;
+
+public class FalsePropertyAccessor extends AbstractPropertyAccessor {
+
+ public Object getValue(Object instance, Property property,
+ DataMapper dataMapper) {
+ return Boolean.FALSE;
+ }
+
+}
diff --git a/sandbox/kgoodson/mappingFramework/sdo-snapshot/src/main/java/com/agfa/hap/sdo/mapper/FilteringPartialDataObjectMapper.java b/sandbox/kgoodson/mappingFramework/sdo-snapshot/src/main/java/com/agfa/hap/sdo/mapper/FilteringPartialDataObjectMapper.java new file mode 100644 index 0000000000..15a7a2026e --- /dev/null +++ b/sandbox/kgoodson/mappingFramework/sdo-snapshot/src/main/java/com/agfa/hap/sdo/mapper/FilteringPartialDataObjectMapper.java @@ -0,0 +1,49 @@ +/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 com.agfa.hap.sdo.mapper;
+
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Set;
+
+import com.agfa.hap.sdo.PartialDataObject;
+import com.agfa.hap.sdo.Type;
+
+/**
+ * ObjectMapper that always considers certain types to be proxies
+ *
+ */
+public class FilteringPartialDataObjectMapper extends PartialDataObjectMapper {
+ Set<Type> typesToConsiderAsProxies;
+
+ public FilteringPartialDataObjectMapper(Collection<Type> typesToConsiderAsProxies){
+ this.typesToConsiderAsProxies = new HashSet<Type>(typesToConsiderAsProxies);
+ }
+
+ @Override
+ public boolean isProxy(PartialDataObject partialDataObject) {
+ if (typesToConsiderAsProxies.contains(partialDataObject.getType())){
+ return true;
+ }
+ return super.isProxy(partialDataObject);
+ }
+
+
+}
diff --git a/sandbox/kgoodson/mappingFramework/sdo-snapshot/src/main/java/com/agfa/hap/sdo/mapper/JavaBeanMapper.java b/sandbox/kgoodson/mappingFramework/sdo-snapshot/src/main/java/com/agfa/hap/sdo/mapper/JavaBeanMapper.java new file mode 100644 index 0000000000..f2c78af410 --- /dev/null +++ b/sandbox/kgoodson/mappingFramework/sdo-snapshot/src/main/java/com/agfa/hap/sdo/mapper/JavaBeanMapper.java @@ -0,0 +1,140 @@ +/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 com.agfa.hap.sdo.mapper;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
+
+import com.agfa.hap.sdo.DataMapper;
+import com.agfa.hap.sdo.Property;
+import com.agfa.hap.sdo.SnapshotDefinition;
+import com.agfa.hap.sdo.Type;
+
+/**
+ * DataMapper that uses Java Bean conventions to access data on java classes.
+ * This mapper assumes that for each {@link Property} there are appropriately named
+ * accessors defined on the corresponding class.
+ * Each {@link Type} is mapped to the java class with the same name. It is also possible
+ * to register a class as corresponding to a type.
+ * <p/>
+ * This DataMapper ensures that opposite properties are properly filled in. As such, objects will
+ * not be added multiple times to a many-valued property if that property has an opposite property.
+ * @author AMOCZ
+ */
+public class JavaBeanMapper implements DataMapper<Object> {
+
+ public JavaBeanMapper(TypeMapper typeMapper) {
+ this.typeMapper = typeMapper;
+ }
+
+ public Iterator<?> getObjects(Object object, Property property) {
+ PropertyAccessor propertyAccessor = typeMapper.property(object.getClass(), property);
+ if (propertyAccessor == null){
+ throw new RuntimeException("no property accessor for sdo property " + property);
+ }
+ return ((Collection<?>) propertyAccessor.getValue(object, property, this)).iterator();
+ }
+
+ public Object getProperty(Object object, Property property) {
+ PropertyAccessor propertyAccessor = typeMapper.property(object.getClass(), property);
+ if (propertyAccessor == null){
+ throw new RuntimeException("no property accessor for sdo property " + property);
+ }
+ return propertyAccessor.getValue(object, property, this);
+ }
+
+ public void setProperty(Object object, Property property, Object value) {
+ PropertyAccessor propertyAccessor = typeMapper.property(object.getClass(), property);
+ if (propertyAccessor == null){
+ throw new RuntimeException("no property accessor for sdo property " + property);
+ }
+ propertyAccessor.setValue(object, property, value, this);
+ if (property.getOpposite() != null && value != null) {
+ setOpposite(object, property, value);
+ }
+ }
+
+ protected void setOpposite(Object object, Property property, Object value) {
+ typeMapper.property(value.getClass(), property.getOpposite()).setValue(value, property.getOpposite(), object, this);
+ }
+
+ public void setUnavailable(Object object, Property property) {
+ }
+
+ public Type getType(Object object) {
+ return typeMapper.getCorrespondingType(object.getClass());
+ }
+
+ public Type getCorrespondingType(Class clazz) {
+ return typeMapper.getCorrespondingType(clazz);
+ }
+
+ public TypeMapper getTypeMapper() {
+ return typeMapper;
+ }
+
+ public Object create(Type type) {
+ Constructor<?> constructor = typeMapper.getConstructor(type);
+ if (constructor == null){
+ return null;
+ }
+ try {
+ return constructor.newInstance((Object[]) null);
+ } catch (InstantiationException e) {
+ throw new IllegalArgumentException("Unable to create new instance of bean class corresponding to " + type.getName(), e);
+ } catch (IllegalAccessException e) {
+ throw new IllegalArgumentException("Unable to create new instance of bean class corresponding to " + type.getName(), e);
+ } catch (InvocationTargetException e) {
+ throw new IllegalArgumentException("Unable to create new instance of bean class corresponding to " + type.getName(), e);
+ }
+ }
+
+ public Object newProxy(Type type, Object identity) {
+ return null;
+ }
+
+ public boolean isProxy(Object instance) {
+ return false;
+ }
+
+
+ public Collection<Object> getProperties(Collection<Object> objects, Property bulkProperty, SnapshotDefinition def) {
+ Iterator<Object> it = objects.iterator();
+ if (!it.hasNext()) {
+ return Collections.emptyList();
+ }
+ return (Collection<Object>) typeMapper.property(it.next().getClass(), bulkProperty).getValues(objects, bulkProperty, def, this);
+ }
+
+ public boolean isBulkProperty(Class clazz, Property property) {
+ PropertyAccessor propertyAccessor = typeMapper.property(clazz, property);
+ if (propertyAccessor == null){
+ throw new RuntimeException("no property accessor for sdo property " + property);
+ }
+ return propertyAccessor.isBulkAccessor();
+ }
+
+
+ private final TypeMapper typeMapper;
+
+}
diff --git a/sandbox/kgoodson/mappingFramework/sdo-snapshot/src/main/java/com/agfa/hap/sdo/mapper/ManyValuedBeanPropertyAccessor.java b/sandbox/kgoodson/mappingFramework/sdo-snapshot/src/main/java/com/agfa/hap/sdo/mapper/ManyValuedBeanPropertyAccessor.java new file mode 100644 index 0000000000..d9a8a640dd --- /dev/null +++ b/sandbox/kgoodson/mappingFramework/sdo-snapshot/src/main/java/com/agfa/hap/sdo/mapper/ManyValuedBeanPropertyAccessor.java @@ -0,0 +1,44 @@ +/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 com.agfa.hap.sdo.mapper;
+
+import java.util.Collection;
+
+import com.agfa.hap.sdo.DataMapper;
+import com.agfa.hap.sdo.Property;
+
+public class ManyValuedBeanPropertyAccessor extends BeanPropertyAccessor {
+
+ /**
+ * Sets the value of this property for the specified Object.
+ * @throws IllegalArgumentException
+ */
+ public void setValue(Object instance, Property property, Object newValue, DataMapper dataMapper) {
+ Collection<Object> coll = (Collection<Object>) super.getValue(instance, property, dataMapper);
+ if (property.getOpposite() == null || !coll.contains(newValue)) {
+ coll.add(newValue);
+ }
+ }
+
+ public void initialize(Object instance, Collection<Object> value) {
+ super.setValue(instance, null, value, null);
+ }
+
+}
diff --git a/sandbox/kgoodson/mappingFramework/sdo-snapshot/src/main/java/com/agfa/hap/sdo/mapper/NullPropertyAccessor.java b/sandbox/kgoodson/mappingFramework/sdo-snapshot/src/main/java/com/agfa/hap/sdo/mapper/NullPropertyAccessor.java new file mode 100644 index 0000000000..2ed9a1ac58 --- /dev/null +++ b/sandbox/kgoodson/mappingFramework/sdo-snapshot/src/main/java/com/agfa/hap/sdo/mapper/NullPropertyAccessor.java @@ -0,0 +1,36 @@ +/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 com.agfa.hap.sdo.mapper;
+
+import com.agfa.hap.sdo.DataMapper;
+import com.agfa.hap.sdo.Property;
+
+/**
+ * {@link PropertyAccessor} that allows returns null.
+ * @author AMOCZ
+ */
+public class NullPropertyAccessor extends AbstractPropertyAccessor {
+
+ public Object getValue(Object instance, Property property,
+ DataMapper dataMapper) {
+ return null;
+ }
+
+}
diff --git a/sandbox/kgoodson/mappingFramework/sdo-snapshot/src/main/java/com/agfa/hap/sdo/mapper/PartialDataObjectMapper.java b/sandbox/kgoodson/mappingFramework/sdo-snapshot/src/main/java/com/agfa/hap/sdo/mapper/PartialDataObjectMapper.java new file mode 100644 index 0000000000..ce4d446629 --- /dev/null +++ b/sandbox/kgoodson/mappingFramework/sdo-snapshot/src/main/java/com/agfa/hap/sdo/mapper/PartialDataObjectMapper.java @@ -0,0 +1,97 @@ +/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 com.agfa.hap.sdo.mapper;
+
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+
+import sun.reflect.generics.reflectiveObjects.NotImplementedException;
+
+import com.agfa.hap.sdo.DataMapper;
+import com.agfa.hap.sdo.PartialDataFactory;
+import com.agfa.hap.sdo.PartialDataObject;
+import com.agfa.hap.sdo.Property;
+import com.agfa.hap.sdo.SnapshotDefinition;
+import com.agfa.hap.sdo.Type;
+
+/**
+ * Default implementation that only support object that are instances
+ * of {@link PartialDataObject}.
+ * @author AMOCZ
+ */
+public class PartialDataObjectMapper implements DataMapper<PartialDataObject> {
+
+ @SuppressWarnings("unchecked")
+ public Iterator<PartialDataObject> getObjects(PartialDataObject object, Property property) {
+ return object.getList(property).iterator();
+ }
+
+ public Object getProperty(PartialDataObject object, Property property) {
+ return object.get(property);
+ }
+
+ public Type getType(PartialDataObject object) {
+ return object.getType();
+ }
+
+ public Type getCorrespondingType(Class clazz) {
+ throw new IllegalArgumentException("No sdo type for class " + clazz.getName());
+ }
+
+ public void setProperty(PartialDataObject object, Property property, Object value) {
+ if (property.isMany()) {
+ if (property.getOpposite() != null) {
+ ((PartialDataObject) value).set(property.getOpposite(), object);
+ } else {
+ object.getList(property).add(value);
+ }
+ } else {
+ object.set(property, value);
+ }
+ }
+
+ public void setUnavailable(PartialDataObject object, Property property) {
+ object.setUnavailable(property);
+ }
+
+ public PartialDataObject create(Type type) {
+ return PartialDataFactory.INSTANCE.create(type);
+ }
+
+ public PartialDataObject newProxy(Type type, Object identity) {
+ return PartialDataFactory.INSTANCE.createProxy(type, identity);
+ }
+
+ public boolean isProxy(PartialDataObject object) {
+ return object.isProxy();
+ }
+
+ public Collection<PartialDataObject> getProperties(Collection<PartialDataObject> object, Property bulkProperty, SnapshotDefinition def) {
+ throw new NotImplementedException();
+ }
+
+ public boolean isBulkProperty(Class clazz, Property property) {
+ return false;
+ }
+
+
+
+}
diff --git a/sandbox/kgoodson/mappingFramework/sdo-snapshot/src/main/java/com/agfa/hap/sdo/mapper/PropertyAccessor.java b/sandbox/kgoodson/mappingFramework/sdo-snapshot/src/main/java/com/agfa/hap/sdo/mapper/PropertyAccessor.java new file mode 100644 index 0000000000..242d111f3f --- /dev/null +++ b/sandbox/kgoodson/mappingFramework/sdo-snapshot/src/main/java/com/agfa/hap/sdo/mapper/PropertyAccessor.java @@ -0,0 +1,45 @@ +/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 com.agfa.hap.sdo.mapper;
+
+import java.util.Collection;
+
+import com.agfa.hap.sdo.DataMapper;
+import com.agfa.hap.sdo.Property;
+import com.agfa.hap.sdo.SnapshotDefinition;
+
+/**
+ * Interface that allows property values to be accessed in a generic way from any instance.
+ * Typically propertyaccessors will be registered in a sdo-propertyaccessors.properties file which is read by ExtendablePropertyAccessorBuilder
+ *
+ * To allow for efficient retrieval, some properties are always accessed in bulk. A typical
+ * example is a property for which a query needs to be made.
+ *
+ * @author AMOCZ
+ *
+ */
+public interface PropertyAccessor {
+
+ Object getValue(Object instance, Property property, DataMapper dataMapper);
+ void setValue(Object instance, Property property, Object value, DataMapper dataMapper);
+
+ boolean isBulkAccessor();
+ Collection<?> getValues(Collection<?> instances, Property property, SnapshotDefinition def, DataMapper dataMapper);
+}
diff --git a/sandbox/kgoodson/mappingFramework/sdo-snapshot/src/main/java/com/agfa/hap/sdo/mapper/PropertyAccessorBuilder.java b/sandbox/kgoodson/mappingFramework/sdo-snapshot/src/main/java/com/agfa/hap/sdo/mapper/PropertyAccessorBuilder.java new file mode 100644 index 0000000000..5a6627af14 --- /dev/null +++ b/sandbox/kgoodson/mappingFramework/sdo-snapshot/src/main/java/com/agfa/hap/sdo/mapper/PropertyAccessorBuilder.java @@ -0,0 +1,72 @@ +/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 com.agfa.hap.sdo.mapper;
+
+import com.agfa.hap.sdo.Property;
+import com.agfa.hap.sdo.Type;
+
+/**
+ * Factory class to build a map containing all {@link PropertyAccessor} instances for a given class.
+ * @author AMOCZ
+ */
+public abstract class PropertyAccessorBuilder {
+
+ public PropertyAccessorBuilder() {
+ this(null);
+ }
+
+ public PropertyAccessorBuilder(PropertyAccessorBuilder next) {
+ this.next = next;
+ }
+
+ public PropertyAccessor[] buildMap(Class cls, Type type) {
+ if (!accepts(cls, type)) {
+ return next == null ? null : next.buildMap(cls, type);
+ }
+ PropertyAccessor[] result = new PropertyAccessor[type.getProperties().size()];
+ for (Property prop : type.getProperties()) {
+ PropertyAccessor property = createPropertyAccessor(cls, prop);
+ result[prop.getIndex()] = property;
+ }
+ return result;
+ }
+
+ /**
+ * @return If this propertyAccessorBuilder can create propertyAccessors for this class/type
+ * combination.
+ */
+ protected abstract boolean accepts(Class cls, Type type);
+
+ protected abstract PropertyAccessor createPropertyAccessor(Class cls, Property property);
+
+ protected PropertyAccessorBuilder next;
+
+ public PropertyAccessor createPropertyAccessorOrDelegate(Class cls, Property property){
+ if (this.accepts(cls, property.getContainingType())){
+ return this.createPropertyAccessor(cls, property);
+ } else {
+ if (next == null){
+ return null;
+ }
+ return next.createPropertyAccessor(cls, property);
+ }
+ }
+
+}
diff --git a/sandbox/kgoodson/mappingFramework/sdo-snapshot/src/main/java/com/agfa/hap/sdo/mapper/TruePropertyAccessor.java b/sandbox/kgoodson/mappingFramework/sdo-snapshot/src/main/java/com/agfa/hap/sdo/mapper/TruePropertyAccessor.java new file mode 100644 index 0000000000..a62824a0f1 --- /dev/null +++ b/sandbox/kgoodson/mappingFramework/sdo-snapshot/src/main/java/com/agfa/hap/sdo/mapper/TruePropertyAccessor.java @@ -0,0 +1,32 @@ +/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 com.agfa.hap.sdo.mapper;
+
+import com.agfa.hap.sdo.DataMapper;
+import com.agfa.hap.sdo.Property;
+
+public class TruePropertyAccessor extends AbstractPropertyAccessor {
+
+ public Object getValue(Object instance, Property property,
+ DataMapper dataMapper) {
+ return true;
+ }
+
+}
diff --git a/sandbox/kgoodson/mappingFramework/sdo-snapshot/src/main/java/com/agfa/hap/sdo/mapper/TypeMapper.java b/sandbox/kgoodson/mappingFramework/sdo-snapshot/src/main/java/com/agfa/hap/sdo/mapper/TypeMapper.java new file mode 100644 index 0000000000..40bc42443a --- /dev/null +++ b/sandbox/kgoodson/mappingFramework/sdo-snapshot/src/main/java/com/agfa/hap/sdo/mapper/TypeMapper.java @@ -0,0 +1,100 @@ +/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 com.agfa.hap.sdo.mapper;
+
+import java.lang.reflect.Constructor;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+import com.agfa.hap.sdo.Property;
+import com.agfa.hap.sdo.Type;
+import com.agfa.hap.sdo.helper.TypeHelper;
+
+/**
+ * Maps classes onto sdo Type instances.
+ * <P/>
+ * This implementation only on classes that are registered
+ * ({@see {@link #register(Class, String, String)}).
+ *
+ * <P />
+ * This class is thread safe and can be concurrently accessed by multiple threads.
+ * @author AMOCZ
+ */
+public class TypeMapper {
+
+ public TypeMapper() {
+ this(new BeanPropertyAccessorBuilder());
+ }
+
+ public TypeMapper(PropertyAccessorBuilder builder) {
+ this.propertyAccessorBuilder = builder;
+ }
+
+ /**
+ * Locates a property accessor
+ */
+ public PropertyAccessor property(Class cls, Property property) {
+ PropertyAccessor[] properties = buildMap(cls);
+ PropertyAccessor propertyAccessor = properties[property.getIndex()];
+ if (propertyAccessor == null) {
+ throw new IllegalArgumentException("Can't access property " + property.getName() + " on " + cls.getName() + ".");
+ }
+ return propertyAccessor;
+ }
+
+ private PropertyAccessor[] buildMap(Class cls) {
+ String clsName = cls.getName();
+ PropertyAccessor[] props = keyedByPropertyNameCache.get(clsName);
+ if (props == null) {
+ // we don't care too much if it is computed more than once under some race conditions
+ props = propertyAccessorBuilder.buildMap(cls, getCorrespondingType(cls));
+ keyedByPropertyNameCache.put(clsName, props);
+ }
+ return props;
+ }
+
+ public Type getCorrespondingType(Class clazz) {
+ String[] exc = exceptions.get(clazz);
+ if (exc != null) {
+ return TypeHelper.INSTANCE.getType(exc[0], exc[1]);
+ }
+ return null;
+ }
+
+ public void register(Class<?> clazz, String uri, String typeName) {
+ exceptions.put(clazz, new String[] { uri, typeName });
+ try {
+ factories.put(typeName, clazz.getConstructor((Class[]) null));
+ } catch (SecurityException e) {
+ } catch (NoSuchMethodException e) {
+ }
+ }
+
+ public Constructor<?> getConstructor(Type type) {
+ return factories.get(type.getName());
+ }
+
+ private final Map<String, Constructor<?>> factories = new ConcurrentHashMap<String, Constructor<?>>();
+ private final Map<Class<?>, String[]> exceptions = new ConcurrentHashMap<Class<?>, String[]>();
+ private final Map<String, PropertyAccessor[]> keyedByPropertyNameCache =
+ new ConcurrentHashMap<String, PropertyAccessor[]>();
+ protected final PropertyAccessorBuilder propertyAccessorBuilder;
+
+}
|