KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > openccm > descriptor > componentassembly > ccm > deployer > installer > ComponentpropertiesDeployer


1 /*====================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2000-2004 INRIA - USTL - LIFL - GOAL
5 Contact: openccm@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): Briclet Frédéric.
23 Contributor(s): Jacek Cala, Philippe Merle.
24
25 ====================================================================*/

26
27
28 package org.objectweb.openccm.descriptor.componentassembly.ccm.deployer.installer;
29
30 //Import all the required packages
31
import java.util.Iterator JavaDoc;
32 import java.util.StringTokenizer JavaDoc;
33 import org.objectweb.openccm.corba.TheORB;
34 import org.objectweb.openccm.corba.TheORBSingleton;
35 import org.objectweb.openccm.descriptor.properties.beans.*;
36 import org.omg.CORBA.*;
37 import org.omg.Components.CCMObject;
38 import org.omg.DynamicAny.DynAny JavaDoc;
39 import org.omg.DynamicAny.DynAnyFactory JavaDoc;
40 import org.omg.DynamicAny.DynAnyFactoryHelper JavaDoc;
41 import org.omg.DynamicAny.DynSequence JavaDoc;
42 import org.omg.DynamicAny.DynSequenceHelper JavaDoc;
43
44 /**
45  * ComponentpropertiesDeployer is used setup all the components properties.
46  *
47  * @author <a HREF="mailto:frederic.briclet@lifl.fr">Briclet Frederic</a>
48  *
49  * @version 0.1
50  */

51 public class ComponentpropertiesDeployer
52     extends ComponentpropertiesDeployerContext {
53
54     // ==================================================================
55
//
56
// Internal state.
57
//
58
// ==================================================================
59
// ==================================================================
60
//
61
// Internals methods.
62
//
63
// ==================================================================
64
/**
65      * Internal method to setup the simple component properties. This method simply
66      * browse simple properties list and setup its
67      *
68      * @param propertiesObject The properties object to apply to component
69      * @param new_component The component to configure
70      */

71     private void
72     setupSimpleProperty(
73         PropertiesBean propertiesObject,
74         org.omg.Components.CCMObject new_component)
75     throws Exception JavaDoc
76     {
77         //browse all the component simple properties
78
for (java.util.Iterator JavaDoc i = propertiesObject.getSimpleList().iterator();
79             i.hasNext();
80             ) {
81             SimpleBean property = (SimpleBean) i.next();
82             // build a set request
83
org.omg.CORBA.Request JavaDoc request =
84                 new_component._request("_set_" + property.getName());
85             //Process the simple property
86
processSimple(property, request.add_in_arg());
87             // invocation of the set request
88
request.invoke();
89         }
90     }
91
92     /**
93      * Internal method to setup the sequence property. This methods browse the sequence
94      * list property and recursivly setup the imbricated properties
95      *
96      * @param new_component THe component to setup
97      * @param propertiesObject The component properties to apply
98      */

99     private void
100     setupSequenceListProperty(
101         CCMObject new_component,
102         PropertiesBean propertiesObject)
103     throws Exception JavaDoc
104     {
105         for (java.util.Iterator JavaDoc i =
106             propertiesObject.getSequenceList().iterator();
107             i.hasNext();)
108            {
109             SequenceBean property = (SequenceBean) i.next();
110
111             org.omg.CORBA.Request JavaDoc request =
112                 new_component._request("_set_" + property.getName());
113
114             NVList nvlist = request.arguments();
115             nvlist.add_value(
116                 property.getName(),
117                 processSequence(property).to_any(),
118                 ARG_IN.value);
119             request.invoke();
120         }
121     }
122
123     /**
124      * Retrieve the TCKind object relative to the typeCodeID
125      *
126      * @param typeCodeId the stringfied typecode to process
127      * @return the binded TCKind
128      */

129     private TCKind
130     getTCKing(String JavaDoc typeCodeId)
131     {
132
133         //System.out.println("Type rechercher :"+typeCodeId);
134
if (typeCodeId.equals("boolean"))
135             return org.omg.CORBA.TCKind.tk_boolean;
136         else if (typeCodeId.equals("char"))
137             return org.omg.CORBA.TCKind.tk_char;
138         else if (typeCodeId.equals("double"))
139             return org.omg.CORBA.TCKind.tk_double;
140         else if (typeCodeId.equals("float"))
141             return org.omg.CORBA.TCKind.tk_float;
142         else if (typeCodeId.equals("short"))
143             return org.omg.CORBA.TCKind.tk_short;
144         else if (typeCodeId.equals("long"))
145             return org.omg.CORBA.TCKind.tk_long;
146         else if (typeCodeId.equals("objref"))
147             return org.omg.CORBA.TCKind.tk_objref;
148         else if (typeCodeId.equals("octet"))
149             return org.omg.CORBA.TCKind.tk_octet;
150         else if (typeCodeId.equals("string"))
151             return org.omg.CORBA.TCKind.tk_string;
152         else if (typeCodeId.equals("ulong"))
153             return org.omg.CORBA.TCKind.tk_ulong;
154         else if (typeCodeId.equals("ushort"))
155             return org.omg.CORBA.TCKind.tk_ushort;
156         else if (typeCodeId.equals("sequence"))
157             return org.omg.CORBA.TCKind.tk_sequence;
158         else if (typeCodeId.equals("struct"))
159             return org.omg.CORBA.TCKind.tk_struct;
160         return null; // UNKNOWN type
161

162     }
163     
164     /**
165      * This method return the TypeCode relative to the given string
166      * representation.
167      *
168      * @param type_identifier The type to found
169      * @return The right TypeCode
170      */

171     private TypeCode
172     getTypeCode(String JavaDoc type_identifier)
173     {
174         return TheORB.getORB().get_primitive_tc(getTCKing(type_identifier));
175     }
176
177     /**
178      * This method provide the TypeCode of a sequence relative to a stringified
179      * type representation.
180      * The format expected of the parameter is sequence:sequence:..:primitive_type
181      * which represent a sequence of sequence of primitive type.
182      *
183      * @param type The well formated sequence type in a string format
184      * @return The relative TypeCode to the given type
185      */

186     private TypeCode
187     getSequenceTypeCode(String JavaDoc type)
188     {
189         return getSequenceType(new StringTokenizer JavaDoc(type, ":"));
190     }
191
192     /**
193      * Récursive method call to build imbricated sequence TypeCode.
194      *
195      * @param tok The token to analyse
196      * @return The TypeCode relative to the current token
197      */

198     private TypeCode
199     getSequenceType(StringTokenizer JavaDoc tok)
200     {
201         String JavaDoc ch = tok.nextToken();
202         if (ch.equals("sequence"))
203             return TheORB.getORB().create_sequence_tc(0, getSequenceType(tok));
204         else
205             return getTypeCode(ch);
206     }
207     
208     /**
209      * This method setup the given @param ds DynSequence with
210      * the given @param si SequenceBean. This method process
211      * recursively the imbricated sequence.
212      *
213      * @param si The sequencebean to analyse
214      * @param ds The DynSequence to setup
215      * @throws Exception is thrown in problem case
216      */

217     private void
218     processSequence(SequenceBean si, DynSequence JavaDoc ds)
219     throws Exception JavaDoc
220     {
221         if (si.getSimpleList() != null && si.getSimpleList().size() != 0) {
222
223             ds.set_length(si.getSimpleList().size());
224
225             for (java.util.Iterator JavaDoc i = si.getSimpleList().iterator();
226                 i.hasNext();
227                 ds.next()) {
228                 SimpleBean property = (SimpleBean) i.next();
229                 DynAny JavaDoc any = ds.current_component(); //TheORB.create_any();
230
processSimple(property, any);
231             }
232             return;
233         }
234         ds.set_length(si.getSequenceList().size());
235
236         for (java.util.Iterator JavaDoc i = si.getSequenceList().iterator();
237             i.hasNext();
238             ds.next()) {
239             SequenceBean seqBean = (SequenceBean) i.next();
240             processSequence(
241                 seqBean,
242                 DynSequenceHelper.narrow(ds.current_component()));
243         }
244         ds.rewind();
245     }
246     
247     /**
248      * This method return the DynSequence relative to the given @param si
249      * SequenceBean configuration.
250      *
251      * @param si The configuration information
252      * @return The DynSequence to use for configuring a CORBA object
253      * @throws Exception is thrown in problem case
254      */

255     private DynSequence JavaDoc
256     processSequence(SequenceBean si)
257     throws Exception JavaDoc
258     {
259         isValid(si,si.getType());
260         
261         DynAnyFactory JavaDoc daf =
262             DynAnyFactoryHelper.narrow(
263                 TheORBSingleton.resolve_initial_reference("DynAnyFactory"));
264
265         DynAny JavaDoc da =
266             daf.create_dyn_any_from_type_code(
267                 getSequenceTypeCode(si.getType().trim()));
268         DynSequence JavaDoc ds = DynSequenceHelper.narrow(da);
269
270         processSequence(si, ds);
271         return ds;
272     }
273
274     /*private void processStruct(SimpleBean property ){
275       }*/

276
277     /**
278      * Internal method to fix a simple simple property
279      *
280      * @param property The simple property to fix
281      * @param any The any object to configure
282      * @return the any object configurated
283      **/

284     private Any JavaDoc
285     processSimple(SimpleBean property, Any JavaDoc any)
286     throws Exception JavaDoc
287     {
288     
289         if (property.getType().equals("double"))
290             any.insert_longlong(Long.parseLong(property.getValue().getValue()));
291         //else if(property.getType().equals( "objref"))
292
// any.insert_Object(property.getValue().getValue());
293
else if (property.getType().equals("boolean"))
294             any.insert_boolean(
295 //
296
// Bug #300826
297
// Contributors: Jacek Cala - Philippe Merle
298
//
299
// Previous code:
300
//
301
// Boolean.getBoolean(property.getValue().getValue()));
302
//
303
// New code:
304

305                 Boolean.valueOf(property.getValue().getValue()).booleanValue());
306
307 //
308
else if (property.getType().equals("char"))
309             any.insert_char(property.getValue().getValue().charAt(0));
310         else if (property.getType().equals("float"))
311             any.insert_float(Integer.parseInt(property.getValue().getValue()));
312         else if (property.getType().equals("short"))
313             any.insert_short(Short.parseShort(property.getValue().getValue()));
314         else if (property.getType().equals("long"))
315             any.insert_long(Integer.parseInt(property.getValue().getValue()));
316         else if (property.getType().equals("octet"))
317             any.insert_octet(Byte.parseByte(property.getValue().getValue()));
318         else if (property.getType().equals("string"))
319             any.insert_string(property.getValue().getValue());
320         else if (property.getType().equals("ulong"))
321             any.insert_ulong(Integer.parseInt(property.getValue().getValue()));
322         else if (property.getType().equals("ushort"))
323             any.insert_ushort(Short.parseShort(property.getValue().getValue()));
324
325         return any;
326     }
327
328     /**
329        * Internal method to fix a simple simple property in a DynAny
330        *
331        * @param property The simple property to fix
332        * @param any The any object to configure
333        * @return the any object configurated
334        **/

335     private void
336     processSimple(SimpleBean property, DynAny JavaDoc any)
337     throws Exception JavaDoc
338     {
339         //Any any=org.objectweb.openccm.corba.TheORB.getORB().create_any();
340
if (property.getType().equals("double"))
341             any.insert_longlong(Long.parseLong(property.getValue().getValue()));
342         //else if(property.getType().equals( "objref"))
343
// any.insert_Object(property.getValue().getValue());
344
else if (property.getType().equals("boolean"))
345             any.insert_boolean(
346 //
347
// Bug #300826
348
// Contributors: Jacek Cala - Philippe Merle
349
//
350
// Previous code:
351
//
352
// Boolean.getBoolean(property.getValue().getValue()));
353
//
354
// New code:
355

356                 Boolean.valueOf(property.getValue().getValue()).booleanValue());
357
358 //
359
else if (property.getType().equals("char"))
360             any.insert_char(property.getValue().getValue().charAt(0));
361         else if (property.getType().equals("float"))
362             any.insert_float(Integer.parseInt(property.getValue().getValue()));
363         else if (property.getType().equals("short"))
364             any.insert_short(Short.parseShort(property.getValue().getValue()));
365         else if (property.getType().equals("long"))
366             any.insert_long(Integer.parseInt(property.getValue().getValue()));
367         else if (property.getType().equals("octet"))
368             any.insert_octet(Byte.parseByte(property.getValue().getValue()));
369         else if (property.getType().equals("string"))
370             any.insert_string(property.getValue().getValue());
371         else if (property.getType().equals("ulong"))
372             any.insert_ulong(Integer.parseInt(property.getValue().getValue()));
373         else if (property.getType().equals("ushort"))
374             any.insert_ushort(Short.parseShort(property.getValue().getValue()));
375
376         //return any;
377
}
378     
379     private void
380     isValid(SequenceBean sb,String JavaDoc typeToRespect)
381     throws ComponentpropertiesMalFormedException
382     {
383
384         if(!sb.getType().equals(typeToRespect))
385             throw new ComponentpropertiesMalFormedException(typeToRespect,sb.getType());
386             
387         String JavaDoc following=typeToRespect.substring(typeToRespect.indexOf(":")+1);
388         
389        /* if(sb.getSequenceList()!=null&&sb.getSimpleList()!=null)
390             return false;*/

391         if(sb.getSequenceList()!=null)
392             for(Iterator JavaDoc i=sb.getSequenceList().iterator();i.hasNext();)
393                isValid((SequenceBean)i.next(),following);
394                     
395         else if(sb.getSimpleList()!=null)
396             for(Iterator JavaDoc it=sb.getSimpleList().iterator();it.hasNext();)
397                isValid((SimpleBean)it.next(),following);
398         
399     }
400     
401     private void
402     isValid(SimpleBean sb,String JavaDoc typeToRespect)
403     throws ComponentpropertiesMalFormedException
404     {
405         if(!sb.getType().equals(typeToRespect))
406           throw new ComponentpropertiesMalFormedException(typeToRespect,sb.getType());
407     }
408     
409     // ==================================================================
410
//
411
// Public methods.
412
//
413
// ==================================================================
414

415     public class ComponentpropertiesMalFormedException extends Exception JavaDoc{
416         
417         public
418         ComponentpropertiesMalFormedException(String JavaDoc excepted, String JavaDoc found){
419             super("The componentproperties seem to be corrupted, imbricated"+
420                   " properties were expected to be ["+excepted+"] and it was found"+
421                   " a ["+found+"]");
422         }
423     }
424     /**
425      * This method setup the component properties.
426      * The first part process the simple properties, the second part process
427      * the sequence properties.
428      * TODO: structure setup process
429      *
430      * @param new_component the component to setup
431      */

432     public void
433     setupComponentProperties(
434         org.omg.Components.CCMObject new_component)
435     throws ComponentPropertiesSetupFailureException
436     {
437         try {
438             java.io.InputStreamReader JavaDoc reader =
439                 getFileinarchiveDeployer().getInputStreamReader();
440
441             // retrieving the properties object descriptor
442
PropertiesBean propertiesObject =
443                 PropertiesBeanImpl.unmarshalBean(reader, true);
444             //setup the simple properties
445
setupSimpleProperty(propertiesObject, new_component);
446             //setup the sequence properties
447
setupSequenceListProperty(new_component, propertiesObject);
448         } catch (Exception JavaDoc e) {
449             e.printStackTrace();
450             throw new ComponentPropertiesSetupFailureException(this, e);
451         }
452     }
453
454 }
Popular Tags