KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > service > BaseServiceProxyFactory


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.service;
23
24 import java.lang.reflect.Constructor JavaDoc;
25 import java.lang.reflect.InvocationHandler JavaDoc;
26 import javax.naming.Context JavaDoc;
27 import javax.naming.NamingException JavaDoc;
28
29 import org.jboss.aop.Advisor;
30 import org.jboss.ejb3.Container;
31 import org.jboss.ejb3.ProxyFactory;
32 import org.jboss.naming.Util;
33
34 /**
35  * @author <a HREF="mailto:kabir.khan@jboss.org">Kabir Khan</a>
36  * @version $Revision: 56518 $
37  */

38 public abstract class BaseServiceProxyFactory implements ProxyFactory
39 {
40
41    protected Class JavaDoc proxyClass;
42    protected Constructor JavaDoc proxyConstructor;
43    protected Context JavaDoc proxyFactoryContext;
44    protected String JavaDoc jndiName;
45    protected Container container;
46    protected Advisor advisor;
47
48    public Object JavaDoc createHomeProxy()
49    {
50       throw new UnsupportedOperationException JavaDoc("service can't have a home interface");
51    }
52    
53    public Object JavaDoc createProxy(Object JavaDoc id)
54    {
55       if(id != null)
56          throw new IllegalArgumentException JavaDoc("service proxy must not have an id");
57       return createProxy();
58    }
59    
60    public void start() throws Exception JavaDoc
61    {
62       initializeJndiName();
63       Class JavaDoc[] interfaces = getInterfaces();
64       Class JavaDoc proxyClass = java.lang.reflect.Proxy.getProxyClass(container.getBeanClass().getClassLoader(), interfaces);
65       final Class JavaDoc[] constructorParams =
66               {InvocationHandler JavaDoc.class};
67       proxyConstructor = proxyClass.getConstructor(constructorParams);
68
69       try
70       {
71          Util.rebind(container.getInitialContext(), jndiName, createProxy());
72       } catch (NamingException JavaDoc e)
73       {
74          NamingException JavaDoc namingException = new NamingException JavaDoc("Could not bind service proxy factory for EJB container with ejb name " + container.getEjbName() + " into JNDI under jndiName: " + container.getInitialContext().getNameInNamespace() + "/" + jndiName);
75          namingException.setRootCause(e);
76          throw namingException;
77       }
78    }
79
80    public void stop() throws Exception JavaDoc
81    {
82       Util.unbind(container.getInitialContext(), jndiName);
83    }
84
85    protected abstract Class JavaDoc[] getInterfaces();
86
87    protected abstract void initializeJndiName();
88
89    public void setContainer(Container container)
90    {
91       this.container = container;
92       this.advisor = (Advisor) container;
93    }
94
95 }
96
Popular Tags