Import Upstream version 1.8.5
[hcoop/debian/openafs.git] / src / JAVA / classes / org / openafs / jafs / Key.java
CommitLineData
805e021f
CE
1/*
2 * @(#)Key.java 1.0 6/29/2001
3 *
4 * Copyright (c) 2001 International Business Machines Corp.
5 * All rights reserved.
6 *
7 * This software has been released under the terms of the IBM Public
8 * License. For details, see the LICENSE file in the top-level source
9 * directory or online at http://www.openafs.org/dl/license10.html
10 *
11 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
12 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
13 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
14 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
15 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
16 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
17 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
18 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
19 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
20 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
21 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22 */
23
24package org.openafs.jafs;
25
26import java.util.GregorianCalendar;
27import java.util.Date;
28import java.io.Serializable;
29
30/**
31 * An abstract representation of an AFS key. It holds information about
32 * the key, such as what its version is.
33 * <BR><BR>
34 *
35 * Constructing an instance of a <code>Key</code> does not mean an actual
36 * AFS key is created on a server -- usually a <code>Key</code>
37 * object is a representation of an already existing AFS key. If,
38 * however, the <code>Key</code> is constructed with the version number of a
39 * key that does not exist on the server represented by the provided
40 * <code>Server</code>, a new key with that version number can be
41 * created on that server by calling the {@link #create(String)} methods If
42 * such a key does already exist when this method is called,
43 * an exception will be thrown.<BR><BR>
44 *
45 * <!--Example of how to use class-->
46 * The following is a simple example of how to construct and use a
47 * <code>Key</code> object. It obtains the list of <code>Key</code>s from
48 * a specified server, and prints the string representation of each key.
49 * <PRE>
50 * import org.openafs.jafs.Cell;
51 * import org.openafs.jafs.AFSException;
52 * import org.openafs.jafs.Key;
53 * import org.openafs.jafs.Server;
54 * ...
55 * public class ...
56 * {
57 * ...
58 * private Cell cell;
59 * private Server server;
60 * ...
61 * public static void main(String[] args) throws Exception
62 * {
63 * String username = arg[0];
64 * String password = arg[1];
65 * String cellName = arg[2];
66 * String serverName = arg[3];
67 *
68 * token = new Token(username, password, cellName);
69 * cell = new Cell(token);
70 * server = new Server(serverName, cell);
71 *
72 * System.out.println("Keys in Server " + server.getName() + ":");
73 * Key[] keys = server.getKeys();
74 * for (int i = 0; i < keys.length; i++) {
75 * System.out.println(" -> " + keys[i] );
76 * }
77 * }
78 * ...
79 * }
80 * </PRE>
81 *
82 */
83public class Key implements Serializable, Comparable
84{
85 protected Server server;
86
87 protected int version;
88 protected long checkSum;
89 protected String encryptionKey;
90 protected int lastModDate;
91 protected int lastModMs;
92
93 protected GregorianCalendar lastModDateDate;
94
95 protected boolean cachedInfo;
96
97 /**
98 * Constructs a new <CODE>Key</CODE> object instance given the version of
99 * the AFS key and the AFS server, represented by <CODE>server</CODE>,
100 * to which it belongs. This does not actually
101 * create a new AFS key, it just represents one.
102 * If <code>version</code> is not an actual AFS key, exceptions
103 * will be thrown during subsequent method invocations on this
104 * object, unless the {@link #create(String)}
105 * method is explicitly called to create it.
106 *
107 * @exception AFSException If an error occurs in the native code
108 * @param version the version of the key to represent
109 * @param server the server to which the key belongs.
110 */
111 public Key( int version, Server server ) throws AFSException
112 {
113 this.server = server;
114 this.version = version;
115
116 lastModDateDate = null;
117
118 cachedInfo = false;
119 }
120
121 /**
122 * Constructs a new <CODE>Key</CODE> object instance given the version of
123 * the AFS key and the AFS server, represented by <CODE>server</CODE>,
124 * to which it belongs. This does not actually
125 * create a new AFS key, it just represents one.
126 * If <code>version</code> is not an actual AFS key, exceptions
127 * will be thrown during subsequent method invocations on this
128 * object, unless the {@link #create(String)}
129 * method is explicitly called to create it. Note that if the key does not
130 * exist and <code>preloadAllMembers</code> is true, an exception will
131 * be thrown.
132 *
133 * <P> This constructor is ideal for point-in-time representation and
134 * transient applications. It ensures all data member values are set and
135 * available without calling back to the filesystem at the first request
136 * for them. Use the {@link #refresh()} method to address any coherency
137 * concerns.
138 *
139 * @param version the version of the key to represent
140 * @param server the server to which the key belongs.
141 * @param preloadAllMembers true will ensure all object members are set
142 * upon construction; otherwise members will be
143 * set upon access, which is the default behavior.
144 * @exception AFSException If an error occurs in the native code
145 * @see #refresh
146 */
147 public Key( int version, Server server, boolean preloadAllMembers )
148 throws AFSException
149 {
150 this(version, server);
151 if (preloadAllMembers) refresh(true);
152 }
153
154 /**
155 * Creates a blank <code>Key</code> given the server to which the key
156 * belongs. This blank object can then be passed into other methods to
157 * fill out its properties.
158 *
159 * @exception AFSException If an error occurs in the native code
160 * @param server the server to which the key belongs.
161 */
162 Key( Server server ) throws AFSException
163 {
164 this( -1, server );
165 }
166
167 /*-------------------------------------------------------------------------*/
168
169 /**
170 * Refreshes the properties of this Key object instance with values from
171 * the AFS key it represents. All properties that have been initialized
172 * and/or accessed will be renewed according to the values of the AFS key
173 * this Key object instance represents.
174 *
175 * <P>Since in most environments administrative changes can be administered
176 * from an AFS command-line program or an alternate GUI application, this
177 * method provides a means to refresh the Java object representation and
178 * thereby ascertain any possible modifications that may have been made
179 * from such alternate administrative programs. Using this method before
180 * an associated instance accessor will ensure the highest level of
181 * representative accuracy, accommodating changes made external to the
182 * Java application space. If administrative changes to the underlying AFS
183 * system are only allowed via this API, then the use of this method is
184 * unnecessary.
185 *
186 * @exception AFSException If an error occurs in the native code
187 */
188 public void refresh() throws AFSException
189 {
190 refresh(false);
191 }
192
193 /**
194 * Refreshes the properties of this Key object instance with values from
195 * the AFS key it represents. If <CODE>all</CODE> is <CODE>true</CODE>
196 * then <U>all</U> of the properties of this Key object instance will be
197 * set, or renewed, according to the values of the AFS key it represents,
198 * disregarding any previously set properties.
199 *
200 * <P> Thus, if <CODE>all</CODE> is <CODE>false</CODE> then properties that
201 * are currently set will be refreshed and properties that are not set will
202 * remain uninitialized. See {@link #refresh()} for more information.
203 *
204 * @param all if true set or renew all object properties; otherwise renew
205 * all set properties
206 * @exception AFSException If an error occurs in the native code
207 * @see #refresh()
208 */
209 protected void refresh(boolean all) throws AFSException
210 {
211 if( all || cachedInfo ) {
212 refreshInfo();
213 }
214 }
215
216 /**
217 * Refreshes the information fields of this <code>Key</code> to reflect the
218 * current state of the AFS server key. These inlclude the last
219 * modification time, etc.
220 *
221 * @exception AFSException If an error occurs in the native code
222 */
223 protected void refreshInfo() throws AFSException
224 {
225 getKeyInfo( server.getBosHandle(), version, this );
226 cachedInfo = true;
227 lastModDateDate = null;
228 }
229
230 /**
231 * Creates a key with this <code>Key's</code> version number at the server,
232 * using the specified <code>String</code> for the key.
233 *
234 * @param keyString the string to use for the encryption key
235 * @exception AFSException If an error occurs in the native code
236 */
237 public void create( String keyString ) throws AFSException
238 {
239 create( server.getCell().getCellHandle(), server.getBosHandle(), version,
240 keyString );
241 }
242
243 /**
244 * Removes the key with this <code>Key's</code> version number from
245 * the server.
246 *
247 * @exception AFSException If an error occurs in the native code
248 */
249 public void delete( ) throws AFSException
250 {
251 delete( server.getBosHandle(), version );
252
253 encryptionKey = null;
254 cachedInfo = false;
255 }
256
257 //////////////// accessors: ////////////////////////
258
259 /**
260 * Returns the version of this key in primitive form.
261 *
262 * @return the version number of this key
263 */
264 public int getVersion()
265 {
266 return version;
267 }
268
269 /**
270 * Returns the server this key is associated with.
271 *
272 * @return this key's server
273 */
274 public Server getServer()
275 {
276 return server;
277 }
278
279 /**
280 * Returns the encrypted key as a string in octal form. This is how AFS
281 * prints it out on the command line. An example would be:
282 * '\040\205\211\241\345\002\023\211'.
283 *
284 * @return the encrypted key
285 * @exception AFSException If an error occurs in the native code
286 */
287 public String getEncryptionKey() throws AFSException
288 {
289 if (!cachedInfo) refreshInfo();
290 return encryptionKey;
291 }
292
293 /**
294 * Returns the check sum of this key.
295 *
296 * @return the check sum of this key
297 * @exception AFSException If an error occurs in the native code
298 */
299 public long getCheckSum() throws AFSException
300 {
301 if (!cachedInfo) refreshInfo();
302 return checkSum;
303 }
304
305 /**
306 * Returns the last modification date of this key.
307 *
308 * @return the last modification date of this key
309 * @exception AFSException If an error occurs in the native code
310 */
311 public GregorianCalendar getLastModDate() throws AFSException
312 {
313 if (!cachedInfo) refreshInfo();
314 if ( lastModDateDate == null && cachedInfo ) {
315 // make it into a date . . .
316 lastModDateDate = new GregorianCalendar();
317 long longTime = ((long) lastModDate)*1000;
318 Date d = new Date( longTime );
319 lastModDateDate.setTime( d );
320 }
321 return lastModDateDate;
322 }
323
324 /////////////// information methods ////////////////////
325
326 /**
327 * Returns a <code>String</code> representation of this <code>Key</code>.
328 * Contains the information fields.
329 *
330 * @return a <code>String</code> representation of the <code>Key</code>
331 */
332 public String getInfo()
333 {
334 String r;
335 try {
336 r = "Key version number: " + getVersion() + "\n";
337 r += "\tencrypted key: " + getEncryptionKey() + "\n";
338 r += "\tcheck sum: " + getCheckSum() + "\n";
339 r += "\tlast mod time: " + getLastModDate().getTime() + "\n";
340 } catch( Exception e ) {
341 return e.toString();
342 }
343 return r;
344 }
345
346 /////////////// override methods ////////////////////
347
348 /**
349 * Compares two Key objects respective to their key version and does not
350 * factor any other attribute.
351 *
352 * @param key The Key object to be compared to this Key instance
353 *
354 * @return Zero if the argument is equal to this Key's version, a
355 * value less than zero if this Key's version is less than
356 * the argument, or a value greater than zero if this Key's
357 * version is greater than the argument
358 */
359 public int compareTo(Key key)
360 {
361 return (this.getVersion() - key.getVersion());
362 }
363
364 /**
365 * Comparable interface method.
366 *
367 * @see #compareTo(Key)
368 */
369 public int compareTo(Object obj)
370 {
371 return compareTo((Key)obj);
372 }
373
374 /**
375 * Tests whether two <code>Key</code> objects are equal, based on their
376 * encryption key, version, and associated Server.
377 *
378 * @param otherKey the Key to test
379 * @return whether the specifed Key is the same as this Key
380 */
381 public boolean equals( Key otherKey )
382 {
383 try {
384 return ( this.getEncryptionKey().equals(otherKey.getEncryptionKey()) ) &&
385 ( this.getVersion() == otherKey.getVersion() ) &&
386 ( this.getServer().equals(otherKey.getServer()) );
387 } catch (Exception e) {
388 return false;
389 }
390 }
391
392 /**
393 * Returns the name of this <CODE>Key</CODE>
394 *
395 * @return the name of this <CODE>Key</CODE>
396 */
397 public String toString()
398 {
399 try {
400 return getVersion() + " - " + getEncryptionKey() + " - " + getCheckSum();
401 } catch (Exception e) {
402 return e.toString();
403 }
404 }
405
406 /////////////// native methods ////////////////////
407
408 /**
409 * Fills in the information fields of the provided <code>Key</code>.
410 *
411 * @param serverHandle the bos handle of the server to which the key
412 * belongs
413 * @see Server#getBosServerHandle
414 * @param version the version of the key for which to get the information
415 * @param key the <code>Key</code> object in which to fill in the
416 * information
417 * @see Server
418 * @exception AFSException If an error occurs in the native code
419 */
420 protected static native void getKeyInfo( long serverHandle, int version,
421 Key key )
422 throws AFSException;
423
424 /**
425 * Create a server key.
426 *
427 * @param cellHandle the handle of the cell to which the server belongs
428 * @see Cell#getCellHandle
429 * @param serverHandle the bos handle of the server to which the key will
430 * belong
431 * @see Server#getBosServerHandle
432 * @param versionNumber the version number of the key to create (0 to 255)
433 * @param keyString the <code>String</code> version of the key that will
434 * be encrypted
435 * @exception AFSException If an error occurs in the native code
436 */
437 protected static native void create( long cellHandle, long serverHandle, int versionNumber, String keyString )
438 throws AFSException;
439
440 /**
441 * Delete a server key.
442 *
443 * @param serverHandle the bos handle of the server to which the key belongs
444 * @see Server#getBosServerHandle
445 * @param versionNumber the version number of the key to remove (0 to 255)
446 * @exception AFSException If an error occurs in the native code
447 */
448 protected static native void delete( long serverHandle, int versionNumber )
449 throws AFSException;
450
451 /**
452 * Reclaims all memory being saved by the key portion of the native library.
453 * This method should be called when no more <code>Key</code> objects are
454 * expected to be
455 * used.
456 */
457 protected static native void reclaimKeyMemory();
458
459}
460
461
462
463
464
465
466
467