Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Equinox » Exception with UserAdmin service and getUser
Exception with UserAdmin service and getUser [message #125273] Thu, 29 January 2009 18:37 Go to next message
Ian Carr is currently offline Ian CarrFriend
Messages: 3
Registered: July 2009
Junior Member
Hi

I am writing an application using the Lotus expeditor framework build on
top of equinox. It's web
application framework uses the osgi UserAdmin service to authenticate user
logins.

I am trying to use the code below to store user information into the
equinox implementation.

>>>>>>>>>

private static final String USERNAME = "12345670";
private static final String PASSWORD = "password";

private static final String PROP_USERNAME = "username";
private static final String CRED_PASSWORD = "password";

ServiceReference ref =
context.getServiceReference("org.osgi.service.useradmin.UserAdmin ");

if (ref != null) {
UserAdmin userAdmin = (UserAdmin) context.getService(ref);

try {
// does the user already exist?
User user = userAdmin.getUser(PROP_USERNAME, USERNAME);

if (user == null) {

// didn't find him so create the set
user = (User) userAdmin.createRole(USERNAME, Role.USER);

// get the properties dictionary, add and set the username
// property
Dictionary<String, String> props = user.getProperties();
props.put(PROP_USERNAME, USERNAME);

// get the credentials dictionary, add and set the password
// property
Dictionary<String, String> creds = user.getCredentials();
creds.put(CRED_PASSWORD, PASSWORD);

}
} finally {
// don't need the service anymore.
context.ungetService(ref);
}
}


>>>>>>>>>>>>>>>

This behaves correctly in a 'clean' environment ie if the configuration
data does not already exist.

The user is created and the web application goes on to be able to log the
user on. So all seems fine whilst the
original information is held in memory.

The problem occurrs if I shutdown the application and start it again
(reloading the UserAdminStore from the backing file)
without cleaning out the preferences store information.

In this case the request to find the user (userAdmin.getUser(property,
name)) fails with a classcast exception
in the UserAdmin service.

The implementation is trying to cast a byte array (B]) to a String.

looking at the equinox source this would appear to be because the values
being stored are multiples of 4 bytes in
length and calling Base64.decode does not throw an exception ie all the
characters are in the base64 character set.

This seems to be bourne out as I can change the username and password
strings to 9 characters long and the problem
goes away make them 12 and it comes back.

The code walk through looks like this:

During the activation of the UserAdmin service the restoration of the
UserAdminStore the following pattern emerges:

UserAdminStore.java

/* modified to solve defect 95982 */
protected void loadRole(Preferences node, Role role) throws
BackingStoreException {

...

//load properties
for (int i = 0; i < keys.length; i++) {
value = propsNode.getByteArray(keys, null);
if (value == null)
value = propsNode.get(keys[i], null);
properties.put(keys[i], value, false);
}

OSGiPreferencesServiceImpl.java

public byte[] getByteArray(String key, byte[] defaultValue) {
String value = wrapped.get(key, null);
byte[] byteArray = null;
if (value != null) {
byte[] encodedBytes = value.getBytes();
if (encodedBytes.length % 4 == 0) {
try {
byteArray = Base64.decode(encodedBytes);
} catch (Exception e) {
//do not raise exception - return defaultValue
}
}
}
return byteArray == null ? defaultValue : byteArray;
}

So any String that is % 4 characters long and "Base64.decode"able will be
be restored as a Byte Array...

But:

UserAdmin.java

public org.osgi.service.useradmin.User getUser(String key, String value) {

...

props = user.getProperties();
keyValue = (String) props.get(key);

getUser will throw the classcast exception unless the property is a
String...

I assume this needs to be raised in Bugzilla??

>>>>>>>>>>>>>>>

Trying to resume bundle
initial@reference:file:../../../eworkspace/ws_sdk_debug/test.registration/
[27]
Bundle: Active sl = 4; Bundle 27 sl = 4
java.lang.ClassCastException: [B cannot be cast to java.lang.String
at
org.eclipse.equinox.internal.useradmin.UserAdmin.getUser(Use rAdmin.java:284)
at test.registration.Activator.configureRoles(Activator.java:51 )
at test.registration.Activator.start(Activator.java:38)
at
org.eclipse.osgi.framework.internal.core.BundleContextImpl$2 .run(BundleContextImpl.java:1009)
at java.security.AccessController.doPrivileged(Native Method)
at
org.eclipse.osgi.framework.internal.core.BundleContextImpl.s tartActivator(BundleContextImpl.java:1003)
at
org.eclipse.osgi.framework.internal.core.BundleContextImpl.s tart(BundleContextImpl.java:984)
at
org.eclipse.osgi.framework.internal.core.BundleHost.startWor ker(BundleHost.java:346)
at
org.eclipse.osgi.framework.internal.core.AbstractBundle.resu me(AbstractBundle.java:355)
at
org.eclipse.osgi.framework.internal.core.Framework.resumeBun dle(Framework.java:1074)
at
org.eclipse.osgi.framework.internal.core.StartLevelManager.r esumeBundles(StartLevelManager.java:616)
at
org.eclipse.osgi.framework.internal.core.StartLevelManager.i ncFWSL(StartLevelManager.java:508)
at
org.eclipse.osgi.framework.internal.core.StartLevelManager.d oSetStartLevel(StartLevelManager.java:299)
at
org.eclipse.osgi.framework.internal.core.StartLevelManager.d ispatchEvent(StartLevelManager.java:489)
at
org.eclipse.osgi.framework.eventmgr.EventManager.dispatchEve nt(EventManager.java:211)
at
org.eclipse.osgi.framework.eventmgr.EventManager$EventThread .run(EventManager.java:321)
Bundle resume exception: Exception in test.registration.Activator.start()
of bundle test.registration.


Thanks in advance

Ian
Re: Exception with UserAdmin service and getUser [message #125297 is a reply to message #125273] Thu, 29 January 2009 19:26 Go to previous message
Simon Kaegi is currently offline Simon KaegiFriend
Messages: 381
Registered: July 2009
Senior Member
Thanks Ian.
....and yes, please open bugs about this stuff.
-Simon

"Ian Carr" <icarr@focus-solutions.co.uk> wrote in message
news:a0bbeb5617f49f4ce05142c24a1db5ef$1@www.eclipse.org...
> Hi
>
> I am writing an application using the Lotus expeditor framework build on
> top of equinox. It's web
> application framework uses the osgi UserAdmin service to authenticate user
> logins.
>
> I am trying to use the code below to store user information into the
> equinox implementation.
>
>>>>>>>>>>
>
> private static final String USERNAME = "12345670";
> private static final String PASSWORD = "password";
>
> private static final String PROP_USERNAME = "username";
> private static final String CRED_PASSWORD = "password";
>
> ServiceReference ref =
> context.getServiceReference("org.osgi.service.useradmin.UserAdmin ");
>
> if (ref != null) {
> UserAdmin userAdmin = (UserAdmin) context.getService(ref);
>
> try {
> // does the user already exist?
> User user = userAdmin.getUser(PROP_USERNAME, USERNAME);
>
> if (user == null) {
>
> // didn't find him so create the set
> user = (User) userAdmin.createRole(USERNAME, Role.USER);
>
> // get the properties dictionary, add and set the username
> // property
> Dictionary<String, String> props = user.getProperties();
> props.put(PROP_USERNAME, USERNAME);
>
> // get the credentials dictionary, add and set the password
> // property
> Dictionary<String, String> creds = user.getCredentials();
> creds.put(CRED_PASSWORD, PASSWORD);
>
> }
> } finally {
> // don't need the service anymore.
> context.ungetService(ref);
> }
> }
>
>
>>>>>>>>>>>>>>>>
>
> This behaves correctly in a 'clean' environment ie if the configuration
> data does not already exist.
>
> The user is created and the web application goes on to be able to log the
> user on. So all seems fine whilst the original information is held in
> memory.
>
> The problem occurrs if I shutdown the application and start it again
> (reloading the UserAdminStore from the backing file)
> without cleaning out the preferences store information.
>
> In this case the request to find the user (userAdmin.getUser(property,
> name)) fails with a classcast exception in the UserAdmin service.
>
> The implementation is trying to cast a byte array (B]) to a String.
>
> looking at the equinox source this would appear to be because the values
> being stored are multiples of 4 bytes in length and calling Base64.decode
> does not throw an exception ie all the characters are in the base64
> character set.
>
> This seems to be bourne out as I can change the username and password
> strings to 9 characters long and the problem goes away make them 12 and it
> comes back.
>
> The code walk through looks like this:
>
> During the activation of the UserAdmin service the restoration of the
> UserAdminStore the following pattern emerges:
>
> UserAdminStore.java
>
> /* modified to solve defect 95982 */
> protected void loadRole(Preferences node, Role role) throws
> BackingStoreException {
>
> ...
>
> //load properties
> for (int i = 0; i < keys.length; i++) {
> value = propsNode.getByteArray(keys, null);
> if (value == null)
> value = propsNode.get(keys[i], null);
> properties.put(keys[i], value, false);
> }
>
> OSGiPreferencesServiceImpl.java
>
> public byte[] getByteArray(String key, byte[] defaultValue) {
> String value = wrapped.get(key, null);
> byte[] byteArray = null;
> if (value != null) {
> byte[] encodedBytes = value.getBytes();
> if (encodedBytes.length % 4 == 0) {
> try {
> byteArray = Base64.decode(encodedBytes);
> } catch (Exception e) {
> //do not raise exception - return defaultValue
> }
> }
> }
> return byteArray == null ? defaultValue : byteArray;
> }
>
> So any String that is % 4 characters long and "Base64.decode"able will be
> be restored as a Byte Array...
>
> But:
>
> UserAdmin.java
>
> public org.osgi.service.useradmin.User getUser(String key, String value) {
>
> ...
>
> props = user.getProperties();
> keyValue = (String) props.get(key);
>
> getUser will throw the classcast exception unless the property is a
> String...
>
> I assume this needs to be raised in Bugzilla??
>
>>>>>>>>>>>>>>>>
>
> Trying to resume bundle
> initial@reference:file:../../../eworkspace/ws_sdk_debug/test.registration/
> [27]
> Bundle: Active sl = 4; Bundle 27 sl = 4
> java.lang.ClassCastException: [B cannot be cast to java.lang.String
> at
> org.eclipse.equinox.internal.useradmin.UserAdmin.getUser(Use rAdmin.java:284)
> at test.registration.Activator.configureRoles(Activator.java:51 )
> at test.registration.Activator.start(Activator.java:38)
> at
> org.eclipse.osgi.framework.internal.core.BundleContextImpl$2 .run(BundleContextImpl.java:1009)
> at java.security.AccessController.doPrivileged(Native Method)
> at
> org.eclipse.osgi.framework.internal.core.BundleContextImpl.s tartActivator(BundleContextImpl.java:1003)
> at
> org.eclipse.osgi.framework.internal.core.BundleContextImpl.s tart(BundleContextImpl.java:984)
> at
> org.eclipse.osgi.framework.internal.core.BundleHost.startWor ker(BundleHost.java:346)
> at
> org.eclipse.osgi.framework.internal.core.AbstractBundle.resu me(AbstractBundle.java:355)
> at
> org.eclipse.osgi.framework.internal.core.Framework.resumeBun dle(Framework.java:1074)
> at
> org.eclipse.osgi.framework.internal.core.StartLevelManager.r esumeBundles(StartLevelManager.java:616)
> at
> org.eclipse.osgi.framework.internal.core.StartLevelManager.i ncFWSL(StartLevelManager.java:508)
> at
> org.eclipse.osgi.framework.internal.core.StartLevelManager.d oSetStartLevel(StartLevelManager.java:299)
> at
> org.eclipse.osgi.framework.internal.core.StartLevelManager.d ispatchEvent(StartLevelManager.java:489)
> at
> org.eclipse.osgi.framework.eventmgr.EventManager.dispatchEve nt(EventManager.java:211)
> at
> org.eclipse.osgi.framework.eventmgr.EventManager$EventThread .run(EventManager.java:321)
> Bundle resume exception: Exception in test.registration.Activator.start()
> of bundle test.registration.
>
>
> Thanks in advance
>
> Ian
>
Previous Topic:question about class loading
Next Topic:ConfigurationAdmin
Goto Forum:
  


Current Time: Thu Apr 25 15:29:18 GMT 2024

Powered by FUDForum. Page generated in 0.02707 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top