01: /*
02: * JBoss, Home of Professional Open Source.
03: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
04: * as indicated by the @author tags. See the copyright.txt file in the
05: * distribution for a full listing of individual contributors.
06: *
07: * This is free software; you can redistribute it and/or modify it
08: * under the terms of the GNU Lesser General Public License as
09: * 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;
25: import java.lang.reflect.InvocationHandler;
26: import javax.naming.Context;
27: import javax.naming.NamingException;
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: 57207 $
37: */
38: public abstract class BaseServiceProxyFactory implements ProxyFactory {
39:
40: protected Class proxyClass;
41: protected Constructor proxyConstructor;
42: protected Context proxyFactoryContext;
43: protected String jndiName;
44: protected Container container;
45: protected Advisor advisor;
46:
47: public Object createHomeProxy() {
48: throw new UnsupportedOperationException(
49: "service can't have a home interface");
50: }
51:
52: public Object createProxy(Object id) {
53: if (id != null)
54: throw new IllegalArgumentException(
55: "service proxy must not have an id");
56: return createProxy();
57: }
58:
59: public void start() throws Exception {
60: initializeJndiName();
61: Class[] interfaces = getInterfaces();
62: Class proxyClass = java.lang.reflect.Proxy.getProxyClass(
63: container.getBeanClass().getClassLoader(), interfaces);
64: final Class[] constructorParams = { InvocationHandler.class };
65: proxyConstructor = proxyClass.getConstructor(constructorParams);
66:
67: try {
68: Util.rebind(container.getInitialContext(), jndiName,
69: createProxy());
70: } catch (NamingException e) {
71: NamingException namingException = new NamingException(
72: "Could not bind service proxy factory for EJB container with ejb name "
73: + container.getEjbName()
74: + " into JNDI under jndiName: "
75: + container.getInitialContext()
76: .getNameInNamespace() + "/"
77: + jndiName);
78: namingException.setRootCause(e);
79: throw namingException;
80: }
81: }
82:
83: public void stop() throws Exception {
84: Util.unbind(container.getInitialContext(), jndiName);
85: }
86:
87: protected abstract Class[] getInterfaces();
88:
89: protected abstract void initializeJndiName();
90:
91: public void setContainer(Container container) {
92: this .container = container;
93: this .advisor = (Advisor) container;
94: }
95:
96: }
|