1 package org.jbpm.identity.xml; 2 3 import java.io.InputStream ; 4 import java.util.ArrayList ; 5 import java.util.HashMap ; 6 import java.util.Iterator ; 7 import java.util.List ; 8 import java.util.Map ; 9 10 import org.dom4j.Document; 11 import org.dom4j.DocumentException; 12 import org.dom4j.Element; 13 import org.dom4j.io.SAXReader; 14 import org.jbpm.identity.Entity; 15 import org.jbpm.identity.Group; 16 import org.jbpm.identity.Membership; 17 import org.jbpm.identity.User; 18 import org.jbpm.instantiation.ClassLoaderUtil; 19 20 public class IdentityXmlParser { 21 22 List entities = new ArrayList (); 23 Map users = new HashMap (); 24 Map groups = new HashMap (); 25 26 public static Entity[] parseEntitiesResource(String resource) { 27 return new IdentityXmlParser().parse(resource); 28 } 29 30 public static Entity[] parseEntitiesResource(InputStream inputStream) { 31 return new IdentityXmlParser().parse(inputStream); 32 } 33 34 public Entity[] parse(String resource) { 35 return parse(ClassLoaderUtil.getStream(resource)); 36 } 37 38 public Entity[] parse(InputStream inputStream) { 39 Document document; 40 try { 41 document = new SAXReader().read(inputStream); 42 } catch (DocumentException e) { 43 throw new RuntimeException ("couldn't parse identities from stream '"+inputStream+"'"); 44 } 45 Element identitiesRootElement = document.getRootElement(); 46 47 readUsers(identitiesRootElement.elements("user")); 48 readGroups(identitiesRootElement.elements("group")); 49 readParentGroups(identitiesRootElement.elements("group")); 50 readMemberships(identitiesRootElement.elements("membership")); 51 52 return (Entity[]) entities.toArray(new Entity[entities.size()]); 53 } 54 55 private void readUsers(List userElements) { 56 Iterator iter = userElements.iterator(); 57 while (iter.hasNext()) { 58 Element userElement = (Element) iter.next(); 59 String name = userElement.attributeValue("name"); 60 String email = userElement.attributeValue("email"); 61 String password = userElement.attributeValue("password"); 62 63 User user = new User(name); 64 user.setEmail(email); 65 user.setPassword(password); 66 users.put(name, user); 67 entities.add(user); 68 } 69 } 70 71 private void readGroups(List groupElements) { 72 Iterator iter = groupElements.iterator(); 73 while (iter.hasNext()) { 74 Element groupElement = (Element) iter.next(); 75 String name = groupElement.attributeValue("name"); 76 String type = groupElement.attributeValue("type"); 77 78 Group group = new Group(name); 79 group.setType(type); 80 entities.add(group); 81 groups.put(name, group); 82 } 83 } 84 85 private void readParentGroups(List groupElements) { 86 Iterator iter = groupElements.iterator(); 87 while (iter.hasNext()) { 88 Element groupElement = (Element) iter.next(); 89 String childName = groupElement.attributeValue("name"); 90 String parentName = groupElement.attributeValue("parent"); 91 92 if (parentName!=null) { 93 Group parent = (Group) groups.get(parentName); 94 if (parent==null) throw new RuntimeException ("unexisting parent group '"+parentName+"'"); 95 Group child = (Group) groups.get(childName); 96 97 parent.addChild(child); 98 } 99 } 100 } 101 102 private void readMemberships(List membershipElements) { 103 Iterator iter = membershipElements.iterator(); 104 while (iter.hasNext()) { 105 Element membershipElement = (Element) iter.next(); 106 String role = membershipElement.attributeValue("role"); 107 String userName = membershipElement.attributeValue("user"); 108 User user = (User) users.get(userName); 109 if (user==null) throw new RuntimeException ("unexisting membership user '"+userName+"'"); 110 String groupName = membershipElement.attributeValue("group"); 111 Group group = (Group) groups.get(groupName); 112 if (group==null) throw new RuntimeException ("unexisting membership group '"+groupName+"'"); 113 114 Membership membership = new Membership(); 115 membership.setRole(role); 116 group.addMembership(membership); 117 user.addMembership(membership); 118 119 entities.add(membership); 120 } 121 } 122 } 123 | Popular Tags |