KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > impl > PortalManagerAspectChain


1 /*
2  * Copyright 2005 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 package org.apache.cocoon.portal.impl;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21
22 import org.apache.avalon.framework.configuration.Configuration;
23 import org.apache.avalon.framework.configuration.ConfigurationException;
24 import org.apache.avalon.framework.parameters.Parameters;
25 import org.apache.avalon.framework.service.ServiceException;
26 import org.apache.avalon.framework.service.ServiceSelector;
27 import org.apache.cocoon.portal.PortalManagerAspect;
28 import org.apache.cocoon.portal.coplet.adapter.CopletAdapter;
29
30 /**
31  * This chain holds all configured aspects for a portal manager.
32  * @since 2.1.8
33  * @version SVN $Id: PortalManagerAspectChain.java 226633 2005-07-31 11:45:21Z cziegeler $
34  */

35 public final class PortalManagerAspectChain {
36     
37     protected List JavaDoc aspects = new ArrayList JavaDoc(3);
38     
39     protected List JavaDoc configs = new ArrayList JavaDoc(3);
40     
41     public void configure(ServiceSelector aspectSelector,
42                           ServiceSelector adapterSelector,
43                           Configuration conf,
44                           PortalManagerAspect endAspect,
45                           Parameters endAspectParameters)
46     throws ConfigurationException {
47         if ( conf != null ) {
48             Configuration[] aspects = conf.getChildren("aspect");
49             for(int i=0; i < aspects.length; i++) {
50                 final Configuration current = aspects[i];
51                 final String JavaDoc role = current.getAttribute("type", null);
52                 PortalManagerAspect pAspect;
53                 if ( role != null ) {
54                     if ( aspectSelector == null ) {
55                     throw new ConfigurationException("No selector for aspects defined.");
56                 }
57                 try {
58                         pAspect = (PortalManagerAspect) aspectSelector.select(role);
59                     } catch (ServiceException se) {
60                         throw new ConfigurationException("Unable to lookup aspect " + role, current, se);
61                     }
62                 } else {
63                     final String JavaDoc adapterName = current.getAttribute("adapter", null);
64                     if ( adapterName == null ) {
65                         throw new ConfigurationException("Aspect configuration requires either a type or an adapter attribute.", current);
66                     }
67                     try {
68                         pAspect = (PortalManagerAspect)adapterSelector.select(adapterName);
69                     } catch (ServiceException se) {
70                         throw new ConfigurationException("Unable to lookup coplet adapter " + adapterName, current, se);
71                     }
72                 }
73                 this.aspects.add(pAspect);
74                 Parameters aspectConfiguration = Parameters.fromConfiguration(current);
75                 this.configs.add(aspectConfiguration);
76             }
77         }
78         this.aspects.add(endAspect);
79         this.configs.add(endAspectParameters);
80     }
81     
82     public Iterator JavaDoc getIterator() {
83         return this.aspects.iterator();
84     }
85     
86     public Iterator JavaDoc getConfigIterator() {
87         return this.configs.iterator();
88     }
89     
90     public void dispose(ServiceSelector aspectSelector, ServiceSelector adapterSelector) {
91         Iterator JavaDoc i = this.aspects.iterator();
92         while (i.hasNext()) {
93             final Object JavaDoc component = i.next();
94             if ( component instanceof CopletAdapter ) {
95                 adapterSelector.release(component);
96             } else {
97                 aspectSelector.release(i.next());
98             }
99         }
100         this.aspects.clear();
101         this.configs.clear();
102     }
103
104     /**
105      * Adds an aspect at the front. This method is only used by the deprecated PortletPortalManager.
106      * @deprecated This method will be removed in 2.2.
107      * @param firstAspect
108      * @param firstAspectParameters
109      */

110     public void addAsFirst(PortalManagerAspect firstAspect, Parameters firstAspectParameters) {
111         this.aspects.add(0, firstAspect);
112         this.configs.add(0, firstAspectParameters);
113     }
114 }
115
Popular Tags