
org.ow2.bonita.identity.IdentityService Maven / Gradle / Ivy
/**
* Copyright (C) 2007 Bull S. A. S.
* Bull, Rue Jean Jaures, B.P.68, 78340, Les Clayes-sous-Bois
* This library is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation
* version 2.1 of the License.
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License along with this
* program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
* Floor, Boston, MA 02110-1301, USA.
**/
package org.ow2.bonita.identity;
import java.lang.management.ManagementFactory;
import java.util.Collection;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.management.InstanceAlreadyExistsException;
import javax.management.MBeanRegistrationException;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.NotCompliantMBeanException;
import javax.management.ObjectName;
import javax.management.StandardMBean;
import org.ow2.bonita.pvm.env.Environment;
import org.ow2.bonita.pvm.env.EnvironmentFactory;
import org.ow2.bonita.pvm.env.PvmEnvironmentFactory;
import org.ow2.bonita.pvm.env.Transaction;
import org.ow2.bonita.util.Misc;
/**
* @author "Pierre Vigneras"
* @date Dec 6, 2007
*/
public class IdentityService implements IdentityServiceMBean {
public static final String CONFIGURATION = "conf/idServiceConfig.xml";
private final IdentityServiceOp service;
private static final Logger logger = Logger.getLogger(IdentityService.class
.getName());
public IdentityService() {
final EnvironmentFactory factory = new PvmEnvironmentFactory(CONFIGURATION);
Misc.badStateIfNull(factory, "Can't parse the environment from resource: "
+ CONFIGURATION);
Environment environment = null;
try {
environment = factory.openEnvironment();
this.service = environment.get(IdentityServiceOp.class);
logger.config("Service: " + service + " taken from environment: "
+ environment);
} catch (Exception e) {
if (environment != null) {
Transaction t = environment.get(Transaction.class);
if (t != null) {
t.setRollbackOnly();
}
}
if (e instanceof RuntimeException) {
throw (RuntimeException) e;
} else {
throw new RuntimeException(e);
}
} finally {
if (environment != null) {
environment.close();
}
}
}
public void commit() throws CommitException {
service.commit();
}
private static void registerMBean(StandardMBean mbean, String bindingName) {
try {
final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
final ObjectName name = new ObjectName(bindingName);
mbs.registerMBean(mbean, name);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private void registerGroupMBean(final GroupOp groupOp) {
try {
registerMBean(new StandardMBean(new Group(groupOp, service),
GroupMBean.class), "org.ow2.bonita.identity.mbean:type=Group,id="
+ groupOp.getId());
} catch (NotCompliantMBeanException e) {
throw new RuntimeException(e);
}
}
private void registerUserMBean(final UserOp userOp) {
try {
registerMBean(new StandardMBean(new User(userOp, service),
UserMBean.class), "org.ow2.bonita.identity.mbean:type=User,id="
+ userOp.getId());
} catch (NotCompliantMBeanException e) {
throw new RuntimeException(e);
}
}
public String createGroup() {
final String id = service.createGroup();
try {
registerGroupMBean(service.getGroup(id));
} catch (GroupNotFoundException gnfe) {
throw new RuntimeException(gnfe);
}
return id;
}
public String createGroup(String parentId) {
try {
GroupOp parentGroup = service.getGroup(parentId);
final String groupId = service.createGroup(parentGroup);
registerGroupMBean(service.getGroup(groupId));
return groupId;
} catch (GroupNotFoundException gnfe) {
throw new RuntimeException(gnfe);
}
}
public String createUser() {
final String id = service.createUser();
try {
registerUserMBean(service.getUser(id));
} catch (UserNotFoundException unfe) {
throw new RuntimeException(unfe);
}
return id;
}
public Collection getAllGroups() {
final Collection groupOps = service.getAllGroups();
final Collection groupsId = new LinkedList();
for (GroupOp g : groupOps) {
groupsId.add(g.getId());
}
return groupsId;
}
public Collection getAllUsers() {
final Collection userOps = service.getAllUsers();
final Collection usersId = new LinkedList();
for (UserOp u : userOps) {
usersId.add(u.getId());
}
return usersId;
}
public void setMembership(String userId, String groupId) {
try {
final GroupOp groupOp = service.getGroup(groupId);
final UserOp userOp = service.getUser(userId);
service.setMembership(userOp, groupOp);
} catch (GroupNotFoundException gnfe) {
throw new RuntimeException(gnfe);
} catch (UserNotFoundException unfe) {
throw new RuntimeException(unfe);
}
}
public static void newServer() throws MBeanRegistrationException {
final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
final String bindingName = "org.ow2.bonita.mbean:type=IdentityService";
ObjectName name;
try {
name = new ObjectName(bindingName);
} catch (MalformedObjectNameException mone) {
final Error e = new Error("Ouch! How can it be that my object name: "
+ bindingName + " is malformed?");
e.initCause(mone);
throw e;
}
final IdentityService mbean = new IdentityService();
try {
mbs.registerMBean(mbean, name);
Collection ids = mbean.getAllGroups();
for (String id : ids) {
GroupOp group = null;
try {
group = mbean.service.getGroup(id);
mbean.registerGroupMBean(group);
} catch (GroupNotFoundException gnfe) {
logger
.log(
Level.INFO,
"The group: "
+ group
+ " is no more mapped in the IdentityService. Concurrent access?");
}
}
ids = mbean.getAllUsers();
for (String id : ids) {
UserOp user = null;
try {
user = mbean.service.getUser(id);
mbean.registerUserMBean(user);
} catch (UserNotFoundException e) {
logger
.log(
Level.INFO,
"The group: "
+ user
+ " is no more mapped in the IdentityService. Concurrent access?");
}
}
} catch (NotCompliantMBeanException ncme) {
final Error e = new Error("Ouch! How can it be that my mbean: " + mbean
+ " is not compliant?!");
e.initCause(ncme);
} catch (InstanceAlreadyExistsException e) {
logger.log(Level.WARNING,
"An IdentityService Bean has already been registered with the name "
+ name + " in the MBean server: " + mbs + ". Ignoring.");
}
try {
logger.info("Waiting for clients");
Thread.sleep(Long.MAX_VALUE);
} catch (InterruptedException e) {
logger.log(Level.WARNING, "Interrupted! Exiting.", e);
}
}
public static void usage() {
System.err
.println("Usage: " + IdentityService.class.getName() + " [-test]");
System.exit(1);
}
public static void main(String[] args) throws MBeanRegistrationException {
if (args.length == 0) {
IdentityService.newServer();
} else if (args[1].equals("-test")) {
IdentityService.testServer();
} else
usage();
}
private static void testServer() {
}
public String getImplName() {
return service.getClass().getName();
}
/*
* (non-Javadoc)
*
* @see
* org.ow2.bonita.identity.IdentityServiceMBean#getGroup(java.lang.String)
*/
public GroupOp getGroup(String id) throws GroupNotFoundException {
return service.getGroup(id);
}
/*
* (non-Javadoc)
*
* @see org.ow2.bonita.identity.IdentityServiceMBean#getUser(java.lang.String)
*/
public UserOp getUser(String id) throws UserNotFoundException {
return service.getUser(id);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy