KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > mdb > ProducerFactory


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.ejb3.mdb;
23
24 import java.lang.reflect.Method JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Hashtable JavaDoc;
27 import javax.jms.Destination JavaDoc;
28 import javax.naming.Context JavaDoc;
29 import javax.naming.InitialContext JavaDoc;
30 import javax.naming.Name JavaDoc;
31 import javax.naming.NamingException JavaDoc;
32 import javax.naming.RefAddr JavaDoc;
33 import javax.naming.Reference JavaDoc;
34 import javax.naming.StringRefAddr JavaDoc;
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 /**
47  * comment
48  *
49  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
50  */

51 public abstract class ProducerFactory implements ProxyFactory
52 {
53    private static final Logger log = Logger.getLogger(ProducerFactory.class);
54    
55    protected Class JavaDoc producer;
56    protected MessageProperties props;
57    protected Destination JavaDoc dest;
58    protected HashMap JavaDoc methodMap;
59    protected ProducerImpl pImpl;
60    protected String JavaDoc jndiName;
61    protected InitialContext JavaDoc ctx;
62    protected Hashtable JavaDoc initialContextProperties;
63
64    public static final String JavaDoc PROXY_FACTORY_NAME = "PRODUCER_FACTORY";
65
66
67    protected ProducerFactory(ConsumerContainer container, Class JavaDoc producer, MessageProperties props, Destination JavaDoc dest, InitialContext JavaDoc ctx, Hashtable JavaDoc 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 JavaDoc();
77       Method JavaDoc[] 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 JavaDoc(MethodHashing.methodHash(methods[i])), new MessagePropertiesImpl(mProps));
86             }
87             catch (Exception JavaDoc e)
88             {
89                throw new RuntimeException JavaDoc(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 JavaDoc createHomeProxy()
113    {
114       throw new UnsupportedOperationException JavaDoc("producer can't have a home interface");
115    }
116    
117    public Object JavaDoc createProxy(Object JavaDoc id)
118    {
119       if(id != null)
120          throw new IllegalArgumentException JavaDoc("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 JavaDoc
129    {
130       Context JavaDoc baseCtx = ctx;
131       Name JavaDoc name = baseCtx.getNameParser("").parse(jndiName);
132       baseCtx = Util.createSubcontext(baseCtx, name.getPrefix(name.size() - 1));
133       String JavaDoc atom = name.get(name.size() - 1);
134       RefAddr JavaDoc refAddr = new StringRefAddr JavaDoc(JndiProxyFactory.FACTORY, atom + PROXY_FACTORY_NAME);
135       Reference JavaDoc ref = new Reference JavaDoc("java.lang.Object", refAddr, JndiProxyFactory.class.getName(), null);
136      
137       try
138       {
139          Util.rebind(baseCtx, atom, ref);
140       } catch (NamingException JavaDoc e)
141       {
142          NamingException JavaDoc namingException = new NamingException JavaDoc("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 JavaDoc
149    {
150       Util.unbind(ctx, jndiName);
151    }
152 }
153
Popular Tags