KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > openccm > pss > generator > common > lib > StorageTypeMapping


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): Christophe Demarey.
23 Contributor(s): ________________________________________.
24
25 ====================================================================*/

26
27 package org.objectweb.openccm.pss.generator.common.lib;
28
29 // Package dependencies
30
/** To access AST. */
31 import org.objectweb.openccm.ast.api.StorageTypeDecl;
32 import org.objectweb.openccm.ast.api.AbstractStorageTypeDecl;
33 import org.objectweb.openccm.ast.api.StorageTypeStateMemberDecl;
34 import org.objectweb.openccm.ast.api.PsdlOperationDecl;
35 import org.objectweb.openccm.ast.api.DeclarationKind;
36 import org.objectweb.openccm.ast.api.Declaration;
37 /** To access Java AST. */
38 import org.objectweb.openccm.generator.java.ast.api.*;
39 import org.objectweb.openccm.generator.java.ast.lib.*;
40 /** Others. */
41 import org.objectweb.openccm.generator.common.lib.GenerationException;
42 import org.objectweb.openccm.generator.common.api.GeneratorBase;
43 import org.objectweb.openccm.pss.generator.common.api.PSDL2JavaGenerator;
44 import org.objectweb.openccm.pss.generator.common.api.StateMemberMapping;
45
46 import java.util.List JavaDoc;
47
48
49 /**
50  * This class generates Storage Type Mapping.
51  *
52  * @author <a HREF="mailto:Christophe.Demarey@lifl.fr">Christophe Demarey</a>
53  *
54  * @version 0.1
55  */

56
57 public abstract class StorageTypeMapping
58            implements org.objectweb.openccm.pss.generator.common.api.StorageTypeMapping
59 {
60
61     // ==================================================================
62
//
63
// Internal states.
64
//
65
// ==================================================================
66

67     /** The storage type declaration. */
68     private StorageTypeDecl st_;
69
70     /** All state members to map. */
71     private java.util.List JavaDoc all_states_;
72
73     /** Read only state members. */
74     private java.util.List JavaDoc read_states_;
75
76     // ==================================================================
77
//
78
// Constructors.
79
//
80
// ==================================================================
81

82     /**
83      * The default constructor.
84      */

85     public StorageTypeMapping()
86     {
87         // Init internal state
88
st_ = null;
89         read_states_ = new java.util.ArrayList JavaDoc();
90         all_states_ = null;
91     }
92
93     // ==================================================================
94
//
95
// Internal methods.
96
//
97
// ==================================================================
98

99     /**
100      * Has this abstract storage type an operation declaration ?
101      * (including implemented abstract storage types)
102      *
103      * @param ast - The abstract storage type to test.
104      *
105      * @return True if an operation is contained, else false.
106      **/

107     private boolean
108     hasOperation(AbstractStorageTypeDecl ast)
109     {
110         Declaration[] decls = null;
111         decls = ast.getContents(true, DeclarationKind.dk_psdl_operation);
112         if (decls.length > 0)
113         {
114             return true;
115         }
116         else
117         {
118             // Check implemented abstract storage types
119
AbstractStorageTypeDecl[] implemented = null;
120
121             implemented = ast.getAbstractStorageTypeList().getAbstractStorageTypes();
122             for (int i=0; i<implemented.length; i++)
123             {
124                 if ( hasOperation(implemented[i]) )
125                     return true;
126             }
127             return false;
128         }
129     }
130
131     /**
132      * Map state members.
133      *
134      * @param state_members - State Mebers to map.
135      * @param clazz - The abstract storage type mapping class.
136      * @param ct - The abstract storage type constructor mapping.
137      * @param generator - The PSDL to Java generator.
138      **/

139     private void
140     mapStateMembers(StorageTypeStateMemberDecl[] state_members,
141                     ClassObject clazz,
142                     ConstructorObject ct,
143                     PSDL2JavaGenerator generator)
144     {
145         for (int i=0; i<state_members.length; i++)
146         {
147             StateMemberMapping state = null;
148             
149             state = (StateMemberMapping) MappingObjectFactory.getClass("StateMemberMapping");
150             state.setStateMember(state_members[i]);
151
152             state.toJavaAccessors(generator.getTranslator(), clazz, true);
153             state.toJavaAttribute(generator.getTranslator(), clazz, st_);
154             if ( state_members[i].isReadonly() )
155             {
156                 state.toJavaParameter(generator.getTranslator(), ct);
157                 read_states_.add(state_members[i].getName());
158                 state.toJavaReadOnlyStateMemberModifier(generator.getTranslator(), clazz);
159             }
160        }
161     }
162
163     /**
164      * Add Instance Callbacks methods to this storage type.
165      *
166      * @param generator - The PSDL to Java generator.
167      * @param clazz - The abstract storage type mapping class.
168      **/

169     abstract protected void
170     addInstanceCallbacks( PSDL2JavaGenerator generator,
171                           ClassObject clazz );
172
173     /**
174      * Get The base class for JDO Storage Type Mapping.
175      *
176      * @return The base class name.
177      */

178     abstract protected String JavaDoc
179     getStorageTypeBaseClass();
180
181     // ==================================================================
182
//
183
// Public methods.
184
//
185
// ==================================================================
186

187    /**
188     * Set the Storage Type Declaration to map.
189     *
190     * @param st - The Storage Type Declaration to map.
191     */

192     public void
193     setStorageType(StorageTypeDecl st)
194     {
195         st_ = st;
196     }
197
198     /**
199      * Get the list of all state members that will be mapped.
200      *
201      * @param generator - A base generator to get declarationss.
202      *
203      * @return The list of all state members.
204      **/

205     public java.util.List JavaDoc
206     getAllStates(GeneratorBase generator)
207     {
208         AbstractStorageTypeDecl[] impl_directly = null;;
209         List JavaDoc vect = null;
210
211         if (all_states_ == null)
212         {
213             all_states_ = new java.util.ArrayList JavaDoc();
214
215             // Add State Members implemented directly
216
impl_directly = st_.getDirectlyImplementedAbstractStorageTypes();
217             for (int j=0; j<impl_directly.length; j++)
218             {
219                 try{
220                     vect = generator.getDeclarations(impl_directly[j], DeclarationKind.dk_storage_type_state_member);
221                 }catch(org.objectweb.openccm.generator.common.lib.GenerationException ex){
222                     ex.printStackTrace();
223                 }
224                 all_states_.addAll( vect );
225             }
226
227             // Add own State Members
228
try{
229                 vect = generator.getDeclarations(st_, DeclarationKind.dk_storage_type_state_member);
230             }catch(org.objectweb.openccm.generator.common.lib.GenerationException ex){
231                 ex.printStackTrace();
232             }
233             all_states_.addAll( vect );
234         }
235         return all_states_;
236     }
237
238     /**
239      * Get the list of all state members that will be mapped to native types.
240      *
241      * @param generator - A base generator to get declarationss.
242      *
243      * @return The list of all state members mapped to native types.
244      **/

245     public java.util.List JavaDoc
246     getStatesToNative(GeneratorBase generator)
247     {
248         StorageTypeStateMemberDecl state = null;
249         String JavaDoc mapping_type = null;
250         java.util.List JavaDoc to_native = null;
251         java.util.Iterator JavaDoc it = null;
252
253         to_native = new java.util.ArrayList JavaDoc();
254         it = getAllStates(generator).iterator();
255         while (it.hasNext())
256         {
257             state = (StorageTypeStateMemberDecl) it.next();
258             mapping_type = PSDLMappingTools.getMarshalledType(st_, state);
259             if (mapping_type == null)
260             {
261                 to_native.add(state);
262             }
263         }
264         return to_native;
265     }
266
267     /**
268      * Get the list of all state members that will be mapped to an IOR.
269      *
270      * @param generator - A base generator to get declarationss.
271      *
272      * @return The list of all state members mapped to an IOR.
273      **/

274     public java.util.List JavaDoc
275     getStatesToIOR(GeneratorBase generator)
276     {
277         StorageTypeStateMemberDecl state = null;
278         String JavaDoc mapping_type = null;
279         java.util.List JavaDoc to_ior = null;
280         java.util.Iterator JavaDoc it = null;
281
282         to_ior = new java.util.ArrayList JavaDoc();
283         it = getAllStates(generator).iterator();
284         while (it.hasNext())
285         {
286             state = (StorageTypeStateMemberDecl) it.next();
287             mapping_type = PSDLMappingTools.getMarshalledType(st_, state);
288             if ( (mapping_type != null) &&
289                  (mapping_type.compareTo("java.lang.String") == 0) )
290             {
291                 to_ior.add(state);
292             }
293         }
294         return to_ior;
295     }
296
297     /**
298      * Get the list of all state members that will be mapped to a byte array.
299      *
300      * @param generator - A base generator to get declarationss.
301      *
302      * @return The list of all state members mapped to a byte array.
303      **/

304     public java.util.List JavaDoc
305     getStatesToCodec(GeneratorBase generator)
306     {
307         StorageTypeStateMemberDecl state = null;
308         String JavaDoc mapping_type = null;
309         java.util.List JavaDoc to_codec = null;
310         java.util.Iterator JavaDoc it = null;
311
312         to_codec = new java.util.ArrayList JavaDoc();
313         it = getAllStates(generator).iterator();
314         while (it.hasNext())
315         {
316             state = (StorageTypeStateMemberDecl) it.next();
317             mapping_type = PSDLMappingTools.getMarshalledType(st_, state);
318             if ( (mapping_type != null) &&
319                  (mapping_type.compareTo("byte[]") == 0) )
320             {
321                 to_codec.add(state);
322             }
323         }
324         return to_codec;
325     }
326
327     /**
328      * Get the list of all state members that will be mapped to a byte array.
329      *
330      * @param generator - A base generator to get declarationss.
331      *
332      * @return The list of all state members mapped to a byte array.
333      **/

334     public java.util.List JavaDoc
335     getStatesToClass(GeneratorBase generator)
336     {
337         StorageTypeStateMemberDecl state = null;
338         String JavaDoc mapping_type = null;
339         java.util.List JavaDoc to_class = null;
340         java.util.Iterator JavaDoc it = null;
341
342         to_class = new java.util.ArrayList JavaDoc();
343         it = getAllStates(generator).iterator();
344         while (it.hasNext())
345         {
346             state = (StorageTypeStateMemberDecl) it.next();
347             mapping_type = PSDLMappingTools.getMarshalledType(st_, state);
348             if ( (mapping_type != null) &&
349                  (mapping_type.compareTo("java.lang.String") != 0) &&
350                  (mapping_type.compareTo("byte[]") != 0) )
351             {
352                 to_class.add(state);
353             }
354         }
355         return to_class;
356     }
357
358     /**
359      * Generate a java persistent capable class for this storage type.
360      *
361      * @param storage_object - If true, generates mapping for a storage object.
362      * @param generator - The PSDL to Java generator.
363      */

364     public void
365     toJava( boolean storage_object,
366             PSDL2JavaGenerator generator )
367     throws GenerationException
368     {
369         ClassObject clazz = null;
370         ConstructorObject ct = null;
371         MethodObject method = null;
372         AttributeObject att = null;
373         ParameterObject param = null;
374         AbstractStorageTypeDecl[] implemented = null,
375                                   impl_directly = null;;
376         StorageTypeDecl base = null;
377         List JavaDoc vect = null;
378         StorageTypeStateMemberDecl[] state_members = null;
379         PsdlOperationDecl[] operations = null;
380
381         read_states_ = new java.util.ArrayList JavaDoc();
382
383         clazz = new ClassObjectImpl(st_.getName());
384         clazz.addComment(" Storage Type "+st_.getName());
385         clazz.addComment("");
386         clazz.addComment(" @author OpenCCM PSDL Compiler");
387         clazz.setModifier(ModifierKindImpl.mk_public);
388         clazz.setPackage(generator.getTranslator().getPackage(st_));
389
390         // Add Implemented Abstract Storage Type by st
391
implemented = st_.getAbstractStorageTypeList().getAbstractStorageTypes();
392         for (int i=0; i<implemented.length; i++)
393         {
394             clazz.addImplementedObject(generator.getTranslator().getAbsoluteName(implemented[i]));
395         }
396
397         // Check if any of the abstract storagetypes implemented declares an operation
398
int i = 0;
399         boolean found = false;
400         clazz.setAbstract(false);
401         while ((i<implemented.length) && !found)
402         {
403             if (hasOperation(implemented[i]))
404             {
405                 clazz.setAbstract(true);
406                 found = true;
407             }
408             i++;
409         }
410
411         // Add Inherited Storage Type if there is one
412
base = st_.getBaseStorageType();
413         if (base != null)
414             clazz.addInheritedObject(generator.getTranslator().getAbsoluteName(base));
415         else if (storage_object)
416             clazz.addInheritedObject( getStorageTypeBaseClass() );
417
418         // Add the default constructor
419
ct = new ConstructorObjectImpl();
420         ct.getImpl().setMacro("ST_CONSTRUCTOR");
421         clazz.addConstructor(ct);
422
423         if (storage_object)
424         {
425             // Add the sh_rid attribute
426
att = new AttributeObjectImpl();
427             att.addComment("The Storage Home Repository ID.");
428             att.setName("sh_rid");
429             att.setType("String");
430             att.setModifier( ModifierKindImpl.mk_private );
431             clazz.addAttribute(att);
432
433             // Override the initialize method
434
method = new MethodObjectImpl();
435             method.addComment("Initialize the storage object.");
436             method.setName("initialize");
437             method.setReturnType("void");
438             param = new ParameterObjectImpl();
439             param.setName("storage_home");
440             param.setType("org.omg.CosPersistentState.StorageHomeBase");
441             method.addParameter(param);
442             param = new ParameterObjectImpl();
443             param.setName("pid");
444             param.setType("byte[]");
445             method.addParameter(param);
446             method.getImpl().setMacro("ST_INITIALIZE_METHOD");
447             clazz.addMethod(method);
448         }
449
450         java.util.List JavaDoc all_states = getAllStates(generator);
451         state_members = (StorageTypeStateMemberDecl[])all_states.toArray(new StorageTypeStateMemberDecl[0]);
452         mapStateMembers(state_members, clazz, ct, generator);
453
454         // Complete the storage type constructor
455
ct.getImpl().addContextValue("read_states", read_states_);
456         // Add store directives
457
vect = generator.getDeclarations(st_, DeclarationKind.dk_storage_type_store_directive);
458         ct.getImpl().addContextValue("store_directives", vect);
459         ct.getImpl().addContextValue("translator", generator.getTranslator());
460
461         addInstanceCallbacks(generator, clazz);
462
463         generator.getJavaRepository().addObject(clazz);
464     }
465 }
466
Popular Tags