KickJava   Java API By Example, From Geeks To Geeks.

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


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.StorageHomeKeyDecl;
32 import org.objectweb.openccm.ast.api.AbstractStorageTypeDecl;
33 import org.objectweb.openccm.ast.api.StorageTypeStateMemberDecl;
34 /** To access Java AST. */
35 import org.objectweb.openccm.generator.java.ast.api.*;
36 import org.objectweb.openccm.generator.java.ast.lib.*;
37 /** Others. */
38 import org.objectweb.openccm.generator.common.lib.GenerationException;
39 import org.objectweb.openccm.pss.generator.common.api.PSDL2JavaGenerator;
40 import org.objectweb.openccm.generator.translator.idl2java.api.PSDL_JavaTranslator;
41
42
43 /**
44  * This class generates the mapping for a PSDL key.
45  *
46  * @author <a HREF="mailto:Christophe.Demarey@lifl.fr">Christophe Demarey</a>
47  *
48  * @version 0.1
49  */

50 public class PSDLKeyMapping
51   implements org.objectweb.openccm.pss.generator.common.api.PSDLKeyMapping
52 {
53
54     // ==================================================================
55
//
56
// Internal state.
57
//
58
// ==================================================================
59

60     /** The PSDL key Declaration. */
61     private StorageHomeKeyDecl key_;
62
63     // ===========================================================
64
//
65
// Constructors.
66
//
67
// ===========================================================
68

69     /**
70      * The default constructor.
71      */

72
73     public PSDLKeyMapping()
74     {
75         // Init internal state
76
key_ = null;
77     }
78
79     // ==================================================================
80
//
81
// Internal methods.
82
//
83
// ==================================================================
84

85     /**
86      * Map a key definition into Java methods.
87      *
88      * @param generator - The PSDL to Java generator.
89      * @param ast - The storage type managed by the storage home.
90      * @param method - The Key mapping object.
91      */

92     protected void
93     mapFactoryParameters( PSDL_JavaTranslator translator,
94                           AbstractStorageTypeDecl ast,
95                           MethodObject method )
96     {
97         String JavaDoc[] members = null,
98                  members_type = null,
99                  states_name = null;
100         StorageTypeStateMemberDecl sm = null;
101         org.objectweb.openccm.ast.api.StorageTypeDecl st = null;
102
103         members = key_.getMemberList().getStrings();
104         members_type = new java.lang.String JavaDoc[members.length];
105         states_name = new java.lang.String JavaDoc[members.length];
106         try{
107             st = (org.objectweb.openccm.ast.api.StorageTypeDecl) ast;
108         }catch(java.lang.ClassCastException JavaDoc ex){
109             st = null;
110         }
111         for (int i=0; i<members.length; i++)
112         {
113             // Retrieve the corresponding field
114
sm = ast.getState(members[i]);
115             members_type[i] = translator.toJava( sm.getType() );
116             states_name[i] = StateMemberMapping.mapStateName( members[i] );
117             if (st == null)
118             {
119                 if ( PSDLMappingTools.getMarshalledType(st, sm) != null)
120                     states_name[i] = StateMemberMapping.mapMarshalledStateName( members[i] );
121             }
122         }
123         method.getImpl().addContextValue("fields_names", members);
124         method.getImpl().addContextValue("states_names", states_name);
125         method.getImpl().addContextValue("fields_types", members_type);
126         method.getImpl().addContextValue("nb", new Integer JavaDoc(members.length) );
127     }
128
129     // ==================================================================
130
//
131
// Public methods.
132
//
133
// ==================================================================
134

135     /**
136      * Set the PSDL Key Declaration to map.
137      *
138      * @param key - The Key to map.
139      */

140      public void
141      setKey(StorageHomeKeyDecl key)
142      {
143          key_ = key;
144      }
145
146     /**
147      * Map a key definition into Java methods.
148      *
149      * @param generator - The PSDL to Java generator.
150      * @param obj - The StorageHome mapping object.
151      * @param ast - The abstract storage type defining this key.
152      */

153     public void
154     toJava(PSDL2JavaGenerator generator,
155            InterfaceObject obj,
156            AbstractStorageTypeDecl ast)
157     throws GenerationException
158     {
159         MethodObject method = null;
160
161         // Add the find_by_<key_name> operation
162
method = new MethodObjectImpl();
163         method.addComment("The find_by_"+key_.getName()+" method.");
164         method.setName("find_by_"+key_.getName());
165         method.setReturnType( generator.getTranslator().getAbsoluteName(ast) );
166         StateMemberMapping.mapStateListToParameters
167             ( generator,
168               key_.getMemberList().getStrings(),
169               method,
170               ast );
171         method.addException("org.omg.CosPersistentState.NotFound");
172         method.getImpl().setMacro("SH_KEY_FIND_BY");
173         mapFactoryParameters(generator.getTranslator(), ast, method);
174         method.getImpl().addContextValue("st_type", generator.getTranslator().getAbsoluteName(ast));
175         obj.addMethod(method);
176
177         // Add the find_ref_by_<key_name> operation
178
method = new MethodObjectImpl();
179         method.addComment("The find_ref_by_"+key_.getName()+" method.");
180         method.setName("find_ref_by_"+key_.getName());
181         method.setReturnType("byte[]");
182         StateMemberMapping.mapStateListToParameters
183             ( generator,
184               key_.getMemberList().getStrings(),
185               method,
186               ast );
187         mapFactoryParameters(generator.getTranslator(), ast, method);
188         method.getImpl().setMacro("SH_KEY_FIND_REF_BY");
189         method.getImpl().addContextValue("key_name", key_.getName());
190         obj.addMethod(method);
191     }
192
193 }
194
Popular Tags