mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
Remove LDAP and Validation objects
This commit is contained in:
-50
@@ -1,50 +0,0 @@
|
||||
package com.avaje.ebeaninternal.server.deploy.parse;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import com.avaje.ebean.validation.ValidatorMeta;
|
||||
import com.avaje.ebean.validation.factory.Validator;
|
||||
import com.avaje.ebean.validation.factory.ValidatorFactory;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class ValidatorFactoryManager {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(ValidatorFactoryManager.class);
|
||||
|
||||
Map<Class<?>, ValidatorFactory> factoryMap;
|
||||
|
||||
public ValidatorFactoryManager() {
|
||||
factoryMap = new HashMap<Class<?>, ValidatorFactory>();
|
||||
}
|
||||
|
||||
public Validator create(Annotation ann, Class<?> type) {
|
||||
synchronized (this) {
|
||||
ValidatorMeta meta = ann.annotationType().getAnnotation(ValidatorMeta.class);
|
||||
if (meta == null) {
|
||||
return null;
|
||||
}
|
||||
Class<?> factoryClass = meta.factory();
|
||||
ValidatorFactory factory = getFactory(factoryClass);
|
||||
return factory.create(ann, type);
|
||||
}
|
||||
}
|
||||
|
||||
private ValidatorFactory getFactory(Class<?> factoryClass) {
|
||||
try {
|
||||
ValidatorFactory factory = factoryMap.get(factoryClass);
|
||||
if (factory == null) {
|
||||
factory = (ValidatorFactory) factoryClass.newInstance();
|
||||
factoryMap.put(factoryClass, factory);
|
||||
}
|
||||
return factory;
|
||||
|
||||
} catch (Exception e) {
|
||||
String msg = "Error creating ValidatorFactory " + factoryClass.getName();
|
||||
logger.error(msg, e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,141 +0,0 @@
|
||||
package com.avaje.ebeaninternal.server.ldap;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.naming.Name;
|
||||
import javax.naming.NamingException;
|
||||
import javax.naming.directory.Attribute;
|
||||
import javax.naming.directory.Attributes;
|
||||
import javax.naming.directory.BasicAttributes;
|
||||
import javax.naming.directory.DirContext;
|
||||
|
||||
import com.avaje.ebean.config.ldap.LdapContextFactory;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanProperty;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class DefaultLdapPersister {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(DefaultLdapPersister.class);
|
||||
|
||||
private final LdapContextFactory contextFactory;
|
||||
|
||||
public DefaultLdapPersister(LdapContextFactory dirContextFactory) {
|
||||
this.contextFactory = dirContextFactory;
|
||||
}
|
||||
|
||||
public int persist(LdapPersistBeanRequest<?> request) {
|
||||
|
||||
switch (request.getType()) {
|
||||
case INSERT:
|
||||
return insert(request);
|
||||
case UPDATE:
|
||||
return update(request);
|
||||
case DELETE:
|
||||
return delete(request);
|
||||
|
||||
default:
|
||||
throw new LdapPersistenceException("Invalid type " + request.getType());
|
||||
}
|
||||
}
|
||||
|
||||
private int insert(LdapPersistBeanRequest<?> request) {
|
||||
|
||||
DirContext dc = contextFactory.createContext();
|
||||
|
||||
Name name = request.createLdapName();
|
||||
Attributes attrs = createAttributes(request, false, request.getLoadedProperties());
|
||||
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Ldap Insert Name:" + name + " Attrs:" + attrs);
|
||||
}
|
||||
try {
|
||||
dc.bind(name, null, attrs);
|
||||
return 1;
|
||||
|
||||
} catch (NamingException e) {
|
||||
throw new LdapPersistenceException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private int delete(LdapPersistBeanRequest<?> request) {
|
||||
|
||||
DirContext dc = contextFactory.createContext();
|
||||
Name name = request.createLdapName();
|
||||
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Ldap Delete Name:" + name);
|
||||
}
|
||||
|
||||
try {
|
||||
dc.unbind(name);
|
||||
return 1;
|
||||
|
||||
} catch (NamingException e) {
|
||||
throw new LdapPersistenceException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private int update(LdapPersistBeanRequest<?> request) {
|
||||
|
||||
Name name = request.createLdapName();
|
||||
|
||||
Set<String> updatedProperties = request.getUpdatedProperties();
|
||||
if (updatedProperties == null || updatedProperties.isEmpty()) {
|
||||
logger.info("Ldap Update has no changed properties? Name:" + name);
|
||||
return 0;
|
||||
}
|
||||
|
||||
DirContext dc = contextFactory.createContext();
|
||||
Attributes attrs = createAttributes(request, true, updatedProperties);
|
||||
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Ldap Update Name:" + name + " Attrs:" + attrs);
|
||||
}
|
||||
|
||||
try {
|
||||
dc.modifyAttributes(name, DirContext.REPLACE_ATTRIBUTE, attrs);
|
||||
return 1;
|
||||
|
||||
} catch (NamingException e) {
|
||||
throw new LdapPersistenceException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private Attributes createAttributes(LdapPersistBeanRequest<?> request, boolean update, Set<String> props) {
|
||||
|
||||
BeanDescriptor<?> desc = request.getBeanDescriptor();
|
||||
|
||||
Attributes attrs = desc.createAttributes();
|
||||
if (update) {
|
||||
attrs = new BasicAttributes(true);
|
||||
} else {
|
||||
attrs = desc.createAttributes();
|
||||
}
|
||||
|
||||
Object bean = request.getBean();
|
||||
|
||||
if (props != null) {
|
||||
for (String propName : props) {
|
||||
BeanProperty p = desc.getBeanPropertyFromPath(propName);
|
||||
Attribute attr = p.createAttribute(bean);
|
||||
if (attr != null) {
|
||||
attrs.put(attr);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Iterator<BeanProperty> it = desc.propertiesAll();
|
||||
while (it.hasNext()) {
|
||||
BeanProperty p = it.next();
|
||||
Attribute attr = p.createAttribute(bean);
|
||||
if (attr != null) {
|
||||
attrs.put(attr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return attrs;
|
||||
}
|
||||
}
|
||||
@@ -1,81 +0,0 @@
|
||||
package com.avaje.ebeaninternal.server.ldap;
|
||||
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.naming.NamingEnumeration;
|
||||
import javax.naming.NamingException;
|
||||
import javax.naming.directory.Attribute;
|
||||
import javax.naming.directory.Attributes;
|
||||
|
||||
import com.avaje.ebean.bean.EntityBean;
|
||||
import com.avaje.ebean.bean.EntityBeanIntercept;
|
||||
import com.avaje.ebean.event.BeanPersistController;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanProperty;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class LdapBeanBuilder<T> {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(LdapBeanBuilder.class);
|
||||
|
||||
private final BeanDescriptor<T> beanDescriptor;
|
||||
|
||||
private final boolean vanillaMode;
|
||||
|
||||
private Set<String> loadedProps;
|
||||
|
||||
public LdapBeanBuilder(BeanDescriptor<T> beanDescriptor, boolean vanillaMode) {
|
||||
this.beanDescriptor = beanDescriptor;
|
||||
this.vanillaMode = vanillaMode;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public T readAttributes(Attributes attributes) throws NamingException {
|
||||
|
||||
Object bean = beanDescriptor.createBean(vanillaMode);
|
||||
|
||||
NamingEnumeration<? extends Attribute> all = attributes.getAll();
|
||||
|
||||
boolean setLoadedProps = false;
|
||||
if (loadedProps == null) {
|
||||
setLoadedProps = true;
|
||||
loadedProps = new LinkedHashSet<String>();
|
||||
}
|
||||
|
||||
while (all.hasMoreElements()) {
|
||||
Attribute attr = all.nextElement();
|
||||
String attrName = attr.getID();
|
||||
|
||||
BeanProperty prop = beanDescriptor.getBeanPropertyFromDbColumn(attrName);
|
||||
if (prop == null) {
|
||||
if ("objectclass".equalsIgnoreCase(attrName)) {
|
||||
// this is expected
|
||||
} else {
|
||||
logger.info("... hmm, no property to map to attribute[" + attrName + "] value["+attr.get()+"]");
|
||||
}
|
||||
|
||||
} else {
|
||||
prop.setAttributeValue(bean, attr);
|
||||
if (setLoadedProps) {
|
||||
loadedProps.add(prop.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (bean instanceof EntityBean) {
|
||||
EntityBeanIntercept ebi = ((EntityBean) bean)._ebean_getIntercept();
|
||||
ebi.setLoadedProps(loadedProps);
|
||||
ebi.setLoaded();
|
||||
}
|
||||
|
||||
BeanPersistController persistController = beanDescriptor.getPersistController();
|
||||
if (persistController != null) {
|
||||
persistController.postLoad(bean, loadedProps);
|
||||
}
|
||||
|
||||
return (T)bean;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,129 +0,0 @@
|
||||
package com.avaje.ebeaninternal.server.ldap;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import javax.naming.NamingEnumeration;
|
||||
import javax.naming.NamingException;
|
||||
import javax.naming.directory.Attributes;
|
||||
import javax.naming.directory.DirContext;
|
||||
import javax.naming.directory.SearchControls;
|
||||
import javax.naming.directory.SearchResult;
|
||||
import javax.naming.ldap.LdapName;
|
||||
|
||||
import com.avaje.ebeaninternal.api.SpiQuery;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class LdapOrmQueryExecute<T> {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(LdapOrmQueryExecute.class);
|
||||
|
||||
private final SpiQuery<?> query;
|
||||
|
||||
private final BeanDescriptor<T> beanDescriptor;
|
||||
|
||||
private final DirContext dc;
|
||||
|
||||
private final LdapBeanBuilder<T> beanBuilder;
|
||||
|
||||
private final String filterExpr;
|
||||
|
||||
private final Object[] filterValues;
|
||||
|
||||
private final String[] selectProps;
|
||||
|
||||
public LdapOrmQueryExecute(LdapOrmQueryRequest<T> request, boolean defaultVanillaMode, DirContext dc) {
|
||||
|
||||
this.query = request.getQuery();
|
||||
this.beanDescriptor = request.getBeanDescriptor();
|
||||
this.dc = dc;
|
||||
|
||||
boolean vanillaMode = query.isVanillaMode(defaultVanillaMode);
|
||||
this.beanBuilder = new LdapBeanBuilder<T>(beanDescriptor, vanillaMode);
|
||||
|
||||
LdapQueryDeployHelper deployHelper = new LdapQueryDeployHelper(request);
|
||||
this.selectProps = deployHelper.getSelectedProperties();
|
||||
this.filterExpr = deployHelper.getFilterExpr();
|
||||
this.filterValues = deployHelper.getFilterValues();
|
||||
}
|
||||
|
||||
public T findId() {
|
||||
|
||||
Object id = query.getId();
|
||||
|
||||
try {
|
||||
LdapName dn = beanDescriptor.createLdapNameById(id);
|
||||
|
||||
String[] findAttrs = selectProps;
|
||||
if (findAttrs == null){
|
||||
findAttrs = beanDescriptor.getDefaultSelectDbArray();
|
||||
}
|
||||
|
||||
// build a string describing the query
|
||||
String debugQuery = "Name:"+dn+" attrs:"+Arrays.toString(findAttrs);
|
||||
|
||||
Attributes attrs = dc.getAttributes(dn, findAttrs);
|
||||
|
||||
T bean = beanBuilder.readAttributes(attrs);
|
||||
|
||||
query.setGeneratedSql(debugQuery);
|
||||
return bean;
|
||||
|
||||
} catch (NamingException e) {
|
||||
throw new LdapPersistenceException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public List<T> findList() {
|
||||
|
||||
SearchControls sc = new SearchControls();
|
||||
sc.setSearchScope(SearchControls.ONELEVEL_SCOPE);
|
||||
|
||||
List<T> list = new ArrayList<T>();
|
||||
|
||||
try {
|
||||
LdapName dn = beanDescriptor.createLdapName(null);
|
||||
|
||||
// build a string describing the query
|
||||
String debugQuery = "Name:"+dn;
|
||||
|
||||
if (selectProps != null) {
|
||||
sc.setReturningAttributes(selectProps);
|
||||
debugQuery += " select:"+Arrays.toString(selectProps);
|
||||
}
|
||||
|
||||
if (logger.isInfoEnabled()){
|
||||
logger.info("Ldap Query Name:"+dn+" filterExpr:"+filterExpr);
|
||||
}
|
||||
|
||||
debugQuery += " filterExpr:"+filterExpr;
|
||||
|
||||
NamingEnumeration<SearchResult> result;
|
||||
if (filterValues == null || filterValues.length == 0) {
|
||||
result = dc.search(dn, filterExpr, sc);
|
||||
} else {
|
||||
debugQuery += " filterValues:"+Arrays.toString(filterValues);
|
||||
result = dc.search(dn, filterExpr, filterValues, sc);
|
||||
}
|
||||
|
||||
query.setGeneratedSql(debugQuery);
|
||||
|
||||
if (result != null){
|
||||
while (result.hasMoreElements()) {
|
||||
SearchResult row = result.nextElement();
|
||||
T bean = beanBuilder.readAttributes(row.getAttributes());
|
||||
list.add(bean);
|
||||
}
|
||||
}
|
||||
|
||||
return list;
|
||||
|
||||
} catch (NamingException e) {
|
||||
throw new LdapPersistenceException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user