KickJava   Java API By Example, From Geeks To Geeks.

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


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.StorageHomeDecl;
32 import org.objectweb.openccm.ast.api.AbstractStorageHomeDecl;
33 import org.objectweb.openccm.ast.api.AbstractStorageTypeDecl;
34 import org.objectweb.openccm.ast.api.StorageTypeDecl;
35 import org.objectweb.openccm.ast.api.StorageHomeKeyDecl;
36 import org.objectweb.openccm.ast.api.StorageHomeFactoryDecl;
37 import org.objectweb.openccm.ast.api.StorageTypeStateMemberDecl;
38 import org.objectweb.openccm.ast.api.DeclarationKind;
39 import org.objectweb.openccm.ast.api.Declaration;
40 /** To access Java AST. */
41 import org.objectweb.openccm.generator.java.ast.api.*;
42 import org.objectweb.openccm.generator.java.ast.lib.*;
43 /** Others. */
44 import org.objectweb.openccm.generator.common.lib.GenerationException;
45 import org.objectweb.openccm.pss.generator.common.api.PSDL2JavaGenerator;
46
47 import java.util.List JavaDoc;
48
49
50 /**
51  * This class generates java mapping for a PSDL Storage Home.
52  *
53  * @author <a HREF="mailto:Christophe.Demarey@lifl.fr">Christophe Demarey</a>
54  *
55  * @version 0.1
56  */

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

68     /** The Storage Home declaration. */
69     private StorageHomeDecl sh_;
70
71     // ==================================================================
72
//
73
// Constructors.
74
//
75
// ==================================================================
76

77     /**
78      * The default constructor.
79      */

80
81     public StorageHomeMapping()
82     {
83         // Init internal state
84
sh_ = null;
85     }
86
87     // ==================================================================
88
//
89
// Internal methods.
90
//
91
// ==================================================================
92

93     /**
94      * Has this abstract storage home an operation declaration ?
95      * (including implemented abstract storage homes)
96      *
97      * @param ash - The abstract storage home to test.
98      *
99      * @return True if an operation is contained, else false.
100      **/

101     private boolean
102     hasOperation(AbstractStorageHomeDecl ash)
103     {
104         Declaration[] decls = null;
105         decls = ash.getContents(true, DeclarationKind.dk_psdl_operation);
106         if (decls.length > 0)
107         {
108             return true;
109         }
110         else
111         {
112             // Check implemented abstract storage types
113
AbstractStorageHomeDecl[] implemented = null;
114
115             implemented = ash.getAbstractStorageHomeList().getAbstractStorageHomes();
116             for (int i=0; i<implemented.length; i++)
117             {
118                 if ( hasOperation(implemented[i]) )
119                     return true;
120             }
121             return false;
122         }
123     }
124
125     /**
126      * Map Storage Home create Operations into Java methods.
127      *
128      * @param generator - The PSDL to Java generator.
129      * @param obj - The StorageHome mapping object.
130      */

131     private void
132     mapStorageHomeCreateOperations( PSDL2JavaGenerator generator,
133                                     ClassObject obj )
134     throws GenerationException
135     {
136         MethodObject method = null;
137         ParameterObject param = null;
138         StorageTypeDecl managed = null,
139                         managed_base = null;
140         AbstractStorageTypeDecl[] ast_impl_directly = null;;
141         StorageTypeStateMemberDecl[] all_states = null;
142         List JavaDoc vect = new java.util.ArrayList JavaDoc();
143         java.util.List JavaDoc states = null;
144
145
146         managed = sh_.getStorageType();
147
148         /* Get all storagetype's state members */
149         // get managed storagetype state members
150
managed_base = managed.getBaseStorageType();
151         if (managed_base != null)
152         {
153             vect.addAll( generator.getDeclarations(managed_base, DeclarationKind.dk_storage_type_state_member) );
154             all_states = (StorageTypeStateMemberDecl[])vect.toArray(new StorageTypeStateMemberDecl[0]);
155         }
156         // get state members implemented directly
157
ast_impl_directly = managed.getAllImplementedAbstractStorageTypes().getAbstractStorageTypes();
158         for (int j=0; j<ast_impl_directly.length; j++)
159         {
160             vect.addAll(
161                 generator.getDeclarations(ast_impl_directly[j], DeclarationKind.dk_storage_type_state_member) );
162         }
163         // get own state members
164
vect.addAll( generator.getDeclarations(managed, DeclarationKind.dk_storage_type_state_member) );
165         all_states = (StorageTypeStateMemberDecl[])vect.toArray(new StorageTypeStateMemberDecl[0]);
166
167         /* The generic create method (not in api) */
168         method = new MethodObjectImpl();
169         method.addComment("The generic create method.");
170         method.setName("create_st_incarnation");
171         method.setReturnType( generator.getTranslator().getAbsoluteName(managed) );
172         method.getImpl().setMacro("SH_CREATE_ST_INCARNATION");
173         method.getImpl().addContextValue("st_type", generator.getTranslator().getAbsoluteName(managed));
174         obj.addMethod(method);
175
176         /* Create method that takes a parameter for each of its storagetype's
177            state members and returns an incarnation */

178         method = new MethodObjectImpl();
179         method.addComment("A create method.");
180         method.setName("_create");
181         method.setReturnType( generator.getTranslator().getAbsoluteName(managed) );
182         states = new java.util.ArrayList JavaDoc();
183         for (int i=0; i<all_states.length; i++)
184         {
185             org.objectweb.openccm.pss.generator.common.api.StateMemberMapping state = null;
186             
187             state = (org.objectweb.openccm.pss.generator.common.api.StateMemberMapping)
188                             MappingObjectFactory.getClass("StateMemberMapping");
189             state.setStateMember(all_states[i]);
190             state.toJavaParameter(generator.getTranslator(), method);
191             states.add(all_states[i]);
192         }
193         method.getImpl().setMacro("SH_CREATE1_METHOD");
194         method.getImpl().addContextValue("states", states);
195         method.getImpl().addContextValue("st_type", generator.getTranslator().getAbsoluteName(managed));
196         obj.addMethod(method);
197
198         /* Create method that takes a parameter for each of its storagetype's
199            reference representation member. */

200         method = new MethodObjectImpl();
201         method.addComment("A create method.");
202         method.setName("_create");
203         method.setReturnType( generator.getTranslator().getAbsoluteName(managed) );
204         states = new java.util.ArrayList JavaDoc();
205         for (int i=0; i<all_states.length; i++)
206         {
207             if (all_states[i].getType().isRef())
208             {
209                 org.objectweb.openccm.pss.generator.common.api.StateMemberMapping state = null;
210             
211                 state = (org.objectweb.openccm.pss.generator.common.api.StateMemberMapping)
212                                 MappingObjectFactory.getClass("StateMemberMapping");
213                 state.setStateMember(all_states[i]);
214                 state.toJavaParameter(generator.getTranslator(), method);
215                 states.add(all_states[i]);
216             }
217         }
218         method.getImpl().setMacro("SH_CREATE1_METHOD");
219         method.getImpl().addContextValue("states", states);
220         method.getImpl().addContextValue("st_type", generator.getTranslator().getAbsoluteName(managed));
221         obj.addMethod(method);
222
223         /* Create method that takes a parameter for each of its storagetype's
224            state members plus a CosPersistentState.YielRef parameter
225            and returns a reference. */

226         method = new MethodObjectImpl();
227         method.addComment("A create method.");
228         method.addComment(" ");
229         method.setName("_create");
230         method.setReturnType("byte[]");
231         states = new java.util.ArrayList JavaDoc();
232         for (int i=0; i<all_states.length; i++)
233         {
234             org.objectweb.openccm.pss.generator.common.api.StateMemberMapping state = null;
235             
236             state = (org.objectweb.openccm.pss.generator.common.api.StateMemberMapping)
237                             MappingObjectFactory.getClass("StateMemberMapping");
238             state.setStateMember(all_states[i]);
239             state.toJavaParameter(generator.getTranslator(), method);
240             states.add(all_states[i]);
241         }
242         param = new ParameterObjectImpl();
243         param.setName("yr");
244         param.setType("org.omg.CosPersistentState.YieldRef");
245         method.addParameter(param);
246         method.getImpl().setMacro("SH_CREATE2_METHOD");
247         method.getImpl().addContextValue("states", states);
248         method.getImpl().addContextValue("st_type", generator.getTranslator().getAbsoluteName(managed));
249         obj.addMethod(method);
250
251         /* Create method that takes a parameter for each of its storagetype's
252            state members plus a CosPersistentState.YielRef parameter
253            reference representation member. */

254         method = new MethodObjectImpl();
255         method.addComment("A create method.");
256         method.setName("_create");
257         method.setReturnType("byte[]");
258         states = new java.util.ArrayList JavaDoc();
259         for (int i=0; i<all_states.length; i++)
260         {
261             if (all_states[i].getType().isRef())
262             {
263                 org.objectweb.openccm.pss.generator.common.api.StateMemberMapping state = null;
264             
265                 state = (org.objectweb.openccm.pss.generator.common.api.StateMemberMapping)
266                                 MappingObjectFactory.getClass("StateMemberMapping");
267                 state.setStateMember(all_states[i]);
268
269                 state.toJavaParameter(generator.getTranslator(), method);
270                 states.add(all_states[i]);
271             }
272         }
273         param = new ParameterObjectImpl();
274         param.setName("yr");
275         param.setType("org.omg.CosPersistentState.YieldRef");
276         method.addParameter(param);
277         method.getImpl().setMacro("SH_CREATE2_METHOD");
278         method.getImpl().addContextValue("states", states);
279         method.getImpl().addContextValue("st_type", generator.getTranslator().getAbsoluteName(managed));
280         obj.addMethod(method);
281     }
282     
283     /**
284      * Get The base class for Storage Home Mapping.
285      *
286      * @return The base class name.
287      */

288     abstract protected String JavaDoc
289     getStorageHomeBaseClass();
290
291     // ==================================================================
292
//
293
// Public methods.
294
//
295
// ==================================================================
296

297     /**
298      * Set the Storage Home Declaration to map.
299      *
300      * @param sh - The Storage Home Declaration to map.
301      */

302      public void
303      setStorageHome(StorageHomeDecl sh)
304      {
305          sh_ = sh;
306      }
307
308     /**
309      * Map a Storage Home definition into a java Class.
310      *
311      * @param generator - The PSDL to Java generator.
312      */

313     public void
314     toJava(PSDL2JavaGenerator generator)
315     throws GenerationException
316     {
317         ClassObject clazz = null;
318         ConstructorObject ct = null;
319         MethodObject method = null;
320         AttributeObject att = null;
321         ParameterObject param = null;
322         AbstractStorageHomeDecl[] implemented = null,
323                                   impl_directly = null;;
324         StorageHomeDecl base = null;
325         StorageHomeKeyDecl[] keys = null;
326         StorageHomeFactoryDecl[] factories = null;
327         List JavaDoc vect = null;
328
329         clazz = new ClassObjectImpl(sh_.getName());
330         clazz.addComment(" Storage Home "+sh_.getName());
331         clazz.addComment("");
332         clazz.addComment(" @author OpenCCM PSDL Compiler");
333         clazz.setModifier(ModifierKindImpl.mk_public);
334         clazz.setPackage(generator.getTranslator().getPackage(sh_));
335
336         // Add Implemented Abstract Storage Home by sh
337
implemented = sh_.getAbstractStorageHomeList().getAbstractStorageHomes();
338         for (int i=0; i<implemented.length; i++)
339         {
340             clazz.addImplementedObject(generator.getTranslator().getAbsoluteName(implemented[i]));
341         }
342
343         // Add Implemented storage home base interface
344
clazz.addImplementedObject("org.objectweb.openccm.pss.runtime.common.api.StorageHomeBase");
345
346         // Check if any of the abstract storagehome implemented declares an operation
347
int i = 0;
348         boolean found = false;
349         clazz.setAbstract(false);
350         while ((i<implemented.length) && !found)
351         {
352             if (hasOperation(implemented[i]))
353             {
354                 clazz.setAbstract(true);
355                 found = true;
356             }
357             i++;
358         }
359
360         // Add Inherited Storage Home if there is one
361
base = sh_.getBaseStorageHome();
362         if (base != null)
363             clazz.addInheritedObject( generator.getTranslator().getAbsoluteName(base) );
364         else
365             clazz.addInheritedObject( getStorageHomeBaseClass() );
366
367         // Add the default constructor
368
ct = new ConstructorObjectImpl();
369         clazz.addConstructor(ct);
370
371         // Map Finder operations (i.e. key mapping) and factories
372
// of directly implemented Abstract storage home
373
impl_directly = sh_.getDirectlyImplementedAbstractStorageHomes();
374         for (int j=0; j<impl_directly.length; j++)
375         {
376             // Map factories
377
vect = generator.getDeclarations(impl_directly[j], DeclarationKind.dk_storage_home_factory);
378             factories = (StorageHomeFactoryDecl[])vect.toArray(new StorageHomeFactoryDecl[0]);
379             for (int k=0; k<factories.length; k++)
380             {
381                 org.objectweb.openccm.pss.generator.common.api.PSDLFactoryMapping factory = null;
382             
383                 factory = (org.objectweb.openccm.pss.generator.common.api.PSDLFactoryMapping)
384                                 MappingObjectFactory.getClass("PSDLFactoryMapping");
385                 factory.setFactory(factories[k]);
386                 factory.toJava(generator, clazz, impl_directly[j].getAbstractStorageType(), sh_.getStorageType());
387             }
388             // Map keys
389
vect = generator.getDeclarations(impl_directly[j], DeclarationKind.dk_storage_home_key);
390             keys = (StorageHomeKeyDecl[])vect.toArray(new StorageHomeKeyDecl[0]);
391             for (int k=0; k<keys.length; k++)
392             {
393                 org.objectweb.openccm.pss.generator.common.api.PSDLKeyMapping key = null;
394             
395                 key = (org.objectweb.openccm.pss.generator.common.api.PSDLKeyMapping)
396                                 MappingObjectFactory.getClass("PSDLKeyMapping");
397                 key.setKey(keys[k]);
398                 key.toJava(generator, clazz, impl_directly[j].getAbstractStorageType());
399             }
400         }
401
402         // Map create operations
403
mapStorageHomeCreateOperations(generator, clazz);
404
405         // Add the get_rid method
406
method = new MethodObjectImpl();
407         method.addComment("Get the Repository ID of a storage home.");
408         method.setName("get_rid");
409         method.setReturnType("java.lang.String");
410         method.getImpl().setMacro("SH_GET_RID_METHOD");
411         method.getImpl().addContextValue("sh_rid", sh_.getId());
412         clazz.addMethod(method);
413
414         // Add the get_rid method
415
method = new MethodObjectImpl();
416         method.addComment("Get the Repository ID of the managed storage type.");
417         method.setName("get_st_rid");
418         method.setReturnType("java.lang.String");
419         method.getImpl().setMacro("SH_GET_ST_RID_METHOD");
420         method.getImpl().addContextValue("st_rid", sh_.getStorageType().getId());
421         clazz.addMethod(method);
422
423         generator.getJavaRepository().addObject(clazz);
424     }
425
426 }
427
Popular Tags