KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > components > modules > input > SkinModule


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.components.modules.input;
17
18 import java.util.Iterator JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import org.apache.avalon.framework.activity.Disposable;
22 import org.apache.avalon.framework.configuration.Configuration;
23 import org.apache.avalon.framework.configuration.ConfigurationException;
24 import org.apache.avalon.framework.service.ServiceException;
25 import org.apache.avalon.framework.service.ServiceSelector;
26 import org.apache.cocoon.components.modules.input.InputModule;
27 import org.apache.cocoon.portal.PortalService;
28 import org.apache.cocoon.portal.layout.Layout;
29 import org.apache.cocoon.portal.layout.SkinDescription;
30
31 /**
32  * This input module provides information about the current selected skin
33  *
34  * @version CVS $Id: SkinModule.java 148910 2005-01-28 18:19:44Z cziegeler $
35  */

36 public class SkinModule
37 extends AbstractModule
38 implements Disposable {
39     
40     protected InputModule globalModule;
41     protected ServiceSelector moduleSelector;
42     
43     /* (non-Javadoc)
44      * @see org.apache.avalon.framework.activity.Disposable#dispose()
45      */

46     public void dispose() {
47         if ( this.manager != null ) {
48             if ( this.moduleSelector != null ) {
49                 this.moduleSelector.release(this.globalModule);
50                 this.manager.release(this.moduleSelector);
51                 this.moduleSelector = null;
52                 this.globalModule = null;
53             }
54         }
55     }
56     
57     /* (non-Javadoc)
58      * @see org.apache.cocoon.components.modules.input.InputModule#getAttribute(java.lang.String, org.apache.avalon.framework.configuration.Configuration, java.util.Map)
59      */

60     public Object JavaDoc getAttribute(String JavaDoc name, Configuration modeConf, Map JavaDoc objectModel)
61     throws ConfigurationException {
62         // lazy init
63
if ( this.moduleSelector == null ) {
64             synchronized ( this ) {
65                 try {
66                     if ( this.moduleSelector == null ) {
67                         this.moduleSelector = (ServiceSelector)this.manager.lookup(InputModule.ROLE+"Selector");
68                         this.globalModule = (InputModule)this.moduleSelector.select("global");
69                     }
70                 } catch (ServiceException e) {
71                     throw new ConfigurationException("Unable to lookup input module.", e);
72                 }
73             }
74         }
75             
76         PortalService portalService = null;
77         try {
78
79             portalService = (PortalService)this.manager.lookup(PortalService.ROLE);
80
81             String JavaDoc skinName = null;
82             // get the current skin
83
// the skin is stored as a parameter on the root layout
84
// if not, the global module is used
85
// fallback is: common
86
final Layout rootLayout = portalService.getComponentManager().getProfileManager().getPortalLayout(null, null);
87             if ( rootLayout != null ) {
88                 skinName = (String JavaDoc)rootLayout.getParameters().get("skin");
89             }
90             // use the global module
91
if ( skinName == null ) {
92                 skinName = (String JavaDoc)this.globalModule.getAttribute("skin", modeConf, objectModel);
93                 if ( skinName == null ) {
94                     skinName = "common";
95                 }
96             }
97             
98             // find the correct skin
99
SkinDescription desc = null;
100             final Iterator JavaDoc i = portalService.getSkinDescriptions().iterator();
101             while ( i.hasNext() && desc == null ) {
102                 final SkinDescription current = (SkinDescription)i.next();
103                 if ( current.getName().equals(skinName) ) {
104                     desc = current;
105                 }
106             }
107             if ( desc != null ) {
108                 if ( "skin".equals(name) ) {
109                     return skinName;
110                 } else if ( "skin.basepath".equals(name) ) {
111                     return desc.getBasePath();
112                 } else if ( "skin.thumbnailpath".equals(name) ) {
113                     return desc.getThumbnailPath();
114                 } else if ( name.startsWith("skin.thumbnailuri.") ) {
115                     String JavaDoc selectedSkinName = name.substring(name.lastIndexOf(".")+ 1, name.length());
116                     for(Iterator JavaDoc it = portalService.getSkinDescriptions().iterator(); it.hasNext();) {
117                         SkinDescription selected = (SkinDescription) it.next();
118                         if(selected.getName().equals(selectedSkinName)) {
119                             return selected.getBasePath() + "/" + selected.getThumbnailPath();
120                         }
121                     }
122                 }
123             }
124             return null;
125         } catch (ServiceException e) {
126             throw new ConfigurationException("Unable to lookup portal service.", e);
127         } finally {
128             this.manager.release(portalService);
129         }
130     }
131
132 }
133
Popular Tags