001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.faces.loader;
023:
024: import org.apache.log4j.Logger;
025:
026: import javax.servlet.Servlet;
027: import javax.servlet.ServletContext;
028: import javax.servlet.ServletContextEvent;
029: import javax.servlet.ServletContextListener;
030: import javax.servlet.ServletException;
031: import javax.servlet.http.HttpServlet;
032: import java.io.File;
033: import java.net.URL;
034: import java.util.ArrayList;
035:
036: /**
037: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
038: * @version $Revision: 8784 $
039: */
040: public class FacesLoaderServlet extends HttpServlet {
041:
042: /** . */
043: private final static Logger log = Logger
044: .getLogger(FacesLoaderServlet.class);
045:
046: /** . */
047: private final String servletContextListenerClassName = "org.apache.myfaces.webapp.StartupServletContextListener";
048:
049: /** . */
050: private FacesClassLoader loader;
051:
052: /** . */
053: private ServletContextListener listener;
054:
055: /** . */
056: private Servlet servlet;
057:
058: public void init() throws ServletException {
059: try {
060: ServletContext ctx = getServletConfig().getServletContext();
061:
062: ArrayList tmp = new ArrayList();
063: File f = new File(ctx.getRealPath("/WEB-INF/lib2/"));
064: File[] libs = f.listFiles();
065: for (int i = 0; i < libs.length; i++) {
066: File lib = libs[i];
067: URL libURL = lib.toURL();
068: log.debug("Added " + libURL + " to faces class loader");
069: tmp.add(libURL);
070: }
071: URL[] urls = (URL[]) tmp.toArray(new URL[tmp.size()]);
072:
073: //
074: loader = new FacesClassLoader(urls, Thread.currentThread()
075: .getContextClassLoader());
076: } catch (Exception e) {
077: throw new ServletException(
078: "Cannot find WEB-INF/lib2 directory to load libraries from",
079: e);
080: }
081:
082: //
083: if (loader != null) {
084: getServletConfig().getServletContext().setAttribute(
085: "FACESCLASSLOADER", loader);
086: ClassLoader containerLoader = Thread.currentThread()
087: .getContextClassLoader();
088: Thread.currentThread().setContextClassLoader(loader);
089:
090: //
091: try {
092: // Perform myfaces bootstrap
093: initMyFaces();
094:
095: //
096: initFacesServlet();
097: } finally {
098: Thread.currentThread().setContextClassLoader(
099: containerLoader);
100: }
101: }
102: }
103:
104: public void destroy() {
105: if (loader != null) {
106: ClassLoader containerLoader = Thread.currentThread()
107: .getContextClassLoader();
108: Thread.currentThread().setContextClassLoader(loader);
109:
110: //
111: try {
112: //
113: destroyFacesServlet();
114:
115: //
116: destroyMyFaces();
117: } finally {
118: Thread.currentThread().setContextClassLoader(
119: containerLoader);
120: }
121: }
122: }
123:
124: private void initFacesServlet() {
125: try {
126: Class facesServletClass = loader
127: .loadClass("javax.faces.webapp.FacesServlet");
128: servlet = (Servlet) facesServletClass.newInstance();
129: servlet.init(getServletConfig());
130: } catch (Exception e) {
131: e.printStackTrace();
132: }
133: }
134:
135: private void destroyFacesServlet() {
136: if (servlet != null) {
137: servlet.destroy();
138: }
139: }
140:
141: private void initMyFaces() {
142: try {
143: Class bridgeClass = Thread.currentThread()
144: .getContextClassLoader().loadClass(
145: servletContextListenerClassName);
146: log.debug("Obtain bridge bootstrap class "
147: + bridgeClass.getName());
148: listener = (ServletContextListener) bridgeClass
149: .newInstance();
150: } catch (ClassNotFoundException e) {
151: // It is not an error, this happens when we run with Sun RI
152: log.debug("Bridge bootstrap not available "
153: + servletContextListenerClassName, e);
154: } catch (IllegalAccessException e) {
155: log.error("Bridge bootstrap not accessible "
156: + servletContextListenerClassName, e);
157: } catch (InstantiationException e) {
158: log.error("Bridge bootstrap not instantiable "
159: + servletContextListenerClassName, e);
160: }
161:
162: //
163: if (listener != null) {
164: log.debug("About to initialize the bridge bootstrap");
165: ServletContextEvent event = new ServletContextEvent(
166: getServletConfig().getServletContext());
167: listener.contextInitialized(event);
168: }
169: }
170:
171: private void destroyMyFaces() {
172: if (listener != null) {
173: log.debug("About to destroy the bridge bootstrap");
174: ServletContextEvent event = new ServletContextEvent(
175: getServletConfig().getServletContext());
176: listener.contextDestroyed(event);
177: }
178: }
179:
180: }
|