KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ditchnet > jsp > taglib > tabs > listener > TabServletContextListener


1 /*
2  * The contents of this file are subject to the GNU Lesser General Public
3  * License Version 2.1 (the "License"); you may not use this file
4  * except in compliance with the License. You may obtain a copy of
5  * the License at http://www.gnu.org/copyleft/lesser.html
6  *
7  * Software distributed under the License is distributed on an "AS
8  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9  * implied. See the License for the specific language governing
10  * rights and limitations under the License.
11  *
12  * Developer:
13  * Todd Ditchendorf, todd@ditchnet.org
14  *
15  */

16
17 /**
18  * @author Todd Ditchendorf
19  * @version 0.8
20  * @since 0.8
21  */

22 package org.ditchnet.jsp.taglib.tabs.listener;
23
24 import java.util.List JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Arrays JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.io.File JavaDoc;
29 import java.io.Reader JavaDoc;
30 import java.io.InputStream JavaDoc;
31 import java.io.BufferedInputStream JavaDoc;
32 import java.io.OutputStream JavaDoc;
33 import java.io.BufferedOutputStream JavaDoc;
34 import java.io.FileOutputStream JavaDoc;
35 import java.net.URL JavaDoc;
36 import javax.servlet.ServletContext JavaDoc;
37 import javax.servlet.ServletContextEvent JavaDoc;
38 import javax.servlet.ServletContextListener JavaDoc;
39
40 /**
41  * @author Todd Ditchendorf
42  * @since 0.8
43  *
44  * <p><code>ServletContextListener</code> that handles extracting CSS,
45  * JavaScript, and image resource files from this taglib's JAR file, and
46  * places them in a directory called <code>org.ditchnet.taglib/</code> in this
47  * web app's root directory. Why do this? The HTML files in which the tabs are
48  * used must have access to the CSS, JavaScript and image files that render
49  * the tabs correctly. Therefore, they must be placed in a known location
50  * outside of the <code>WEB-INF</code> directory so that client browsers can
51  * access them.</p>
52  * <p>By implementing this feature as as <code>ServletContextListener</code>,
53  * These resources are deployed immediately on reloading your web app, and
54  * are therefore available before the first request!
55  */

56 public class TabServletContextListener implements ServletContextListener JavaDoc {
57     
58     private static final String JavaDoc DEST_FOLDER_NAME = "/org.ditchnet.taglib/";
59     public static final String JavaDoc SCRIPT_URI = DEST_FOLDER_NAME + "tabs.js";
60     public static final String JavaDoc STYLE_URI = DEST_FOLDER_NAME + "tabstyle.jsp";
61     
62     private static final List JavaDoc DEST_PATHS = Arrays.asList(
63             new String JavaDoc [] { SCRIPT_URI, STYLE_URI });
64     
65     private static final List JavaDoc RESOURCE_PATHS = Arrays.asList(
66         new String JavaDoc [] {"/lib/js/tabs.js", "/lib/style/tabs.css" });
67
68     private static final String JavaDoc IMAGE_RESOURCE_FOLDER = "/lib/images/";
69     
70     private static final List JavaDoc IMAGE_RESOURCE_NAMES = Arrays.asList(
71             new String JavaDoc [] {"default_tab_bg_left.gif","default_tab_bg_right.gif",
72             "default_tab_bg_white_left.gif","default_tab_bg_white_right.gif"});
73     
74     /**
75      * Handles finding all CSS, JavaScript, and image resources from this
76      * taglib's JAR file and copies them to a directory in the web app's
77      * root directory where the tab components can link to them.
78      */

79     public void contextInitialized(final ServletContextEvent JavaDoc evt) {
80         ServletContext JavaDoc servletContext = evt.getServletContext();
81         createDitchnetDir(servletContext);
82         String JavaDoc sourcePath,destPath,fileName;
83         URL JavaDoc sourceURL;
84         int i = 0;
85         for (Iterator JavaDoc iter = RESOURCE_PATHS.iterator(); iter.hasNext();) {
86             sourcePath = (String JavaDoc)iter.next();
87             sourceURL = getClass().getResource(sourcePath);
88             destPath = (String JavaDoc)DEST_PATHS.get(i);
89             destPath = servletContext.getRealPath(destPath);
90             writeFile(sourceURL,destPath,servletContext);
91             i++;
92         }
93         for (Iterator JavaDoc iter = IMAGE_RESOURCE_NAMES.iterator(); iter.hasNext();) {
94             fileName = (String JavaDoc)iter.next();
95             sourcePath = IMAGE_RESOURCE_FOLDER + fileName;
96             sourceURL = getClass().getResource(sourcePath);
97             destPath = DEST_FOLDER_NAME + fileName;
98             destPath = servletContext.getRealPath(destPath);
99             writeFile(sourceURL,destPath,servletContext);
100         }
101     }
102     
103     public void contextDestroyed(final ServletContextEvent JavaDoc evt) { }
104     
105     private void createDitchnetDir(final ServletContext JavaDoc servletContext) {
106         String JavaDoc dirPath = servletContext.getRealPath(DEST_FOLDER_NAME);
107         File JavaDoc dir = null;
108         try {
109             dir = new File JavaDoc(dirPath);
110             dir.mkdir();
111         } catch (Exception JavaDoc e) {
112             //log.error("Error creating Ditchnet dir");
113
}
114     }
115     
116     private void writeFile(final URL JavaDoc fromURL,
117                            final String JavaDoc toPath,
118                            final ServletContext JavaDoc servletContext) {
119         InputStream JavaDoc in = null;
120         OutputStream JavaDoc out = null;
121         try {
122             in = new BufferedInputStream JavaDoc(fromURL.openStream());
123             out = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(toPath));
124             int len;
125             byte [] buffer = new byte [4096];
126             while ((len = in.read(buffer,0,buffer.length)) != -1) {
127                 out.write(buffer,0,len);
128             }
129             out.flush();
130         } catch (Exception JavaDoc e) {
131             //log.error("Error writing file dude: " + e.getMessage());
132
} finally {
133             try { in.close(); out.close(); } catch (Exception JavaDoc e) { }
134         }
135     }
136     
137 }
138
Popular Tags