KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > cms > cocoon > components > modules > input > FallbackModule


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: FallbackModule.java 43201 2004-08-11 13:11:31Z andreas $ */
19
20 package org.apache.lenya.cms.cocoon.components.modules.input;
21
22 import java.io.File JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import org.apache.avalon.framework.configuration.Configuration;
30 import org.apache.avalon.framework.configuration.ConfigurationException;
31 import org.apache.avalon.framework.service.ServiceException;
32 import org.apache.avalon.framework.service.ServiceManager;
33 import org.apache.avalon.framework.service.Serviceable;
34 import org.apache.excalibur.source.Source;
35 import org.apache.excalibur.source.SourceResolver;
36 import org.apache.excalibur.source.SourceUtil;
37
38 /**
39  * <p>
40  * This module checks if a file exists in a publiation, and if not, it chooses the core file. The
41  * attribute name must a path relatively to the <code>webapps/lenya/lenya</code> directory.
42  * </p>
43  * <p>
44  * Example: <code>{fallback:xslt/style.xsl}</code> looks if
45  * <code>lenya/pubs/(publication-id)/lenya/xslt/style.xsl</code> exists, and if not, it uses
46  * <code>lenya/xslt/style.xsl</code>.
47  */

48 public class FallbackModule extends AbstractPageEnvelopeModule implements Serviceable {
49
50     private ServiceManager manager;
51
52     private String JavaDoc[] baseUris;
53
54     public static final String JavaDoc PATH_PREFIX = "lenya/";
55
56     protected static final String JavaDoc ELEMENT_PATH = "directory";
57
58     protected static final String JavaDoc ATTRIBUTE_SRC = "src";
59
60     /**
61      * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
62      */

63     public void configure(Configuration conf) throws ConfigurationException {
64         super.configure(conf);
65
66         Configuration[] pathConfigs = conf.getChildren(ELEMENT_PATH);
67         List JavaDoc baseUriList = new ArrayList JavaDoc();
68
69         SourceResolver resolver = null;
70         try {
71             resolver = (SourceResolver) manager.lookup(SourceResolver.ROLE);
72             Source source = null;
73             for (int i = 0; i < pathConfigs.length; i++) {
74                 String JavaDoc uri = pathConfigs[i].getAttribute(ATTRIBUTE_SRC);
75                 try {
76                     source = resolver.resolveURI(uri);
77                     if (source.exists()) {
78                         File JavaDoc file = SourceUtil.getFile(source);
79                         if (file.isDirectory()) {
80                             baseUriList.add(uri);
81                         } else {
82                             getLogger().warn("Omitting path [" + uri + "] (not a directory).");
83                         }
84                     } else {
85                         getLogger().warn("Omitting path [" + uri + "] (does not exist).");
86                     }
87                 } catch (Exception JavaDoc e) {
88                     getLogger().error("Could not resolve path [" + uri + "]: ", e);
89                     throw e;
90                 } finally {
91                     if (source != null) {
92                         resolver.release(source);
93                     }
94                 }
95             }
96         } catch (Exception JavaDoc e) {
97             throw new ConfigurationException("Configuring failed: ", e);
98         } finally {
99             if (resolver != null) {
100                 manager.release(resolver);
101             }
102         }
103
104         this.baseUris = (String JavaDoc[]) baseUriList.toArray(new String JavaDoc[baseUriList.size()]);
105     }
106
107     /**
108      * @see org.apache.cocoon.components.modules.input.InputModule#getAttribute(java.lang.String,
109      * org.apache.avalon.framework.configuration.Configuration, java.util.Map)
110      */

111     public Object JavaDoc getAttribute(String JavaDoc name, Configuration modeConf, Map JavaDoc objectModel)
112             throws ConfigurationException {
113
114         if (getLogger().isDebugEnabled()) {
115             getLogger().debug("Resolving file for path [" + name + "]");
116         }
117
118         String JavaDoc resolvedUri = resolveURI(name, objectModel);
119         return resolvedUri;
120     }
121
122     /**
123      * Resolves the URI for a certain path.
124      * @param path The path.
125      * @param objectModel The object model.
126      * @return A string.
127      * @throws ConfigurationException if an error occurs.
128      */

129     protected String JavaDoc resolveURI(String JavaDoc path, Map JavaDoc objectModel) throws ConfigurationException {
130         String JavaDoc resolvedUri = null;
131         String JavaDoc checkedUris = "\n";
132
133         SourceResolver resolver = null;
134         try {
135             resolver = (SourceResolver) manager.lookup(SourceResolver.ROLE);
136
137             String JavaDoc[] baseUris = getBaseURIs(objectModel);
138             Source source = null;
139             int i = 0;
140             while (resolvedUri == null && i < baseUris.length) {
141                 String JavaDoc uri = baseUris[i] + "/" + path;
142
143                 checkedUris += uri + "\n";
144
145                 if (getLogger().isDebugEnabled()) {
146                     getLogger().debug("Trying to resolve URI [" + uri + "]");
147                 }
148
149                 try {
150                     source = resolver.resolveURI(uri);
151                     if (source.exists()) {
152                         resolvedUri = uri;
153                     } else {
154                         if (getLogger().isDebugEnabled()) {
155                             getLogger().debug("Skipping URI [" + uri + "] (does not exist).");
156                         }
157                     }
158                 } catch (Exception JavaDoc e) {
159                     getLogger().error("Could not resolve URI [" + uri + "]: ", e);
160                     throw e;
161                 } finally {
162                     if (source != null) {
163                         resolver.release(source);
164                     }
165                 }
166                 i++;
167             }
168
169         } catch (Exception JavaDoc e) {
170             throw new ConfigurationException("Resolving attribute [" + path + "] failed: ", e);
171         } finally {
172             if (resolver != null) {
173                 manager.release(resolver);
174             }
175         }
176
177         if (resolvedUri == null) {
178             throw new ConfigurationException("Could not resolve file for path [" + path + "]."
179                     + "\nChecked URIs:" + checkedUris);
180         }
181         else {
182             if (getLogger().isDebugEnabled()) {
183                 getLogger().debug("Resolved URI: [" + resolvedUri + "]");
184             }
185         }
186         return resolvedUri;
187     }
188
189     /**
190      * Returns the base directory URIs in the order they should be traversed.
191      * @param objectModel The object model.
192      * @return An array of strings.
193      * @throws ConfigurationException if an error occurs.
194      */

195     protected String JavaDoc[] getBaseURIs(Map JavaDoc objectModel) throws ConfigurationException {
196         return this.baseUris;
197     }
198
199     /**
200      * @see org.apache.cocoon.components.modules.input.InputModule#getAttributeNames(org.apache.avalon.framework.configuration.Configuration,
201      * java.util.Map)
202      */

203     public Iterator JavaDoc getAttributeNames(Configuration modeConf, Map JavaDoc objectModel)
204             throws ConfigurationException {
205         return Collections.EMPTY_SET.iterator();
206     }
207
208     /**
209      * @see org.apache.cocoon.components.modules.input.InputModule#getAttributeValues(java.lang.String,
210      * org.apache.avalon.framework.configuration.Configuration, java.util.Map)
211      */

212     public Object JavaDoc[] getAttributeValues(String JavaDoc name, Configuration modeConf, Map JavaDoc objectModel)
213             throws ConfigurationException {
214         Object JavaDoc[] objects = { getAttribute(name, modeConf, objectModel) };
215
216         return objects;
217     }
218
219     /**
220      * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
221      */

222     public void service(ServiceManager manager) throws ServiceException {
223         this.manager = manager;
224     }
225
226 }
Popular Tags