KickJava   Java API By Example, From Geeks To Geeks.

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


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: ResourceFilePublisher.java 43229 2004-08-16 13:05:40Z andreas $ */
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.excalibur.io.FileUtil;
28 import org.apache.avalon.framework.parameters.Parameters;
29 import org.apache.lenya.cms.task.ExecutionException;
30 import org.apache.log4j.Category;
31
32
33 /**
34  * The <code>ResourceFilePublisher</code> is a task that extends the
35  * <code>DefaultFilePublisher</code>. In addition to the default
36  * publisher it also copies resource files (e.g. images) from the
37  * authoring directory to another location, usually the live
38  * directory.
39  *
40  * In addition to the parameters that have to be provided for
41  * <code>DefaultFilePublisher</code> the following parameters have to
42  * be specified:<br/>
43  * <code><strong>resources-authoring-path</strong></code>: the
44  * authoring path for resources<br/>
45  * <code><strong>resources-live-path</strong></code>: the live path
46  * for resources<br/>
47  *
48  * @deprecated use the publish ant task instead.
49  */

50 public class ResourceFilePublisher extends DefaultFilePublisher {
51     private static Category log = Category.getInstance(ResourceFilePublisher.class);
52
53     /**
54      * (non-Javadoc)
55      * @see org.apache.lenya.cms.publishing.DefaultFilePublisher#publishResources(java.lang.String, java.lang.String, java.lang.String, java.lang.String[])
56      */

57     protected void publishResources(String JavaDoc publicationPath, String JavaDoc resourcesAuthoringPath,
58         String JavaDoc resourcesLivePath, String JavaDoc[] sources) throws PublishingException {
59         String JavaDoc absoluteResourceAuthoringPath = publicationPath + resourcesAuthoringPath;
60         String JavaDoc absoluteResourceLivePath = publicationPath + resourcesLivePath;
61
62         log.debug("Publishing resources from " + absoluteResourceAuthoringPath + " to " +
63             absoluteResourceLivePath);
64
65         for (int index = 0; index < sources.length; index++) {
66             File JavaDoc sourceDir = new File JavaDoc(absoluteResourceAuthoringPath +
67                     FileUtil.getPath(sources[index]));
68             File JavaDoc destinationDir = new File JavaDoc(absoluteResourceLivePath +
69                     FileUtil.getPath(sources[index]));
70
71             if (!sourceDir.isDirectory()) {
72                 // Hmm, the source dir doesn't exist. Ok, this
73
// document possibly doesn't have any related
74
// resources (e.g. images, etc.). Skip it.
75
continue;
76             }
77
78             log.debug("Copying resources from " + sourceDir + " to " + destinationDir);
79
80             File JavaDoc[] resourceSources = sourceDir.listFiles();
81
82             for (int j = 0; j < resourceSources.length; j++) {
83                 if (resourceSources[j].isFile()) {
84                     try {
85                         // only copy resources that are files
86
FileUtil.copyFileToDirectory(resourceSources[j], destinationDir);
87                         log.debug("COPY\nresource: " + resourceSources[j] + "\nto " +
88                             destinationDir);
89                     } catch (FileNotFoundException JavaDoc fnfe) {
90                         throw new PublishingException("Resource not published: Source file (" +
91                             resourceSources[j] + ") not found!", fnfe);
92                     } catch (IllegalArgumentException JavaDoc iae) {
93                         throw new PublishingException(
94                             "Resource not published: Live resources path (" + destinationDir +
95                             ") is not a directory ");
96                     } catch (IOException JavaDoc ioe) {
97                         throw new PublishingException("Resource not published: " +
98                             resourceSources[j] + " " + destinationDir, ioe);
99                     }
100                 }
101             }
102         }
103     }
104
105     /**
106      * Implementation of <code>execute</code> (to implement the Task
107      * interface) which also parses the parameters for resource
108      * publishing and invokes the publish method.
109      * @param contextPath a <code>String</code> value
110      * @exception ExecutionException if an error occurs
111      */

112     public void execute(String JavaDoc contextPath) throws ExecutionException {
113         try {
114             String JavaDoc publicationId = getParameters().getParameter(PARAMETER_PUBLICATION_ID);
115
116             Parameters taskParameters = new Parameters();
117
118             ResourcePublishingEnvironment environment = new ResourcePublishingEnvironment(contextPath,
119                     publicationId);
120
121             // read default parameters from PublishingEnvironment
122
taskParameters.setParameter(PublishingEnvironment.PARAMETER_AUTHORING_PATH,
123                 environment.getAuthoringPath());
124             taskParameters.setParameter(PublishingEnvironment.PARAMETER_TREE_AUTHORING_PATH,
125                 environment.getTreeAuthoringPath());
126             taskParameters.setParameter(ResourcePublishingEnvironment.PARAMETER_RESOURCE_AUTHORING_PATH,
127                 environment.getResourceAuthoringPath());
128             taskParameters.setParameter(PublishingEnvironment.PARAMETER_LIVE_PATH,
129                 environment.getLivePath());
130             taskParameters.setParameter(PublishingEnvironment.PARAMETER_TREE_LIVE_PATH,
131                 environment.getTreeLivePath());
132             taskParameters.setParameter(ResourcePublishingEnvironment.PARAMETER_RESOURCE_LIVE_PATH,
133                 environment.getResourceLivePath());
134
135             taskParameters.setParameter(PublishingEnvironment.PARAMETER_REPLICATION_PATH,
136                 environment.getReplicationDirectory());
137
138             taskParameters.merge(getParameters());
139             parameterize(taskParameters);
140
141             String JavaDoc sourcesString = getParameters().getParameter(PARAMETER_SOURCES);
142             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(sourcesString, ",");
143             String JavaDoc[] sources = new String JavaDoc[st.countTokens()];
144             int i = 0;
145
146             while (st.hasMoreTokens()) {
147                 sources[i++] = st.nextToken();
148             }
149
150             publish(PublishingEnvironment.getPublicationPath(contextPath, publicationId),
151                 getParameters().getParameter(PublishingEnvironment.PARAMETER_AUTHORING_PATH),
152                 getParameters().getParameter(PublishingEnvironment.PARAMETER_TREE_AUTHORING_PATH),
153                 getParameters().getParameter(ResourcePublishingEnvironment.PARAMETER_RESOURCE_AUTHORING_PATH),
154                 getParameters().getParameter(PublishingEnvironment.PARAMETER_LIVE_PATH),
155                 getParameters().getParameter(PublishingEnvironment.PARAMETER_TREE_LIVE_PATH),
156                 getParameters().getParameter(ResourcePublishingEnvironment.PARAMETER_RESOURCE_LIVE_PATH),
157                 getParameters().getParameter(PublishingEnvironment.PARAMETER_REPLICATION_PATH),
158                 sources);
159         } catch (Exception JavaDoc e) {
160             throw new ExecutionException(e);
161         }
162     }
163 }
164
Popular Tags