Skip to content

Commit

Permalink
[DROOLS-969] implement import tag in kie-aries-blueprint integration
Browse files Browse the repository at this point in the history
  • Loading branch information
mariofusco committed Jul 28, 2016
1 parent 67d7c0f commit 20ad89f
Show file tree
Hide file tree
Showing 25 changed files with 667 additions and 87 deletions.
12 changes: 12 additions & 0 deletions kie-aries-blueprint/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,18 @@
<scope>test</scope>
</dependency>-->

<!-- needed for annotations/releaseId tests-->
<dependency>
<groupId>org.kie</groupId>
<artifactId>kie-ci</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-core</artifactId>
<type>test-jar</type>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,35 @@
*/
package org.kie.aries.blueprint;

import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;

import org.apache.aries.blueprint.ParserContext;
import org.apache.aries.blueprint.mutable.MutableBeanMetadata;
import org.apache.aries.blueprint.mutable.MutablePassThroughMetadata;
import org.kie.aries.blueprint.namespace.*;
import org.kie.aries.blueprint.namespace.AbstractElementParser;
import org.kie.aries.blueprint.namespace.KieBaseElementParser;
import org.kie.aries.blueprint.namespace.KieContainerElementParser;
import org.kie.aries.blueprint.namespace.KieEnvironmentElementParser;
import org.kie.aries.blueprint.namespace.KieEventListenersElementParser;
import org.kie.aries.blueprint.namespace.KieImportElementParser;
import org.kie.aries.blueprint.namespace.KieModuleElementParser;
import org.kie.aries.blueprint.namespace.KieObjectsInjector;
import org.kie.aries.blueprint.namespace.KieRuntimeManagerElementParser;
import org.kie.aries.blueprint.namespace.KieRuntimeManagerSessionElementParser;
import org.kie.aries.blueprint.namespace.KieSessionElementParser;
import org.kie.aries.blueprint.namespace.KieStoreElementParser;
import org.kie.aries.blueprint.namespace.ReleaseIdElementParser;
import org.osgi.service.blueprint.container.ComponentDefinitionException;
import org.osgi.service.blueprint.reflect.ComponentMetadata;
import org.osgi.service.blueprint.reflect.Metadata;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;

public class KieNamespaceHandler implements org.apache.aries.blueprint.NamespaceHandler {

/** The list of Aries Blueprint XML files*/
Expand All @@ -43,7 +55,6 @@ public class KieNamespaceHandler implements org.apache.aries.blueprint.Namespace
private static final String BLUEPRINT_NS = "http://www.osgi.org/xmlns/blueprint/v1.0.0";

public static final String ELEMENT_RELEASE_ID = "releaseId";
public static final String ELEMENT_KBASE_REF = "kbase-ref";
public static final String ELEMENT_KBASE = "kbase";
public static final String ELEMENT_KCONTAINER = "kcontainer-ref";
public static final String ELEMENT_KSTORE = "kstore";
Expand All @@ -54,11 +65,11 @@ public class KieNamespaceHandler implements org.apache.aries.blueprint.Namespace
public static final String ELEMENT_KRUNTIMEMANAGER = "kruntimeManager";
public static final String ELEMENT_KSESSION_RUNTIMEMANAGER = "kruntimeManagerSession";
public static final String ELEMENT_KMODULE = "kmodule";
public static final String ELEMENT_IMPORT = "import";

protected static Map<String, AbstractElementParser> droolsElementParserMap = new HashMap<String, AbstractElementParser>();
static {
droolsElementParserMap.put(ELEMENT_RELEASE_ID, new ReleaseIdElementParser());
droolsElementParserMap.put(ELEMENT_KBASE_REF, new KieBaseElementParser());
droolsElementParserMap.put(ELEMENT_KBASE, new KieBaseElementParser());
droolsElementParserMap.put(ELEMENT_KSESSION, new KieSessionElementParser());
droolsElementParserMap.put(ELEMENT_KSESSION_REF, new KieSessionElementParser());
Expand All @@ -69,6 +80,7 @@ public class KieNamespaceHandler implements org.apache.aries.blueprint.Namespace
droolsElementParserMap.put(ELEMENT_KRUNTIMEMANAGER, new KieRuntimeManagerElementParser());
droolsElementParserMap.put(ELEMENT_KSESSION_RUNTIMEMANAGER, new KieRuntimeManagerSessionElementParser());
droolsElementParserMap.put(ELEMENT_KMODULE, new KieModuleElementParser());
droolsElementParserMap.put(ELEMENT_IMPORT, new KieImportElementParser() );
}

public KieNamespaceHandler() {
Expand Down Expand Up @@ -100,12 +112,14 @@ public Metadata parse(Element element, ParserContext parserContext) {
throw new ComponentDefinitionException("Unsupported Kie Blueprint Element '"+elementName+"'");
}
if (ELEMENT_KMODULE.equalsIgnoreCase(elementName)) {
addKieObjectsProcessor(element, parserContext, elementParser);
addProcessor( element, parserContext, elementParser, "afterKmoduleSet" );
} else if (ELEMENT_IMPORT.equalsIgnoreCase(elementName)) {
addProcessor( element, parserContext, elementParser, "afterImportSet" );
}
return elementParser.parseElement(parserContext, element);
}

private void addKieObjectsProcessor(Element element, ParserContext context, AbstractElementParser elementParser) {
private void addProcessor( Element element, ParserContext context, AbstractElementParser elementParser, String initMethodName ) {
// Register processors
MutablePassThroughMetadata beanProcessorFactory = context.createMetadata(MutablePassThroughMetadata.class);

Expand All @@ -121,7 +135,7 @@ private void addKieObjectsProcessor(Element element, ParserContext context, Abst
beanProcessor.setFactoryComponent(beanProcessorFactory);
beanProcessor.setFactoryMethod("call");
beanProcessor.setProcessor(true);
beanProcessor.setInitMethod("afterPropertiesSet");
beanProcessor.setInitMethod(initMethodName);
beanProcessor.addProperty("blueprintContainer", AbstractElementParser.createRef(context, "blueprintContainer"));
context.getComponentDefinitionRegistry().registerComponentDefinition(beanProcessor);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@
import org.osgi.framework.BundleContext;
import org.osgi.framework.wiring.BundleWiring;

import java.util.concurrent.Callable;

public abstract class AbstractKieObjectsResolver implements Callable<Object> {
public abstract class AbstractKieObjectsResolver implements Initializable {
private BundleContext bundleContext;

protected final ReleaseId releaseId;
Expand All @@ -57,19 +55,21 @@ public KieBase resolveKBase( String id, ReleaseId releaseId ) {
return kieBase;
}

public Object resolveKSession(String id, ReleaseId releaseId) {
KieContainer kieContainer = resolveKContainer(releaseId);
public Object resolveKSession( String id, ReleaseId releaseId ) {
return resolveKSession( id, resolveKContainer(releaseId) );
}

// KieBase kbase = resolveKBase(kbaseName, releaseId);
KieProject kProject = ((KieContainerImpl) kieContainer).getKieProject();
protected Object resolveKSession( String id, KieContainer kieContainer ) {
KieContainerImpl kcontainer = (KieContainerImpl) kieContainer;
KieProject kProject = kcontainer.getKieProject();
KieSessionModel kieSessionModel = kProject.getKieSessionModel( id );
if ( kieSessionModel == null) {
return null;
}
if (kieSessionModel.getType() == KieSessionModel.KieSessionType.STATEFUL) {
return ((KieContainerImpl) kieContainer).getKieSession(id);
return kcontainer.getKieSession( id );
} else if (kieSessionModel.getType() == KieSessionModel.KieSessionType.STATELESS) {
return ((KieContainerImpl) kieContainer).getStatelessKieSession(id);
return kcontainer.getStatelessKieSession( id );
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2016 Red Hat, Inc. and/or its affiliates.
*
* 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.aries.blueprint.factorybeans;

import org.kie.aries.blueprint.namespace.BlueprintContextHelper;

public interface Initializable {
Object init(BlueprintContextHelper context );
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.KieSessionConfiguration;
import org.kie.api.runtime.StatelessKieSession;
import org.kie.aries.blueprint.namespace.BlueprintContextHelper;

import java.util.Collection;
import java.util.Set;
Expand All @@ -44,7 +45,7 @@ public KieBaseResolver( ReleaseId releaseId, String id ) {
}

@Override
public Object call() throws Exception {
public Object init(BlueprintContextHelper context ) {
return getKieBase();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.KieSessionConfiguration;
import org.kie.api.runtime.StatelessKieSession;
import org.kie.aries.blueprint.namespace.BlueprintContextHelper;

import java.util.Collection;

Expand All @@ -39,7 +40,7 @@ public KieContainerResolver( ReleaseId releaseId ) {
}

@Override
public Object call() throws Exception {
public Object init(BlueprintContextHelper context ) {
return getKieContainer();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright 2016 Red Hat, Inc. and/or its affiliates.
*
* 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.aries.blueprint.factorybeans;


import org.kie.api.KieBase;
import org.kie.api.KieServices;
import org.kie.api.builder.KieScanner;
import org.kie.api.builder.ReleaseId;
import org.kie.api.runtime.KieContainer;
import org.kie.aries.blueprint.namespace.BlueprintContextHelper;

public class KieImportResolver extends AbstractKieObjectsResolver {

private final String releaseIdName;
private final boolean scannerEnabled;
private final long scannerInterval;

private KieContainer kieContainer;

public KieImportResolver( String releaseIdName, ReleaseId releaseId, boolean scannerEnabled, long scannerInterval ) {
super( releaseId );
this.releaseIdName = releaseIdName;
this.scannerEnabled = scannerEnabled;
this.scannerInterval = scannerInterval;
}

@Override
public Object init(BlueprintContextHelper context) {
KieContainer kContainer = registerKieContainer(context);
registerKieBases(context, kContainer);
return kContainer;
}

private synchronized KieContainer registerKieContainer(BlueprintContextHelper context) {
if (kieContainer == null) {
KieServices ks = KieServices.Factory.get();
if ( releaseId == null ) {
kieContainer = ks.getKieClasspathContainer();
} else {
kieContainer = resolveKContainer( releaseId );
if (scannerEnabled) {
KieScanner kieScanner = KieServices.Factory.get().newKieScanner( kieContainer );
context.registerBean(releaseIdName+"#scanner", kieScanner);
if (scannerInterval > 0) {
kieScanner.start( scannerInterval );
}
}
}
}
return kieContainer;
}

private void registerKieBases(BlueprintContextHelper context, KieContainer kContainer) {
for (String kieBaseName : kContainer.getKieBaseNames()) {
KieBase kieBase = kContainer.getKieBase( kieBaseName );
context.registerBean(kieBaseName, kieBase);
registerKieSessions(context, kieBaseName, kContainer);
}
}

private void registerKieSessions(BlueprintContextHelper context, String kieBaseName, KieContainer kContainer) {
for (String kieSessionName : kContainer.getKieSessionNamesInKieBase(kieBaseName)) {
Object ksession = resolveKSession(kieSessionName, kContainer);
context.registerBean(kieSessionName, ksession);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ public static ReleaseId createReleaseId(String id, String groupId, String artifa
return new ReleaseIdImpl(groupId, artifactId, version);
}

public static Object createImport(String releaseIdName, ReleaseId releaseId, boolean enableScanner, long scannerInterval) {
return new KieImportResolver( releaseIdName, releaseId, enableScanner, scannerInterval );
}

public static Environment createEnvironment(String id, HashMap<String, Object> parameters, List<Object> marshallingStrategies){
Environment environment = EnvironmentFactory.newEnvironment();
if ( parameters != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.kie.api.builder.ReleaseId;
import org.kie.api.event.KieRuntimeEventManager;
import org.kie.api.runtime.KieSession;
import org.kie.aries.blueprint.namespace.BlueprintContextHelper;
import org.osgi.service.blueprint.container.ComponentDefinitionException;

import java.util.List;
Expand All @@ -38,7 +39,7 @@ public KieSessionRefResolver( ReleaseId releaseId, String id, List<KieListenerAd
}

@Override
public Object call() throws Exception {
public Object init(BlueprintContextHelper context) {
Object obj = resolveKSession(id, releaseId);
if ( obj != null) {
KieSessionFactoryBeanHelper.addListeners((KieRuntimeEventManager) obj, listeners);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.kie.api.runtime.rule.QueryResults;
import org.kie.api.runtime.rule.ViewChangedEventListener;
import org.kie.api.time.SessionClock;
import org.kie.aries.blueprint.namespace.BlueprintContextHelper;

import java.util.Collection;
import java.util.List;
Expand All @@ -67,7 +68,7 @@ public KieSessionResolver( ReleaseId releaseId, List<KieListenerAdaptor> listene
}

@Override
public Object call() throws Exception {
public Object init(BlueprintContextHelper context) {
return getSession();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,15 @@ public static ValueMetadata createValue(ParserContext context, int value) {
return createValue(context, value, "java.lang.Integer");
}

public static ValueMetadata createValue(ParserContext context, int value, String type) {
public static ValueMetadata createValue(ParserContext context, long value) {
return createValue(context, value, "java.lang.Long");
}

public static ValueMetadata createValue(ParserContext context, boolean value) {
return createValue(context, value, "java.lang.Boolean");
}

public static ValueMetadata createValue(ParserContext context, Object value, String type) {
MutableValueMetadata m = context.createMetadata(MutableValueMetadata.class);
m.setStringValue(""+value);
m.setType(type);
Expand Down
Loading

0 comments on commit 20ad89f

Please sign in to comment.