1 package org.jacorb.ir; 2 3 22 23 import java.lang.reflect.*; 24 import java.util.*; 25 26 import org.omg.CORBA.INTF_REPOS ; 27 import org.omg.CORBA.Any ; 28 import org.omg.PortableServer.POA ; 29 30 import org.apache.avalon.framework.logger.Logger; 31 32 37 38 public class ConstantDef 39 extends Contained 40 implements org.omg.CORBA.ConstantDefOperations 41 { 42 protected static char fileSeparator = 43 System.getProperty("file.separator").charAt(0); 44 45 private Field field; 46 private org.omg.CORBA.TypeCode typeCode; 47 private org.omg.CORBA.IDLType type_def; 48 private org.jacorb.orb.Any value; 49 private org.omg.CORBA.ConstantDescription description; 50 private boolean defined = false; 51 org.omg.CORBA.Contained myContainer; 52 private Logger logger; 53 private POA poa; 54 55 58 59 public ConstantDef( Field field, 60 org.omg.CORBA.Container _defined_in, 61 org.omg.CORBA.Repository _containing_repository, 62 Logger logger, 63 POA 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 ("Constant should be in an interface!"); 81 } 82 83 String 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 cnfe ) 101 { 102 this.logger.error("Error: ConstantDef " + 103 absolute_name() + " could not be created!", 104 cnfe); 105 } 106 } 107 108 109 112 113 public ConstantDef( Class c, 114 org.omg.CORBA.Container _defined_in, 115 org.omg.CORBA.Repository 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 classId = c.getName(); 128 if( classId.indexOf('.') > 0 ) 129 { 130 name( classId.substring( classId.lastIndexOf('.')+1)); 131 String 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 e ) 147 { 148 e.printStackTrace(); 149 throw new INTF_REPOS ( 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 ("typeCode is null!"); 173 } 174 if (type_def == null) 175 { 176 throw new INTF_REPOS ("type_def is null!"); 177 } 178 179 try 180 { 181 value.insert_object(typeCode, 182 orb, 183 field.get(null) ); 184 } 185 catch ( Exception e ) 186 { 187 e.printStackTrace(); 188 } 189 defined = true; 190 } 191 192 public org.omg.CORBA.TypeCode type() 193 { 194 return typeCode; 195 } 196 197 public org.omg.CORBA.Any value() 198 { 199 return value; 200 } 201 202 public void value( org.omg.CORBA.Any _value) 203 { 204 value = (org.jacorb.orb.Any)_value; 205 } 206 207 public org.omg.CORBA.IDLType type_def() 208 { 209 return type_def; 210 } 211 212 public void type_def(org.omg.CORBA.IDLType a) 213 { 214 type_def = a; 215 } 216 217 org.omg.CORBA.ConstantDescription describe_constant() 218 { 219 if ( ! defined) 220 { 221 throw new INTF_REPOS ("ConstantDef " + name + " not defined!"); 222 } 223 if( description == null ) 224 { 225 String 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 ( name, 232 id, 233 def_in, 234 version, 235 typeCode, 236 value); 237 } 238 return description; 239 } 240 241 243 public org.omg.CORBA.ContainedPackage.Description describe() 244 { 245 org.omg.CORBA.Any 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 253 public void destroy(){} 254 255 256 } 257 | Popular Tags |