KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > module > CmsModuleImportExportHandler


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/module/CmsModuleImportExportHandler.java,v $
3  * Date : $Date: 2006/03/27 14:53:03 $
4  * Version: $Revision: 1.33 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.module;
33
34 import org.opencms.configuration.CmsConfigurationException;
35 import org.opencms.configuration.CmsModuleConfiguration;
36 import org.opencms.file.CmsObject;
37 import org.opencms.file.CmsProject;
38 import org.opencms.i18n.CmsMessageContainer;
39 import org.opencms.importexport.CmsExport;
40 import org.opencms.importexport.CmsImport;
41 import org.opencms.importexport.CmsImportExportException;
42 import org.opencms.importexport.CmsImportExportManager;
43 import org.opencms.importexport.I_CmsImportExportHandler;
44 import org.opencms.main.CmsException;
45 import org.opencms.main.CmsLog;
46 import org.opencms.main.OpenCms;
47 import org.opencms.report.CmsHtmlReport;
48 import org.opencms.report.I_CmsReport;
49 import org.opencms.security.CmsRole;
50 import org.opencms.security.CmsRoleViolationException;
51 import org.opencms.security.CmsSecurityException;
52 import org.opencms.xml.CmsXmlErrorHandler;
53 import org.opencms.xml.CmsXmlException;
54
55 import java.io.File JavaDoc;
56 import java.io.FileInputStream JavaDoc;
57 import java.io.IOException JavaDoc;
58 import java.io.InputStream JavaDoc;
59 import java.util.Arrays JavaDoc;
60 import java.util.Collections JavaDoc;
61 import java.util.Iterator JavaDoc;
62 import java.util.List JavaDoc;
63 import java.util.zip.ZipEntry JavaDoc;
64 import java.util.zip.ZipFile JavaDoc;
65
66 import org.apache.commons.digester.Digester;
67 import org.apache.commons.logging.Log;
68
69 import org.dom4j.Document;
70 import org.dom4j.Element;
71 import org.xml.sax.SAXException JavaDoc;
72
73 /**
74  * Import/export handler implementation for Cms modules.<p>
75  *
76  * @author Thomas Weckert
77  *
78  * @version $Revision: 1.33 $
79  *
80  * @since 6.0.0
81  */

82 public class CmsModuleImportExportHandler implements I_CmsImportExportHandler {
83
84     /** The log object for this class. */
85     private static final Log LOG = CmsLog.getLog(CmsModuleImportExportHandler.class);
86
87     /** The VFS resources to be exported additionally with the module.<p> */
88     private List JavaDoc m_additionalResources;
89
90     /** The description of this import/export handler.<p> */
91     private String JavaDoc m_description;
92
93     /** The name of the export file in the real file system.<p> */
94     private String JavaDoc m_fileName;
95
96     /** The module imported with the digester. */
97     private CmsModule m_importedModule;
98
99     /** The (package) name of the module to be exported.<p> */
100     private String JavaDoc m_moduleName;
101
102     /**
103      * Creates a new Cms module import/export handler.<p>
104      */

105     public CmsModuleImportExportHandler() {
106
107         super();
108         m_description = org.opencms.importexport.Messages.get().getBundle().key(
109             org.opencms.importexport.Messages.GUI_CMSIMPORTHANDLER_DEFAULT_DESC_0);
110     }
111
112     /**
113      * Reads a module object from an external file source.<p>
114      *
115      * @param importResource the name of the input source
116      * @return the imported module
117      * @throws CmsConfigurationException if the module could not be imported
118      */

119     public static CmsModule readModuleFromImport(String JavaDoc importResource) throws CmsConfigurationException {
120
121         // instantiate Digester and enable XML validation
122
Digester digester = new Digester();
123         digester.setUseContextClassLoader(true);
124         digester.setValidating(false);
125         digester.setRuleNamespaceURI(null);
126         digester.setErrorHandler(new CmsXmlErrorHandler());
127
128         // add this class to the Digester
129
CmsModuleImportExportHandler handler = new CmsModuleImportExportHandler();
130         digester.push(handler);
131
132         CmsModuleXmlHandler.addXmlDigesterRules(digester);
133
134         InputStream JavaDoc stream = null;
135         ZipFile JavaDoc importZip = null;
136
137         try {
138
139             File JavaDoc file = new File JavaDoc(importResource);
140             if (file.isFile()) {
141                 importZip = new ZipFile JavaDoc(importResource);
142                 ZipEntry JavaDoc entry = importZip.getEntry(CmsImportExportManager.EXPORT_MANIFEST);
143                 if (entry != null) {
144                     stream = importZip.getInputStream(entry);
145                 } else {
146                     CmsMessageContainer message = Messages.get().container(
147                         Messages.ERR_NO_MANIFEST_MODULE_IMPORT_1,
148                         importResource);
149                     LOG.error(message.key());
150                     throw new CmsConfigurationException(message);
151                 }
152             } else if (file.isDirectory()) {
153                 file = new File JavaDoc(file, CmsImportExportManager.EXPORT_MANIFEST);
154                 stream = new FileInputStream JavaDoc(file);
155             }
156
157             // start the parsing process
158
digester.parse(stream);
159
160         } catch (IOException JavaDoc e) {
161             CmsMessageContainer message = Messages.get().container(Messages.ERR_IO_MODULE_IMPORT_1, importResource);
162             LOG.error(message.key(), e);
163             throw new CmsConfigurationException(message, e);
164         } catch (SAXException JavaDoc e) {
165             CmsMessageContainer message = Messages.get().container(Messages.ERR_SAX_MODULE_IMPORT_1, importResource);
166             LOG.error(message.key(), e);
167             throw new CmsConfigurationException(message, e);
168         } finally {
169             try {
170                 if (importZip != null) {
171                     importZip.close();
172                 }
173                 if (stream != null) {
174                     stream.close();
175                 }
176             } catch (Exception JavaDoc e) {
177                 // noop
178
}
179         }
180
181         CmsModule importedModule = handler.getModule();
182
183         // the digester must have set the module now
184
if (importedModule == null) {
185             throw new CmsConfigurationException(Messages.get().container(
186                 Messages.ERR_IMPORT_MOD_ALREADY_INSTALLED_1,
187                 importResource));
188         }
189
190         return importedModule;
191     }
192
193     /**
194      * @see org.opencms.importexport.I_CmsImportExportHandler#exportData(org.opencms.file.CmsObject, org.opencms.report.I_CmsReport)
195      */

196     public void exportData(CmsObject cms, I_CmsReport report)
197     throws CmsConfigurationException, CmsImportExportException, CmsRoleViolationException {
198
199         // check if the user has the required permissions
200
cms.checkRole(CmsRole.MODULE_MANAGER);
201
202         report.print(Messages.get().container(Messages.RPT_EXPORT_MODULE_BEGIN_0), I_CmsReport.FORMAT_HEADLINE);
203         if (report instanceof CmsHtmlReport) {
204             report.print(org.opencms.report.Messages.get().container(
205                 org.opencms.report.Messages.RPT_ARGUMENT_1,
206                 "<i>" + getModuleName() + "</i>"));
207
208         } else {
209             report.print(org.opencms.report.Messages.get().container(
210                 org.opencms.report.Messages.RPT_ARGUMENT_1,
211                 getModuleName()));
212         }
213         report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0));
214
215         if (!OpenCms.getModuleManager().hasModule(getModuleName())) {
216             // module not available
217
throw new CmsConfigurationException(Messages.get().container(
218                 Messages.ERR_NO_MOD_FOR_EXPORT_1,
219                 getModuleName()));
220         }
221
222         // generate module XML
223
CmsModule module = OpenCms.getModuleManager().getModule(getModuleName());
224         if (!module.getVersion().isUpdated()) {
225             // increment version number if not recently updated
226
module.getVersion().increment();
227             // update the XML configuration
228
OpenCms.writeConfiguration(CmsModuleConfiguration.class);
229         }
230         // reset update status so that all following exports auto-increment the number
231
module.getVersion().setUpdated(false);
232         Element moduleElement = CmsModuleXmlHandler.generateXml(module);
233
234         // export the module using the standard export
235
new CmsExport(cms, getFileName(), getAdditionalResources(), true, true, moduleElement, false, 0, report, true);
236
237         report.println(Messages.get().container(Messages.RPT_EXPORT_MODULE_END_0), I_CmsReport.FORMAT_HEADLINE);
238     }
239
240     /**
241      * Returns the VFS resources to be exported additionally with the module.<p>
242      *
243      * @return the VFS resources to be exported additionally with the module
244      */

245     public List JavaDoc getAdditionalResources() {
246
247         return m_additionalResources;
248     }
249
250     /**
251      * @see org.opencms.importexport.I_CmsImportExportHandler#getDescription()
252      */

253     public String JavaDoc getDescription() {
254
255         return m_description;
256     }
257
258     /**
259      * Returns the name of the export file in the real file system.<p>
260      *
261      * @return the name of the export file in the real file system
262      */

263     public String JavaDoc getFileName() {
264
265         return m_fileName;
266     }
267
268     /**
269      * Returns the (package) name of the module to be exported.<p>
270      *
271      * @return the (package) name of the module to be exported
272      */

273     public String JavaDoc getModuleName() {
274
275         return m_moduleName;
276     }
277
278     /**
279      * Returns the VFS resources to be exported additionally with the module as a list.<p>
280      *
281      * @return the VFS resources to be exported additionally with the module as a list
282      */

283     public List JavaDoc getResourcesAsList() {
284
285         return m_additionalResources;
286     }
287
288     /**
289      * @see org.opencms.importexport.I_CmsImportExportHandler#importData(org.opencms.file.CmsObject, java.lang.String, java.lang.String, org.opencms.report.I_CmsReport)
290      */

291     public synchronized void importData(CmsObject cms, String JavaDoc importFile, String JavaDoc importPath, I_CmsReport report)
292     throws CmsXmlException, CmsImportExportException, CmsRoleViolationException, CmsException {
293
294         CmsProject previousProject = cms.getRequestContext().currentProject();
295         try {
296
297             importFile = importFile.replace('\\', '/');
298             String JavaDoc moduleZipName = importFile.substring(importFile.lastIndexOf('/') + 1);
299             String JavaDoc modulePackageName;
300
301             if (moduleZipName.toLowerCase().endsWith(".zip")) {
302                 modulePackageName = moduleZipName.substring(0, moduleZipName.lastIndexOf('.'));
303                 int pos = modulePackageName.lastIndexOf('_');
304                 if (pos > 0) {
305                     modulePackageName = modulePackageName.substring(0, pos);
306                 }
307             } else {
308                 modulePackageName = moduleZipName;
309             }
310
311             CmsProject importProject = null;
312
313             try {
314                 cms.getRequestContext().saveSiteRoot();
315                 cms.getRequestContext().setSiteRoot("/");
316
317                 try {
318                     // try to read a (leftover) module import project
319
importProject = cms.readProject(Messages.get().getBundle(cms.getRequestContext().getLocale()).key(
320                         Messages.GUI_IMPORT_MODULE_PROJECT_NAME_1,
321                         new Object JavaDoc[] {modulePackageName}));
322                 } catch (CmsException e) {
323                     // create a Project to import the module
324
importProject = cms.createProject(
325                         Messages.get().getBundle(cms.getRequestContext().getLocale()).key(
326                             Messages.GUI_IMPORT_MODULE_PROJECT_NAME_1,
327                             new Object JavaDoc[] {modulePackageName}),
328                         Messages.get().getBundle(cms.getRequestContext().getLocale()).key(
329                             Messages.GUI_IMPORT_MODULE_PROJECT_DESC_1,
330                             new Object JavaDoc[] {modulePackageName}),
331                         OpenCms.getDefaultUsers().getGroupAdministrators(),
332                         OpenCms.getDefaultUsers().getGroupAdministrators(),
333                         CmsProject.PROJECT_TYPE_TEMPORARY);
334                 }
335
336                 cms.getRequestContext().setCurrentProject(importProject);
337
338                 // copy the root folder to the project
339
cms.copyResourceToProject("/");
340             } finally {
341                 cms.getRequestContext().restoreSiteRoot();
342             }
343
344             report.print(Messages.get().container(Messages.RPT_IMPORT_MODULE_BEGIN_0), I_CmsReport.FORMAT_HEADLINE);
345             if (report instanceof CmsHtmlReport) {
346                 report.print(org.opencms.report.Messages.get().container(
347                     org.opencms.report.Messages.RPT_ARGUMENT_1,
348                     "<i>" + modulePackageName + "</i>"));
349             } else {
350                 report.print(org.opencms.report.Messages.get().container(
351                     org.opencms.report.Messages.RPT_ARGUMENT_1,
352                     modulePackageName));
353             }
354             report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0));
355
356             importModule(cms, importFile, report);
357
358             report.println(Messages.get().container(Messages.RPT_PUBLISH_PROJECT_BEGIN_0), I_CmsReport.FORMAT_HEADLINE);
359             // now unlock and publish the project
360
cms.unlockProject(importProject.getId());
361             cms.publishProject(report);
362
363             report.println(Messages.get().container(Messages.RPT_PUBLISH_PROJECT_END_0), I_CmsReport.FORMAT_HEADLINE);
364             report.println(Messages.get().container(Messages.RPT_IMPORT_MODULE_END_0), I_CmsReport.FORMAT_HEADLINE);
365         } finally {
366             cms.getRequestContext().setCurrentProject(previousProject);
367         }
368     }
369
370     /**
371      * @see org.opencms.importexport.I_CmsImportExportHandler#matches(org.dom4j.Document)
372      */

373     public boolean matches(Document manifest) {
374
375         Element rootElement = manifest.getRootElement();
376
377         boolean hasModuleNode = (rootElement.selectNodes("./module/name").size() > 0);
378         return (hasModuleNode);
379     }
380
381     /**
382      * Sets the VFS resources to be exported additionally with the module.<p>
383      *
384      * @param resources the VFS resources to be exported additionally with the module
385      */

386     public void setAdditionalResources(String JavaDoc[] resources) {
387
388         m_additionalResources = Arrays.asList(resources);
389     }
390
391     /**
392      * @see org.opencms.importexport.I_CmsImportExportHandler#setDescription(java.lang.String)
393      */

394     public void setDescription(String JavaDoc description) {
395
396         m_description = description;
397     }
398
399     /**
400      * Sets the name of the export file in the real file system.<p>
401      *
402      * @param fileName the name of the export file in the real file system
403      */

404     public void setFileName(String JavaDoc fileName) {
405
406         m_fileName = fileName;
407     }
408
409     /**
410      * Will be called by the digester if a module was imported.<p>
411      *
412      * @param moduleHandler contains the imported module
413      */

414     public void setModule(CmsModuleXmlHandler moduleHandler) {
415
416         m_importedModule = moduleHandler.getModule();
417     }
418
419     /**
420      * Sets the (package) name of the module to be exported.<p>
421      *
422      * @param moduleName the (package) name of the module to be exported
423      */

424     public void setModuleName(String JavaDoc moduleName) {
425
426         m_moduleName = moduleName;
427     }
428
429     /**
430      * @see java.lang.Object#finalize()
431      */

432     protected void finalize() throws Throwable JavaDoc {
433
434         try {
435             if (m_additionalResources != null) {
436                 m_additionalResources.clear();
437             }
438             m_additionalResources = null;
439         } catch (Exception JavaDoc e) {
440             // noop
441
} finally {
442             super.finalize();
443         }
444     }
445
446     /**
447      * Returns the module imported with the digester.<p>
448      *
449      * @return the module imported with the digester
450      */

451     private CmsModule getModule() {
452
453         return m_importedModule;
454     }
455
456     /**
457      * Imports a module from a external file source.<p>
458      *
459      * @param cms must have been initialized with "Admin" permissions
460      * @param importResource the name of the input source
461      * @param report the report to print the progess information to
462      * @throws CmsSecurityException if no "Admin" permissions are available
463      * @throws CmsConfigurationException if the module is already installed or the
464      * dependencies are not fulfilled
465      * @throws CmsException if errors occur reading the module data
466      */

467     private synchronized void importModule(CmsObject cms, String JavaDoc importResource, I_CmsReport report)
468     throws CmsSecurityException, CmsConfigurationException, CmsException {
469
470         // check if the user has the required permissions
471
cms.checkRole(CmsRole.MODULE_MANAGER);
472
473         // read the module from the import file
474
CmsModule importedModule = readModuleFromImport(importResource);
475
476         // check if the module is already istalled
477
if (OpenCms.getModuleManager().hasModule(importedModule.getName())) {
478             throw new CmsConfigurationException(Messages.get().container(
479                 Messages.ERR_MOD_ALREADY_INSTALLED_1,
480                 importedModule.getName()));
481         }
482
483         // check the module dependencies
484
List JavaDoc dependencies = OpenCms.getModuleManager().checkDependencies(
485             importedModule,
486             CmsModuleManager.DEPENDENCY_MODE_IMPORT);
487         if (dependencies.size() > 0) {
488             // some dependencies not fulfilled
489
StringBuffer JavaDoc missingModules = new StringBuffer JavaDoc();
490             Iterator JavaDoc it = dependencies.iterator();
491             while (it.hasNext()) {
492                 CmsModuleDependency dependency = (CmsModuleDependency)it.next();
493                 missingModules.append(" ").append(dependency.getName()).append(", Version ").append(
494                     dependency.getVersion()).append("\r\n");
495             }
496             throw new CmsConfigurationException(Messages.get().container(
497                 Messages.ERR_MOD_DEPENDENCY_INFO_2,
498                 importedModule.getName() + ", Version " + importedModule.getVersion(),
499                 missingModules));
500         }
501
502         // add the imported module to the module manager
503
OpenCms.getModuleManager().addModule(cms, importedModule);
504
505         // reinitialize the resource manager with additional module resourcetypes if nescessary
506
if (importedModule.getResourceTypes() != Collections.EMPTY_LIST) {
507             OpenCms.getResourceManager().initialize(cms);
508         }
509         // reinitialize the workplace manager with addititonal module explorertypes if nescessary
510
if (importedModule.getExplorerTypes() != Collections.EMPTY_LIST) {
511             OpenCms.getWorkplaceManager().addExplorerTypeSettings(importedModule);
512         }
513
514         // import the module resources
515
CmsImport cmsImport = new CmsImport(cms, importResource, "/", report);
516         cmsImport.importResources();
517     }
518 }
Popular Tags