Skip to content

Instantly share code, notes, and snippets.

@xhanin
Created February 12, 2015 12:55
Show Gist options
  • Save xhanin/6e14ca54951e285bf0e6 to your computer and use it in GitHub Desktop.
Save xhanin/6e14ca54951e285bf0e6 to your computer and use it in GitHub Desktop.
restx spring integration
package restx.spring;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import javax.servlet.ServletContextEvent;
/**
* User: xavierhanin
* Date: 5/6/13
* Time: 6:58 PM
*/
public class RestxSpringContextLoaderListener extends ContextLoaderListener {
@Override
public void contextInitialized(ServletContextEvent event) {
super.contextInitialized(event);
WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
SpringMachine.setBeanFactory(applicationContext);
}
}
package restx.spring;
import com.google.common.base.Optional;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.beans.factory.ListableBeanFactory;
import restx.factory.*;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;
/**
* User: xavierhanin
* Date: 5/6/13
* Time: 6:25 PM
*/
@Machine
public class SpringMachine implements FactoryMachine {
private static ListableBeanFactory beanFactory;
public static void setBeanFactory(ListableBeanFactory beanFactory) {
SpringMachine.beanFactory = beanFactory;
}
public static Optional<ListableBeanFactory> getBeanFactory() {
return Optional.fromNullable(beanFactory);
}
@Override
public boolean canBuild(Name<?> name) {
Optional<ListableBeanFactory> factory = getBeanFactory();
if (!factory.isPresent()) {
return false;
}
try {
return factory.get().getBean(name.getName(), name.getClazz()) != null;
} catch (BeansException e) {
return false;
}
}
@Override
public <T> MachineEngine<T> getEngine(final Name<T> name) {
final Optional<ListableBeanFactory> factory = getBeanFactory();
if (!factory.isPresent()) {
throw new IllegalArgumentException(name + " is not supported");
}
return new NoDepsMachineEngine<T>(name, DisposableComponentBox.FACTORY) {
@Override
protected T doNewComponent(SatisfiedBOM satisfiedBOM) {
return factory.get().getBean(name.getName(), name.getClazz());
}
};
}
@Override
public <T> Set<Name<T>> nameBuildableComponents(Class<T> tClass) {
Optional<ListableBeanFactory> factory = getBeanFactory();
if (!factory.isPresent()) {
return Collections.emptySet();
}
String[] names = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(factory.get(), tClass);
Set<Name<T>> namesSet = new LinkedHashSet<>();
for (String name : names) {
namesSet.add(Name.of(tClass, name));
}
return namesSet;
}
@Override
public int priority() {
return 0;
}
}
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0" metadata-complete="true">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>restx.spring.RestxSpringContextLoaderListener</listener-class>
</listener>
[...]
</web-app>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment