KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opencms > legacy > CmsLegacyModuleAction


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src-modules/com/opencms/legacy/CmsLegacyModuleAction.java,v $
3  * Date : $Date: 2005/06/27 23:27:46 $
4  * Version: $Revision: 1.9 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (C) 2002 Alkacon Software (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, 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 com.opencms.legacy;
33
34 import org.opencms.configuration.CmsConfigurationManager;
35 import org.opencms.db.CmsDbPool;
36 import org.opencms.db.CmsPublishList;
37 import org.opencms.file.CmsObject;
38 import org.opencms.file.CmsProject;
39 import org.opencms.file.CmsRequestContext;
40 import org.opencms.main.CmsException;
41 import org.opencms.main.CmsLog;
42 import org.opencms.main.OpenCms;
43 import org.opencms.module.A_CmsModuleAction;
44 import org.opencms.module.CmsModule;
45 import org.opencms.report.I_CmsReport;
46 import org.opencms.util.CmsStringUtil;
47 import org.opencms.util.CmsUUID;
48
49 import com.opencms.defaults.master.genericsql.*;
50
51 import java.util.ArrayList JavaDoc;
52 import java.util.Collections JavaDoc;
53 import java.util.HashMap JavaDoc;
54 import java.util.Iterator JavaDoc;
55 import java.util.List JavaDoc;
56 import java.util.Map JavaDoc;
57 import java.util.Vector JavaDoc;
58
59 /**
60  * Provided backward compatiblity for legacy (5.0) module "publishClass"
61  * and "maintainanceClass" methods.<p>
62  *
63  * @author Alexander Kandzior (a.kandzior@alkacon.com)
64  *
65  * @deprecated Will not be supported past the OpenCms 6 release.
66  */

67 public class CmsLegacyModuleAction extends A_CmsModuleAction {
68
69     /** Name of the "publishclasses" parameter. */
70     public static final String JavaDoc C_PARAM_PUBLISH_CLASSES = "publishclasses";
71
72     /** Name of the "maintananceclasses" parameter. */
73     public static final String JavaDoc C_PARAM_MAINTANANCE_CLASSES = "maintananceclasses";
74     
75     /** Name of the legacy compatibility module. */
76     public static final String JavaDoc C_LEGACY_MODULE_NAME = "org.opencms.legacy.compatibility";
77     
78     /**
79      * Splitsa a list of class names with a ";" delimiter.<p>
80      *
81      * @param value the value to split
82      * @return a list of class names
83      */

84     private static List splitClassNames(String JavaDoc value) {
85
86         ArrayList JavaDoc result = new ArrayList JavaDoc();
87         
88         if (value == null) {
89             return result;
90         }
91         
92         String JavaDoc[] names = CmsStringUtil.splitAsArray(value, ';');
93         for (int i=0; i<names.length; i++) {
94             String JavaDoc name = names[i].trim();
95             if (CmsStringUtil.isNotEmpty(name)) {
96                 result.add(name);
97             }
98         }
99         
100         return result;
101     }
102     
103     /**
104      * Returns the list of configured legacy module publish classes.<p>
105      *
106      * @return the list of configured legacy module publish classes
107      */

108     public static List getLegacyModulePublishClasses() {
109         
110         CmsModule module = OpenCms.getModuleManager().getModule(C_LEGACY_MODULE_NAME);
111         if (module == null) {
112             // module is not (correctly) configured
113
return Collections.EMPTY_LIST;
114         }
115         
116         return splitClassNames(module.getParameter(C_PARAM_PUBLISH_CLASSES));
117     }
118
119     /**
120      * @see org.opencms.module.I_CmsModuleAction#initialize(org.opencms.file.CmsObject, CmsConfigurationManager, CmsModule)
121      */

122     public void initialize(CmsObject adminCms, CmsConfigurationManager configurationManager, CmsModule module) {
123         
124         Map JavaDoc config = adminCms.getConfigurations();
125         String JavaDoc dbName = config.get(CmsDbPool.KEY_DATABASE_NAME).toString().toLowerCase();
126         String JavaDoc poolUrl = config.get("db.cos.pool").toString();
127         
128         CmsDbAccess dbAccess = new CmsDbAccess(poolUrl);
129         boolean masterAvailable = dbAccess.checkTables();
130
131         if (CmsLog.getLog(this).isDebugEnabled()) {
132             CmsLog.getLog(this).debug("Checking master module tables for " + dbName
133                 + " - " + ((masterAvailable) ? "found" : "not found"));
134         }
135         
136         if (!masterAvailable) {
137             if (CmsLog.getLog(this).isDebugEnabled()) {
138                 CmsLog.getLog(this).debug("Calling master module table setup script for " + dbName);
139             }
140             
141             String JavaDoc[] modulePath = (String JavaDoc[])module.getResources().toArray(new String JavaDoc[1]);
142             
143             String JavaDoc scriptPath;
144             if (dbName.toLowerCase().startsWith("mysql")) {
145                 // use the mysql setup script for all MySQL variations (Innodb, MySQL 4.1 etc. pp.)
146
scriptPath = modulePath[0] + "setup/mysql.sql";
147             } else {
148                 scriptPath = modulePath[0] + "setup/" + dbName + ".sql";
149             }
150             
151             try {
152                 String JavaDoc updateScript = new String JavaDoc(adminCms.readFile(scriptPath).getContents());
153                 
154                 HashMap JavaDoc replacers = new HashMap JavaDoc();
155                 for (Iterator JavaDoc i = module.getParameters().keySet().iterator(); i.hasNext();) {
156                     String JavaDoc param = (String JavaDoc)i.next(), value = null;
157                     if (param.startsWith("$")) {
158                         value = (String JavaDoc)module.getParameters().get(param);
159                         replacers.put(param, value);
160                     }
161                 }
162                 dbAccess.updateDatabase(updateScript, replacers);
163             } catch (CmsException exc) {
164                 if (CmsLog.getLog(this).isErrorEnabled()) {
165                     CmsLog.getLog(this).error(
166                         "Error while creating master module tables",
167                         exc);
168                 }
169             }
170             
171             if (CmsLog.getLog(this).isInfoEnabled()) {
172                 CmsLog.getLog(this).info(
173                         ". Legacy initialization: Created master module tables");
174             }
175         }
176     }
177     
178     /**
179      * @see org.opencms.module.I_CmsModuleAction#publishProject(org.opencms.file.CmsObject, org.opencms.db.CmsPublishList, int, org.opencms.report.I_CmsReport)
180      */

181     public void publishProject(CmsObject cms, CmsPublishList publishList, int backupTagId, I_CmsReport report) {
182
183         CmsRequestContext context = cms.getRequestContext();
184
185         if (!publishList.isDirectPublish()
186             && context.currentProject().getType() != CmsProject.PROJECT_TYPE_TEMPORARY) {
187             
188             List legacyPublishClasses = getLegacyModulePublishClasses();
189             if (legacyPublishClasses.isEmpty()) {
190                 // no legacy publish classes configured in module
191
return;
192             }
193
194             long publishDate = System.currentTimeMillis();
195             boolean backupEnabled = OpenCms.getSystemInfo().isVersionHistoryEnabled();
196
197             if (backupEnabled) {
198                 try {
199                     publishDate = cms.readBackupProject(backupTagId).getPublishingDate();
200                 } catch (CmsException e) {
201                     // nothing to do
202
}
203
204                 if (publishDate == 0) {
205                     publishDate = System.currentTimeMillis();
206                 }
207             }
208
209             Vector JavaDoc changedResources = new Vector JavaDoc();
210             Vector JavaDoc changedModuleMasters = new Vector JavaDoc();
211
212             for (int i = 0; i < legacyPublishClasses.size(); i++) {
213                 // call the publishProject method of the class with parameters:
214
// cms, m_enableHistory, project_id, version_id, publishDate, subId,
215
// the vector changedResources and the vector changedModuleMasters
216
try {
217                     // The changed masters are added to the vector changedModuleMasters, so after the last module
218
// was published the vector contains the changed masters of all published modules
219
Class.forName((String JavaDoc)legacyPublishClasses.get(i)).getMethod(
220                         "publishProject",
221                         new Class JavaDoc[] {
222                             CmsObject.class,
223                             CmsUUID.class,
224                             Boolean JavaDoc.class,
225                             Integer JavaDoc.class,
226                             Integer JavaDoc.class,
227                             Long JavaDoc.class,
228                             Vector JavaDoc.class,
229                             Vector JavaDoc.class}).invoke(
230                         null,
231                         new Object JavaDoc[] {
232                             cms,
233                             publishList.getPublishHistoryId(),
234                             new Boolean JavaDoc(OpenCms.getSystemInfo().isVersionHistoryEnabled()),
235                             new Integer JavaDoc(context.currentProject().getId()),
236                             new Integer JavaDoc(backupTagId),
237                             new Long JavaDoc(publishDate),
238                             changedResources,
239                             changedModuleMasters});
240                 } catch (ClassNotFoundException JavaDoc ec) {
241                     report.println(Messages.get().container(Messages.RPT_NONEXISTENT_PUBLISH_CLASS_FOR_MODULE_1,
242                         legacyPublishClasses.get(i)), I_CmsReport.FORMAT_WARNING);
243                     if (CmsLog.getLog(this).isErrorEnabled()) {
244                         CmsLog.getLog(this).error(
245                             "Error calling publish class of module " + (String JavaDoc)legacyPublishClasses.get(i),
246                             ec);
247                     }
248                 } catch (Throwable JavaDoc t) {
249                     report.println(t);
250                     if (CmsLog.getLog(this).isErrorEnabled()) {
251                         CmsLog.getLog(this).error(
252                             "Error while publishing data of module " + (String JavaDoc)legacyPublishClasses.get(i),
253                             t);
254                     }
255                 }
256             }
257         }
258     }
259 }
Popular Tags