1 5 package org.exoplatform.services.backup.impl; 6 7 import java.io.File ; 8 import java.io.FileOutputStream ; 9 import java.io.InputStream ; 10 import java.util.Collection ; 11 import java.util.Iterator ; 12 import java.util.List ; 13 import java.util.jar.JarEntry ; 14 import java.util.jar.JarFile ; 15 import java.util.jar.JarOutputStream ; 16 import net.sf.hibernate.Session; 17 import org.apache.commons.logging.Log; 18 import org.exoplatform.container.PortalContainer; 19 import org.exoplatform.services.backup.BackupService; 20 import org.exoplatform.services.backup.ExportLogger; 21 import org.exoplatform.services.backup.ImportLogger; 22 import org.exoplatform.services.backup.ImporterExporter; 23 import org.exoplatform.services.database.HibernateService; 24 import org.exoplatform.services.log.LogService; 25 import org.exoplatform.services.organization.OrganizationService; 26 import org.exoplatform.services.organization.User; 27 import org.exoplatform.container.configuration.*; 28 29 35 public class BackupServiceImpl implements BackupService { 36 private String directory_ ; 37 private String userDataDirectory_ ; 38 private String serviceDataDirectory_ ; 39 private Log log_ ; 40 private ExportLogger exportLogger_ ; 41 private ImportLogger importLogger_ ; 42 private OrganizationService orgService_ ; 43 private HibernateService hservice_ ; 44 45 public BackupServiceImpl(LogService lservice, 46 OrganizationService orgService, 47 HibernateService hservice, 48 ConfigurationManager cmanager) throws Exception { 49 orgService_ = orgService ; 50 hservice_ = hservice ; 51 log_ = lservice.getLog("org.exoplatform.services.exporter") ; 52 exportLogger_ = new ExportLogger() ; 53 importLogger_ = new ImportLogger() ; 54 String backupDir = null ; 55 ServiceConfiguration sconf = cmanager.getServiceConfiguration(BackupService.class) ; 56 ValueParam param = sconf.getValueParam("backup.directory"); 57 if(param != null) backupDir = param.getValue() ; 58 if(cmanager.isDefault(backupDir)) { 59 backupDir = System.getProperty("java.io.tmpdir") ; 60 } 61 setDirectory(backupDir); 62 } 63 64 public List getImportersExporters() { 65 PortalContainer pcontainer = PortalContainer.getInstance() ; 66 List list = pcontainer.getComponentInstancesOfType(ImporterExporter.class) ; 67 return list ; 68 } 69 70 public String getDirectory() { return directory_ ;} 71 public void setDirectory(String s) throws Exception { 72 File dir = new File (s) ; 73 if (!dir.exists()) { 74 dir.mkdirs() ; 75 } 76 if(dir.isDirectory()) { 77 directory_ = dir.getPath(); 78 userDataDirectory_ = directory_ + File.separator + "user-data" ; 79 dir = new File (userDataDirectory_) ; 80 if(!dir.exists())dir.mkdir() ; 81 serviceDataDirectory_ = directory_ + File.separator + "service-data" ; 82 dir = new File (serviceDataDirectory_) ; 83 if(!dir.exists())dir.mkdir() ; 84 } else { 85 throw new Exception (s + " is a file. Cannot create an export directory for backup service") ; 86 } 87 } 88 89 public String getUserDataDirectory() { return userDataDirectory_ ; } 90 public String getServiceDataDirectory() { return serviceDataDirectory_ ; } 91 92 synchronized public void addImporterExporter(ImporterExporter exporter) { 93 } 95 96 public ImporterExporter getImporterExporter(String name) { 97 List importersExporters = getImportersExporters() ; 98 ImporterExporter exporter = null ; 99 for(int j = 0; j < importersExporters.size() ; j++) { 100 exporter = (ImporterExporter) importersExporters.get(j) ; 101 if(name.equals(exporter.getName())) { 102 return exporter ; 103 } 104 } 105 return null ; 106 } 107 108 109 public ExportLogger getExportLogger() { return exportLogger_ ; } 110 111 public ImportLogger getImportLogger() { return importLogger_ ; } 112 113 public void exportUserData() throws Exception { 114 Collection users = orgService_.getUserPageList(20).getAll() ; 115 Iterator i = users.iterator(); 116 while(i.hasNext()) { 117 User user = (User) i.next() ; 118 String userName = user.getUserName() ; 119 exportUserData(userName) ; 120 } 121 } 122 123 public void exportUserData(String userName) throws Exception { 124 Session session = null ; 125 String fileName = userDataDirectory_ + File.separator + userName + ".jar" ; 126 File file = new File (fileName) ; 127 JarOutputStream out = new JarOutputStream (new FileOutputStream (file)) ; 128 List importersExporters = getImportersExporters() ; 129 try { 130 session = hservice_.openSession() ; 131 for(int j = 0; j < importersExporters.size() ; j++) { 132 ImporterExporter exporter = (ImporterExporter) importersExporters.get(j) ; 133 JarEntry entry = new JarEntry (exporter.getName()+ ".xml") ; 134 out.putNextEntry(entry) ; 135 exporter.exportUserData(userName, session, out, entry) ; 136 out.closeEntry() ; 137 } 138 session.flush() ; 139 hservice_.closeSession() ; 140 exportLogger_.log(userName) ; 141 } catch (Exception ex) { 142 exportLogger_.log(userName, ex) ; 143 if(session != null) session.close() ; 144 ex.printStackTrace() ; 145 } 146 out.close() ; 147 } 148 149 public void importUserData() throws Exception { 150 File dir = new File (userDataDirectory_) ; 151 String [] file = dir.list() ; 152 for(int i = 0 ; i < file.length; i++) { 153 int idx = file[i].indexOf('.') ; 154 String userName = file[i].substring(0, idx) ; 155 importUserData(userName) ; 156 } 157 } 158 159 public void importUserData(String userName) throws Exception { 160 Session session = null ; 161 String fileName = userDataDirectory_ + File.separator + userName + ".jar" ; 162 JarFile jar = new JarFile (new File (fileName)) ; 163 List importersExporters = getImportersExporters() ; 164 try { 165 session = hservice_.openSession() ; 166 for(int j = 0; j < importersExporters.size() ; j++) { 167 ImporterExporter exporter = (ImporterExporter) importersExporters.get(j) ; 168 String entryName = exporter.getName()+ ".xml" ; 169 JarEntry entry = jar.getJarEntry(entryName) ; 170 InputStream in = jar.getInputStream(entry) ; 171 exporter.importUserData(userName, session, in, entry) ; 172 in.close() ; 173 } 174 session.flush() ; 175 hservice_.closeSession() ; 176 importLogger_.log(userName) ; 177 } catch (Exception ex) { 178 importLogger_.log(userName, ex) ; 179 ex.printStackTrace() ; 180 } 181 jar.close() ; 182 } 183 184 public void exportServiceData() throws Exception { 185 List importersExporters = getImportersExporters() ; 186 for(int j = 0; j < importersExporters.size() ; j++) { 187 ImporterExporter exporter = (ImporterExporter) importersExporters.get(j) ; 188 exportServiceData(exporter.getName()) ; 189 } 190 } 191 192 public void exportServiceData(String serviceName) throws Exception { 193 Session session = null ; 194 ImporterExporter exporter = getImporterExporter(serviceName) ; 195 String fileName = serviceDataDirectory_ + File.separator + serviceName + ".jar" ; 196 JarOutputStream out = new JarOutputStream (new FileOutputStream (fileName)) ; 197 try { 198 session = hservice_.openSession() ; 199 exporter.exportServiceData(session, out) ; 200 out.close() ; 201 session.flush() ; 202 hservice_.closeSession() ; 203 exportLogger_.log(serviceName) ; 204 } catch (Exception ex) { 205 exportLogger_.log(serviceName, ex) ; 206 ex.printStackTrace() ; 207 } 208 } 209 210 public void importServiceData() throws Exception { 211 File dir = new File (serviceDataDirectory_) ; 212 String [] file = dir.list() ; 213 for(int i = 0 ; i < file.length; i++) { 214 int idx = file[i].indexOf('.') ; 215 String serviceName = file[i].substring(0, idx) ; 216 importServiceData(serviceName) ; 217 } 218 } 219 220 public void importServiceData(String serviceName) throws Exception { 221 ImporterExporter exporter = getImporterExporter(serviceName) ; 222 if(exporter == null) return ; 223 Session session = null ; 224 String fileName = serviceDataDirectory_ + File.separator + serviceName + ".jar" ; 225 JarFile jar = new JarFile (new File (fileName)) ; 226 try { 227 session = hservice_.openSession() ; 228 exporter.importServiceData(session, jar) ; 229 session.flush() ; 230 session.close() ; 231 importLogger_.log(serviceName) ; 232 } catch (Exception ex) { 233 importLogger_.log(serviceName, ex) ; 234 if(session != null) session.close() ; 235 ex.printStackTrace() ; 236 } 237 jar.close() ; 238 } 239 } | Popular Tags |