KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > backup > impl > BackupServiceImpl


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.services.backup.impl;
6
7 import java.io.File JavaDoc;
8 import java.io.FileOutputStream JavaDoc;
9 import java.io.InputStream JavaDoc;
10 import java.util.Collection JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.List JavaDoc;
13 import java.util.jar.JarEntry JavaDoc;
14 import java.util.jar.JarFile JavaDoc;
15 import java.util.jar.JarOutputStream JavaDoc;
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 /**
30  * May 27, 2004
31  * @author: Tuan Nguyen
32  * @email: tuan08@users.sourceforge.net
33  * @version: $ID$
34  **/

35 public class BackupServiceImpl implements BackupService {
36     private String JavaDoc directory_ ;
37     private String JavaDoc userDataDirectory_ ;
38     private String JavaDoc 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 JavaDoc {
49         orgService_ = orgService ;
50         hservice_ = hservice ;
51         log_ = lservice.getLog("org.exoplatform.services.exporter") ;
52         exportLogger_ = new ExportLogger() ;
53         importLogger_ = new ImportLogger() ;
54     String JavaDoc 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 JavaDoc getImportersExporters() {
65     PortalContainer pcontainer = PortalContainer.getInstance() ;
66     List JavaDoc list = pcontainer.getComponentInstancesOfType(ImporterExporter.class) ;
67         return list ;
68     }
69
70     public String JavaDoc getDirectory() { return directory_ ;}
71     public void setDirectory(String JavaDoc s) throws Exception JavaDoc {
72         File JavaDoc dir = new File JavaDoc(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 JavaDoc(userDataDirectory_) ;
80             if(!dir.exists())dir.mkdir() ;
81             serviceDataDirectory_ = directory_ + File.separator + "service-data" ;
82             dir = new File JavaDoc(serviceDataDirectory_) ;
83             if(!dir.exists())dir.mkdir() ;
84         } else {
85             throw new Exception JavaDoc(s + " is a file. Cannot create an export directory for backup service") ;
86         }
87     }
88     
89     public String JavaDoc getUserDataDirectory() { return userDataDirectory_ ; }
90     public String JavaDoc getServiceDataDirectory() { return serviceDataDirectory_ ; }
91     
92     synchronized public void addImporterExporter(ImporterExporter exporter) {
93         //importersExporters_.add(exporter) ;
94
}
95     
96     public ImporterExporter getImporterExporter(String JavaDoc name) {
97         List JavaDoc 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 JavaDoc {
114         Collection JavaDoc users = orgService_.getUserPageList(20).getAll() ;
115         Iterator JavaDoc i = users.iterator();
116         while(i.hasNext()) {
117             User user = (User) i.next() ;
118             String JavaDoc userName = user.getUserName() ;
119           exportUserData(userName) ;
120         }
121     }
122     
123     public void exportUserData(String JavaDoc userName) throws Exception JavaDoc {
124         Session session = null ;
125         String JavaDoc fileName = userDataDirectory_ + File.separator + userName + ".jar" ;
126     File JavaDoc file = new File JavaDoc(fileName) ;
127       JarOutputStream JavaDoc out = new JarOutputStream JavaDoc(new FileOutputStream JavaDoc(file)) ;
128       List JavaDoc 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 JavaDoc entry = new JarEntry JavaDoc(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 JavaDoc 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 JavaDoc {
150         File JavaDoc dir = new File JavaDoc(userDataDirectory_) ;
151         String JavaDoc[] file = dir.list() ;
152         for(int i = 0 ; i < file.length; i++) {
153             int idx = file[i].indexOf('.') ;
154         String JavaDoc userName = file[i].substring(0, idx) ;
155         importUserData(userName) ;
156         }
157     }
158     
159     public void importUserData(String JavaDoc userName) throws Exception JavaDoc {
160         Session session = null ;
161         String JavaDoc fileName = userDataDirectory_ + File.separator + userName + ".jar" ;
162       JarFile JavaDoc jar = new JarFile JavaDoc(new File JavaDoc(fileName)) ;
163       List JavaDoc 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 JavaDoc entryName = exporter.getName()+ ".xml" ;
169                 JarEntry JavaDoc entry = jar.getJarEntry(entryName) ;
170                 InputStream JavaDoc 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 JavaDoc ex) {
178             importLogger_.log(userName, ex) ;
179             ex.printStackTrace() ;
180         }
181       jar.close() ;
182     }
183     
184     public void exportServiceData() throws Exception JavaDoc {
185         List JavaDoc 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 JavaDoc serviceName) throws Exception JavaDoc {
193         Session session = null ;
194         ImporterExporter exporter = getImporterExporter(serviceName) ;
195         String JavaDoc fileName = serviceDataDirectory_ + File.separator + serviceName + ".jar" ;
196         JarOutputStream JavaDoc out = new JarOutputStream JavaDoc(new FileOutputStream JavaDoc(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 JavaDoc ex) {
205             exportLogger_.log(serviceName, ex) ;
206             ex.printStackTrace() ;
207     }
208     }
209     
210     public void importServiceData() throws Exception JavaDoc {
211         File JavaDoc dir = new File JavaDoc(serviceDataDirectory_) ;
212         String JavaDoc[] file = dir.list() ;
213         for(int i = 0 ; i < file.length; i++) {
214             int idx = file[i].indexOf('.') ;
215         String JavaDoc serviceName = file[i].substring(0, idx) ;
216         importServiceData(serviceName) ;
217         }
218     }
219     
220     public void importServiceData(String JavaDoc serviceName) throws Exception JavaDoc {
221     ImporterExporter exporter = getImporterExporter(serviceName) ;
222     if(exporter == null) return ;
223         Session session = null ;
224         String JavaDoc fileName = serviceDataDirectory_ + File.separator + serviceName + ".jar" ;
225       JarFile JavaDoc jar = new JarFile JavaDoc(new File JavaDoc(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 JavaDoc ex) {
233             importLogger_.log(serviceName, ex) ;
234             if(session != null) session.close() ;
235             ex.printStackTrace() ;
236         }
237       jar.close() ;
238     }
239 }
Popular Tags