001: package org.jacorb.ir;
002:
003: /*
004: * JacORB - a free Java ORB
005: *
006: * Copyright (C) 1997-2004 Gerald Brose.
007: *
008: * This library is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU Library General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * This library is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * Library General Public License for more details.
017: *
018: * You should have received a copy of the GNU Library General Public
019: * License along with this library; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: */
022:
023: import java.lang.reflect.*;
024:
025: import org.omg.CORBA.INTF_REPOS;
026: import org.omg.PortableServer.POA;
027:
028: import org.apache.avalon.framework.logger.Logger;
029:
030: /**
031: * JacORB implementation of IR ConstantDef objects
032: * @author Gerald Brose
033: * @version $Id: ConstantDef.java,v 1.17 2006/10/11 20:33:00 iliyan.jeliazkov Exp $
034: */
035:
036: public class ConstantDef extends Contained implements
037: org.omg.CORBA.ConstantDefOperations {
038: private Field field;
039: private org.omg.CORBA.TypeCode typeCode;
040: private org.omg.CORBA.IDLType type_def;
041: private org.jacorb.orb.Any value;
042: private org.omg.CORBA.ConstantDescription description;
043: private boolean defined = false;
044: org.omg.CORBA.Contained myContainer;
045: private Logger logger;
046: private POA poa;
047:
048: /**
049: * Constructor to create constants defined with an interface
050: */
051:
052: public ConstantDef(Field field,
053: org.omg.CORBA.Container _defined_in,
054: org.omg.CORBA.Repository _containing_repository,
055: Logger logger, POA poa) {
056: this .logger = logger;
057: this .poa = poa;
058: def_kind = org.omg.CORBA.DefinitionKind.dk_Constant;
059: containing_repository = _containing_repository;
060: defined_in = _defined_in;
061: this .field = field;
062:
063: name(field.getName());
064: version = "1.0";
065:
066: myContainer = org.omg.CORBA.ContainedHelper.narrow(defined_in);
067:
068: if (myContainer == null) {
069: throw new INTF_REPOS("Constant should be in an interface!");
070: }
071:
072: String def_in_id = myContainer.id();
073: id = def_in_id.substring(0, def_in_id.lastIndexOf(":")) + "/"
074: + name + ":" + version;
075:
076: absolute_name = myContainer.absolute_name() + "::" + name;
077:
078: try {
079: typeCode = TypeCodeUtil.getTypeCode(field.getType(), null,
080: this .logger);
081:
082: if (this .logger.isDebugEnabled()) {
083: this .logger.debug("New ConstantDef " + absolute_name());
084: }
085: } catch (ClassNotFoundException cnfe) {
086: this .logger.error("Error: ConstantDef " + absolute_name()
087: + " could not be created!", cnfe);
088: }
089: }
090:
091: /**
092: * Constructor to create constants mapped to a separate class
093: */
094:
095: public ConstantDef(Class c, org.omg.CORBA.Container _defined_in,
096: org.omg.CORBA.Repository ir, Logger logger, POA poa) {
097: this .logger = logger;
098: this .poa = poa;
099:
100: def_kind = org.omg.CORBA.DefinitionKind.dk_Constant;
101: containing_repository = ir;
102: defined_in = _defined_in;
103: myContainer = org.omg.CORBA.ContainedHelper.narrow(defined_in);
104: try {
105: field = c.getDeclaredField("value");
106: version = "1.0";
107: String classId = c.getName();
108: if (classId.indexOf('.') > 0) {
109: name(classId.substring(classId.lastIndexOf('.') + 1));
110: String path = classId.substring(0, classId
111: .lastIndexOf('.'));
112: id = "IDL:" + path.replace('.', '/') + "/" + name + ":"
113: + version;
114: absolute_name = myContainer.absolute_name() + "::"
115: + name;
116: } else {
117: name(classId);
118: defined_in = containing_repository;
119: id = "IDL:" + name + ":" + version;
120: absolute_name = "::" + name;
121: }
122: typeCode = TypeCodeUtil.getTypeCode(field.getType(), null,
123: this .logger);
124: } catch (Exception e) {
125: logger.debug("unexpected exception", e);
126: throw new INTF_REPOS(ErrorMsg.IR_Not_Implemented,
127: org.omg.CORBA.CompletionStatus.COMPLETED_NO);
128: }
129:
130: if (this .logger.isDebugEnabled()) {
131: this .logger.debug("New ConstantDef " + absolute_name());
132: }
133: }
134:
135: void define() {
136: if (this .logger.isDebugEnabled()) {
137: this .logger.debug("ConstantDef " + absolute_name()
138: + " defining.");
139: }
140:
141: value = (org.jacorb.orb.Any) orb.create_any();
142: type_def = IDLType.create(typeCode, containing_repository,
143: this .logger, this .poa);
144: if (typeCode == null) {
145: throw new INTF_REPOS("typeCode is null!");
146: }
147: if (type_def == null) {
148: throw new INTF_REPOS("type_def is null!");
149: }
150:
151: try {
152: value.insert_object(typeCode, field.get(null));
153: } catch (Exception e) {
154: logger.debug("unexpected exception", e);
155: }
156: defined = true;
157: }
158:
159: public org.omg.CORBA.TypeCode type() {
160: return typeCode;
161: }
162:
163: public org.omg.CORBA.Any value() {
164: return value;
165: }
166:
167: public void value(org.omg.CORBA.Any _value) {
168: value = (org.jacorb.orb.Any) _value;
169: }
170:
171: public org.omg.CORBA.IDLType type_def() {
172: return type_def;
173: }
174:
175: public void type_def(org.omg.CORBA.IDLType a) {
176: type_def = a;
177: }
178:
179: org.omg.CORBA.ConstantDescription describe_constant() {
180: if (!defined) {
181: throw new INTF_REPOS("ConstantDef " + name
182: + " not defined!");
183: }
184: if (description == null) {
185: String def_in = null;
186: if (myContainer == null)
187: def_in = "Global";
188: else
189: def_in = myContainer.id();
190: description = new org.omg.CORBA.ConstantDescription(name,
191: id, def_in, version, typeCode, value);
192: }
193: return description;
194: }
195:
196: // from Contained
197:
198: public org.omg.CORBA.ContainedPackage.Description describe() {
199: org.omg.CORBA.Any a = orb.create_any();
200: org.omg.CORBA.ConstantDescriptionHelper.insert(a,
201: describe_constant());
202: return new org.omg.CORBA.ContainedPackage.Description(
203: org.omg.CORBA.DefinitionKind.dk_Constant, a);
204: }
205:
206: // from IRObject
207:
208: public void destroy() {
209: }
210:
211: }
|