KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > tools > ReadLicense


1 //
2
// ____.
3
// __/\ ______| |__/\. _______
4
// __ .____| | \ | +----+ \
5
// _______| /--| | | - \ _ | : - \_________
6
// \\______: :---| : : | : | \________>
7
// |__\---\_____________:______: :____|____:_____\
8
// /_____|
9
//
10
// . . . i n j a h i a w e t r u s t . . .
11
//
12

13 package org.jahia.tools;
14
15 import java.io.ByteArrayInputStream JavaDoc;
16 import java.io.DataInputStream JavaDoc;
17 import java.io.EOFException JavaDoc;
18 import java.io.File JavaDoc;
19 import java.io.FileInputStream JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.util.zip.CRC32 JavaDoc;
22
23 /**
24  *
25  * @version 1.0
26  */

27 public class ReadLicense
28 {
29     private static final String JavaDoc LICENSE_FILENAME = "jahia.license";
30
31
32     //-------------------------------------------------------------------------
33
public static void main (String JavaDoc args[])
34     {
35         System.out.println ("\nLicense key reader, version 1.0");
36         System.out.println ("(c) Jahia Ltd 2002\n\n");
37
38         try {
39             File JavaDoc file = new File JavaDoc (LICENSE_FILENAME);
40
41             FileInputStream JavaDoc fstream = new FileInputStream JavaDoc (file);
42             DataInputStream JavaDoc stream = new DataInputStream JavaDoc (fstream);
43
44             // create the raw byte stream from the file size by removing the
45
// checksum bytes and it's offset.
46
int streamSize = (new Long JavaDoc(file.length() - 10)).intValue();
47             byte[] bytes = new byte[streamSize];
48             int index = 0;
49
50             short crcOffset = stream.readShort ();
51             // read the <crcOffset> bytes before the crc info
52
for (int i=0; i<crcOffset; i++) {
53                 bytes[index] = stream.readByte();
54                 index++;
55             }
56
57             long storedChecksum = stream.readLong ();
58             System.out.println (" stored checksum = "+Long.toHexString (storedChecksum));
59
60             // read the remaining bytes of the file.
61
try {
62                 while (index < bytes.length) {
63                     bytes[index] = stream.readByte();
64                     index++;
65                 }
66             }
67             catch (EOFException JavaDoc ex) {
68             }
69
70             // free memory of unused objects
71
fstream.close();
72             stream = null;
73             fstream = null;
74             file = null;
75
76             //DisplayBytes (bytes);
77

78             // compute the stream CRC
79
CRC32 JavaDoc crc = new CRC32 JavaDoc();
80             crc.update (bytes);
81
82             long streamChecksum = crc.getValue ();
83             //System.out.println (" stream checksum = "+Long.toHexString (streamChecksum));
84

85             //Read the license info
86
ByteArrayInputStream JavaDoc byteStream = new ByteArrayInputStream JavaDoc (bytes);
87             stream = new DataInputStream JavaDoc (byteStream);
88             int offset = 0;
89
90
91
92             //System.out.println ("License info :");
93

94             try {
95                 // license type
96
offset = stream.readInt ();
97                 stream.skipBytes(offset);
98                 int licenseType = stream.readInt();
99                 System.out.println (" - license type = "+licenseType);
100             } catch ( Throwable JavaDoc t ){
101                 System.out.println (" - error reading license type");
102                 return;
103             }
104
105             try {
106                 // page limit
107
offset = stream.readInt ();
108                 stream.skipBytes(offset);
109                 int pageLimit = stream.readInt();
110                 System.out.println (" - page limit = "+pageLimit);
111             } catch ( Throwable JavaDoc t ){
112                 System.out.println (" - error reading page limit");
113                 return;
114             }
115
116             try{
117                 // page template limit
118
offset = stream.readInt ();
119                 stream.skipBytes(offset);
120                 int templateLimit = stream.readInt();
121                 System.out.println (" - template limit = "+templateLimit);
122             } catch ( Throwable JavaDoc t ){
123                 System.out.println (" - error reading page template limit");
124                 return;
125             }
126
127             try {
128                 // user limit
129
offset = stream.readInt ();
130                 stream.skipBytes(offset);
131                 int userLimit = stream.readInt();
132                 System.out.println (" - user limit = "+userLimit);
133             } catch ( Throwable JavaDoc t ){
134                 System.out.println (" - error reading user limit");
135                 return;
136             }
137
138             try {
139                 // site limit
140
offset = stream.readInt ();
141                 stream.skipBytes(offset);
142                 int siteLimit = stream.readInt();
143                 System.out.println (" - site limit = "+siteLimit);
144             } catch ( Throwable JavaDoc t ){
145                 System.out.println (" - error reading site limit");
146                 return;
147             }
148
149             try {
150                 // license id
151
offset = stream.readInt ();
152                 stream.skipBytes(offset);
153                 int nbBytes = stream.readInt();
154                 byte[] stringAsBytes = new byte[nbBytes];
155                 stream.read(stringAsBytes);
156                 String JavaDoc licenseID = new String JavaDoc(stringAsBytes,"UTF-16");
157                 System.out.println (" - license id = "+licenseID);
158             } catch ( Throwable JavaDoc t ){
159                 System.out.println (" - error reading license type");
160                 return;
161             }
162
163             try {
164                 // build number
165
offset = stream.readInt ();
166                 stream.skipBytes(offset);
167                 int buildNumber = stream.readInt();
168                 System.out.println (" - build number = "+buildNumber);
169             } catch ( Throwable JavaDoc t ){
170                 System.out.println (" - error reading build number");
171                 return;
172             }
173
174             try {
175                 // release number
176
offset = stream.readInt ();
177                 stream.skipBytes(offset);
178                 double releaseNumber = stream.readDouble();
179                 System.out.println (" - release number = "+releaseNumber);
180             } catch ( Throwable JavaDoc t ){
181                 System.out.println (" - error reading release number");
182                 return;
183             }
184
185             System.out.println ("\nLicense file successfully readed.");
186
187         }
188         catch (IOException JavaDoc ex) {
189             //System.out.println ("ERROR : I/O exception while reading the file.");
190
return;
191         }
192
193     }
194
195
196     //-------------------------------------------------------------------------
197
private static void DisplayBytes (byte[] bytes)
198     {
199         //System.out.print (" stream = ");
200

201         for (int i=0; i<bytes.length; i++) {
202             //System.out.print (bytes[i]+" ");
203
}
204
205         //System.out.print("\n");
206
}
207
208
209 }
210
Popular Tags