KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > core > theme > deployment > jboss > ThemeDeployment


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.core.theme.deployment.jboss;
10
11 import org.apache.log4j.Logger;
12 import org.apache.xpath.CachedXPathAPI;
13 import org.jboss.deployment.DeploymentException;
14 import org.jboss.portal.common.util.XML;
15 import org.jboss.portal.core.theme.impl.PortalThemeImpl;
16 import org.jboss.portal.core.theme.impl.ThemeLinkImpl;
17 import org.jboss.portal.core.theme.impl.ThemeScriptImpl;
18 import org.jboss.portal.server.ServerManager;
19 import org.jboss.portal.server.ServerRegistrationID;
20 import org.jboss.portal.server.ThemeServer;
21 import org.jboss.portal.server.deployment.PortalWebApp;
22 import org.jboss.portal.server.deployment.jboss.Deployment;
23 import org.jboss.portal.server.metadata.ThemeRegistrationMetaData;
24 import org.jboss.portal.server.theme.PortalTheme;
25 import org.jboss.portal.server.theme.ThemeException;
26 import org.jboss.portal.server.theme.ThemeLink;
27 import org.jboss.portal.server.theme.ThemeScript;
28 import org.w3c.dom.Document JavaDoc;
29 import org.w3c.dom.Node JavaDoc;
30 import org.w3c.dom.NodeList JavaDoc;
31 import org.xml.sax.SAXException JavaDoc;
32
33 import javax.servlet.ServletContext JavaDoc;
34 import javax.xml.parsers.ParserConfigurationException JavaDoc;
35 import javax.xml.transform.TransformerException JavaDoc;
36 import java.io.IOException JavaDoc;
37 import java.io.InputStream JavaDoc;
38 import java.net.URL JavaDoc;
39
40 /**
41  * Deploy the themes contained in a WebApplication.
42  * <p>This deployer scans the archive for /WEB-INF/portal-themes.xml. This file contains the
43  * definition of one of more themes. A theme is a, or a set of, css, and the resources that go with it.</p>
44  *
45  * @author <a HREF="mailto:mholzner@novell.com">Martin Holzner</a>
46  * @version $Revision: 1.5 $
47  */

48 public class ThemeDeployment extends Deployment
49 {
50    private static Logger log = Logger.getLogger(ThemeDeployment.class);
51    private final ThemeServer themeServer;
52
53    /**
54     * Create the deployer.
55     *
56     * @param serverManager
57     * @param url
58     * @param pwa
59     * @param appName
60     */

61    public ThemeDeployment(ServerManager serverManager, URL JavaDoc url, PortalWebApp pwa, String JavaDoc appName)
62    {
63       super(serverManager, url, pwa, appName);
64       themeServer = serverManager.getThemeServer();
65       log.info("new theme deployer for : " + appName);
66    }
67
68    /**
69     * Create all the theme resources that are container in this portal web application.
70     *
71     * @throws DeploymentException if anything goes wrong
72     */

73    public void create() throws DeploymentException
74    {
75       log.info("create new theme(s), found in : " + appName);
76       try
77       {
78          InputStream JavaDoc themeStream = pwa.getServletContext().getResourceAsStream("/WEB-INF/portal-themes.xml");
79          Document JavaDoc themes = XML.createDocument(themeStream);
80          CachedXPathAPI xpath = new CachedXPathAPI();
81          NodeList JavaDoc nl = xpath.selectNodeList(themes, "/themes/theme");
82          for (int i = 0; i < nl.getLength(); i++)
83          {
84             String JavaDoc name = xpath.selectSingleNode(nl.item(i), "./name/text()").getNodeValue();
85             PortalTheme theme = createTheme(pwa.getServletContext(), pwa.getContextPath(), appName, name);
86             // get the link and script tags and their attributes
87
addThemeLinks(theme, xpath, nl.item(i));
88             addThemeScripts(theme, xpath, nl.item(i));
89
90             themeServer.addTheme((ThemeRegistrationMetaData)theme);
91          }
92          log.info("done creating new theme(s), found in : " + appName);
93       }
94       catch (IOException JavaDoc e)
95       {
96          throw new DeploymentException(e);
97       }
98       catch (ParserConfigurationException JavaDoc e)
99       {
100          throw new DeploymentException(e);
101       }
102       catch (SAXException JavaDoc e)
103       {
104          throw new DeploymentException(e);
105       }
106       catch (TransformerException JavaDoc e)
107       {
108          throw new DeploymentException(e);
109       }
110       catch (ThemeException e)
111       {
112          throw new DeploymentException(e);
113       }
114    }
115
116    /**
117     * Destroy all the theme resources that are contained in this portal web application.
118     *
119     * @throws DeploymentException if anything goes wrong
120     */

121    public void destroy() throws DeploymentException
122    {
123       log.info("destroying theme(s), found in : " + appName);
124       try
125       {
126          themeServer.removeThemes(appName);
127          log.info("done destroying theme(s), found in : " + appName);
128       }
129       catch (ThemeException e)
130       {
131          throw new DeploymentException(e);
132       }
133    }
134
135    // Application related **********************************************************************************************
136

137    private PortalTheme createTheme(ServletContext JavaDoc servletContext, String JavaDoc contextPath, String JavaDoc appName, String JavaDoc name)
138    {
139       log.debug("create theme for context " + servletContext.getServletContextName() + " and name " + name);
140       if (servletContext == null || name == null)
141       {
142          throw new NullPointerException JavaDoc("ServletContext[" + servletContext + "] name[" + name + "]");
143       }
144       ServerRegistrationID id = ServerRegistrationID.createID(ServerRegistrationID.TYPE_THEME, new String JavaDoc[]{appName, name});
145       return new PortalThemeImpl(id, servletContext, contextPath, appName, name);
146    }
147
148    private void addThemeScripts(PortalTheme theme, CachedXPathAPI xpath, Node JavaDoc item) throws TransformerException JavaDoc
149    {
150       NodeList JavaDoc scripts = xpath.selectNodeList(item, "./script");
151       if (scripts.getLength() > 0)
152       {
153          for (int s = 0; s < scripts.getLength(); s++)
154          {
155             String JavaDoc src = getNodeValue(xpath, scripts.item(s), "@src");
156             String JavaDoc type = getNodeValue(xpath, scripts.item(s), "@type");
157             createScript(theme, src, type);
158          }
159       }
160    }
161
162    private void addThemeLinks(PortalTheme theme, CachedXPathAPI xpath, Node JavaDoc item) throws TransformerException JavaDoc
163    {
164       NodeList JavaDoc links = xpath.selectNodeList(item, "./link");
165       if (links.getLength() > 0)
166       {
167          for (int l = 0; l < links.getLength(); l++)
168          {
169             String JavaDoc href = getNodeValue(xpath, links.item(l), "@href");
170             String JavaDoc type = getNodeValue(xpath, links.item(l), "@type");
171             String JavaDoc title = getNodeValue(xpath, links.item(l), "@title");
172             String JavaDoc media = getNodeValue(xpath, links.item(l), "@media");
173             String JavaDoc rel = getNodeValue(xpath, links.item(l), "@rel");
174             createLink(theme, rel, type, href, title, media);
175          }
176       }
177    }
178
179    private static ThemeLink createLink(PortalTheme theme, String JavaDoc rel, String JavaDoc type, String JavaDoc href, String JavaDoc title, String JavaDoc media)
180    {
181       log.debug("create link for theme " + theme.getName());
182       ThemeLink link = new ThemeLinkImpl(theme, rel, type, href, title, media);
183       theme.addLink(link);
184       return link;
185    }
186
187    /**
188     * Create a theme script element. A script is an element based on a script tag that will later on be rendered into
189     * the theme as a <script SRC=..../> element
190     *
191     * @param theme the theme this script belongs to
192     * @param src the src attribute's value
193     * @param type the type attribute's value
194     * @return the create script
195     */

196    public static ThemeScript createScript(PortalTheme theme, String JavaDoc src, String JavaDoc type)
197    {
198       log.debug("create script for theme " + theme.getName());
199       ThemeScript script = new ThemeScriptImpl(theme, src, type);
200       theme.addScript(script);
201       return script;
202    }
203
204    private static String JavaDoc getNodeValue(CachedXPathAPI xpath, Node JavaDoc item, String JavaDoc exp) throws TransformerException JavaDoc
205    {
206       Node JavaDoc node = xpath.selectSingleNode(item, exp);
207       if (node != null)
208       {
209          return node.getNodeValue();
210       }
211       return null;
212    }
213 }
214
Popular Tags