1 5 package org.exoplatform.services.organization.ldap; 6 import java.io.InputStream ; 7 import java.io.OutputStream ; 8 import java.sql.Timestamp ; 9 import java.util.Collection ; 10 import java.util.Enumeration ; 11 import java.util.Iterator ; 12 import java.util.jar.JarEntry ; 13 import java.util.jar.JarFile ; 14 import java.util.jar.JarOutputStream ; 15 16 import net.sf.hibernate.Session; 17 18 import org.exoplatform.commons.utils.IOUtil; 19 import org.exoplatform.services.backup.ImporterExporter; 20 import org.exoplatform.services.ldap.LDAPService; 21 import org.exoplatform.services.ldap.LDAPServiceContainer; 22 import org.exoplatform.services.organization.impl.GroupImpl; 23 import org.exoplatform.services.organization.impl.MembershipImpl; 24 import org.exoplatform.services.organization.impl.MembershipTypeImpl; 25 import org.exoplatform.services.organization.impl.UserBackupData; 26 import org.exoplatform.services.organization.impl.UserImpl; 27 import org.exoplatform.services.organization.impl.UserProfileImpl; 28 29 import com.thoughtworks.xstream.XStream; 30 import com.thoughtworks.xstream.io.xml.XppDriver; 31 37 public class LDAPOrganizationImporterExporter implements ImporterExporter { 38 private XStream xstream_; 39 private OrganizationServiceLDAPImpl service_; 40 private LDAPServiceContainer ldapServiceContainer_; 41 public LDAPOrganizationImporterExporter( 42 OrganizationServiceLDAPImpl service, 43 LDAPServiceContainer ldapServiceContainer) { 44 ldapServiceContainer_ = ldapServiceContainer; 45 service_ = service; 46 xstream_ = new XStream(new XppDriver()); 47 xstream_.alias("userData", UserBackupData.class); 48 xstream_.alias("membership", MembershipImpl.class); 49 xstream_.alias("membershipType", MembershipTypeImpl.class); 50 xstream_.alias("group", GroupImpl.class); 51 xstream_.alias("date", Timestamp .class); 52 } 53 public String getName() { 54 return "organization"; 55 } 56 public String getDescription() { 57 return "Export User , UserProfile , Memberships of an user"; 58 } 59 public void exportUserData( 60 String userName, 61 Object transaction, 62 OutputStream out, 63 JarEntry entry) 64 throws Exception { 65 Session session = (Session) transaction; 66 LDAPService service = ldapServiceContainer_.createLDAPService(); 67 UserImpl user = 68 (UserImpl) service_.userLDAPHandler_.findUserByName( 69 userName, 70 service); 71 Collection memberships = 72 service_.membershipLDAPHandler_.findMembershipsByUser( 73 userName, 74 session); 75 UserProfileImpl up = 76 ( 77 UserProfileImpl) service_ 78 .userProfileLDAPHandler_ 79 .findUserProfileByName( 80 userName, 81 service); 82 UserBackupData data = new UserBackupData(user, up, memberships); 83 String xml = xstream_.toXML(data); 84 out.write(xml.getBytes()); 85 } 86 public void importUserData( 87 String userName, 88 Object transaction, 89 InputStream is, 90 JarEntry entry) 91 throws Exception { 92 Session session = (Session) transaction; 93 removeExistingUserData(userName, session); 94 String xml = IOUtil.getStreamContentAsString(is); 95 UserBackupData data = (UserBackupData) xstream_.fromXML(xml); 96 UserImpl user = data.getUser(); 97 UserProfileImpl up = data.getUserProfile(); 98 Collection memberships = data.getMemberships(); 99 service_.userLDAPHandler_.createUserEntry(user, session); 100 service_.membershipLDAPHandler_.createMembershipEntries(memberships,session); 101 service_.userProfileLDAPHandler_.createUserProfileEntry(up, session); 102 } 103 104 public void exportServiceData(Object transaction, JarOutputStream out) 105 throws Exception { 106 Session session = (Session) transaction; 107 Collection groups = 108 session.find( 109 "from g in class org.exoplatform.services.organization.impl.GroupBackupData"); 110 Iterator i = groups.iterator(); 111 while (i.hasNext()) { 112 GroupImpl data = (GroupImpl) i.next(); 113 JarEntry entry = 114 new JarEntry ("group/" + data.getGroupName() + ".xml"); 115 out.putNextEntry(entry); 116 String xml = xstream_.toXML(data); 117 out.write(xml.getBytes()); 118 out.closeEntry(); 119 } 120 Collection membershipTypes = service_.findMembershipTypes(); 121 i = membershipTypes.iterator(); 122 while (i.hasNext()) { 123 MembershipTypeImpl data = (MembershipTypeImpl) i.next(); 124 JarEntry entry = 125 new JarEntry ("membershipType/" + data.getName() + ".xml"); 126 out.putNextEntry(entry); 127 String xml = xstream_.toXML(data); 128 out.write(xml.getBytes()); 129 out.closeEntry(); 130 } 131 } 132 public void importServiceData(Object transaction, JarFile jar) 133 throws Exception { 134 Session session = (Session) transaction; 135 LDAPService service = ldapServiceContainer_.createLDAPService(); 136 Enumeration e = jar.entries(); 137 while (e.hasMoreElements()) { 138 JarEntry entry = (JarEntry ) e.nextElement(); 139 InputStream is = jar.getInputStream(entry); 140 String xml = IOUtil.getStreamContentAsString(is); 141 String entryName = entry.getName(); 142 if (entryName.startsWith("group")) { 143 GroupImpl data = (GroupImpl) xstream_.fromXML(xml); 144 GroupLDAPHandler.removeGroupEntry(data.getGroupName(), session,service); 145 session.save(data); 146 } else if (entryName.startsWith("membershipType")) { 147 MembershipTypeImpl data = 148 (MembershipTypeImpl) xstream_.fromXML(xml); 149 MembershipTypeLDAPHandler.removeMembershipTypeEntry( 150 data.getName(), 151 session,service); 152 session.save(data); 153 } 154 } 155 } 156 private void removeExistingUserData(String userName, Session session) 157 throws Exception { 158 LDAPService service = ldapServiceContainer_.createLDAPService(); 159 UserImpl user = 160 (UserImpl) service_.userLDAPHandler_.findUserByName( 161 userName, 162 service); 163 if (user != null) { 164 service_.userLDAPHandler_.removeUserEntry(user, session); 165 MembershipLDAPHandler.removeMembershipEntriesOfUser(userName,session,service); 166 UserProfileLDAPHandler.removeUserProfileEntry(userName, session,service); 167 } 168 } 169 } | Popular Tags |