add integration to aps
parent
3e06a11cb3
commit
4a78dfa493
@ -0,0 +1,271 @@
|
||||
package cn.estsh.i3plus.pojo.aps.common;
|
||||
|
||||
import cn.estsh.i3plus.pojo.aps.bean.DateDuration;
|
||||
import cn.estsh.i3plus.pojo.aps.enums.FIELD_TYPE;
|
||||
import cn.estsh.i3plus.pojo.base.bean.BaseBean;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class BeanInfo {
|
||||
private Class<? extends BaseBean> cls;
|
||||
private BeanInfo superBeanInfo;
|
||||
private List<BeanInfo> childsBeanInfos = new ArrayList<>();
|
||||
private Map<Enum<?>, RelationInfo> relations = new HashMap<>();
|
||||
|
||||
public BeanInfo(Class<? extends BaseBean> cls) {
|
||||
this.cls = cls;
|
||||
}
|
||||
|
||||
public void initHolder(Class<Enum<? extends Enum<?>>> holderCls) {
|
||||
Enum<? extends Enum<?>>[] ens = holderCls.getEnumConstants();
|
||||
for (Enum<? extends Enum<?>> en : ens) {
|
||||
RelationInfo info = new RelationInfo();
|
||||
info.setHolder(en);
|
||||
info.setBeanInfo(this);
|
||||
relations.put(en, info);
|
||||
}
|
||||
}
|
||||
|
||||
public BeanInfo getSuperBeanInfo() { return superBeanInfo; }
|
||||
|
||||
public boolean validHolder(Enum<?> holder) {
|
||||
return this.relations.get(holder) != null;
|
||||
}
|
||||
|
||||
public Enum<?> getHolder(String name) {
|
||||
for (Enum<?> holder : relations.keySet()) {
|
||||
if (holder.name().equalsIgnoreCase(name)) {
|
||||
return holder;
|
||||
}
|
||||
}
|
||||
if (superBeanInfo != null) {
|
||||
return superBeanInfo.getHolder(name);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public Enum<?> getReverseHolder(Enum<?> holder) {
|
||||
RelationInfo relaInfo = getRelationInfo(holder);
|
||||
if (relaInfo != null) {
|
||||
return relaInfo.getReverseHolder();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Enum<?> getReverseHolder(String name) {
|
||||
RelationInfo relaInfo = getRelationInfo(name);
|
||||
if (relaInfo != null) {
|
||||
return relaInfo.getReverseHolder();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public RelationInfo getRelationInfo(Enum<?> holder) {
|
||||
if (validHolder(holder)) {
|
||||
return relations.get(holder);
|
||||
}
|
||||
if (superBeanInfo != null) {
|
||||
return superBeanInfo.getRelationInfo(holder);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public RelationInfo getRelationInfo(String name) {
|
||||
Enum<?> holder = getHolder(name);
|
||||
if (holder != null) {
|
||||
return getRelationInfo(holder);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void addRelationInfo(Enum<?> holder, RelationInfo info) {
|
||||
this.relations.put(holder, info);
|
||||
}
|
||||
|
||||
public <T extends BaseBean> Class<T> getBeanClass() {
|
||||
return (Class<T>)cls;
|
||||
}
|
||||
|
||||
public BeanInfo getRelationBeanInfo(Enum<?> holder) {
|
||||
RelationInfo relaInfo = getRelationInfo(holder);
|
||||
if (relaInfo != null) {
|
||||
return relaInfo.getBeanInfo();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public BeanInfo getRelationBeanInfo(String name) {
|
||||
RelationInfo relaInfo = getRelationInfo(name);
|
||||
if (relaInfo != null) {
|
||||
return relaInfo.getBeanInfo();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public RELATION_TYPE getRelationType(Enum<?> holder) {
|
||||
RelationInfo relaInfo = getRelationInfo(holder);
|
||||
if (relaInfo != null) {
|
||||
return relaInfo.getType();
|
||||
}
|
||||
return RELATION_TYPE.INVALID;
|
||||
}
|
||||
|
||||
public RELATION_TYPE getRelationType(String name) {
|
||||
RelationInfo relaInfo = getRelationInfo(name);
|
||||
if (relaInfo != null) {
|
||||
return relaInfo.getType();
|
||||
}
|
||||
|
||||
return RELATION_TYPE.INVALID;
|
||||
}
|
||||
|
||||
public List<Enum<?>> getAllHolders() {
|
||||
List<Enum<?>> holders = new ArrayList<>();
|
||||
for (Map.Entry<Enum<?>, RelationInfo> entry : relations.entrySet()) {
|
||||
holders.add(entry.getKey());
|
||||
}
|
||||
return holders;
|
||||
}
|
||||
|
||||
public List<Enum<?>> getOwnerHolders() {
|
||||
List<Enum<?>> owners = new ArrayList<>();
|
||||
for (Map.Entry<Enum<?>, RelationInfo> entry : relations.entrySet()) {
|
||||
if (entry.getValue().isOwner()) {
|
||||
owners.add(entry.getKey());
|
||||
}
|
||||
}
|
||||
return owners;
|
||||
}
|
||||
|
||||
public List<Enum<?>> getNormalSigns() {
|
||||
List<Enum<?>> holders = new ArrayList<>();
|
||||
for (Map.Entry<Enum<?>, RelationInfo> entry : relations.entrySet()) {
|
||||
if (!entry.getValue().isOwner()) {
|
||||
holders.add(entry.getKey());
|
||||
}
|
||||
}
|
||||
return holders;
|
||||
}
|
||||
|
||||
private static Map<Class<? extends BaseBean>, BeanInfo> beanInfos = new HashMap<>();
|
||||
private static Map<String, BeanInfo> nameMapBeanInfos = new HashMap<>();
|
||||
|
||||
static {
|
||||
BeanInfo beanInfo = new BeanInfo(BaseBean.class);
|
||||
beanInfos.put(BaseBean.class, beanInfo);
|
||||
nameMapBeanInfos.put(BaseBean.class.getSimpleName(), beanInfo);
|
||||
}
|
||||
|
||||
public static void register(Class<? extends BaseBean> cls) {
|
||||
if (beanInfos.containsKey(cls)) {
|
||||
return;
|
||||
}
|
||||
BeanInfo beanInfo = new BeanInfo(cls);
|
||||
beanInfos.put(cls, beanInfo);
|
||||
nameMapBeanInfos.put(cls.getSimpleName(), beanInfo);
|
||||
register((Class<? extends BaseBean>)cls.getSuperclass());
|
||||
}
|
||||
|
||||
public static void init() {
|
||||
for (Map.Entry<Class<? extends BaseBean>, BeanInfo> entry : beanInfos.entrySet()) {
|
||||
Class<? extends BaseBean> superClass = (Class<? extends BaseBean>)entry.getKey().getSuperclass();
|
||||
BeanInfo superBeanInfo = beanInfos.get(superClass);
|
||||
if (superBeanInfo != null) {
|
||||
entry.getValue().superBeanInfo = superBeanInfo;
|
||||
superBeanInfo.childsBeanInfos.add(entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
BeanRelationUtil.initData("cn.estsh.i3plus.pojo.aps.holders");
|
||||
BeanRelationUtil.loadConfig("relations");
|
||||
}
|
||||
|
||||
public static Set<Class<? extends BaseBean>> getBeanClasses() {
|
||||
return beanInfos.keySet();
|
||||
}
|
||||
|
||||
public static BeanInfo getBeanInfo(Class<? extends BaseBean> cls) {
|
||||
BeanInfo info = beanInfos.get(cls);
|
||||
return info;
|
||||
}
|
||||
|
||||
public static BeanInfo getBeanInfo(String name) {
|
||||
BeanInfo info = nameMapBeanInfos.get(name);
|
||||
return info;
|
||||
}
|
||||
|
||||
public static Class<? extends BaseBean> getSuperClass(Class<? extends BaseBean> cls) {
|
||||
BeanInfo superBeanInfo = getBeanInfo(cls).superBeanInfo;
|
||||
if (superBeanInfo != null) {
|
||||
return superBeanInfo.getBeanClass();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static FIELD_TYPE getFieldType(Class<?> cls) {
|
||||
FIELD_TYPE type = null;
|
||||
if (cls == Boolean.class || cls == boolean.class) {
|
||||
type = FIELD_TYPE.BOOLEAN;
|
||||
} else if (cls == Character.class) {
|
||||
type = FIELD_TYPE.CHAR;
|
||||
} else if (cls == Short.class || cls == short.class) {
|
||||
type = FIELD_TYPE.SHORT;
|
||||
} else if (cls == Integer.class || cls == int.class) {
|
||||
type = FIELD_TYPE.INTEGER;
|
||||
} else if (cls == Long.class || cls == long.class) {
|
||||
type = FIELD_TYPE.LONG;
|
||||
} else if (cls == Double.class || cls == Float.class ||
|
||||
cls == double.class || cls == float.class) {
|
||||
type = FIELD_TYPE.DOUBLE;
|
||||
} else if (cls == String.class) {
|
||||
type = FIELD_TYPE.STRING;
|
||||
} else if(cls == Date.class || cls == java.sql.Date.class) {
|
||||
type = FIELD_TYPE.DATE_TIME;
|
||||
} else if (cls == DateDuration.class) {
|
||||
type = FIELD_TYPE.DURATION;
|
||||
} else if (Enum.class.isAssignableFrom(cls)) {
|
||||
type = FIELD_TYPE.ENUM;
|
||||
} else if (BaseBean.class.isAssignableFrom(cls)) {
|
||||
type = FIELD_TYPE.OBJECT;
|
||||
}
|
||||
return type;
|
||||
}
|
||||
|
||||
public static Class<?> getFieldClass(FIELD_TYPE type) {
|
||||
Class<?> cls = null;
|
||||
switch (type) {
|
||||
case BOOLEAN:
|
||||
cls = Boolean.class;
|
||||
break;
|
||||
case CHAR:
|
||||
cls = Character.class;
|
||||
break;
|
||||
case SHORT:
|
||||
cls = Short.class;
|
||||
break;
|
||||
case INTEGER:
|
||||
cls = Integer.class;
|
||||
break;
|
||||
case LONG:
|
||||
cls = Long.class;
|
||||
break;
|
||||
case DOUBLE:
|
||||
cls = Double.class;
|
||||
break;
|
||||
case DATE:
|
||||
case TIME:
|
||||
case DATE_TIME:
|
||||
cls = Date.class;
|
||||
break;
|
||||
case STRING:
|
||||
cls = String.class;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return cls;
|
||||
}
|
||||
}
|
@ -0,0 +1,241 @@
|
||||
package cn.estsh.i3plus.pojo.aps.common;
|
||||
|
||||
import cn.estsh.i3plus.pojo.base.bean.BaseBean;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
public class BeanRelation {
|
||||
Map<Class<? extends BaseBean>, Map<Long, Map<Enum<?>, List<BaseBean>>>> caches = new HashMap<>();
|
||||
|
||||
private static Map<Long, BeanRelation> relations = new ConcurrentHashMap<>();
|
||||
private static BeanRelation get() {
|
||||
Long userId = 0l;
|
||||
BeanRelation relation = relations.get(userId);
|
||||
if (relation == null) {
|
||||
synchronized (BeanRelation.class) {
|
||||
relation = relations.get(userId);
|
||||
if (relation == null) {
|
||||
relation = new BeanRelation();
|
||||
relations.put(userId, relation);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return relation;
|
||||
}
|
||||
|
||||
private static Map<Enum<?>, List<BaseBean>> createRelation(Class<? extends BaseBean> cls) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Map<Enum<?>, List<BaseBean>> get(BaseBean bean) {
|
||||
Map<Enum<?>, List<BaseBean>> temp = get().caches.get(bean.getClass()).get(bean.getId());
|
||||
if (temp == null) {
|
||||
synchronized (bean.getClass()) {
|
||||
if (temp == null) {
|
||||
temp = createRelation(bean.getClass());
|
||||
get().caches.get(bean.getClass()).put(bean.getId(), temp);
|
||||
}
|
||||
}
|
||||
}
|
||||
return temp;
|
||||
}
|
||||
|
||||
public static void init() {
|
||||
|
||||
}
|
||||
|
||||
public static <T extends BaseBean> T get(BaseBean bean, Enum<?> holder) {
|
||||
List<T> beans = (List<T>)get(bean).get(holder);
|
||||
if (beans == null || beans.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
return beans.get(0);
|
||||
}
|
||||
|
||||
public static <T extends BaseBean> T get(BaseBean bean, Enum<?> holder, Enum<?>... args) {
|
||||
return get(bean, null, holder, args);
|
||||
}
|
||||
|
||||
public static <T extends BaseBean> T get(BaseBean bean, Predicate<T> pred, Enum<?> holder, Enum<?>... args) {
|
||||
return null;
|
||||
}
|
||||
|
||||
private static <T extends BaseBean> T getImpl(BaseBean bean, Predicate<T> pred, Enum<?>[] args, int index) {
|
||||
if (index >= args.length) {
|
||||
if (pred == null || pred.test((T)bean)) {
|
||||
return (T)bean;
|
||||
}
|
||||
} else {
|
||||
//List<BaseAPS> relaBeans =
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static <T extends BaseBean> List<T> list(BaseBean bean, Enum<?> holder) {
|
||||
List<T> beans = (List<T>)get(bean).get(holder);
|
||||
if (beans == null) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
return beans;
|
||||
}
|
||||
|
||||
public static <T extends BaseBean> List<T> list(BaseBean bean, Enum<?> holder, Enum<?>... args) {
|
||||
return list(bean, null, holder, args);
|
||||
}
|
||||
|
||||
public static <T extends BaseBean> List<T> list(BaseBean bean, Predicate<T> pred, Enum<?> holder, Enum<?>... args) {
|
||||
List<T> result = new ArrayList<>();
|
||||
List<BaseBean> nextBeans = list(bean, holder);
|
||||
for (BaseBean nextBean : nextBeans) {
|
||||
listImpl(result, nextBean, pred, args, 0);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static <T extends BaseBean> void listImpl(List<T> result, BaseBean bean, Predicate<T> pred, Enum<?>[] holders, int index) {
|
||||
if (index >= holders.length) {
|
||||
if (pred == null || pred.test((T)bean)) {
|
||||
result.add((T)bean);
|
||||
}
|
||||
} else {
|
||||
List<BaseBean> nextBeans = list(bean, holders[index]);
|
||||
for (BaseBean nextBean : nextBeans) {
|
||||
listImpl(result, nextBean, pred, holders, index + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设值两个对象之间的关联
|
||||
*
|
||||
* @param bean
|
||||
* @param holder
|
||||
* @param relaBean
|
||||
*/
|
||||
public static void set(BaseBean bean, Enum<?> holder, BaseBean relaBean) {
|
||||
if (bean == null) {
|
||||
return;
|
||||
}
|
||||
if (relaBean == null) {
|
||||
remove(bean, holder);
|
||||
} else {
|
||||
BeanInfo beanInfo = BeanInfo.getBeanInfo(bean.getClass());
|
||||
final Enum<?> reverseHolder = beanInfo.getReverseHolder(holder);
|
||||
switch (beanInfo.getRelationType(holder)) {
|
||||
case MULTI_TO_MULTI:
|
||||
break;
|
||||
case MULTI_TO_ONE:
|
||||
remove(bean, holder);
|
||||
break;
|
||||
case INVALID:
|
||||
break;
|
||||
case ONE_TO_MULTI:
|
||||
remove(relaBean, reverseHolder);
|
||||
break;
|
||||
case ONE_TO_ONE:
|
||||
remove(bean, holder);
|
||||
remove(relaBean, reverseHolder);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
setImpl(bean, holder, relaBean, reverseHolder);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 建立对象之间双向引用实现
|
||||
*
|
||||
* @param bean
|
||||
* @param holder
|
||||
* @param relaBean
|
||||
* @param reverseHolder
|
||||
*/
|
||||
private static void setImpl(BaseBean bean, Enum<?> holder, BaseBean relaBean, Enum<?> reverseHolder) {
|
||||
get(bean).get(holder).add(relaBean);
|
||||
if (reverseHolder != null) {
|
||||
get(relaBean).get(reverseHolder).add(bean);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除关联
|
||||
*
|
||||
* @param bean
|
||||
* @param holder
|
||||
*/
|
||||
private static void remove(BaseBean bean, Enum<?> holder) {
|
||||
if (holder == null) {
|
||||
return;
|
||||
}
|
||||
BeanInfo beanInfo = BeanInfo.getBeanInfo(bean.getClass());
|
||||
final Enum<?> reverseHolder = beanInfo.getReverseHolder(holder);
|
||||
if (reverseHolder != null) {
|
||||
for (BaseBean relaBean : get(bean).get(holder)) {
|
||||
if (relaBean == null) {
|
||||
continue;
|
||||
}
|
||||
get(relaBean).get(reverseHolder).remove(bean);
|
||||
}
|
||||
}
|
||||
get(bean).get(holder).clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除关联的指定对象
|
||||
*
|
||||
* @param bean
|
||||
* @param holder
|
||||
* @param relaBean
|
||||
*/
|
||||
private static void remove(BaseBean bean, Enum<?> holder, BaseBean relaBean) {
|
||||
if (bean == null) {
|
||||
return;
|
||||
}
|
||||
BeanInfo beanInfo = BeanInfo.getBeanInfo(bean.getClass());
|
||||
final Enum<?> reverseHolder = beanInfo.getReverseHolder(holder);
|
||||
if (reverseHolder != null) {
|
||||
get(relaBean).get(reverseHolder).remove(bean);
|
||||
}
|
||||
get(bean).get(holder).remove(relaBean);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除对象
|
||||
*
|
||||
* @param bean
|
||||
*/
|
||||
public static void delete(BaseBean bean) {
|
||||
if (bean == null) {
|
||||
return;
|
||||
}
|
||||
BeanInfo beanInfo = BeanInfo.getBeanInfo(bean.getClass());
|
||||
List<Enum<?>> ownerSigns = beanInfo.getOwnerHolders();
|
||||
for (Enum<?> holder : ownerSigns) {
|
||||
List<BaseBean> relaBeans = new ArrayList<>(list(bean, holder));
|
||||
for (BaseBean relaBean : relaBeans) {
|
||||
//MemoryManager.delete(relaBean);
|
||||
}
|
||||
}
|
||||
|
||||
List<Enum<?>> normalSigns = beanInfo.getNormalSigns();
|
||||
for (Enum<?> holder : normalSigns) {
|
||||
Enum<?> reverseHolder = beanInfo.getReverseHolder(holder);
|
||||
List<BaseBean> relaBeans = new ArrayList<>(list(bean, holder));
|
||||
for (BaseBean relaBean : relaBeans) {
|
||||
if (reverseHolder != null) {
|
||||
remove(relaBean, reverseHolder, bean);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,320 @@
|
||||
package cn.estsh.i3plus.pojo.aps.common;
|
||||
|
||||
import org.xml.sax.Attributes;
|
||||
import org.xml.sax.InputSource;
|
||||
import org.xml.sax.SAXException;
|
||||
import org.xml.sax.helpers.DefaultHandler;
|
||||
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import javax.xml.parsers.SAXParser;
|
||||
import javax.xml.parsers.SAXParserFactory;
|
||||
import java.io.File;
|
||||
import java.io.FileFilter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.JarURLConnection;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Enumeration;
|
||||
import java.util.List;
|
||||
import java.util.jar.JarEntry;
|
||||
import java.util.jar.JarFile;
|
||||
|
||||
public class BeanRelationUtil {
|
||||
|
||||
static class XMLReader extends DefaultHandler {
|
||||
private BeanInfo firstInfo = null;
|
||||
private Enum<?> firstHolder = null;
|
||||
private BeanInfo secondInfo = null;
|
||||
private Enum<?> secondHolder = null;
|
||||
private RELATION_TYPE type = RELATION_TYPE.INVALID;
|
||||
private boolean owner = false;
|
||||
public void startElement(String uri, String localName, String nodeName, Attributes attributes) throws SAXException {
|
||||
if (nodeName.equalsIgnoreCase("Class")) {
|
||||
final String name = attributes.getValue("name");
|
||||
if (name == null) {
|
||||
throw new SAXException("Class节点的属性name未配置");
|
||||
}
|
||||
|
||||
this.firstInfo = BeanInfo.getBeanInfo(name);
|
||||
if (this.firstInfo == null) {
|
||||
throw new SAXException("未找到" + name + "的类定义");
|
||||
}
|
||||
} else if (nodeName.equalsIgnoreCase("Relation")) {
|
||||
if (this.firstInfo == null) {
|
||||
throw new SAXException("未配置Class节点");
|
||||
}
|
||||
|
||||
String firstSignName = attributes.getValue("field");
|
||||
if (firstSignName == null) {
|
||||
throw new SAXException("Relation节点缺少field属性");
|
||||
}
|
||||
this.firstHolder = this.firstInfo.getHolder(firstSignName);
|
||||
if (this.firstHolder == null) {
|
||||
throw new SAXException("未定义枚举标识" + firstSignName);
|
||||
}
|
||||
|
||||
String secondFactoryName = attributes.getValue("name");
|
||||
if (secondFactoryName == null) {
|
||||
throw new SAXException("Relation节点缺少name属性");
|
||||
}
|
||||
this.secondInfo = BeanInfo.getBeanInfo(secondFactoryName);
|
||||
if (this.secondInfo == null) {
|
||||
throw new SAXException("未找到" + secondFactoryName + "的类定义");
|
||||
}
|
||||
|
||||
String secondSignName = attributes.getValue("reverse");
|
||||
if (secondSignName != null) {
|
||||
this.secondHolder = this.secondInfo.getHolder(secondSignName);
|
||||
if (this.secondHolder == null) {
|
||||
throw new SAXException(secondFactoryName + "类未定义枚举标识" + secondSignName);
|
||||
}
|
||||
} else {
|
||||
this.secondHolder = null;
|
||||
}
|
||||
|
||||
String typeName = attributes.getValue("type");
|
||||
if (typeName == null) {
|
||||
throw new SAXException("Relation节点缺少type属性");
|
||||
}
|
||||
this.type = RELATION_TYPE.valueOf(typeName);
|
||||
|
||||
String ownerName = attributes.getValue("owner");
|
||||
if (ownerName == null) {
|
||||
this.owner = false;
|
||||
} else {
|
||||
this.owner = ownerName.equalsIgnoreCase("true") ? true : false;
|
||||
}
|
||||
|
||||
RelationInfo firstData = this.getSignData(firstInfo, this.firstHolder);
|
||||
firstData.setBeanInfo(this.secondInfo);
|
||||
firstData.setType(this.type);
|
||||
firstData.setOwner(this.owner);
|
||||
firstData.setReverseHolder(this.secondHolder);
|
||||
|
||||
if (this.secondHolder != null) {
|
||||
RelationInfo secondData = this.getSignData(secondInfo, this.secondHolder);
|
||||
secondData.setBeanInfo(this.firstInfo);
|
||||
secondData.setOwner(false);
|
||||
secondData.setReverseHolder(this.firstHolder);
|
||||
switch (this.type) {
|
||||
case MULTI_TO_MULTI:
|
||||
secondData.setType(RELATION_TYPE.MULTI_TO_MULTI);
|
||||
break;
|
||||
case MULTI_TO_ONE:
|
||||
secondData.setType(RELATION_TYPE.ONE_TO_MULTI);
|
||||
break;
|
||||
case INVALID:
|
||||
secondData.setType(RELATION_TYPE.INVALID);
|
||||
break;
|
||||
case ONE_TO_MULTI:
|
||||
secondData.setType(RELATION_TYPE.MULTI_TO_ONE);
|
||||
break;
|
||||
case ONE_TO_ONE:
|
||||
secondData.setType(RELATION_TYPE.ONE_TO_ONE);
|
||||
break;
|
||||
default:
|
||||
secondData.setType(RELATION_TYPE.INVALID);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
RelationInfo getSignData(BeanInfo beanInfo, Enum<?> holder) {
|
||||
RelationInfo data = beanInfo.getRelationInfo(holder);
|
||||
if (data == null) {
|
||||
data = new RelationInfo();
|
||||
data.setHolder(holder);
|
||||
beanInfo.addRelationInfo(holder, data);
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
public static void initData(String holderPackage) {
|
||||
List<Class<?>> holderClses = loadClass(holderPackage);
|
||||
for (Class<?> enumCls : holderClses) {
|
||||
if (!Enum.class.isAssignableFrom(enumCls)) {
|
||||
continue;
|
||||
}
|
||||
String name = enumCls.getSimpleName();
|
||||
BeanInfo beanInfo = BeanInfo.getBeanInfo(name.substring(1));
|
||||
if (beanInfo == null) {
|
||||
continue;
|
||||
}
|
||||
beanInfo.initHolder((Class<Enum<? extends Enum<?>>>) enumCls);
|
||||
}
|
||||
}
|
||||
|
||||
public static void loadConfig(String packName) {
|
||||
ClassLoader loader = Thread.currentThread().getContextClassLoader();
|
||||
final String strFile = packName.replaceAll("\\.", "/");
|
||||
try {
|
||||
Enumeration<URL> urls = loader.getResources(strFile);
|
||||
while (urls.hasMoreElements()) {
|
||||
URL url = urls.nextElement();
|
||||
if (url != null) {
|
||||
String protocol = url.getProtocol();
|
||||
String filePath = url.getPath();
|
||||
if (protocol.equals("file")) {
|
||||
loadFileImpl(filePath);
|
||||
} else if (protocol.equals("jar")) {
|
||||
loadJarImpl(packName, url);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
private static void loadFileImpl(String dirPath) {
|
||||
File dir = new File(dirPath);
|
||||
if (!dir.exists() || !dir.isDirectory()) {
|
||||
return;
|
||||
}
|
||||
|
||||
File[] dirFiles = dir.listFiles(new FileFilter() {
|
||||
|
||||
@Override
|
||||
public boolean accept(File file) {
|
||||
return file.isDirectory() || file.getName().endsWith(".xml");
|
||||
}
|
||||
});
|
||||
|
||||
for (File file : dirFiles) {
|
||||
if (file.isDirectory()) {
|
||||
loadFileImpl(dirPath + "/" + file.getName());
|
||||
} else {
|
||||
try {
|
||||
loadXMLConfigure(file.getCanonicalPath());
|
||||
} catch (IOException e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void loadJarImpl(String packName, URL url) throws IOException {
|
||||
JarURLConnection jarURLConnection = (JarURLConnection) url.openConnection();
|
||||
JarFile jarFile = jarURLConnection.getJarFile();
|
||||
Enumeration<JarEntry> jarEntries = jarFile.entries();
|
||||
while (jarEntries.hasMoreElements()) {
|
||||
JarEntry jarEntry = jarEntries.nextElement();
|
||||
String jarEntryName = jarEntry.getName();
|
||||
if (jarEntryName.endsWith(".xml")) {
|
||||
String packNameNew = packName.replace(".", "/");
|
||||
if (jarEntryName.contains(packNameNew)) {
|
||||
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(jarEntryName);
|
||||
loadXMLConfigure(is);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载本地开发环境中的xml配置文件。
|
||||
* @param xmlPath
|
||||
*/
|
||||
private static void loadXMLConfigure(String xmlPath) {
|
||||
SAXParserFactory sf = SAXParserFactory.newInstance();
|
||||
try {
|
||||
SAXParser sp = sf.newSAXParser();
|
||||
sp.parse(new InputSource(xmlPath), new XMLReader());
|
||||
} catch (ParserConfigurationException | SAXException | IOException e) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载jar中的xml配置文件。
|
||||
* @param is
|
||||
*/
|
||||
private static void loadXMLConfigure(InputStream is) {
|
||||
SAXParserFactory sf = SAXParserFactory.newInstance();
|
||||
try {
|
||||
SAXParser sp = sf.newSAXParser();
|
||||
sp.parse(new InputSource(is), new XMLReader());
|
||||
} catch (ParserConfigurationException | SAXException | IOException e) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将指定包下的类加载到内存中
|
||||
* @param packName
|
||||
*/
|
||||
private static List<Class<?>> loadClass(String packName) {
|
||||
List<Class<?>> clses = new ArrayList<>();
|
||||
ClassLoader loader = Thread.currentThread().getContextClassLoader();
|
||||
String strFile = packName.replaceAll("\\.", "/");
|
||||
try {
|
||||
Enumeration<URL> urls = loader.getResources(strFile);
|
||||
while (urls.hasMoreElements()) {
|
||||
URL url = urls.nextElement();
|
||||
if (url != null) {
|
||||
String protocol = url.getProtocol();
|
||||
String filePath = url.getPath();
|
||||
if (protocol.equals("file")) {
|
||||
loadClassImpl(packName, filePath, clses);
|
||||
} else if (protocol.equals("jar")) {
|
||||
loadJarImpl(packName, url, clses);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return clses;
|
||||
}
|
||||
|
||||
private static void loadClassImpl(String packName, String dirPath, List<Class<?>> clses) {
|
||||
File dir = new File(dirPath);
|
||||
if (!dir.exists() || !dir.isDirectory()) {
|
||||
return;
|
||||
}
|
||||
|
||||
File[] dirFiles = dir.listFiles(new FileFilter() {
|
||||
|
||||
@Override
|
||||
public boolean accept(File file) {
|
||||
return file.isDirectory() || file.getName().endsWith(".class");
|
||||
}
|
||||
});
|
||||
|
||||
for (File file : dirFiles) {
|
||||
if (file.isDirectory()) {
|
||||
loadClassImpl(packName + "." + file.getName(), dirPath + "/" + file.getName(), clses);
|
||||
} else {
|
||||
String clsName = file.getName();
|
||||
clsName = clsName.substring(0, clsName.length() - 6);
|
||||
try {
|
||||
clses.add(Class.forName(packName + "." + clsName));
|
||||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void loadJarImpl(String packName, URL url, List<Class<?>> clses) throws IOException {
|
||||
JarURLConnection jarURLConnection = (JarURLConnection) url.openConnection();
|
||||
JarFile jarFile = jarURLConnection.getJarFile();
|
||||
Enumeration<JarEntry> jarEntries = jarFile.entries();
|
||||
while (jarEntries.hasMoreElements()) {
|
||||
JarEntry jarEntry = jarEntries.nextElement();
|
||||
final String jarEntryName = jarEntry.getName();
|
||||
if (jarEntryName.endsWith(".class")) {
|
||||
String clsName = jarEntryName.replace("/", ".");
|
||||
if (clsName.startsWith(packName)) {
|
||||
clsName = clsName.substring(0, clsName.length() - 6);
|
||||
try {
|
||||
clses.add(Class.forName(clsName));
|
||||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package cn.estsh.i3plus.pojo.aps.common;
|
||||
|
||||
public enum RELATION_TYPE {
|
||||
INVALID, // 无效关联
|
||||
ONE_TO_ONE, // 1对1关系
|
||||
ONE_TO_MULTI, // 1对多关系
|
||||
MULTI_TO_ONE, // 多对1关系
|
||||
MULTI_TO_MULTI,// 多对多关系
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package cn.estsh.i3plus.pojo.aps.common;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class RelationInfo {
|
||||
private BeanInfo beanInfo;
|
||||
private Enum<?> holder;
|
||||
private Enum<?> reverseHolder;
|
||||
private RELATION_TYPE type;
|
||||
private boolean owner;
|
||||
}
|
Loading…
Reference in New Issue