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.components.modules.input;
018:
019: import java.util.Iterator;
020: import java.util.Map;
021:
022: import org.apache.avalon.framework.activity.Disposable;
023: import org.apache.avalon.framework.configuration.Configuration;
024: import org.apache.avalon.framework.configuration.ConfigurationException;
025: import org.apache.avalon.framework.service.ServiceException;
026: import org.apache.avalon.framework.service.ServiceSelector;
027: import org.apache.cocoon.components.modules.input.InputModule;
028: import org.apache.cocoon.portal.PortalService;
029: import org.apache.cocoon.portal.layout.Layout;
030: import org.apache.cocoon.portal.layout.SkinDescription;
031:
032: /**
033: * This input module provides information about the current selected skin
034: *
035: * @version CVS $Id: SkinModule.java 433543 2006-08-22 06:22:54Z crossley $
036: */
037: public class SkinModule extends AbstractModule implements Disposable {
038:
039: protected InputModule globalModule;
040: protected ServiceSelector moduleSelector;
041:
042: /* (non-Javadoc)
043: * @see org.apache.avalon.framework.activity.Disposable#dispose()
044: */
045: public void dispose() {
046: if (this .manager != null) {
047: if (this .moduleSelector != null) {
048: this .moduleSelector.release(this .globalModule);
049: this .manager.release(this .moduleSelector);
050: this .moduleSelector = null;
051: this .globalModule = null;
052: }
053: }
054: }
055:
056: /* (non-Javadoc)
057: * @see org.apache.cocoon.components.modules.input.InputModule#getAttribute(java.lang.String, org.apache.avalon.framework.configuration.Configuration, java.util.Map)
058: */
059: public Object getAttribute(String name, Configuration modeConf,
060: Map objectModel) throws ConfigurationException {
061: // lazy init
062: if (this .moduleSelector == null) {
063: synchronized (this ) {
064: try {
065: if (this .moduleSelector == null) {
066: this .moduleSelector = (ServiceSelector) this .manager
067: .lookup(InputModule.ROLE + "Selector");
068: this .globalModule = (InputModule) this .moduleSelector
069: .select("global");
070: }
071: } catch (ServiceException e) {
072: throw new ConfigurationException(
073: "Unable to lookup input module.", e);
074: }
075: }
076: }
077:
078: PortalService portalService = null;
079: try {
080:
081: portalService = (PortalService) this .manager
082: .lookup(PortalService.ROLE);
083:
084: String skinName = null;
085: // get the current skin
086: // the skin is stored as a parameter on the root layout
087: // if not, the global module is used
088: // fallback is: common
089: final Layout rootLayout = portalService
090: .getComponentManager().getProfileManager()
091: .getPortalLayout(null, null);
092: if (rootLayout != null) {
093: skinName = (String) rootLayout.getParameters().get(
094: "skin");
095: }
096: // use the global module
097: if (skinName == null) {
098: skinName = (String) this .globalModule.getAttribute(
099: "skin", modeConf, objectModel);
100: if (skinName == null) {
101: skinName = "common";
102: }
103: }
104:
105: // find the correct skin
106: SkinDescription desc = null;
107: final Iterator i = portalService.getSkinDescriptions()
108: .iterator();
109: while (i.hasNext() && desc == null) {
110: final SkinDescription current = (SkinDescription) i
111: .next();
112: if (current.getName().equals(skinName)) {
113: desc = current;
114: }
115: }
116: if (desc != null) {
117: if ("skin".equals(name)) {
118: return skinName;
119: } else if ("skin.basepath".equals(name)) {
120: return desc.getBasePath();
121: } else if ("skin.thumbnailpath".equals(name)) {
122: return desc.getThumbnailPath();
123: } else if (name.startsWith("skin.thumbnailuri.")) {
124: String selectedSkinName = name.substring(name
125: .lastIndexOf(".") + 1, name.length());
126: for (Iterator it = portalService
127: .getSkinDescriptions().iterator(); it
128: .hasNext();) {
129: SkinDescription selected = (SkinDescription) it
130: .next();
131: if (selected.getName().equals(selectedSkinName)) {
132: return selected.getBasePath() + "/"
133: + selected.getThumbnailPath();
134: }
135: }
136: }
137: }
138: return null;
139: } catch (ServiceException e) {
140: throw new ConfigurationException(
141: "Unable to lookup portal service.", e);
142: } finally {
143: this.manager.release(portalService);
144: }
145: }
146:
147: }
|