سؤال

I'm implementing a Java key store and one of the methods I could override is KeyStoreSpi.engineStore(KeyStore.LoadStoreParameter param), which is supposed to store the key store based on the supplied parameters.

I don't understand how to implement this method, for two reasons:

  1. Apparently param can be null. In this case, how does my class know where to store the data?

  2. Supposing param is not null - it still doesn't provide any output location information. The LoadStoreParameter argument is a loose wrapper around ProtectionParameter, which seems to only provide access to password information. Again, nothing about where to store this keystore.

Any suggestions on how to tackle implementing this? I'm leaning towards not overriding this method and relying on the default implementation, which throws UnsupportedOperationException. But that's simply avoiding the problem.

I've tried Googling for "extends KeyStoreSpi" for examples of key store source files, but nobody seems to implement this method. I guess there might be a reason for that...

هل كانت مفيدة؟

المحلول

The KeyStore.engineStore(KeyStore.LoadStoreParameter) and KeyStoreSpi.engineLoad(KeyStore.LoadStoreParameter) are really needed only when the keystore format is not based on a single file, for example the keystore itself is a directory and entries are individual files inside that directory.

When the keystore format is a single file then stream-based versions of engineStore/engineLoad usually suffice and there is no need to overwrite param-based versions.

Assuming that the keystore is indeed can not be represented as a single file and therefore the keystore can not be loaded or saved using stream-based API:

  1. Throw UnsupportedOperationException from KeyStore.engineStore(OutputStream,char[]) and KeyStore.engineStore(InputStream,char[]) methods.
  2. Create class MyLoadStoreParameter implements KeyStore.LoadStoreParameter that will contain all the information required to store or load a keystore, like the path to the directory for the example above, password, etc. Making it immutable is an obviously good thing. Your keystore implementation can even define multiple variants of LoadStoreParameter of the keystore has multiple external representations.
  3. Implement KeyStore.engineStore(KeyStore.LoadStoreParameter) and KeyStoreSpi.engineLoad(KeyStore.LoadStoreParameter) accordingly.

You should throw IllegalArgumentException when KeyStore.LoadStoreParameter argument is not an instance of MyLoadStoreParameter. That will automatically cover the case when the parameter is null.

After checking the param type and casting it you just proceed to write or read the keystore in your specific external representation using the information contained in the param.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top