KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > ccm > runtime > MonolithicIdCodec


1 // ====================================================================
2
//
3
// ECM: The Extensible Container Model
4
// Copyright (C) 2004 THALES
5
// Contact: openccm-ecm@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): Mathieu Vadet.
23
// Initial Funding: IST COACH European project (IST-2001-34445)
24
// http://www.ist-coach.org
25
//
26
// ====================================================================
27

28
29
30 package org.objectweb.ccm.runtime;
31
32 import org.objectweb.corba.runtime.*;
33
34 /**
35  ** <p>Codec for monolithic components object ids.</p>
36  **/

37 public class MonolithicIdCodec
38 {
39     // codec
40
static final private String JavaDoc _class_name = "MonolithicIdCodec";
41     private ORBService _orb_service;
42     private org.omg.IOP.Codec JavaDoc _iiop12codec;
43
44     // default constructor
45
public
46     MonolithicIdCodec(ORBService orbs)
47     {
48         // codec
49
_orb_service = orbs;
50
51         // create IIOP 1.2 codec
52
org.omg.CORBA.Object JavaDoc obj = _orb_service.resolve_initial_references("CodecFactory");
53         org.omg.IOP.CodecFactory JavaDoc fact = org.omg.IOP.CodecFactoryHelper.narrow(obj);
54         org.omg.IOP.Encoding JavaDoc enc12 = new org.omg.IOP.Encoding JavaDoc();
55         enc12.format = org.omg.IOP.ENCODING_CDR_ENCAPS.value;
56         enc12.major_version = (byte)1;
57         enc12.minor_version = (byte)2;
58
59         try {
60             _iiop12codec = fact.create_codec(enc12);
61         } catch (org.omg.IOP.CodecFactoryPackage.UnknownEncoding JavaDoc ex) {
62             final String JavaDoc opname = "SessionIdCodec";
63             TheLogger.error(_class_name, opname, "FAILED (IIOP 1.2 no supported)", ex);
64         }
65     }
66
67     //
68
// public operations
69
//
70

71     final public byte[]
72     encode(MonolithicId sid)
73     {
74         final String JavaDoc opname = "encode";
75
76         // marshall
77
org.omg.CORBA.Any JavaDoc any_id = _orb_service.create_any();
78         MonolithicIdHelper.insert(any_id, sid);
79         byte[] id = null;
80         try {
81             id = _iiop12codec.encode_value(any_id);
82         } catch (org.omg.IOP.CodecPackage.InvalidTypeForEncoding JavaDoc ex) {
83             // NOTE: TODO: in which case can this exception occur ?
84
TheLogger.error(_class_name, opname, "FAILED (invalid type)", ex);
85             return null;
86         } catch (Throwable JavaDoc ex) {
87             TheLogger.debug(_class_name, opname, "FAILED", ex);
88             return null;
89         }
90
91         return id;
92     }
93
94     final public MonolithicId
95     decode(byte[] id)
96     {
97         final String JavaDoc opname = "decode";
98
99         // unmarshall
100
org.omg.CORBA.Any JavaDoc any_id = _orb_service.create_any();
101         try {
102             any_id = _iiop12codec.decode_value(id, MonolithicIdHelper.type());
103         } catch (org.omg.IOP.CodecPackage.FormatMismatch JavaDoc ex) {
104             // if bad format, do not fail
105
TheLogger.debug(_class_name, opname, "IGNORE", ex);
106             return null;
107         } catch (org.omg.IOP.CodecPackage.TypeMismatch JavaDoc ex) {
108             // if bad type, fail (should not happen)
109
TheLogger.error(_class_name, opname, "FAILED (wrong type)", ex);
110             return null;
111         } catch (Throwable JavaDoc ex) {
112             TheLogger.debug(_class_name, opname, "FAILED", ex);
113             return null;
114         }
115
116         // extract and build component id
117
MonolithicId mid = MonolithicIdHelper.extract(any_id);
118
119         return mid;
120     }
121 }
122
Popular Tags