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.jetspeed.util.descriptor;
018:
019: import java.io.Reader;
020: import java.util.ArrayList;
021: import java.util.Collection;
022: import java.util.Iterator;
023:
024: import org.apache.commons.digester.Digester;
025: import org.apache.commons.digester.Rule;
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028: import org.apache.jetspeed.om.common.portlet.CustomPortletMode;
029: import org.apache.jetspeed.om.common.portlet.CustomWindowState;
030: import org.apache.jetspeed.om.common.portlet.MutablePortletApplication;
031: import org.apache.jetspeed.om.portlet.impl.CustomPortletModeImpl;
032: import org.apache.jetspeed.om.portlet.impl.CustomWindowStateImpl;
033: import org.apache.jetspeed.om.portlet.impl.PortletApplicationDefinitionImpl;
034: import org.apache.jetspeed.tools.pamanager.rules.JetspeedServicesRuleSet;
035: import org.apache.jetspeed.tools.pamanager.rules.MetadataRuleSet;
036: import org.apache.jetspeed.tools.pamanager.rules.PortletRule;
037: import org.apache.jetspeed.tools.pamanager.rules.SecurityConstraintRefRule;
038: import org.apache.jetspeed.tools.pamanager.rules.UserAttributeRefRuleSet;
039: import org.xml.sax.Attributes;
040:
041: /**
042: * This class is used to load extended MetaData, like that of the Dublin Core,
043: * into an exsting PortletApplicationDefinition's object graph.
044: *
045: * @author <a href="mailto:jford@apache.org">Jeremy Ford </a>
046: * @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>
047: * @version $Id: JetspeedDescriptorUtilities.java,v 1.10 2004/06/08 01:35:01
048: * dlestrat Exp $
049: */
050: public class ExtendedPortletMetadata {
051: private static class CollectionRule extends Rule {
052: private Collection collection;
053:
054: public CollectionRule(Collection collection) {
055: this .collection = collection;
056: }
057:
058: public void begin(String arg0, String arg1, Attributes arg2)
059: throws Exception {
060: digester.push(collection);
061: }
062:
063: public void end(String arg0, String arg1) throws Exception {
064: digester.pop();
065: }
066: }
067:
068: protected final static Log log = LogFactory
069: .getLog(ExtendedPortletMetadata.class);
070:
071: protected Reader extendedMetaData;
072: protected MutablePortletApplication portletApp;
073:
074: /**
075: *
076: * @param extendedMetaData Reader that contains the extended metadata, usually jetspeed-portlet.xml
077: * @param portletApp the MutablePortletApplication we are adding the extended metadata to.
078: */
079: public ExtendedPortletMetadata(Reader extendedMetaData,
080: MutablePortletApplication portletApp) {
081: this .extendedMetaData = extendedMetaData;
082: this .portletApp = portletApp;
083: }
084:
085: /**
086: * Performs the actual loading and mapping of the metadata into the PortletApplicationDefinition.
087: *
088: */
089: public void load() throws MetaDataException {
090: try {
091: Digester digester = new Digester();
092: digester.setClassLoader(this .getClass().getClassLoader());
093: digester.setValidating(false);
094: digester.setNamespaceAware(true);
095: digester.push(portletApp);
096:
097: digester.addRuleSet(new MetadataRuleSet("portlet-app/"));
098: digester
099: .addRuleSet(new JetspeedServicesRuleSet(portletApp));
100: digester.addRule("portlet-app/security-constraint-ref",
101: new SecurityConstraintRefRule(portletApp));
102:
103: digester.addRule("portlet-app/portlet/portlet-name",
104: new PortletRule(portletApp));
105: digester.addRuleSet(new MetadataRuleSet(
106: "portlet-app/portlet/"));
107:
108: digester.addRule(
109: "portlet-app/portlet/security-constraint-ref",
110: new SecurityConstraintRefRule(portletApp));
111:
112: digester
113: .addRuleSet(new UserAttributeRefRuleSet(portletApp));
114:
115: ArrayList mappedPortletModes = new ArrayList();
116: digester.addRule("portlet-app/custom-portlet-mode",
117: new CollectionRule(mappedPortletModes));
118: digester.addObjectCreate("portlet-app/custom-portlet-mode",
119: CustomPortletModeImpl.class);
120:
121: digester.addBeanPropertySetter(
122: "portlet-app/custom-portlet-mode/name",
123: "customName");
124: digester.addBeanPropertySetter(
125: "portlet-app/custom-portlet-mode/mapped-name",
126: "mappedName");
127: digester.addSetNext("portlet-app/custom-portlet-mode",
128: "add");
129:
130: ArrayList mappedWindowStates = new ArrayList();
131: digester.addRule("portlet-app/custom-window-state",
132: new CollectionRule(mappedWindowStates));
133: digester.addObjectCreate("portlet-app/custom-window-state",
134: CustomWindowStateImpl.class);
135:
136: digester.addBeanPropertySetter(
137: "portlet-app/custom-window-state/name",
138: "customName");
139: digester.addBeanPropertySetter(
140: "portlet-app/custom-window-state/mapped-name",
141: "mappedName");
142: digester.addSetNext("portlet-app/custom-window-state",
143: "add");
144:
145: digester.parse(extendedMetaData);
146:
147: if (mappedPortletModes.size() > 0) {
148: PortletApplicationDefinitionImpl pa = (PortletApplicationDefinitionImpl) portletApp;
149: ArrayList customModes = new ArrayList(pa
150: .getCustomPortletModes());
151: Iterator mappedModesIter = mappedPortletModes
152: .iterator();
153: while (mappedModesIter.hasNext()) {
154: CustomPortletModeImpl mappedMode = (CustomPortletModeImpl) mappedModesIter
155: .next();
156: if (!mappedMode.getMappedMode().equals(
157: mappedMode.getCustomMode())) {
158: int index = customModes.indexOf(mappedMode);
159: if (index > -1) {
160: CustomPortletMode customMode = (CustomPortletMode) customModes
161: .get(index);
162: mappedMode.setDescription(customMode
163: .getDescription());
164: customModes.set(index, mappedMode);
165: }
166: }
167: }
168: pa.setCustomPortletModes(customModes);
169: }
170: if (mappedWindowStates.size() > 0) {
171: PortletApplicationDefinitionImpl pa = (PortletApplicationDefinitionImpl) portletApp;
172: ArrayList customStates = new ArrayList(pa
173: .getCustomWindowStates());
174: Iterator mappedStatesIter = mappedWindowStates
175: .iterator();
176: while (mappedStatesIter.hasNext()) {
177: CustomWindowStateImpl mappedState = (CustomWindowStateImpl) mappedStatesIter
178: .next();
179: if (!mappedState.getMappedState().equals(
180: mappedState.getCustomState())) {
181: int index = customStates.indexOf(mappedState);
182: if (index > -1) {
183: CustomWindowState customState = (CustomWindowState) customStates
184: .get(index);
185: mappedState.setDescription(customState
186: .getDescription());
187: customStates.set(index, mappedState);
188: }
189: }
190: }
191: pa.setCustomWindowStates(customStates);
192: }
193: } catch (Throwable t) {
194: throw new MetaDataException(
195: "Unable to marshall extended metadata. "
196: + t.toString(), t);
197: }
198: }
199: }
|