1 22 package org.jboss.invocation.http.servlet; 23 24 import java.io.IOException ; 25 import java.io.ObjectInputStream ; 26 import java.io.ObjectOutputStream ; 27 import java.lang.reflect.InvocationTargetException ; 28 import java.security.PrivilegedAction ; 29 import java.security.Principal ; 30 import java.security.AccessController ; 31 import javax.management.MalformedObjectNameException ; 32 import javax.management.MBeanServer ; 33 import javax.management.ObjectName ; 34 import javax.servlet.ServletConfig ; 35 import javax.servlet.ServletException ; 36 import javax.servlet.ServletInputStream ; 37 import javax.servlet.ServletOutputStream ; 38 import javax.servlet.http.HttpServlet ; 39 import javax.servlet.http.HttpServletRequest ; 40 import javax.servlet.http.HttpServletResponse ; 41 42 import org.jboss.invocation.InvocationException; 43 import org.jboss.invocation.MarshalledInvocation; 44 import org.jboss.invocation.MarshalledValue; 45 import org.jboss.logging.Logger; 46 import org.jboss.mx.util.MBeanServerLocator; 47 import org.jboss.mx.util.JMXExceptionDecoder; 48 import org.jboss.system.Registry; 49 import org.jboss.security.SecurityAssociation; 50 51 65 public class InvokerServlet extends HttpServlet 66 { 67 private static Logger log = Logger.getLogger(InvokerServlet.class); 68 69 private static String REQUEST_CONTENT_TYPE = 70 "application/x-java-serialized-object; class=org.jboss.invocation.MarshalledInvocation"; 71 72 private static String RESPONSE_CONTENT_TYPE = 73 "application/x-java-serialized-object; class=org.jboss.invocation.MarshalledValue"; 74 private MBeanServer mbeanServer; 75 private ObjectName localInvokerName; 76 77 79 public void init(ServletConfig config) throws ServletException 80 { 81 super.init(config); 82 try 83 { 84 String name = config.getInitParameter("invokerName"); 86 if( name != null ) 87 { 88 localInvokerName = new ObjectName (name); 89 log.debug("localInvokerName="+localInvokerName); 90 } 91 } 92 catch(MalformedObjectNameException e) 93 { 94 throw new ServletException ("Failed to build invokerName", e); 95 } 96 97 mbeanServer = MBeanServerLocator.locateJBoss(); 99 if( mbeanServer == null ) 100 throw new ServletException ("Failed to locate the MBeanServer"); 101 } 102 103 105 public void destroy() 106 { 107 108 } 109 110 116 protected void processRequest(HttpServletRequest request, HttpServletResponse response) 117 throws ServletException , IOException 118 { 119 boolean trace = log.isTraceEnabled(); 120 if( trace ) 121 { 122 log.trace("processRequest, ContentLength: "+request.getContentLength()); 123 log.trace("processRequest, ContentType: "+request.getContentType()); 124 } 125 126 Boolean returnValueAsAttribute = (Boolean ) request.getAttribute("returnValueAsAttribute"); 127 try 128 { 129 response.setContentType(RESPONSE_CONTENT_TYPE); 130 MarshalledInvocation mi = (MarshalledInvocation) request.getAttribute("MarshalledInvocation"); 132 if( mi == null ) 133 { 134 ServletInputStream sis = request.getInputStream(); 136 ObjectInputStream ois = new ObjectInputStream (sis); 137 mi = (MarshalledInvocation) ois.readObject(); 138 ois.close(); 139 } 140 145 if (mi.getPrincipal() == null && mi.getCredential() == null) 146 { 147 mi.setPrincipal(GetPrincipalAction.getPrincipal()); 148 mi.setCredential(GetCredentialAction.getCredential()); 149 } 150 Object [] params = {mi}; 151 String [] sig = {"org.jboss.invocation.Invocation"}; 152 ObjectName invokerName = localInvokerName; 153 if( invokerName == null ) 155 { 156 Integer nameHash = (Integer ) mi.getObjectName(); 157 invokerName = (ObjectName ) Registry.lookup(nameHash); 158 if( invokerName == null ) 159 throw new ServletException ("Failed to find invoker name for hash("+nameHash+")"); 160 } 161 Object value = mbeanServer.invoke(invokerName, "invoke", params, sig); 163 if( returnValueAsAttribute == null || returnValueAsAttribute.booleanValue() == false ) 164 { 165 MarshalledValue mv = new MarshalledValue(value); 166 ServletOutputStream sos = response.getOutputStream(); 167 ObjectOutputStream oos = new ObjectOutputStream (sos); 168 oos.writeObject(mv); 169 oos.close(); 170 } 171 else 172 { 173 request.setAttribute("returnValue", value); 174 } 175 } 176 catch(Throwable t) 177 { 178 t = JMXExceptionDecoder.decode(t); 179 if( t instanceof InvocationTargetException ) 181 { 182 InvocationTargetException ite = (InvocationTargetException ) t; 183 t = ite.getTargetException(); 184 } 185 188 InvocationException appException = new InvocationException(t); 189 log.debug("Invoke threw exception", t); 190 if( returnValueAsAttribute == null || returnValueAsAttribute.booleanValue() == false ) 192 { 193 response.resetBuffer(); 194 MarshalledValue mv = new MarshalledValue(appException); 195 ServletOutputStream sos = response.getOutputStream(); 196 ObjectOutputStream oos = new ObjectOutputStream (sos); 197 oos.writeObject(mv); 198 oos.close(); 199 } 200 else 201 { 202 request.setAttribute("returnValue", appException); 203 } 204 } 205 } 206 207 211 protected void doGet(HttpServletRequest request, HttpServletResponse response) 212 throws ServletException , IOException 213 { 214 processRequest(request, response); 215 } 216 217 221 protected void doPost(HttpServletRequest request, HttpServletResponse response) 222 throws ServletException , IOException 223 { 224 processRequest(request, response); 225 } 226 227 229 public String getServletInfo() 230 { 231 return "An HTTP to JMX invocation servlet"; 232 } 233 234 private static class GetPrincipalAction implements PrivilegedAction 235 { 236 static PrivilegedAction ACTION = new GetPrincipalAction(); 237 public Object run() 238 { 239 Principal principal = SecurityAssociation.getPrincipal(); 240 return principal; 241 } 242 static Principal getPrincipal() 243 { 244 Principal principal = (Principal ) AccessController.doPrivileged(ACTION); 245 return principal; 246 } 247 } 248 249 private static class GetCredentialAction implements PrivilegedAction 250 { 251 static PrivilegedAction ACTION = new GetCredentialAction(); 252 public Object run() 253 { 254 Object credential = SecurityAssociation.getCredential(); 255 return credential; 256 } 257 static Object getCredential() 258 { 259 Object credential = AccessController.doPrivileged(ACTION); 260 return credential; 261 } 262 } 263 } 264 | Popular Tags |