KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > cms > publishing > DefaultFilePublisher


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 /* $Id: DefaultFilePublisher.java 42598 2004-03-01 16:18:28Z gregor $ */
19
20 package org.apache.lenya.cms.publishing;
21
22 import java.io.File JavaDoc;
23 import java.io.FileNotFoundException JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.util.StringTokenizer JavaDoc;
26
27 import org.apache.avalon.framework.parameters.Parameters;
28 import org.apache.lenya.cms.task.ExecutionException;
29 import org.apache.log4j.Category;
30
31
32 /**
33  * The <code>DefaultFilePublisher</code> is a task that copies XML
34  * source files from the authoring directory to another location,
35  * usually the live directory.
36  *
37  * The following task parameters must be provided:<br/
38  * ><code><strong>publication-id</strong></code>: the publication ID<br/
39  * ><code><strong>authoring-path</strong></code>: the authoring path<br/
40  * ><code><strong>tree-authoring-path</strong></code>: the location of the <code>tree.xml</code> file<br/
41  * ><code><strong>live-path</strong></code>: the live path<br/
42  * ><code><strong>tree-live-path</strong></code>: the location of the <code>tree.xml</code> file<br/
43  * ><code><strong>replication-path</strong></code>: the replication path, where sources are waiting
44  * for replication<br/
45  * ><code><strong>sources</strong></code>: a comma-separated list of files to publish<br/>
46  *
47  * * @deprecated use the publish ant task instead.
48  */

49 public class DefaultFilePublisher extends AbstractFilePublisher {
50     private static Category log = Category.getInstance(DefaultFilePublisher.class);
51     public static final String JavaDoc PARAMETER_SOURCES = "sources";
52
53     /**
54      * Default implementation of <code>publish</code> which simply
55      * copies the files from the absoluteAuthoringPath to the
56      * absoluteLivePath.
57      *
58      * @param publicationPath DOCUMENT ME!
59      * @param authoringPath DOCUMENT ME!
60      * @param treeAuthoringPath DOCUMENT ME!
61      * @param resourcesAuthoringPath path to authoring resources base directory
62      * @param livePath DOCUMENT ME!
63      * @param treeLivePath DOCUMENT ME!
64      * @param resourcesLivePath path to live resources base directory
65      * @param replicationPath DOCUMENT ME!
66      * @param sources DOCUMENT ME!
67      * @exception PublishingException if an error occurs
68      */

69     public void publish(String JavaDoc publicationPath, String JavaDoc authoringPath, String JavaDoc treeAuthoringPath,
70         String JavaDoc resourcesAuthoringPath, String JavaDoc livePath, String JavaDoc treeLivePath,
71         String JavaDoc resourcesLivePath, String JavaDoc replicationPath, String JavaDoc[] sources)
72         throws PublishingException {
73         log.debug("PUBLICATION: " + publicationPath);
74         log.debug("CONFIGURATION:\nauthoring path=" + authoringPath + "\nlive path=" + livePath);
75
76         // Set absolute paths
77
String JavaDoc absoluteAuthoringPath = publicationPath + authoringPath;
78         String JavaDoc absoluteTreeAuthoringPath = publicationPath + treeAuthoringPath;
79         String JavaDoc absoluteLivePath = publicationPath + livePath;
80         String JavaDoc absoluteTreeLivePath = publicationPath + treeLivePath;
81         String JavaDoc absoluteReplicationPath = publicationPath + replicationPath;
82
83         log.debug("DefaultFilePublisher.publish() has been called.");
84
85         for (int index = 0; index < sources.length; index++) {
86             File JavaDoc sourceFile = new File JavaDoc(absoluteAuthoringPath + sources[index]);
87             File JavaDoc destinationFile = new File JavaDoc(absoluteLivePath + sources[index]);
88             File JavaDoc destinationReplicationFile = new File JavaDoc(absoluteReplicationPath + sources[index]);
89
90             try {
91                 copyFile(sourceFile, destinationFile);
92                 log.debug("Document published: " + sourceFile + " " + destinationFile);
93                 copyFile(sourceFile, destinationReplicationFile);
94                 log.debug("Document ready for replication: " + sourceFile + " " +
95                     destinationReplicationFile);
96             } catch (FileNotFoundException JavaDoc fnfe) {
97                 throw new PublishingException("Document not published: Source file (" + sourceFile +
98                     ") not found!", fnfe);
99             } catch (IOException JavaDoc ioe) {
100                 throw new PublishingException("Document not published: " + sourceFile + " " +
101                     destinationFile, ioe);
102             }
103         }
104
105         // Update resources
106
publishResources(publicationPath, resourcesAuthoringPath, resourcesLivePath, sources);
107
108         // Update (copy) tree
109
try {
110             copyFile(new File JavaDoc(absoluteTreeAuthoringPath), new File JavaDoc(absoluteTreeLivePath));
111             log.debug("COPY\ntree source=" + absoluteTreeAuthoringPath + "\ntree destination=" +
112                 absoluteTreeLivePath);
113             log.debug("Tree published");
114         } catch (IOException JavaDoc ioe) {
115             throw new PublishingException("Tree not published: " + absoluteTreeAuthoringPath + " " +
116                 absoluteTreeLivePath, ioe);
117         }
118     }
119
120     /**
121      * Default implementation of <code>execute</code> (to implement
122      * the Task interface) which basically parses the parameters and
123      * invokes the publish method.
124      * @param contextPath a <code>String</code> value
125      * @exception ExecutionException if an error occurs
126      */

127     public void execute(String JavaDoc contextPath) throws ExecutionException {
128         log.debug(".execute(): Context Path: " + contextPath);
129
130         try {
131             String JavaDoc publicationId = getParameters().getParameter(PARAMETER_PUBLICATION_ID);
132
133             Parameters taskParameters = new Parameters();
134
135             PublishingEnvironment environment = new PublishingEnvironment(contextPath, publicationId);
136
137             // read default parameters from PublishingEnvironment
138
taskParameters.setParameter(PublishingEnvironment.PARAMETER_AUTHORING_PATH,
139                 environment.getAuthoringPath());
140             taskParameters.setParameter(PublishingEnvironment.PARAMETER_TREE_AUTHORING_PATH,
141                 environment.getTreeAuthoringPath());
142             taskParameters.setParameter(PublishingEnvironment.PARAMETER_LIVE_PATH,
143                 environment.getLivePath());
144             taskParameters.setParameter(PublishingEnvironment.PARAMETER_TREE_LIVE_PATH,
145                 environment.getTreeLivePath());
146
147             taskParameters.setParameter(PublishingEnvironment.PARAMETER_REPLICATION_PATH,
148                 environment.getReplicationDirectory());
149
150             taskParameters.merge(getParameters());
151             parameterize(taskParameters);
152
153             String JavaDoc sourcesString = getParameters().getParameter(PARAMETER_SOURCES);
154             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(sourcesString, ",");
155             String JavaDoc[] sources = new String JavaDoc[st.countTokens()];
156             int i = 0;
157
158             while (st.hasMoreTokens()) {
159                 sources[i++] = st.nextToken();
160             }
161
162             publish(PublishingEnvironment.getPublicationPath(contextPath, publicationId),
163                 getParameters().getParameter(PublishingEnvironment.PARAMETER_AUTHORING_PATH),
164                 getParameters().getParameter(PublishingEnvironment.PARAMETER_TREE_AUTHORING_PATH),
165                 null, getParameters().getParameter(PublishingEnvironment.PARAMETER_LIVE_PATH),
166                 getParameters().getParameter(PublishingEnvironment.PARAMETER_TREE_LIVE_PATH), null,
167                 getParameters().getParameter(PublishingEnvironment.PARAMETER_REPLICATION_PATH),
168                 sources);
169         } catch (Exception JavaDoc e) {
170             throw new ExecutionException(e);
171         }
172     }
173
174     /**
175      * A template method to publish the resources. The default
176      * implementation doesn't deal with resources. It simply ignores
177      * them and assumes that they end up in the live directory be some
178      * other means.
179      *
180      * @param publicationPath path to the publication
181      * @param resourcesAuthoringPath authoring path for the resources
182      * @param resourcesLivePath live path for the resources
183      * @param sources array of docIds
184      *
185      * @throws PublishingException if the publishing of resources fails
186      */

187     protected void publishResources(String JavaDoc publicationPath, String JavaDoc resourcesAuthoringPath,
188         String JavaDoc resourcesLivePath, String JavaDoc[] sources) throws PublishingException {
189     }
190 }
191
Popular Tags