1 /*
2  * %CopyrightBegin%
3  *
4  * Copyright Ericsson AB 1997-2016. All Rights Reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * %CopyrightEnd%
19  *
20  */
21 /**
22  * InitialReference is a class which generates the INIT reference
23  * which can be used by the InitialReferences interface.
24  */
25 package Orber;
26 
27 public class InitialReference
28 {
29 
30   /**
31    * Constructor.
32    */
InitialReference()33   public InitialReference(){;}
34 
35   /**
36    * Returns the stringified objectreference to the initial reference server
37    */
stringified_ior(String host, int port)38   public String stringified_ior(String host, int port)
39     {
40       String iorByteString;
41       String profileData;
42       String iorString;
43 
44       // byte_order followed by ' {"", [{0, '
45       //      char iorBytesFirstPart[] = {0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0};
46       char iorBytesFirstPart[] = {0,0,0,0,0,0,0,32,73,68,76,58,79,114,98,101,114,47,73,110,105,116,105,97,108,82,101,102,101,114,101,110,99,101,115,58,49,46,48,0,0,0,0,1,0,0,0,0};
47       // the objectkey "INIT
48       char iorBytesLastPart[] = {0,0,0,4,73,78,73,84};
49 
50       // Fix the ProfileData struct.
51       char pdPrefix[] = {0,1,0,0};
52       char nullbyte[] = {0};
53       profileData = new String(pdPrefix) + enc_ulong(host.length() + 1) + host + new String(nullbyte);
54       profileData = align(profileData, 2);
55       profileData +=  enc_ushort(port);
56       profileData = align(profileData, 4);
57       profileData += new String(iorBytesLastPart);
58       // Fix the whole IOR
59       iorByteString = new String(iorBytesFirstPart) + enc_ulong(profileData.length()) +
60 	profileData;
61 
62       //      System.out.print("Start[" + profileData.length() + "]");
63       //      System.out.print("[");
64       //      for(int x = 0; x < iorByteString.length(); x++)
65       //	{
66       //	  System.out.print((int) iorByteString.charAt(x) + ",");
67       //	}
68       //      System.out.println("]");
69 
70       iorString = createIOR(iorByteString);
71       //      System.out.println(iorString);
72       return iorString;
73     }
74 
75 
enc_ushort(int s)76   private String enc_ushort(int s)
77     {
78       char byteArray[] = {(char) ((s >>> 8) & 0xFF),
79 			  (char) ((s >>> 0) & 0xFF)};
80 
81       return new String(byteArray);
82     }
83 
enc_ulong(int l)84   private String enc_ulong(int l)
85     {
86       char byteArray[] = {(char) ((l >>> 24) & 0xFF),
87 			  (char) ((l >>> 16) & 0xFF),
88 			  (char) ((l >>> 8) & 0xFF),
89 			  (char) ((l >>> 0) & 0xFF)};
90 
91       return new String(byteArray);
92 
93     }
94 
createIOR(String bytes)95   private String createIOR(String bytes)
96     {
97       int i;
98       StringBuffer sb = new StringBuffer("IOR:");
99 
100       for(i = 0; i < bytes.length(); i++)
101 	{
102 	  int b = bytes.charAt(i);
103 	  if(b<0) b+= 256;
104 	  int n1 = b / 16;
105 	  int n2 = b % 16;
106 	  int c1 = (n1 < 10) ? ('0' + n1) : ('a' + (n1 - 10));
107 	  int c2 = (n2 < 10) ? ('0' + n2) : ('a' + (n2 - 10));
108 	  sb.append((char)c1);
109 	  sb.append((char)c2);
110 	}
111 
112       return sb.toString();
113     }
114 
align(String buffer, int alignment)115   private String align(String buffer, int alignment)
116     {
117       String s = buffer;
118       char nullbyte[] = {0};
119 
120       int remainder = alignment - (buffer.length() % alignment);
121       if (remainder == alignment) return s;
122 
123       for (int i = 0; i < remainder; i++)
124 	{
125 	  s += new String(nullbyte);
126 	}
127       return s;
128     }
129 
130 
131 }
132