1 22 package org.jboss.invocation.http.servlet; 23 24 import java.io.IOException ; 25 import java.io.ObjectOutputStream ; 26 import javax.servlet.ServletConfig ; 27 import javax.servlet.ServletException ; 28 import javax.servlet.ServletOutputStream ; 29 import javax.servlet.http.HttpServlet ; 30 import javax.servlet.http.HttpServletRequest ; 31 import javax.servlet.http.HttpServletResponse ; 32 import javax.management.ObjectName ; 33 import javax.management.MalformedObjectNameException ; 34 import javax.management.MBeanServer ; 35 36 import org.jboss.invocation.MarshalledValue; 37 import org.jboss.logging.Logger; 38 import org.jboss.mx.util.MBeanServerLocator; 39 40 49 public class NamingFactoryServlet extends HttpServlet 50 { 51 52 private static String RESPONSE_CONTENT_TYPE = 53 "application/x-java-serialized-object; class=org.jboss.invocation.MarshalledValue"; 54 private Logger log; 55 56 57 private Object namingProxy; 58 59 private ObjectName namingProxyMBean; 60 61 private String proxyAttribute; 62 63 65 public void init(ServletConfig config) throws ServletException 66 { 67 super.init(config); 68 String category = getClass().getName() + '.' + config.getServletName(); 69 log = Logger.getLogger(category); 70 71 String name = config.getInitParameter("namingProxyMBean"); 73 if( name == null ) 74 throw new ServletException ("An namingProxyMBean must be specified"); 75 proxyAttribute = config.getInitParameter("proxyAttribute"); 76 if( proxyAttribute == null ) 77 proxyAttribute = "Proxy"; 78 79 try 80 { 81 namingProxyMBean = new ObjectName (name); 82 } 83 catch (MalformedObjectNameException e) 84 { 85 throw new ServletException ("Failed to create object name: "+name, e); 86 } 87 } 88 89 91 public void destroy() 92 { 93 } 94 95 97 public String getServletInfo() 98 { 99 return "A factory servlet for Naming proxies"; 100 } 101 102 105 protected void processRequest(HttpServletRequest request, HttpServletResponse response) 106 throws ServletException , IOException 107 { 108 boolean trace = log.isTraceEnabled(); 109 if( trace ) 110 log.trace("processRequest"); 111 lookupNamingProxy(); 113 try 114 { 115 response.setContentType(RESPONSE_CONTENT_TYPE); 116 MarshalledValue mv = new MarshalledValue(namingProxy); 117 if( trace ) 118 log.trace("Serialized Naming proxy, size="+mv.size()); 119 ServletOutputStream sos = response.getOutputStream(); 121 ObjectOutputStream oos = new ObjectOutputStream (sos); 122 oos.writeObject(mv); 123 oos.flush(); 124 oos.close(); 125 } 126 catch(Throwable t) 127 { 128 log.debug("Invoke failed", t); 129 response.resetBuffer(); 131 MarshalledValue mv = new MarshalledValue(t); 132 ServletOutputStream sos = response.getOutputStream(); 133 ObjectOutputStream oos = new ObjectOutputStream (sos); 134 oos.writeObject(mv); 135 oos.close(); 136 } 137 138 } 139 140 144 protected void doGet(HttpServletRequest request, HttpServletResponse response) 145 throws ServletException , IOException 146 { 147 processRequest(request, response); 148 } 149 150 154 protected void doPost(HttpServletRequest request, HttpServletResponse response) 155 throws ServletException , IOException 156 { 157 processRequest(request, response); 158 } 159 160 164 private synchronized void lookupNamingProxy() 165 throws ServletException 166 { 167 if( namingProxy != null ) 168 return; 169 170 MBeanServer mbeanServer = MBeanServerLocator.locateJBoss(); 171 try 172 { 173 namingProxy = mbeanServer.getAttribute(namingProxyMBean, proxyAttribute); 174 } 175 catch(Exception e) 176 { 177 String msg = "Failed to obtain proxy from: "+namingProxyMBean 178 + " via attribute:" + proxyAttribute; 179 log.debug(msg, e); 180 throw new ServletException (msg, e); 181 } 182 183 } 184 } 185 | Popular Tags |