001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.portal.layout.renderer.aspect.impl;
018:
019: import java.util.ArrayList;
020: import java.util.Iterator;
021: import java.util.List;
022:
023: import org.apache.avalon.framework.configuration.Configuration;
024: import org.apache.avalon.framework.configuration.ConfigurationException;
025: import org.apache.avalon.framework.parameters.ParameterException;
026: import org.apache.avalon.framework.parameters.Parameters;
027: import org.apache.avalon.framework.service.ServiceException;
028: import org.apache.avalon.framework.service.ServiceSelector;
029: import org.apache.cocoon.portal.layout.renderer.aspect.RendererAspect;
030:
031: /**
032: * This chain holds all configured renderer aspects for one renderer.
033: *
034: * <h2>Configuration</h2>
035: * <table><tbody>
036: * <tr><th>aspect</th>
037: * <td>Multiple aspect renderer configurations. Required attribute
038: * <code>type</code>. Nested configuration must contain parameters
039: * for aspect rederer.
040: * </td>
041: * <td>req</td><td>Configuration</td><td><code>null</code></td>
042: * </tr>
043: * </tbody></table>
044: *
045: * @author <a href="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
046: * @author <a href="mailto:volker.schmitt@basf-it-services.com">Volker Schmitt</a>
047: *
048: * @version CVS $Id: RendererAspectChain.java 433543 2006-08-22 06:22:54Z crossley $
049: */
050: public final class RendererAspectChain {
051:
052: protected List aspects = new ArrayList(3);
053:
054: protected List configs = new ArrayList(3);
055:
056: protected List aspectDescriptions = new ArrayList(2);
057:
058: private boolean isRequired = false;
059:
060: public void configure(ServiceSelector selector, Configuration conf)
061: throws ConfigurationException {
062: if (conf != null) {
063: Configuration[] aspects = conf.getChildren("aspect");
064: if (aspects != null) {
065: for (int i = 0; i < aspects.length; i++) {
066: final Configuration current = aspects[i];
067: final String role = current.getAttribute("type");
068: try {
069: RendererAspect rAspect = (RendererAspect) selector
070: .select(role);
071: this .aspects.add(rAspect);
072: if (rAspect.isRequired()) {
073: isRequired = true;
074: }
075: Parameters aspectConfiguration = Parameters
076: .fromConfiguration(current);
077: Object compiledConf = rAspect
078: .prepareConfiguration(aspectConfiguration);
079: this .configs.add(compiledConf);
080:
081: Iterator descriptionIterator = rAspect
082: .getAspectDescriptions(compiledConf);
083: if (descriptionIterator != null) {
084: while (descriptionIterator.hasNext()) {
085: this .aspectDescriptions
086: .add(descriptionIterator.next());
087: }
088: }
089: } catch (ParameterException pe) {
090: throw new ConfigurationException(
091: "Unable to configure renderer aspect "
092: + role, pe);
093: } catch (ServiceException se) {
094: throw new ConfigurationException(
095: "Unable to lookup aspect " + role, se);
096: }
097: }
098: }
099: } else {
100: throw new ConfigurationException("No aspects configured");
101: }
102: }
103:
104: public Iterator getIterator() {
105: return this .aspects.iterator();
106: }
107:
108: public Iterator getConfigIterator() {
109: return this .configs.iterator();
110: }
111:
112: public Iterator getAspectDescriptionIterator() {
113: return this .aspectDescriptions.iterator();
114: }
115:
116: public void dispose(ServiceSelector selector) {
117: Iterator i = this .aspects.iterator();
118: while (i.hasNext()) {
119: selector.release(i.next());
120: }
121: this .aspects.clear();
122: }
123:
124: public boolean isRequired() {
125: return this.isRequired;
126: }
127: }
|