1 16 package org.apache.cocoon.components.axis.providers; 19 20 import org.apache.avalon.framework.component.Component; 21 import org.apache.avalon.framework.component.ComponentManager; 22 23 import org.apache.axis.AxisFault; 24 import org.apache.axis.MessageContext; 25 import org.apache.axis.providers.java.RPCProvider; 26 import org.apache.axis.handlers.soap.SOAPService; 27 28 import java.lang.reflect.Method ; 29 import java.lang.reflect.InvocationHandler ; 30 import java.lang.reflect.Proxy ; 31 32 import javax.xml.rpc.server.ServiceLifecycle ; 33 34 89 public class AvalonProvider extends RPCProvider 90 { 91 95 public static final String COMPONENT_MANAGER = "component-manager"; 96 97 103 public static final String ROLE = "role"; 104 105 113 protected Object makeNewServiceObject(MessageContext msgContext, String role) 114 throws Exception { 115 ComponentManager manager = 116 (ComponentManager) msgContext.getProperty(COMPONENT_MANAGER); 117 118 if (manager == null) 119 throw new AxisFault("Could not access Avalon ComponentManager"); 120 121 return decorate(manager.lookup(role), manager); 122 } 123 124 133 private Object decorate(final Component object, final ComponentManager manager) 134 throws Exception { 135 Class [] interfaces = object.getClass().getInterfaces(); 137 138 Class [] adjusted = new Class [ interfaces.length + 1 ]; 140 System.arraycopy(interfaces, 0, adjusted, 0, interfaces.length); 141 adjusted[interfaces.length] = ServiceLifecycle .class; 142 143 Object proxy = 145 Proxy.newProxyInstance( 146 this.getClass().getClassLoader(), 147 adjusted, 148 new Handler (object, manager) 149 ); 150 151 return proxy; 153 } 154 155 159 protected String getServiceClassNameOptionName() { 160 return ROLE; 161 } 162 163 172 protected Class getServiceClass( 173 String role, SOAPService service, MessageContext msgContext) 174 throws AxisFault { 175 179 try { 180 int i; 181 182 if ((i = role.indexOf('/')) != -1) 183 { 184 return Class.forName(role.substring(0, i)); 185 } 186 return Class.forName(role); 187 } catch (ClassNotFoundException e) { 188 throw new AxisFault("Couldn't create class object for role " + role, e); 189 } 190 } 191 192 224 final static class Handler implements InvocationHandler { 225 226 private final static String SL_DESTROY = "destroy"; 228 private final Class SL_CLASS = ServiceLifecycle .class; 229 230 private final Component m_object; 232 private final ComponentManager m_manager; 233 234 240 public Handler(final Component object, final ComponentManager manager) { 241 m_object = object; 242 m_manager = manager; 243 } 244 245 262 public Object invoke(Object proxy, Method method, Object [] args) 263 throws Throwable { 264 if (method.getDeclaringClass().equals(SL_CLASS)) { 266 if (method.getName().equals(SL_DESTROY)) { 267 m_manager.release(m_object); 268 } 269 270 return null; 271 } 272 return method.invoke(m_object, args); 274 } 275 } 276 } 277 | Popular Tags |