{
// Getting the connection to the Registry (reading config)
Connection connection = JAXRConnectionSingleton.getConnection(jaxrConnectionFactory);
try {
BulkResponse br = null;
//Logging in
RegistryService rs = connection.getRegistryService();
//Building organization
BusinessLifeCycleManager blm = rs.getBusinessLifeCycleManager();
final String interfaceName ;
if (registryObject instanceof Organization) {
interfaceName = Organization.class.getCanonicalName() ;
Collection<Organization> orginations = new ArrayList<Organization>();
orginations.add((Organization) registryObject);
br = blm.saveOrganizations(orginations);
} else if (registryObject instanceof Service) {
interfaceName = Organization.class.getCanonicalName() ;
Collection<Service> services = new ArrayList<Service>();
services.add((Service) registryObject);
br = blm.saveServices(services);
} else if (registryObject instanceof ServiceBinding) {
interfaceName = Organization.class.getCanonicalName() ;
Collection<ServiceBinding> serviceBindings = new ArrayList<ServiceBinding>();
serviceBindings.add((ServiceBinding) registryObject);
br = blm.saveServiceBindings(serviceBindings);
} else {
logger.log(Level.ERROR, "Trying to save an unsupported RegistryObject");
throw new JAXRException("Trying to save an unsupported RegistryObject");
}
//Verify the return
if (br!=null && br.getStatus() == JAXRResponse.STATUS_SUCCESS) {
logger.log(Level.DEBUG, interfaceName + " successfully saved");
Collection coll = br.getCollection();
Iterator iter = coll.iterator();
while (iter.hasNext()) {
Key key = (Key) iter.next();
registryObject.setKey(key);
logger.log(Level.DEBUG, "Saved Key=" + key.getId());
}
} else {
logger.log(Level.ERROR, "Errors occurred during save.");
if (br!=null) {
Collection exceptions = br.getExceptions();
if (exceptions != null) {
Iterator iter = exceptions.iterator();
StringBuilder errors = new StringBuilder();
JAXRException je = new JAXRException("JAXRExceptions occurred during save");
while (iter.hasNext()) {
Exception e = (Exception) iter.next();
errors.append(e.getLocalizedMessage()).append("\n");
je.setStackTrace(e.getStackTrace());
logger.log(Level.ERROR, e.getLocalizedMessage(), e);
//if it's the last error, throw it now and set the current stacktrace
if (!iter.hasNext()) {
throw new JAXRException(errors.toString(), e);
}
}
}
throw new JAXRException("Errors occurred during save. Response status=" + br.getStatus());
}
throw new JAXRException("Errors occurred during save");
}
} finally {
JAXRConnectionSingleton.recycleConnection(jaxrConnectionFactory, connection);