1 22 package org.jboss.ejb3.mdb; 23 24 import java.lang.reflect.Method ; 25 import java.util.HashMap ; 26 import java.util.Hashtable ; 27 import javax.jms.Destination ; 28 import javax.naming.Context ; 29 import javax.naming.InitialContext ; 30 import javax.naming.Name ; 31 import javax.naming.NamingException ; 32 import javax.naming.RefAddr ; 33 import javax.naming.Reference ; 34 import javax.naming.StringRefAddr ; 35 import org.jboss.annotation.ejb.MessageProperties; 36 import org.jboss.annotation.ejb.MessagePropertiesImpl; 37 import org.jboss.annotation.ejb.Producer; 38 import org.jboss.annotation.ejb.Producers; 39 import org.jboss.aop.util.MethodHashing; 40 import org.jboss.ejb3.Container; 41 import org.jboss.ejb3.JndiProxyFactory; 42 import org.jboss.ejb3.ProxyFactory; 43 import org.jboss.logging.Logger; 44 import org.jboss.naming.Util; 45 46 51 public abstract class ProducerFactory implements ProxyFactory 52 { 53 private static final Logger log = Logger.getLogger(ProducerFactory.class); 54 55 protected Class producer; 56 protected MessageProperties props; 57 protected Destination dest; 58 protected HashMap methodMap; 59 protected ProducerImpl pImpl; 60 protected String jndiName; 61 protected InitialContext ctx; 62 protected Hashtable initialContextProperties; 63 64 public static final String PROXY_FACTORY_NAME = "PRODUCER_FACTORY"; 65 66 67 protected ProducerFactory(ConsumerContainer container, Class producer, MessageProperties props, Destination dest, InitialContext ctx, Hashtable ctxProperties) 68 { 69 this.producer = producer; 70 this.props = props; 71 this.dest = dest; 72 this.ctx = ctx; 73 this.initialContextProperties = ctxProperties; 74 75 76 methodMap = new HashMap (); 77 Method [] methods = producer.getMethods(); 78 for (int i = 0 ; i < methods.length ; ++i) 79 { 80 MessageProperties mProps = (MessageProperties)methods[i].getAnnotation(MessageProperties.class); 81 if (mProps != null) 82 { 83 try 84 { 85 methodMap.put(new Long (MethodHashing.methodHash(methods[i])), new MessagePropertiesImpl(mProps)); 86 } 87 catch (Exception e) 88 { 89 throw new RuntimeException (e); 90 } 91 } 92 } 93 94 Producer p = (Producer) producer.getAnnotation(Producer.class); 95 if (p == null) 96 p = (Producer)container.resolveAnnotation(Producer.class); 97 if (p == null) 98 { 99 Producers annotation = (Producers)container.resolveAnnotation(Producers.class); 100 Producer[] producers = annotation.value(); 101 for (int i = 0 ; i < producers.length ; ++i) 102 { 103 if (producers[i].producer() != null && producers[i].producer().equals(producer)) 104 p = producers[i]; 105 } 106 } 107 108 pImpl = new ProducerImpl(p); 109 jndiName = producer.getName(); 110 } 111 112 public Object createHomeProxy() 113 { 114 throw new UnsupportedOperationException ("producer can't have a home interface"); 115 } 116 117 public Object createProxy(Object id) 118 { 119 if(id != null) 120 throw new IllegalArgumentException ("producer proxy must not have an id"); 121 return createProxy(); 122 } 123 124 public void setContainer(Container container) 125 { 126 } 127 128 public void start() throws Exception 129 { 130 Context baseCtx = ctx; 131 Name name = baseCtx.getNameParser("").parse(jndiName); 132 baseCtx = Util.createSubcontext(baseCtx, name.getPrefix(name.size() - 1)); 133 String atom = name.get(name.size() - 1); 134 RefAddr refAddr = new StringRefAddr (JndiProxyFactory.FACTORY, atom + PROXY_FACTORY_NAME); 135 Reference ref = new Reference ("java.lang.Object", refAddr, JndiProxyFactory.class.getName(), null); 136 137 try 138 { 139 Util.rebind(baseCtx, atom, ref); 140 } catch (NamingException e) 141 { 142 NamingException namingException = new NamingException ("Could not bind producer factory into JNDI under jndiName: " + baseCtx.getNameInNamespace() + "/" + atom); 143 namingException.setRootCause(e); 144 throw namingException; 145 } 146 } 147 148 public void stop() throws Exception 149 { 150 Util.unbind(ctx, jndiName); 151 } 152 } 153 | Popular Tags |