KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > interop > adapter > AdapterManager


1 /**
2  *
3  * Copyright 2004-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.geronimo.interop.adapter;
19
20 //import org.openejb.EJBContainer;
21
//import org.openejb.EJBComponentType;
22
import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.apache.geronimo.gbean.*;
25 import org.apache.geronimo.interop.naming.NameService;
26
27 import java.util.HashMap JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.Collections JavaDoc;
30
31 public class AdapterManager implements ReferenceCollectionListener {
32
33     private final Log log = LogFactory.getLog(AdapterManager.class);
34     private HashMap JavaDoc homeAdapters;
35     private HashMap JavaDoc remoteAdapters;
36     private Collection JavaDoc containers = Collections.EMPTY_SET;
37     private ClassLoader JavaDoc classLoader;
38
39     private NameService nameService = NameService.getInstance();
40
41     /*
42      * This is a singleton GBean. Do not reference it directly, but use a
43      * GBean reference to get it.
44      */

45     public AdapterManager()
46     {
47         this.homeAdapters = null;
48         this.remoteAdapters = null;
49         this.containers = null;
50         this.classLoader = null;
51     }
52
53     //public AdapterManager( ClassLoader classLoader, Collection containers ) {
54
public AdapterManager( ClassLoader JavaDoc classLoader) {
55         //log.debug( "AdapterManager(): containers = " + containers );
56

57         this.classLoader = classLoader;
58
59         /*
60         ReferenceCollection ref = (ReferenceCollection) containers;
61         ref.addReferenceCollectionListener(this);
62
63         this.containers = containers;
64
65         int len = ( containers != null ? containers.size() : 20 );
66         homeAdapters = new HashMap( len );
67         remoteAdapters = new HashMap( len );
68         */

69
70         // Todo: Add the containers .. invoke memberAdded for each element in containers.
71

72         registerNameServer();
73     }
74
75     protected void registerNameServer()
76     {
77         //
78
// NameService
79
//
80

81         String JavaDoc bindNames[] = new String JavaDoc[] { "NameService" };
82         String JavaDoc remoteClassName = "org.apache.geronimo.interop.rmi.iiop.server.ServerNamingContext";
83         //String remoteInterfaceName = "org.apache.geronimo.interop.rmi.iiop.NameService";
84
String JavaDoc remoteInterfaceName = "org.apache.geronimo.interop.CosNaming.NamingContext";
85         String JavaDoc ids[] = new String JavaDoc[] { "IDL:omg.org/CosNaming/NamingContext:1.0",
86                                       "IDL:omg.org/CosNaming/NamingContextExt:1.0" };
87         ClassLoader JavaDoc cl = this.getClass().getClassLoader();
88
89         CorbaAdapter a = new CorbaAdapter( bindNames, ids, remoteClassName, remoteInterfaceName, cl );
90         a.start();
91         registerRemoteAdapter(a);
92     }
93
94     protected Adapter getAdapter( Object JavaDoc adapterID ) {
95         // more likely to be using the remoteadapter...
96
Adapter rc = getRemoteAdapter( adapterID );
97         if (rc == null)
98         {
99             rc = getHomeAdapter( adapterID );
100         }
101         return rc;
102     }
103
104     protected Adapter getHomeAdapter( Object JavaDoc adapterID )
105     {
106         return (Adapter)homeAdapters.get( adapterID );
107     }
108
109     protected Adapter getRemoteAdapter( Object JavaDoc adapterID )
110     {
111         return (Adapter)remoteAdapters.get( adapterID );
112     }
113
114     protected void registerHomeAdapter( Adapter adapter )
115     {
116         homeAdapters.put( adapter.getAdapterID(), adapter );
117     }
118
119     protected void registerRemoteAdapter( Adapter adapter )
120     {
121         remoteAdapters.put( adapter.getAdapterID(), adapter );
122     }
123
124     protected void unregisterHomeAdapter( Adapter adapter )
125     {
126         homeAdapters.remove( adapter.getAdapterID() );
127     }
128
129     protected void unregisterRemoteAdapter( Adapter adapter )
130     {
131         remoteAdapters.remove( adapter.getAdapterID() );
132     }
133
134     public void memberAdded(ReferenceCollectionEvent event) {
135         /*
136         EJBContainer container = (EJBContainer) event.getMember();
137
138         log.debug( "AdapterManager.memberAdded(): container = " + container );
139         log.debug( "AdapterManager.memberAdded(): containerID = " + container.getContainerID() );
140
141         switch (container.getProxyInfo().getComponentType()) {
142             case EJBComponentType.STATELESS:
143                 //generator = new AdapterStateless(container, orb, poa, tieLoader);
144                 break;
145             case EJBComponentType.STATEFUL:
146                 //generator = new AdapterStateful(container, orb, poa, tieLoader);
147                 break;
148             case EJBComponentType.BMP_ENTITY:
149             case EJBComponentType.CMP_ENTITY:
150                 //generator = new AdapterEntity(container, orb, poa, tieLoader);
151                 break;
152             default:
153                 // throw new CORBAException("CORBA HomeAdapter does not handle MDB containers");
154         }
155
156         Adapter adapter = new HomeAdapter( container );
157         adapter.start();
158         registerHomeAdapter( adapter );
159
160         adapter = new RemoteAdapter( container );
161         adapter.start();
162         registerRemoteAdapter( adapter );
163         */

164     }
165
166     public void memberRemoved(ReferenceCollectionEvent event) {
167         /*
168         EJBContainer container = (EJBContainer) event.getMember();
169
170         log.debug( "AdapterManager.memberRemoved(): container = " + container );
171         log.debug( "AdapterManager.memberRemoved(): containerID = " + container.getContainerID() );
172
173         Adapter adapter = getHomeAdapter( container.getContainerID() );
174         adapter.stop();
175         unregisterHomeAdapter( adapter );
176
177         adapter = getRemoteAdapter( container.getContainerID() );
178         adapter.stop();
179         unregisterRemoteAdapter( adapter );
180         */

181     }
182
183     public static final GBeanInfo GBEAN_INFO;
184
185     static {
186         GBeanInfoBuilder infoFactory = new GBeanInfoBuilder(AdapterManager.class);
187
188         infoFactory.addAttribute("classLoader", ClassLoader JavaDoc.class, false);
189         //infoFactory.addReference("containers", EJBContainer.class);
190

191         //infoFactory.setConstructor(new String[]{"classLoader", "containers"});
192
infoFactory.setConstructor(new String JavaDoc[]{"classLoader"});
193
194         GBEAN_INFO = infoFactory.getBeanInfo();
195     }
196
197     public static GBeanInfo getGBeanInfo() {
198         return GBEAN_INFO;
199     }
200 }
201
Popular Tags