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.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.Parameters;
026: import org.apache.avalon.framework.service.ServiceException;
027: import org.apache.avalon.framework.service.ServiceSelector;
028: import org.apache.cocoon.portal.PortalManagerAspect;
029: import org.apache.cocoon.portal.coplet.adapter.CopletAdapter;
030:
031: /**
032: * This chain holds all configured aspects for a portal manager.
033: * @since 2.1.8
034: * @version SVN $Id: PortalManagerAspectChain.java 433543 2006-08-22 06:22:54Z crossley $
035: */
036: public final class PortalManagerAspectChain {
037:
038: protected List aspects = new ArrayList(3);
039:
040: protected List configs = new ArrayList(3);
041:
042: public void configure(ServiceSelector aspectSelector,
043: ServiceSelector adapterSelector, Configuration conf,
044: PortalManagerAspect endAspect,
045: Parameters endAspectParameters)
046: throws ConfigurationException {
047: if (conf != null) {
048: Configuration[] aspects = conf.getChildren("aspect");
049: for (int i = 0; i < aspects.length; i++) {
050: final Configuration current = aspects[i];
051: final String role = current.getAttribute("type", null);
052: PortalManagerAspect pAspect;
053: if (role != null) {
054: if (aspectSelector == null) {
055: throw new ConfigurationException(
056: "No selector for aspects defined.");
057: }
058: try {
059: pAspect = (PortalManagerAspect) aspectSelector
060: .select(role);
061: } catch (ServiceException se) {
062: throw new ConfigurationException(
063: "Unable to lookup aspect " + role,
064: current, se);
065: }
066: } else {
067: final String adapterName = current.getAttribute(
068: "adapter", null);
069: if (adapterName == null) {
070: throw new ConfigurationException(
071: "Aspect configuration requires either a type or an adapter attribute.",
072: current);
073: }
074: try {
075: pAspect = (PortalManagerAspect) adapterSelector
076: .select(adapterName);
077: } catch (ServiceException se) {
078: throw new ConfigurationException(
079: "Unable to lookup coplet adapter "
080: + adapterName, current, se);
081: }
082: }
083: this .aspects.add(pAspect);
084: Parameters aspectConfiguration = Parameters
085: .fromConfiguration(current);
086: this .configs.add(aspectConfiguration);
087: }
088: }
089: this .aspects.add(endAspect);
090: this .configs.add(endAspectParameters);
091: }
092:
093: public Iterator getIterator() {
094: return this .aspects.iterator();
095: }
096:
097: public Iterator getConfigIterator() {
098: return this .configs.iterator();
099: }
100:
101: public void dispose(ServiceSelector aspectSelector,
102: ServiceSelector adapterSelector) {
103: Iterator i = this .aspects.iterator();
104: while (i.hasNext()) {
105: final Object component = i.next();
106: if (component instanceof CopletAdapter) {
107: adapterSelector.release(component);
108: } else {
109: aspectSelector.release(component);
110: }
111: }
112: this .aspects.clear();
113: this .configs.clear();
114: }
115:
116: /**
117: * Adds an aspect at the front. This method is only used by the deprecated PortletPortalManager.
118: * @deprecated This method will be removed in 2.2.
119: * @param firstAspect
120: * @param firstAspectParameters
121: */
122: public void addAsFirst(PortalManagerAspect firstAspect,
123: Parameters firstAspectParameters) {
124: this .aspects.add(0, firstAspect);
125: this .configs.add(0, firstAspectParameters);
126: }
127: }
|