/**
* 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;
import java.net.MalformedURLException;
import javax.servlet.ServletContext;
import org.olat.core.CoreSettings;
import org.olat.core.logging.AssertException;
import org.olat.core.logging.Tracing;
import org.olat.core.util.WebappHelper;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Description:<br>
* TODO: patrickb Class Description for CoreSpringFactory
* <P>
* Initial Date: 12.06.2006 <br>
*
* @author patrickb
*/
public class ServiceFactory {
private static ApplicationContext ac;
private static CoreSettings coreSettings;
private ServiceFactory() {
// do not create instance
}
/**
* @param beanName
* @return
*/
public static Object getService(String beanName) {
Object o = ac.getBean(beanName);
return o;
}
/**
* gets the service for the given interface class
* @param serviceClass e.g. "ch.goodsolutions.services.user.bl.UserSearchBLService.class"
* @return the implementation of the service
*/
public static Object getService(Class serviceClass) {
Object o = ac.getBean(serviceClass.getName());
return o;
}
/**
* "safe" method to look for a service - to be used if services are optional, like e.g. the pre and post request hooks.
* gets the service for the given interface class
* @param serviceClass e.g. "ch.goodsolutions.services.user.bl.UserSearchBLService.class"
* @return the implementation of the service or null if the service does not exist.
*/
public static Object getServiceOrNull(Class serviceClass) {
Object o = null;
try {
o = ac.getBean(serviceClass.getName());
} catch(NoSuchBeanDefinitionException nsbde) {
// ignore
}
return o;
}
/**
* @param servletContext
*/
public static void init(final ServletContext servletContext) {
// expose coresettings via factory-method to spring context
coreSettings = new CoreSettings() {
public ServletContext getServletContext() {
return servletContext;
}
};
try {
// let spring do the work
ac = new ClassPathXmlApplicationContext(new String[] {
"classpath*:serviceconfig/brasatoconfig.xml",
"classpath*:**/_spring/brasatoconfigpart.xml",
"classpath*:serviceconfig/**/_spring/brasatoconfigpart.xml"
});
} catch (Exception e) {
e.printStackTrace(System.out);
Tracing.logError("error in spring configuration!", e, ServiceFactory.class);
throw new AssertException("error in spring configuration:", e);
}
try {
// extract context path and set it in webapphelper
String contextPath = "";
String path = servletContext.getResource("/").getPath();
// Path will look like this: /localhost/CONTEXTPATH/
// Special case is the ROOT context, there it will look like this: /localhost/
// first get rid of the last /
contextPath = path.substring(0, path.lastIndexOf("/"));
// second get rid of the /localhost part
if (contextPath.indexOf("/", 1) == -1) {
// ROOT context
contextPath = "";
} else {
// should now be "/CONTEXTPATH"
contextPath = contextPath.substring(contextPath.indexOf("/", 1));
}
WebappHelper.setServletContextPath(contextPath);
} catch (NullPointerException e) {
Tracing.logWarn("When running in Junit-Test Mode, NullPointerException is ok", e, ServiceFactory.class);
} catch (MalformedURLException e) {
Tracing.logError("error in spring configuration! Could not build contextPath!", e, ServiceFactory.class);
throw new AssertException("error in spring configuration! Could not build contextPath!", e);
}
try {
// post-init: verify services
ServiceRegistry serviceRegistry = (ServiceRegistry) getService(org.olat.core.service.ServiceRegistry.class);
serviceRegistry.validateDependencies();
} catch (Exception e) {
Tracing.logError("error in service configuration!", e, ServiceFactory.class);
throw new AssertException("error in service configuration:", e);
}
}
/**
* [used by spring]
* @return Returns the coreSettings.
*/
public CoreSettings getCoreSettings() {
return coreSettings;
}
/**
* wrapper to the applicationcontext (we are facading spring's
* applicationcontext)
*
* @param path a path in spring notation (e.g. "classpath*:/*.hbm.xml", see
* springframework.org)
* @return the resources found
*/
// TODO:REVIEW:02-2008:Code Cleanup
/*public static Resource[] getResources(String path) {
Resource[] res;
try {
res = ac.getResources(path);
} catch (IOException e) {
throw new AssertException("i/o error while asking for resources, path:" + path);
}
return res;
}*/
}