KickJava   Java API By Example, From Geeks To Geeks.

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


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>Default implementation for the OMG IDL <tt>Components::Cookie</tt> valuetype.</p>
36  **/

37 public class CookieImpl
38 extends org.omg.Components.Cookie
39 {
40     transient private int _channel_id;
41     transient private String JavaDoc _name;
42
43     // default constructor
44
public
45     CookieImpl()
46     {
47         // cookie
48
cookieValue = null;
49     }
50
51     // constructor with initial value
52
public
53     CookieImpl(byte[] val)
54     {
55         // cookie
56
cookieValue = val;
57     }
58
59     // constructor for event port cookie
60
public
61     CookieImpl(int chid)
62     {
63         // cookie
64
_channel_id = chid;
65         _name = null;
66
67         // compute value: <channel_id>:<random>
68
double d = Math.random();
69         String JavaDoc sval = Integer.toString(_channel_id)+":"+Double.toString(d);
70         cookieValue = sval.getBytes();
71     }
72
73     // constructor for receptacle cookie
74
public
75     CookieImpl(String JavaDoc n)
76     {
77         // cookie
78
_channel_id = -1;
79         _name = n;
80
81         // compute value: <name>:<random>
82
double d = Math.random();
83         String JavaDoc sval = _name+":"+Double.toString(d);
84         cookieValue = sval.getBytes();
85     }
86
87     //
88
// internal operations
89
//
90

91     final protected byte[]
92     getCookieValue()
93     {
94         return cookieValue;
95     }
96
97     final protected void
98     initialize()
99     {
100         String JavaDoc sval = new String JavaDoc(cookieValue);
101         int idx = sval.indexOf(':');
102         _name = sval.substring(0, idx);
103         try {
104             _channel_id = Integer.parseInt(_name);
105         } catch (java.lang.NumberFormatException JavaDoc ex) {
106             // ignore
107
}
108     }
109
110     //
111
// public operations
112
//
113

114     final public int
115     getChannelId()
116     {
117         if (_channel_id==-1) {
118             initialize();
119         }
120
121         return _channel_id;
122     }
123
124     final public String JavaDoc
125     getName()
126     {
127         if (_name==null) {
128             initialize();
129         }
130
131         return _name;
132     }
133
134     //
135
// java.lang.Object
136
//
137

138     final public boolean
139     equals(Object JavaDoc obj)
140     {
141         // check if CookieImpl
142
if (!(obj instanceof CookieImpl)) {
143             return false;
144         }
145
146         // we are forced to cast to CookieImpl
147
CookieImpl ck = (CookieImpl)obj;
148         if (java.util.Arrays.equals(cookieValue, ck.getCookieValue())) {
149             return true;
150         } else {
151             return false;
152         }
153     }
154 }
155
Popular Tags