org.openrdf.repository.object.advisers.PropertyMapperProvider Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of alibaba-repository-object Show documentation
Show all versions of alibaba-repository-object Show documentation
The Object Repository maps Java objects to and from RDF resources and OWL classes to Java classes in a non-intrusive manner that enables developers to work at the object level.
The newest version!
/*
* Copyright (c) 2012 3 Round Stones Inc., Some rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither the name of the openrdf.org nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
package org.openrdf.repository.object.advisers;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javassist.NotFoundException;
import org.openrdf.repository.object.advice.Advice;
import org.openrdf.repository.object.advisers.base.AbstractBehaviourFactory;
import org.openrdf.repository.object.advisers.helpers.PropertySetFactory;
import org.openrdf.repository.object.composition.BehaviourFactory;
import org.openrdf.repository.object.composition.BehaviourProvider;
import org.openrdf.repository.object.composition.ClassFactory;
import org.openrdf.repository.object.composition.ClassTemplate;
import org.openrdf.repository.object.exceptions.ObjectCompositionException;
import org.openrdf.repository.object.managers.PropertyMapper;
import org.openrdf.repository.object.traits.ObjectMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Creates BehaviourFactories to modify classes and changed their @Iri annotated
* properties to read/write to an RDF store.
*/
public class PropertyMapperProvider implements BehaviourProvider {
private static final Method intercept;
static {
try {
intercept = Advice.class.getMethod("intercept",
ObjectMessage.class);
} catch (NoSuchMethodException e) {
throw new AssertionError(e);
}
}
private final Logger logger = LoggerFactory.getLogger(PropertyMapperProvider.class);
private ClassFactory cp;
private PropertyMapper properties;
public void setClassDefiner(ClassFactory definer) {
this.cp = definer;
}
public void setBaseClasses(Set> bases) {
// TODO Auto-generated method stub
}
public void setPropertyMapper(PropertyMapper mapper) {
this.properties = mapper;
}
public Collection extends BehaviourFactory> getBehaviourFactories(
Collection> classes) throws ObjectCompositionException {
Set> faces = new HashSet>();
for (Class> i : classes) {
faces.add(i);
faces = getSuperClasses(i, faces);
}
List result;
result = new ArrayList(faces.size());
for (Class> concept : faces) {
result.addAll(getBehaviourFactories(concept));
}
return result;
}
private Set> getSuperClasses(Class> role, Set> set) {
for (Class> face : role.getInterfaces()) {
if (!set.contains(face)) {
set.add(face);
getSuperClasses(face, set);
}
}
Class> superclass = role.getSuperclass();
if (superclass != null) {
set.add(superclass);
getSuperClasses(superclass, set);
}
return set;
}
private Collection extends BehaviourFactory> getBehaviourFactories(
Class> concept) {
List result = new ArrayList();
for (Field field : findAllFields(concept, new ArrayList())) {
result.add(createFieldAdviser(concept, field));
}
for (PropertyDescriptor pd : properties.findProperties(concept)) {
result.add(createPropertyAdviser(pd));
}
return result;
}
private Collection findAllFields(Class> concept, Collection fields) {
fields.addAll(properties.findFields(concept));
Class> superclass = concept.getSuperclass();
if (superclass != null) {
findAllFields(superclass, fields);
}
return fields;
}
private BehaviourFactory createFieldAdviser(final Class> role, final Field field) {
final PropertySetFactory factory = new PropertySetFactory(
field, properties.findPredicate(field));
List methods = new ArrayList();
for (Method method : role.getDeclaredMethods()) {
ClassTemplate t = cp.loadClassTemplate(role);
try {
Set fieldsRead = getMappedFieldsRead(t, method);
Set fieldsWriten = getMappedFieldsWritten(t, method);
if (fieldsRead.contains(field) || fieldsWriten.contains(field)) {
methods.add(method);
}
} catch (NotFoundException e) {
logger.warn(e.toString(), e);
}
}
Class>[] interfaces = PropertyBehaviour.class.getInterfaces();
final Method[] ar = methods.toArray(new Method[methods.size()]);
return new AbstractBehaviourFactory(FieldBehaviour.class, interfaces,
intercept, ar) {
public boolean precedes(Method in, BehaviourFactory factory,
Method to) {
for (Method m : ar) {
if (m.getName().equals(to.getName())
&& Arrays.equals(m.getParameterTypes(),
to.getParameterTypes()))
return role
.isAssignableFrom(factory.getBehaviourType());
}
return false;
}
public FieldBehaviour newInstance(Object proxy) throws Throwable {
return new FieldBehaviour(factory.createPropertySet(proxy), field, proxy);
}
public String getName() {
return field.getName();
}
};
}
private Set getMappedFieldsRead(ClassTemplate t, Method method)
throws NotFoundException {
Set fields = t.getFieldsRead(method);
Iterator iter = fields.iterator();
while (iter.hasNext()) {
Field field = iter.next();
if (!properties.isMappedField(field)) {
iter.remove();
}
}
return fields;
}
private Set getMappedFieldsWritten(ClassTemplate t, Method method)
throws NotFoundException {
Set fields = t.getFieldsWritten(method);
Iterator iter = fields.iterator();
while (iter.hasNext()) {
Field field = iter.next();
if (!properties.isMappedField(field)) {
iter.remove();
}
}
return fields;
}
private BehaviourFactory createPropertyAdviser(final PropertyDescriptor pd) {
final PropertySetFactory factory = new PropertySetFactory(pd,
properties.findPredicate(pd));
List two = new ArrayList();
if (pd.getReadMethod() != null) {
two.add(pd.getReadMethod());
}
if (pd.getWriteMethod() != null) {
two.add(pd.getWriteMethod());
}
Class>[] interfaces = PropertyBehaviour.class.getInterfaces();
Method[] methods = two.toArray(new Method[two.size()]);
return new AbstractBehaviourFactory(PropertyBehaviour.class,
interfaces, intercept, methods) {
public boolean precedes(Method in, BehaviourFactory factory, Method to) {
Method r = pd.getReadMethod();
if (r.getName().equals(to.getName())) {
return Arrays.equals(r.getParameterTypes(),
to.getParameterTypes());
}
Method w = pd.getWriteMethod();
if (w == null || !w.getName().equals(to.getName()))
return false;
return Arrays.equals(w.getParameterTypes(),
to.getParameterTypes());
}
public PropertyBehaviour newInstance(Object proxy) throws Throwable {
return new PropertyBehaviour(factory.createPropertySet(proxy),
pd);
}
public String getName() {
return pd.getName();
}
};
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy