Skip to content

Instantly share code, notes, and snippets.

@vinodkiran
Created October 2, 2014 12:01
Show Gist options
  • Select an option

  • Save vinodkiran/f3b4ab7c30d64375e43d to your computer and use it in GitHub Desktop.

Select an option

Save vinodkiran/f3b4ab7c30d64375e43d to your computer and use it in GitHub Desktop.
/*
* Copyright 2013 JBoss Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.kie.spring.factorybeans;
import org.drools.compiler.kie.builder.impl.*;
import org.drools.compiler.kie.builder.impl.event.KieModuleDiscovered;
import org.drools.compiler.kie.builder.impl.event.KieServicesEventListerner;
import org.drools.core.io.impl.ClassPathResource;
import org.drools.persistence.jpa.KnowledgeStoreServiceImpl;
import org.kie.api.KieBase;
import org.kie.api.KieServices;
import org.kie.api.builder.KieModule;
import org.kie.api.builder.KieRepository;
import org.kie.api.builder.ReleaseId;
import org.kie.api.builder.model.KieBaseModel;
import org.kie.api.persistence.jpa.KieStoreServices;
import org.kie.api.runtime.KieContainer;
import org.kie.spring.KieSpringUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import java.util.ArrayList;
import java.util.List;
public class KieImportFactoryBean
implements
FactoryBean<KieContainer>,
InitializingBean, BeanFactoryPostProcessor, ApplicationContextAware {
private ReleaseId releaseId;
private KieContainer kContainer;
private ApplicationContext applicationContext;
public ReleaseId getReleaseId() {
return releaseId;
}
public void setReleaseId(ReleaseId releaseId) {
this.releaseId = releaseId;
}
public KieContainer getObject() throws Exception {
return kContainer;
}
public Class<? extends KieContainer> getObjectType() {
return KieContainer.class;
}
public boolean isSingleton() {
return true;
}
public void afterPropertiesSet() throws Exception {
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
System.out.println(":: KieImportFactoryBean.setApplicationContext() ::");
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
System.out.println(":: KieImportFactoryBean.postProcessBeanFactory() ::");
registerKieBases(configurableListableBeanFactory);
}
private void registerKieBases(ConfigurableListableBeanFactory configurableListableBeanFactory) {
if (releaseId != null) {
System.out.println(":: KieImportFactoryBean.postProcessBeanFactory() ReleaseIdKieContainer::");
kContainer = KieServices.Factory.get().newKieContainer(releaseId);
} else {
System.out.println(":: KieImportFactoryBean.postProcessBeanFactory() ClasspathKieContainer::");
kContainer = KieServices.Factory.get().getKieClasspathContainer();
}
KieBase kieBase = kContainer.getKieBase("kbase1");
configurableListableBeanFactory.registerSingleton("kbase1", kieBase);
//to find an API to get the KieBases from the kieContainer
//something like kieContainer.getAllKieBases() or something similar
//for (String kieBaseName : kieContainer.getKieBaseNames()) {
// KieBase kieBase = kieContainer.getKieBase("kbase1");
// configurableListableBeanFactory.registerSingleton("kbase1", kieBase);
//}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment