KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > ProxyDeployer


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;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.List JavaDoc;
26
27 import org.jboss.annotation.ejb.Clustered;
28 import org.jboss.annotation.ejb.LocalBinding;
29 import org.jboss.annotation.ejb.LocalBindingImpl;
30 import org.jboss.annotation.ejb.RemoteBinding;
31 import org.jboss.annotation.ejb.RemoteBindingImpl;
32 import org.jboss.annotation.ejb.RemoteBindings;
33 import org.jboss.annotation.ejb.RemoteBindingsImpl;
34 import org.jboss.aop.Advisor;
35 import org.jboss.ejb3.remoting.RemoteProxyFactory;
36 import org.jboss.ejb3.service.ServiceContainer;
37 import org.jboss.ejb3.service.ServiceLocalProxyFactory;
38 import org.jboss.ejb3.service.ServiceRemoteProxyFactory;
39 import org.jboss.ejb3.stateful.StatefulClusterProxyFactory;
40 import org.jboss.ejb3.stateful.StatefulContainer;
41 import org.jboss.ejb3.stateful.StatefulLocalProxyFactory;
42 import org.jboss.ejb3.stateful.StatefulRemoteProxyFactory;
43 import org.jboss.ejb3.stateless.StatelessClusterProxyFactory;
44 import org.jboss.ejb3.stateless.StatelessLocalProxyFactory;
45 import org.jboss.ejb3.stateless.StatelessRemoteProxyFactory;
46 import org.jboss.logging.Logger;
47
48 /**
49  * Comment
50  *
51  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
52  * @version $Revision: 56592 $
53  */

54 public class ProxyDeployer
55 {
56    private static final Logger log = Logger.getLogger(ProxyDeployer.class);
57    private Container container;
58    private Advisor advisor;
59    private ArrayList JavaDoc<ProxyFactory> proxyFactories = new ArrayList JavaDoc<ProxyFactory>();
60    private RemoteBindings remoteBindings;
61    private LocalBinding localBinding;
62
63    public ProxyDeployer(Container container)
64    {
65       this.container = container;
66       this.advisor = (Advisor) container;
67    }
68
69    public List JavaDoc<ProxyFactory> getProxyFactories() { return proxyFactories; }
70
71    public void start() throws Exception JavaDoc
72    {
73       if (remoteBindings != null)
74       {
75          RemoteBinding[] list = remoteBindings.value();
76          for (int i = 0; i < list.length; i++)
77          {
78             Class JavaDoc factoryClass = list[i].factory();
79             if (factoryClass.equals(RemoteProxyFactory.class)) factoryClass = getDefaultRemoteProxyFactory();
80             RemoteProxyFactory factory = (RemoteProxyFactory) factoryClass.newInstance();
81             factory.setRemoteBinding(list[i]);
82             factory.setContainer(container);
83             factory.start();
84             proxyFactories.add(factory);
85          }
86       }
87
88       if (localBinding != null)
89       {
90          ProxyFactory factory = null;
91          if (container instanceof StatefulContainer)
92          {
93             factory = new StatefulLocalProxyFactory();
94          }
95          else if (container instanceof ServiceContainer)
96          {
97             factory = new ServiceLocalProxyFactory();
98          }
99          else
100          {
101             factory = new StatelessLocalProxyFactory();
102          }
103
104          factory.setContainer(container);
105          factory.start();
106          proxyFactories.add(factory);
107       }
108    }
109
110    public void initializeLocalBindingMetadata()
111    {
112       localBinding = (LocalBinding) advisor.resolveAnnotation(LocalBinding.class);
113       if (localBinding == null)
114       {
115          if (ProxyFactoryHelper.getLocalInterfaces(container) != null)
116          {
117             localBinding = new LocalBindingImpl(ProxyFactoryHelper.getLocalJndiName(container));
118             advisor.getAnnotations().addClassAnnotation(LocalBinding.class, localBinding);
119          }
120       }
121    }
122
123    public void initializeRemoteBindingMetadata()
124    {
125       remoteBindings = (RemoteBindings) advisor.resolveAnnotation(RemoteBindings.class);
126       if (remoteBindings == null)
127       {
128          RemoteBinding binding = (RemoteBinding) advisor.resolveAnnotation(RemoteBinding.class);
129          if (binding == null)
130          {
131             log.debug("no declared remote bindings for : " + container.getEjbName());
132             if (ProxyFactoryHelper.getRemoteInterfaces(container) != null)
133             {
134                log.debug("there is remote interfaces for " + container.getEjbName());
135                String JavaDoc jndiName = ProxyFactoryHelper.getDefaultRemoteJndiName(container);
136                log.debug("default remote binding has jndiName of " + jndiName);
137                String JavaDoc uri = ""; // use the default
138
Class JavaDoc factory = null;
139                factory = getDefaultRemoteProxyFactory();
140                RemoteBinding[] list = {new RemoteBindingImpl(jndiName, "", uri, factory)};
141                remoteBindings = new RemoteBindingsImpl(list);
142                advisor.getAnnotations().addClassAnnotation(RemoteBindings.class, remoteBindings);
143             }
144          }
145          else
146          {
147             RemoteBinding[] list = {binding};
148             remoteBindings = new RemoteBindingsImpl(list);
149             advisor.getAnnotations().addClassAnnotation(RemoteBindings.class, remoteBindings);
150          }
151       }
152    }
153
154    private Class JavaDoc getDefaultRemoteProxyFactory()
155    {
156       Class JavaDoc factory;
157       if (container instanceof StatefulContainer)
158       {
159          if (advisor.resolveAnnotation(Clustered.class) != null)
160          {
161             factory = StatefulClusterProxyFactory.class;
162          }
163          else
164          {
165             factory = StatefulRemoteProxyFactory.class;
166          }
167       }
168       else if (container instanceof ServiceContainer)
169       {
170          //TODO Implement clustering
171
factory = ServiceRemoteProxyFactory.class;
172       }
173       else
174       {
175          if (advisor.resolveAnnotation(Clustered.class) != null)
176          {
177             factory = StatelessClusterProxyFactory.class;
178          }
179          else
180          {
181             factory = StatelessRemoteProxyFactory.class;
182          }
183       }
184       return factory;
185    }
186
187
188    public void stop() throws Exception JavaDoc
189    {
190       for (int i = 0; i < proxyFactories.size(); i++)
191       {
192          ProxyFactory factory = (ProxyFactory) proxyFactories.get(i);
193          factory.stop();
194       }
195    }
196 }
197
Popular Tags