KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > ccm > runtime > ComponentServerImpl


1 // ====================================================================
2
//
3
// ECM: The Extensible Container Model
4
// Copyright (C) 2004 THALES
5
// Contact: openccm-ecm@objectweb.org
6
//
7
// This library is free software; you can redistribute it and/or
8
// modify it under the terms of the GNU Lesser General Public
9
// License as published by the Free Software Foundation; either
10
// version 2.1 of the License, or any later version.
11
//
12
// This library 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 library; if not, write to the Free Software
19
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20
// USA
21
//
22
// Initial developer(s): Mathieu Vadet.
23
// Initial Funding: IST COACH European project (IST-2001-34445)
24
// http://www.ist-coach.org
25
//
26
// ====================================================================
27

28
29
30 package org.objectweb.ccm.runtime;
31
32 import org.objectweb.corba.runtime.*;
33
34 /**
35  ** <p>Default implementation of the <tt>Components::ComponentServer</tt> interface.</p>
36  **/

37 public class ComponentServerImpl
38 extends org.omg.CORBA.LocalObject JavaDoc
39 implements Servant, org.omg.Components.Deployment.ComponentServerOperations
40 {
41     // component server
42
static private String JavaDoc _class_name = "ComponentServerImpl";
43     static final private String JavaDoc _type_id = org.omg.Components.Deployment.ComponentServerHelper.id();
44     private ORBService _orb_service;
45     private POAService _cont_poa_service;
46     private java.util.ArrayList JavaDoc _cont_refs; // (list of ::Components::Deployment::Container)
47
private org.omg.Components.ConfigValue[] _configuration;
48     private org.omg.Components.Deployment.ServerActivator _sa_ref;
49
50     // default constructor
51
public
52     ComponentServerImpl()
53     {
54         // component server
55
_orb_service = null;
56         _cont_poa_service = null;
57         _cont_refs = new java.util.ArrayList JavaDoc();
58         _configuration = new org.omg.Components.ConfigValue[0];
59         _sa_ref = null;
60     }
61
62     //
63
// Entry point
64
//
65

66     static public Servant
67     create_servant()
68     {
69         return new ComponentServerImpl();
70     }
71
72     //
73
// internal operations
74
//
75

76     //
77
// IDL:org/objectweb/corba/runtime/SystemComponent:1.0
78
//
79

80     final public void
81     system_configuration_complete(SystemConfiguration cfg)
82     {
83         // obtain services
84
ServantConfiguration scfg = (ServantConfiguration)cfg;
85         _orb_service = scfg.orb_service();
86
87         // create POA service for container servants
88
FactoryFinderService ffs = org.objectweb.corba.runtime.Runtime.getFFService();
89         SystemFactory poafact = ffs.find_service_factory(POAService.SERVICE_ID);
90
91         // configuration (requires ORB service)
92
// params: no name, no pols, _orb_service
93
POAConfiguration poacfg = new POAConfigurationImpl(null, null, _orb_service);
94         _cont_poa_service = (POAService)poafact.create_system_component(poacfg);
95
96         // register ConfigValue factory
97
String JavaDoc typeid = org.omg.Components.ConfigValueHelper.id();
98         org.omg.CORBA.portable.ValueFactory JavaDoc fact = org.objectweb.ccm.runtime.ConfigValueFactoryImpl.create_factory();
99         _orb_service.register_valuefactory(new ValueFactoryImpl(typeid, fact));
100
101         // NOTE: how is the ServerActivator reference obtained ?
102
// - using the (local) name service
103
// - using a config value
104
// - other ?
105
}
106
107     final public void
108     destroy()
109     {
110         // destroy all created container
111
org.omg.Components.Deployment.Container[] refs = get_containers();
112         for (int i=0;i<refs.length;i++) {
113             try {
114                 //
115
refs[i].remove();
116
117                 // NOTE: deactivate container servant
118
byte[] cid = _cont_poa_service.get_id_from_ref(refs[i]);
119                 _cont_poa_service.deactivate_servant(cid);
120             } catch (org.omg.Components.RemoveFailure ex) {
121                 // ignore
122
final String JavaDoc opname = "destroy";
123                 TheLogger.debug(_class_name, opname, "IGNORE", ex);
124                 return ;
125             }
126         }
127
128         // clear list
129
_cont_refs.clear();
130
131         // destroy container POA service
132
_cont_poa_service.destroy();
133     }
134
135     //
136
// IDL:org/objectweb/corba/runtime/Servant:1.0
137
//
138

139     final public String JavaDoc
140     type_id()
141     {
142         return _type_id;
143     }
144
145     final public org.omg.PortableServer.Servant JavaDoc
146     as_native_servant()
147     {
148         return new org.omg.Components.Deployment.ComponentServerPOATie(this);
149     }
150
151     //
152
// IDL:omg.org/Components/Deployment/ComponentServer:1.0
153
//
154

155     final public org.omg.Components.ConfigValue[]
156     configuration()
157     {
158         return _configuration;
159     }
160
161     final public org.omg.Components.Deployment.ServerActivator
162     get_server_activator()
163     {
164         return _sa_ref;
165     }
166
167     final public org.omg.Components.Deployment.Container
168     create_container(org.omg.Components.ConfigValue[] config)
169     throws org.omg.Components.CreateFailure,
170            org.omg.Components.Deployment.InvalidConfiguration
171     {
172         // NOTE: currently, the container created in this way does not support
173
// ECM services, so the configuration is not used
174
// NOTE: TODO ASAP
175

176         // create instance
177
ContainerImpl cont = new ContainerImpl();
178
179         // create configuration
180
ServantConfiguration scfg = new ServantConfigurationImpl(_orb_service, _cont_poa_service);
181         cont.system_configuration_complete(scfg);
182
183         // create ref and activate servant
184
// fake oid
185
byte[] oid = Double.toString(Math.random()).getBytes();
186         org.omg.CORBA.Object JavaDoc ref = _cont_poa_service.create_ref_with_id(oid, cont.type_id());
187         _cont_poa_service.activate_servant_with_id(cont, oid);
188
189         // store
190
org.omg.Components.Deployment.Container cref = org.omg.Components.Deployment.ContainerHelper.narrow(ref);
191         _cont_refs.add(cref);
192
193         return cref;
194     }
195
196     final public void
197     remove_container(org.omg.Components.Deployment.Container cref)
198     throws org.omg.Components.RemoveFailure
199     {
200         // remove from storage
201
org.omg.Components.Deployment.Container[] refs = get_containers();
202         int i=0;
203         for (;i<refs.length;i++) {
204             if (cref._is_equivalent(refs[i])) {
205                 _cont_refs.remove(i);
206
207                 // NOTE: only call remove on the container if it was found
208
cref.remove();
209
210                 // NOTE: deactivate container servant
211
byte[] cid = _cont_poa_service.get_id_from_ref(cref);
212                 _cont_poa_service.deactivate_servant(cid);
213
214                 break;
215             }
216         }
217
218         // if it wasn't found, report removal failure
219
if (i==refs.length) {
220             throw new org.omg.Components.RemoveFailure();
221         }
222     }
223
224     final public org.omg.Components.Deployment.Container[]
225     get_containers()
226     {
227         return (org.omg.Components.Deployment.Container[])_cont_refs.toArray(new org.omg.Components.Deployment.Container[0]);
228     }
229
230     final public void
231     remove()
232     throws org.omg.Components.RemoveFailure
233     {
234         // destroy
235
destroy();
236     }
237 }
238
239
Popular Tags