View Javadoc

1   /*
2    * Copyright 2003,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   */
19  
20  package org.apache.pluto.portalImpl.om.servlet.impl;
21  
22  import java.util.Collection;
23  import java.util.HashMap;
24  import java.util.Iterator;
25  import java.util.Vector;
26  
27  import org.apache.pluto.om.servlet.ServletDefinition;
28  import org.apache.pluto.om.servlet.ServletDefinitionList;
29  import org.apache.pluto.om.servlet.ServletDefinitionListCtrl;
30  import org.apache.pluto.om.servlet.WebApplicationDefinition;
31  import org.apache.pluto.portalImpl.om.common.AbstractSupportSet;
32  import org.apache.pluto.portalImpl.om.common.Support;
33  import org.apache.pluto.portalImpl.xml.XmlException;
34  import org.apache.pluto.util.StringUtils;
35  
36  public class ServletDefinitionListImpl extends AbstractSupportSet
37  implements ServletDefinitionList, ServletDefinitionListCtrl, java.io.Serializable, Support {
38  
39      // ServletDefinitionList implementation.
40  
41      public ServletDefinition get(String name)
42      {
43          Iterator iterator = this.iterator();
44          while (iterator.hasNext()) {
45              ServletDefinition servletDefinition = (ServletDefinition)iterator.next();
46              if (servletDefinition.getServletName().equals(name)) {
47                  return servletDefinition;
48              }
49          }
50          return null;
51      }
52  
53      // ServletDefinitionListCtrl implementation.
54  
55      public ServletDefinition add(String name, String className)
56      {
57          ServletDefinitionImpl servletDefinition = new ServletDefinitionImpl();
58          servletDefinition.setServletName(name);
59          servletDefinition.setServletClass(className);
60  
61          super.add(servletDefinition);
62  
63          return servletDefinition;
64      }
65  
66      public ServletDefinition remove(String name)
67      {
68          Iterator iterator = this.iterator();
69          while (iterator.hasNext()) {
70              ServletDefinition servletDefinition = (ServletDefinition)iterator.next();
71              if (servletDefinition.getServletName().equals(name)) {
72                  super.remove(servletDefinition);
73                  return servletDefinition;
74              }
75          }
76          return null;
77      }
78  
79      public void remove(ServletDefinition servletDefinition)
80      {
81          super.remove(servletDefinition);
82      }
83  
84      // Support implementation.
85  
86      public void preBuild(Object parameter) throws Exception
87      {
88          Vector structure = (Vector)parameter;
89          WebApplicationDefinition webApplicationDefinition =  (WebApplicationDefinition)structure.get(0);
90          Collection servletMappings = (Collection)structure.get(1);
91          HashMap servletMap = (HashMap)structure.get(2);
92  
93          // build internal hashtable to cross link mappings with servlets
94          HashMap mappings = new HashMap(servletMappings.size());
95          Iterator iterator = servletMappings.iterator();
96          while (iterator.hasNext()) {
97              ServletMappingImpl servletMapping = (ServletMappingImpl)iterator.next();
98              mappings.put(servletMapping.getServletName(),servletMapping);
99          }
100         // update servlets
101         iterator = this.iterator();
102         while (iterator.hasNext()) {
103             ServletDefinition servlet = (ServletDefinition)iterator.next();
104             ((Support)servlet).preBuild(webApplicationDefinition);
105 
106             if (servlet.getInitParameterSet() != null) {
107                 if (servlet.getInitParameterSet().get("portlet-guid") != null) {
108                     String guid = servlet.getInitParameterSet().get("portlet-guid").getValue();
109                     servletMap.put(guid, servlet);
110 
111                     ServletMappingImpl servletMapping = (ServletMappingImpl)mappings.get(servlet.getServletName());
112                     if (mappings==null) {
113                         throw new XmlException("No corresponding servlet mapping found for servlet name '"+servlet.getServletName()+"'");
114                     }
115                     ((Support)servlet).postBuild(servletMapping);
116 
117                 }
118             }
119         }
120 
121     }
122 
123     /* (non-Javadoc)
124      * @see org.apache.pluto.portalImpl.om.common.Support#postBuild(Object)
125      */
126     public void postBuild(Object parameter) throws Exception {
127     }
128 
129     /* (non-Javadoc)
130      * @see org.apache.pluto.portalImpl.om.common.Support#postLoad(Object)
131      */
132     public void postLoad(Object parameter) throws Exception {
133         Iterator iterator = this.iterator();
134         while (iterator.hasNext()) {
135             ((ServletDefinitionImpl)iterator.next()).postLoad(parameter);
136         }
137 
138     }
139 
140     /* (non-Javadoc)
141      * @see org.apache.pluto.portalImpl.om.common.Support#postStore(Object)
142      */
143     public void postStore(Object parameter) throws Exception {
144     }
145 
146     /* (non-Javadoc)
147      * @see org.apache.pluto.portalImpl.om.common.Support#preStore(Object)
148      */
149     public void preStore(Object parameter) throws Exception {
150     }
151     
152     // additional methods.
153 
154     public String toString()
155     {
156         return toString(0);
157     }
158 
159     public String toString(int indent)
160     {
161         StringBuffer buffer = new StringBuffer(50);
162         StringUtils.newLine(buffer,indent);
163         buffer.append(getClass().toString());
164         buffer.append(": ");
165         Iterator iterator = this.iterator();
166         while (iterator.hasNext()) {
167             buffer.append(((ServletDefinitionImpl)iterator.next()).toString(indent+2));
168         }
169         return buffer.toString();
170     }
171 }