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.server.deployment;
023:
024: import org.jboss.mx.util.MBeanProxy;
025: import org.jboss.portal.common.xml.XMLTools;
026: import org.jboss.portal.server.config.ServerConfig;
027: import org.w3c.dom.Document;
028: import org.w3c.dom.Element;
029:
030: import javax.management.ObjectName;
031: import java.util.Iterator;
032:
033: /**
034: * The role of this object is to modify the web application so it is possible to invoke it by request distpatching.
035: *
036: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
037: * @version $Revision: 8784 $
038: */
039: public class WebAppEnhancer extends WebAppIntercepter {
040:
041: /** . */
042: private ServerConfig config;
043:
044: public ServerConfig getConfig() {
045: return config;
046: }
047:
048: public void setConfig(ServerConfig config) {
049: this .config = config;
050: }
051:
052: protected void deploy(PortalWebApp pwa) {
053: try {
054: // Instrument war file first
055: pwa.instrument();
056:
057: // Inject proxies in the servlet context
058: Document desc = pwa.getDescriptor();
059: if (desc != null) {
060: Element jbossAppElt = desc.getDocumentElement();
061: for (Iterator i = XMLTools.getChildren(jbossAppElt,
062: "service").iterator(); i.hasNext();) {
063: Element serviceElt = (Element) i.next();
064:
065: //
066: log
067: .debug("About to inject a service in the servlet context of "
068: + pwa.getURL());
069:
070: //
071: Element serviceNameElt = XMLTools.getUniqueChild(
072: serviceElt, "service-name", true);
073: Element serviceClassElt = XMLTools.getUniqueChild(
074: serviceElt, "service-class", true);
075: Element serviceRefElt = XMLTools.getUniqueChild(
076: serviceElt, "service-ref", true);
077: String serviceName = XMLTools
078: .asString(serviceNameElt);
079: String serviceClass = XMLTools
080: .asString(serviceClassElt);
081: String serviceRef = XMLTools
082: .asString(serviceRefElt);
083:
084: //
085: if (serviceRef.startsWith(":")) {
086: log
087: .debug("Detecting a relative service reference "
088: + serviceRef
089: + " prepending it with "
090: + config.getDomain());
091: serviceRef = config.getDomain() + serviceRef;
092: }
093:
094: //
095: Class proxyClass = pwa.getClassLoader().loadClass(
096: serviceClass);
097: ObjectName objectName = ObjectName
098: .getInstance(serviceRef);
099: Object proxy = MBeanProxy.get(proxyClass,
100: objectName, server);
101:
102: //
103: log.debug("Want to inject " + serviceRef
104: + " with class " + proxy + " and name "
105: + serviceName);
106: pwa.getServletContext().setAttribute(serviceName,
107: proxy);
108: }
109: }
110:
111: } catch (Exception e) {
112: log.error("Cannot instrument the web application", e);
113: }
114: }
115:
116: protected void undeploy(PortalWebApp jwa) {
117: }
118: }
|