KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > ir > ConstantDef


1 package org.jacorb.ir;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 1997-2004 Gerald Brose.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */

22
23 import java.lang.reflect.*;
24 import java.util.*;
25
26 import org.omg.CORBA.INTF_REPOS JavaDoc;
27 import org.omg.CORBA.Any JavaDoc;
28 import org.omg.PortableServer.POA JavaDoc;
29
30 import org.apache.avalon.framework.logger.Logger;
31
32 /**
33  * JacORB implementation of IR ConstantDef objects
34  * @author Gerald Brose
35  * @version $Id: ConstantDef.java,v 1.13 2004/05/06 12:39:59 nicolas Exp $
36  */

37
38 public class ConstantDef
39     extends Contained
40     implements org.omg.CORBA.ConstantDefOperations JavaDoc
41 {
42     protected static char fileSeparator =
43         System.getProperty("file.separator").charAt(0);
44
45     private Field field;
46     private org.omg.CORBA.TypeCode JavaDoc typeCode;
47     private org.omg.CORBA.IDLType JavaDoc type_def;
48     private org.jacorb.orb.Any value;
49     private org.omg.CORBA.ConstantDescription JavaDoc description;
50     private boolean defined = false;
51     org.omg.CORBA.Contained JavaDoc myContainer;
52     private Logger logger;
53     private POA JavaDoc poa;
54
55     /**
56      * Constructor to create constants defined with an interface
57      */

58
59     public ConstantDef( Field field,
60                         org.omg.CORBA.Container JavaDoc _defined_in,
61                         org.omg.CORBA.Repository JavaDoc _containing_repository,
62                         Logger logger,
63                         POA JavaDoc poa )
64     {
65         this.logger = logger;
66         this.poa = poa;
67         def_kind = org.omg.CORBA.DefinitionKind.dk_Constant;
68         containing_repository = _containing_repository;
69         defined_in = _defined_in;
70         this.field = field;
71
72         name( field.getName());
73         version = "1.0";
74
75         myContainer =
76             org.omg.CORBA.ContainedHelper.narrow( defined_in );
77
78         if (myContainer == null)
79         {
80             throw new INTF_REPOS JavaDoc ("Constant should be in an interface!");
81         }
82
83         String JavaDoc def_in_id = myContainer.id();
84         id = def_in_id.substring(0,def_in_id.lastIndexOf(":")) +
85             "/" + name + ":" + version;
86
87         absolute_name = myContainer.absolute_name() + "::" + name;
88
89         try
90         {
91             typeCode = TypeCodeUtil.getTypeCode( field.getType(),
92                                                  null,
93                                                  this.logger );
94             
95             if (this.logger.isDebugEnabled())
96             {
97                 this.logger.debug("New ConstantDef " + absolute_name());
98             }
99         }
100         catch( ClassNotFoundException JavaDoc cnfe )
101         {
102             this.logger.error("Error: ConstantDef " +
103                               absolute_name() + " could not be created!",
104                               cnfe);
105         }
106     }
107
108
109     /**
110      * Constructor to create constants mapped to a separate class
111      */

112
113     public ConstantDef( Class JavaDoc c,
114                         org.omg.CORBA.Container JavaDoc _defined_in,
115                         org.omg.CORBA.Repository JavaDoc ir,
116                         Logger logger )
117     {
118         this.logger = logger;
119         def_kind = org.omg.CORBA.DefinitionKind.dk_Constant;
120         containing_repository = ir;
121         defined_in = _defined_in;
122         myContainer = org.omg.CORBA.ContainedHelper.narrow( defined_in );
123         try
124         {
125             field = c.getDeclaredField("value");
126             version = "1.0";
127             String JavaDoc classId = c.getName();
128             if( classId.indexOf('.') > 0 )
129             {
130                 name( classId.substring( classId.lastIndexOf('.')+1));
131                 String JavaDoc path = classId.substring(0, classId.lastIndexOf('.'));
132                 id = "IDL:" + path.replace('.','/') + "/" + name + ":" + version;
133                 absolute_name = myContainer.absolute_name() + "::" + name;
134             }
135             else
136             {
137                 name( classId );
138                 defined_in = containing_repository;
139                 id = "IDL:" + name + ":" + version;
140                 absolute_name = "::" + name;
141             }
142             typeCode = TypeCodeUtil.getTypeCode( field.getType(),
143                                                  null,
144                                                  this.logger );
145         }
146         catch ( Exception JavaDoc e )
147         {
148             e.printStackTrace();
149             throw new INTF_REPOS JavaDoc( ErrorMsg.IR_Not_Implemented,
150                                   org.omg.CORBA.CompletionStatus.COMPLETED_NO);
151         }
152
153         if (this.logger.isDebugEnabled())
154         {
155             this.logger.debug("New ConstantDef " + absolute_name());
156         }
157     }
158
159
160     void define()
161     {
162         if (this.logger.isDebugEnabled())
163         {
164             this.logger.debug("ConstantDef " + absolute_name() + " defining.");
165         }
166
167         value = (org.jacorb.orb.Any) orb.create_any();
168         type_def = IDLType.create( typeCode, containing_repository,
169                                    this.logger, this.poa);
170         if (typeCode == null)
171         {
172             throw new INTF_REPOS JavaDoc ("typeCode is null!");
173         }
174         if (type_def == null)
175         {
176             throw new INTF_REPOS JavaDoc ("type_def is null!");
177         }
178
179         try
180         {
181             value.insert_object(typeCode,
182                                 orb,
183                                 field.get(null) );
184         }
185         catch ( Exception JavaDoc e )
186         {
187             e.printStackTrace();
188         }
189         defined = true;
190     }
191
192     public org.omg.CORBA.TypeCode JavaDoc type()
193     {
194         return typeCode;
195     }
196
197     public org.omg.CORBA.Any JavaDoc value()
198     {
199         return value;
200     }
201
202     public void value( org.omg.CORBA.Any JavaDoc _value)
203     {
204         value = (org.jacorb.orb.Any)_value;
205     }
206
207     public org.omg.CORBA.IDLType JavaDoc type_def()
208     {
209         return type_def;
210     }
211
212     public void type_def(org.omg.CORBA.IDLType JavaDoc a)
213     {
214         type_def = a;
215     }
216
217     org.omg.CORBA.ConstantDescription JavaDoc describe_constant()
218     {
219         if ( ! defined)
220         {
221             throw new INTF_REPOS JavaDoc ("ConstantDef " + name + " not defined!");
222         }
223         if( description == null )
224         {
225             String JavaDoc def_in = null;
226             if( myContainer == null )
227                 def_in = "Global";
228             else
229                 def_in = myContainer.id();
230             description =
231                 new org.omg.CORBA.ConstantDescription JavaDoc( name,
232                                                        id,
233                                                        def_in,
234                                                        version,
235                                                        typeCode,
236                                                        value);
237         }
238         return description;
239     }
240
241     // from Contained
242

243     public org.omg.CORBA.ContainedPackage.Description describe()
244     {
245         org.omg.CORBA.Any JavaDoc a = orb.create_any();
246         org.omg.CORBA.ConstantDescriptionHelper.insert( a, describe_constant() );
247         return new org.omg.CORBA.ContainedPackage.Description(
248                      org.omg.CORBA.DefinitionKind.dk_Constant, a);
249     }
250
251     // from IRObject
252

253     public void destroy(){}
254
255
256 }
257
Popular Tags