package org.leadfar.oa.service.impl;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.leadfar.oa.dao.ACLDao;
import org.leadfar.oa.model.ACL;
import org.leadfar.oa.model.ActionResource;
import org.leadfar.oa.model.Person;
import org.leadfar.oa.model.Principle;
import org.leadfar.oa.model.Role;
import org.leadfar.oa.model.SysResource;
import org.leadfar.oa.model.User;
import org.leadfar.oa.model.UserRole;
import org.leadfar.oa.service.InitService;
import org.leadfar.oa.service.ResourceService;
import org.leadfar.oa.utils.OperResource;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.stereotype.Service;
import com.sun.org.apache.commons.beanutils.BeanUtils;
@Service("initService")
public class InitServiceImpl implements InitService,BeanFactoryAware {
private BeanFactory beanFactory;
@javax.annotation.Resource(name="aclDao")
private ACLDao aclDao;
@Override
public void initXML(String XMLPath) {
InputStream in=Thread.currentThread().getContextClassLoader().getResourceAsStream(XMLPath);
SAXReader reader=new SAXReader();
Document doc=null;
try {
doc=reader.read(in);
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Element root=doc.getRootElement();
String pag=root.attributeValue("package");
String call=root.attributeValue("call");
Iterator it=root.elementIterator();
while(it.hasNext()){
Element e=(Element)it.next();
this.addEntry(e, pag, call, null);
}
}
public void addEntry(Element e,String pag,String call,Object parent){
String clazz=pag+"."+e.attributeValue("class");
try {
Object entry=Class.forName(clazz).newInstance();
Iterator attrIt=e.attributeIterator();
while(attrIt.hasNext()){
Attribute attr=(Attribute)attrIt.next();
if(!attr.getName().equals("class")){
BeanUtils.copyProperty(entry, attr.getName(), attr.getValue());
}
}
BeanUtils.copyProperty(entry, "parent", parent);
String serviceId=call.split("\\.")[0];
String methodName=call.split("\\.")[1];
Object service=beanFactory.getBean(serviceId);
Method[] methods=service.getClass().getMethods();
for(Method m:methods){
if(m.getName().equals(methodName)){
m.invoke(service, entry);
break;
}
}
Iterator it=e.elementIterator();
while(it.hasNext()){
Element child=(Element)it.next();
this.addEntry(child, pag, call, entry);
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory=beanFactory;
}
@Override
public void initResource() {
// org.leadfar.oa.action目录下发现并提取所有包含@OperResource注解的类
ResourceService resourceService=(ResourceService)this.beanFactory.getBean("resourceService");
// 1.路径模式解析器
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
try {
// 取得应用程序中所有后缀为Action的字节码文件>取得所有的Action类字节码指令
Resource[] resources = resolver
.getResources("org/leadfar/oa/action/*Action.class");
//元数据读取工厂
MetadataReaderFactory factory = new CachingMetadataReaderFactory();
for (Resource r : resources) {
//将resource包装为元数据读取器
MetadataReader mr = factory.getMetadataReader(r);
if(mr.getAnnotationMetadata().hasAnnotation(OperResource.class.getName())){
ActionResource ar=new ActionResource();
ar.setClassName(mr.getClassMetadata().getClassName());
Map<String,Object> attrs=mr.getAnnotationMetadata().getAnnotationAttributes(OperResource.class.getName());
ar.setName((String)attrs.get("name"));
ar.setSn((String)attrs.get("sn"));
/*Iterator it=attrs.entrySet().iterator();
while(it.hasNext()){
Map.Entry entr+y =(Map.Entry)it.next();
BeanUtils.copyProperty(ar, (String)entry.getKey(), (String)entry.getValue());
}*/
// 提取注解中的属性,并将其汇总ActionResource对象中,持久化对象
resourceService.add(ar);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void initAuth() {
//取得缺省角色-超级管理员
Principle p=this.aclDao.getByKey(Role.class, "name", "超级管理员");
//取得所有菜单资源
List<SysResource> srs=this.aclDao.queryResources("MENU");
//取得所有操作资源与菜单资源集合合并
srs.addAll(this.aclDao.queryResources("AR"));
for(SysResource sr:srs){
ACL acl=new ACL();
acl.setPrincipleId(p.getPrincipleId());
acl.setPrincipleType(p.getPrincipleType());
acl.setResourceId(sr.getResourceId());
acl.setResourceType(sr.getResourceType());
if(sr.getResourceType().equals("MENU")){
acl.setAclState(1);
}else{
acl.setAclState(15);
}
this.aclDao.save(acl);
}
}
@Override
public void initRole() {
//初始化缺省角色
Role r=new Role();
r.setName("超级管理员");
this.aclDao.save(r);
//初始化缺省用户
Person p=(Person) this.aclDao.getByKey(Person.class, "name", "缺省用户");
User user=new User();
user.setUsername("admin");
user.setPassword("admin");
user.setPerson(p);
this.aclDao.save(user);
//初始化缺省用户与缺省角色的关联
UserRole ur=new UserRole();
ur.setRole(r);
ur.setUser(user);
this.aclDao.save(ur);
}
}
- 1
- 2
- 3
- 4
前往页