use of org.osgi.service.useradmin.User in project felix by apache.
the class UserManager method addCredential.
public void addCredential(String key, Object value, String username) throws IllegalArgumentException {
try {
if (value instanceof Byte[]) {
Byte[] ByteValue = (Byte[]) value;
byte[] primitive = new byte[ByteValue.length];
for (int i = 0; i < ByteValue.length; i++) primitive[i] = ByteValue[i].byteValue();
value = primitive;
} else if (!(value instanceof String) && !(value instanceof byte[]))
throw new IllegalArgumentException("Credentials can only be byte[] or String");
User user = (User) ac.getUserAdmin().getRole(username);
user.getCredentials().put(key, value);
} catch (NullPointerException npe) {
ac.debug("UserAdmin not available. ");
}
}
use of org.osgi.service.useradmin.User in project felix by apache.
the class RoleRepositorySerializer method writeRepository.
/**
* Writes the given repository to the given output stream.
*
* @param repository the repository to write, cannot be <code>null</code>;
* @param dos the output stream to write the data to, cannot be <code>null</code>.
* @throws IOException in case of I/O problems.
*/
private void writeRepository(Map repository, DataOutputStream dos) throws IOException {
Collection values = repository.values();
Iterator valuesIter = values.iterator();
// Write the total number of entries in our repository first...
dos.writeInt(values.size());
while (valuesIter.hasNext()) {
Role role = (Role) valuesIter.next();
int type = role.getType();
dos.writeInt(type);
if (Role.GROUP == type) {
writeGroup((Group) role, dos);
} else if (Role.USER == type) {
writeUser((User) role, dos);
} else {
writeRole(role, dos);
}
}
}
use of org.osgi.service.useradmin.User in project felix by apache.
the class RoleRepositorySerializer method readUser.
/**
* Reads a user from the given input stream.
*
* @param dis the input stream to read the data from, cannot be <code>null</code>.
* @return the read user, never <code>null</code>.
* @throws IOException in case of I/O problems.
*/
private User readUser(DataInputStream dis) throws IOException {
User user = (User) RoleFactory.createRole(Role.USER, dis.readUTF());
readDictionary(user.getProperties(), dis);
readDictionary(user.getCredentials(), dis);
return user;
}
use of org.osgi.service.useradmin.User in project felix by apache.
the class RoleRepositorySerializerTest method createUser.
private User createUser(int idx) {
String name = "User" + idx;
User result = RoleFactory.createUser(name);
setCredentials(result);
setProperties(result);
return result;
}
use of org.osgi.service.useradmin.User in project felix by apache.
the class FileStoreInitializationTest method testStoreIsInitializedAndClosedProperlyOk.
/**
* Tests that initialization and closing of the repository store is
* performed correctly.
*/
@Test
public void testStoreIsInitializedAndClosedProperlyOk() throws Exception {
UserAdmin ua = getUserAdmin();
// Start the file store bundle...
Bundle fileStoreBundle = getFileStoreBundle();
fileStoreBundle.start();
// Create two roles...
User user = (User) ua.createRole("user1", Role.USER);
assertNotNull(user);
Group group = (Group) ua.createRole("group1", Role.GROUP);
assertNotNull(group);
group.addMember(user);
group.addRequiredMember(ua.getRole(Role.USER_ANYONE));
// Stop the file store; should persist the two roles...
fileStoreBundle.stop();
// Wait a little until the bundle is really stopped...
Thread.sleep(100);
// Retrieve the roles again; should both yield null due to the store not being available...
user = (User) ua.getRole("user1");
assertNull(user);
group = (Group) ua.getRole("group1");
assertNull(group);
// This will not succeed: no backend to store the user in...
assertNull(ua.createRole("user2", Role.USER));
fileStoreBundle.start();
awaitService(ORG_APACHE_FELIX_USERADMIN_FILESTORE);
// Retrieve the roles again; should both yield valid values...
user = (User) ua.getRole("user1");
assertNotNull(user);
group = (Group) ua.getRole("group1");
assertNotNull(group);
Role[] members = group.getMembers();
assertNotNull(members);
assertEquals(1, members.length);
assertEquals("user1", members[0].getName());
members = group.getRequiredMembers();
assertNotNull(members);
assertEquals(1, members.length);
assertEquals(Role.USER_ANYONE, members[0].getName());
user = (User) ua.getRole("user2");
assertNull(user);
}
Aggregations