KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gcc > rmi > iiop > ObjectKey


1 /*
2  * Copyright 2004 The Apache Software Foundation or its licensors, as
3  * applicable.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14  * implied.
15  *
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19 package gcc.rmi.iiop;
20
21 import gcc.security.*;
22 import gcc.util.*;
23 import java.util.*;
24
25 public class ObjectKey
26 {
27     public static final int TYPE_MANAGER = 'M';
28
29     public static final int TYPE_SESSION = 'S';
30
31     public int type;
32     public String username = "";
33     public String password = "";
34     public String component = "";
35     public String sessionID = "";
36
37     public byte[] encode()
38     {
39         int un = username.length();
40         int pn = password.length();
41         int cn = component.length();
42         int sn = sessionID.length();
43         StringBuffer keyBuffer = new StringBuffer(12 + un + pn + cn + sn);
44         keyBuffer.append("U=");
45         keyBuffer.append(username);
46         keyBuffer.append("\tP=");
47         keyBuffer.append(password);
48         keyBuffer.append("\tC=");
49         keyBuffer.append(component);
50         if (sn > 0)
51         {
52             keyBuffer.append("\tS=");
53             keyBuffer.append(sessionID);
54         }
55         byte[] bytes = SecurityInfo.encode(keyBuffer.toString());
56         bytes[0] = (byte)type;
57         return bytes;
58     }
59
60     public void decode(byte[] bytes)
61     {
62         type = bytes.length == 0 ? 0 : bytes[0];
63         String key = SecurityInfo.decode(bytes);
64         List items = ListUtil.getListWithSeparator(key, "\t");
65         for (Iterator i = items.iterator(); i.hasNext();)
66         {
67             String item = (String)i.next();
68             if (item.startsWith("U="))
69             {
70                 username = item.substring(2);
71             }
72             else if (item.startsWith("P="))
73             {
74                 password = item.substring(2);
75             }
76             else if (item.startsWith("C="))
77             {
78                 component = item.substring(2);
79             }
80             else if (item.startsWith("S="))
81             {
82                 sessionID = item.substring(2);
83             }
84         }
85     }
86
87     public void checkPassword()
88     {
89         User.getInstance(username).login(password);
90     }
91 }
92
Popular Tags