View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.util.descriptor;
18  
19  import java.io.Reader;
20  import java.util.ArrayList;
21  import java.util.Collection;
22  import java.util.Iterator;
23  
24  import org.apache.commons.digester.Digester;
25  import org.apache.commons.digester.Rule;
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.jetspeed.om.common.portlet.CustomPortletMode;
29  import org.apache.jetspeed.om.common.portlet.CustomWindowState;
30  import org.apache.jetspeed.om.common.portlet.MutablePortletApplication;
31  import org.apache.jetspeed.om.portlet.impl.CustomPortletModeImpl;
32  import org.apache.jetspeed.om.portlet.impl.CustomWindowStateImpl;
33  import org.apache.jetspeed.om.portlet.impl.PortletApplicationDefinitionImpl;
34  import org.apache.jetspeed.tools.pamanager.rules.JetspeedServicesRuleSet;
35  import org.apache.jetspeed.tools.pamanager.rules.MetadataRuleSet;
36  import org.apache.jetspeed.tools.pamanager.rules.PortletRule;
37  import org.apache.jetspeed.tools.pamanager.rules.SecurityConstraintRefRule;
38  import org.apache.jetspeed.tools.pamanager.rules.UserAttributeRefRuleSet;
39  import org.xml.sax.Attributes;
40  
41  /***
42   * This class is used to load extended MetaData, like that of the Dublin Core, 
43   * into an exsting PortletApplicationDefinition's object graph.
44   * 
45   * @author <a href="mailto:jford@apache.org">Jeremy Ford </a>
46   * @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>
47   * @version $Id: JetspeedDescriptorUtilities.java,v 1.10 2004/06/08 01:35:01
48   *                dlestrat Exp $
49   */
50  public class ExtendedPortletMetadata
51  {
52      private static class CollectionRule extends Rule
53      {
54          private Collection collection;
55          
56          public CollectionRule(Collection collection)
57          {
58              this.collection = collection;
59          }
60  
61          public void begin(String arg0, String arg1, Attributes arg2) throws Exception
62          {
63              digester.push(collection);
64          }
65  
66          public void end(String arg0, String arg1) throws Exception
67          {
68              digester.pop();
69          }        
70      }
71      
72      protected final static Log log = LogFactory.getLog(ExtendedPortletMetadata.class);
73  
74      protected Reader extendedMetaData;
75      protected MutablePortletApplication portletApp;
76      
77      /***
78       * 
79       * @param extendedMetaData Reader that contains the extended metadata, usually jetspeed-portlet.xml
80       * @param portletApp the MutablePortletApplication we are adding the extended metadata to.
81       */
82      public ExtendedPortletMetadata( Reader extendedMetaData, MutablePortletApplication portletApp )
83      {
84          this.extendedMetaData = extendedMetaData;
85          this.portletApp = portletApp;
86      }
87  
88      /***
89       * Performs the actual loading and mapping of the metadata into the PortletApplicationDefinition.
90       * 
91       */
92      public void load() throws MetaDataException
93      {
94          try
95          {
96              Digester digester = new Digester();
97              digester.setClassLoader(this.getClass().getClassLoader());
98              digester.setValidating(false);
99              digester.setNamespaceAware(true);
100             digester.push(portletApp);
101 
102             digester.addRuleSet(new MetadataRuleSet("portlet-app/"));
103             digester.addRuleSet(new JetspeedServicesRuleSet(portletApp));
104             digester.addRule("portlet-app/security-constraint-ref", new SecurityConstraintRefRule(portletApp));                        
105             
106             digester.addRule("portlet-app/portlet/portlet-name", new PortletRule(portletApp));
107             digester.addRuleSet(new MetadataRuleSet("portlet-app/portlet/"));
108             
109             digester.addRule("portlet-app/portlet/security-constraint-ref", new SecurityConstraintRefRule(portletApp));        
110             
111             digester.addRuleSet(new UserAttributeRefRuleSet(portletApp));
112             
113             ArrayList mappedPortletModes = new ArrayList();
114             digester.addRule("portlet-app/custom-portlet-mode",new CollectionRule(mappedPortletModes));
115             digester.addObjectCreate("portlet-app/custom-portlet-mode",CustomPortletModeImpl.class);
116             
117             digester.addBeanPropertySetter("portlet-app/custom-portlet-mode/name", "customName");
118             digester.addBeanPropertySetter("portlet-app/custom-portlet-mode/mapped-name", "mappedName");
119             digester.addSetNext("portlet-app/custom-portlet-mode", "add");
120             
121             ArrayList mappedWindowStates = new ArrayList();
122             digester.addRule("portlet-app/custom-window-state",new CollectionRule(mappedWindowStates));
123             digester.addObjectCreate("portlet-app/custom-window-state",CustomWindowStateImpl.class);
124             
125             digester.addBeanPropertySetter("portlet-app/custom-window-state/name", "customName");
126             digester.addBeanPropertySetter("portlet-app/custom-window-state/mapped-name", "mappedName");
127             digester.addSetNext("portlet-app/custom-window-state", "add");
128             
129             digester.parse(extendedMetaData);
130             
131             if (mappedPortletModes.size() > 0)
132             {
133                 PortletApplicationDefinitionImpl pa = (PortletApplicationDefinitionImpl)portletApp;
134                 ArrayList customModes = new ArrayList(pa.getCustomPortletModes());
135                 Iterator mappedModesIter = mappedPortletModes.iterator();
136                 while ( mappedModesIter.hasNext() )
137                 {
138                     CustomPortletModeImpl mappedMode = (CustomPortletModeImpl)mappedModesIter.next();
139                     if (!mappedMode.getMappedMode().equals(mappedMode.getCustomMode()))
140                     {
141                         int index = customModes.indexOf(mappedMode);
142                         if ( index > -1 )
143                         {
144                             CustomPortletMode customMode = (CustomPortletMode)customModes.get(index);
145                             mappedMode.setDescription(customMode.getDescription());
146                             customModes.set(index,mappedMode);
147                         }
148                     }
149                 }
150                 pa.setCustomPortletModes(customModes);
151             }
152             if ( mappedWindowStates.size() > 0)
153             {
154                 PortletApplicationDefinitionImpl pa = (PortletApplicationDefinitionImpl)portletApp;
155                 ArrayList customStates = new ArrayList(pa.getCustomWindowStates());
156                 Iterator mappedStatesIter = mappedWindowStates.iterator();
157                 while ( mappedStatesIter.hasNext() )
158                 {
159                     CustomWindowStateImpl mappedState = (CustomWindowStateImpl)mappedStatesIter.next();
160                     if (!mappedState.getMappedState().equals(mappedState.getCustomState()))
161                     {
162                         int index = customStates.indexOf(mappedState);
163                         if ( index > -1 )
164                         {
165                             CustomWindowState customState = (CustomWindowState)customStates.get(index);
166                             mappedState.setDescription(customState.getDescription());
167                             customStates.set(index,mappedState);
168                         }
169                     }
170                 }
171                 pa.setCustomWindowStates(customStates);
172             }
173         }
174         catch (Throwable t)
175         {
176             throw new MetaDataException("Unable to marshall extended metadata.  " + t.toString(), t);
177         }
178     }
179 }
180