/**
* OLAT - Online Learning and Training<br>
* http://www.olat.org
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); <br>
* you may not use this file except in compliance with the License.<br>
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing,<br>
* software distributed under the License is distributed on an "AS IS" BASIS, <br>
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
* See the License for the specific language governing permissions and <br>
* limitations under the License.
* <p>
* Copyright (c) 1999-2006 at Multimedia- & E-Learning Services (MELS),<br>
* University of Zurich, Switzerland.
* <p>
*/
package org.olat.core.service.impl;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.olat.core.logging.AssertException;
import org.olat.core.logging.Tracing;
import org.olat.core.service.ServiceDescriptor;
import org.olat.core.service.ServiceFactory;
import org.olat.core.service.ServiceRegistry;
/**
* Description:<br>
* TODO: Felix Jost Class Description for Trans
*
* <P>
* Initial Date: 12.03.2007 <br>
* @author Felix Jost, http://www.goodsolutions.ch
*/
public class ServiceRegistryImpl implements ServiceRegistry {
private List<ServiceDescriptor> serviceDescriptors = new ArrayList<ServiceDescriptor>();
private Set<String> okClasses = new HashSet<String>();
/**
*
*/
public ServiceRegistryImpl() {
super();
// TODO Auto-generated constructor stub
}
/* (non-Javadoc)
* @see org.olat.core.service.ServiceRegistry#registerService(org.olat.core.service.ServiceDescriptor)
*/
public void registerService(ServiceDescriptor serviceDescriptor) {
serviceDescriptors.add(serviceDescriptor);
}
/* (non-Javadoc)
* @see org.olat.core.service.ServiceRegistry#validateDependencies()
*/
public void validateDependencies() {
Exception e = null;
ServiceDescriptor sd = null;
for (int i = 0; e == null && i < serviceDescriptors.size(); i++) {
// access the interface class of each service to verify that it can be reached by the classloader
sd = serviceDescriptors.get(i);
e = validateService(sd);
if (e == null) {
Tracing.logInfo("SERVICE OK:" + sd.getServiceClassName()+" v"+sd.getMajorVersion()+"."+sd.getMinorVersion()+" description: "+sd.getDescription(), this.getClass());
}
}
if (e != null) {
throw new AssertException("invalid service: name '"+sd.getServiceClassName()+"' , spring exception = "+e);
}
}
private Exception validateService(ServiceDescriptor sd) {
String className = sd.getServiceClassName();
// 1. check in cache
if (okClasses.contains(className)) return null;
// 2. validate service
try {
ServiceFactory.getService(className);
okClasses.add(className);
} catch (Exception e) {
return e;
}
// 3. validate dependencies
String[] cns = sd.getDependentServiceClassNames();
if (cns == null) {
// no dependent services
return null;
}
for (int i = 0; i < cns.length; i++) {
String dependentClassName = cns[i];
if (!okClasses.contains(dependentClassName)) {
try {
ServiceFactory.getService(dependentClassName);
okClasses.add(dependentClassName);
} catch (Exception e) {
return e;
}
}
}
return null;
}
}