OW2 Consortium jasmine

Compare Revisions

Ignore whitespace Rev 7288 → Rev 7289

/jasmine-selfmanagement/trunk/jasmine-selfmanagement/Drools_for_JOnAS/modules/osgi/src/main/java/org/ow2/jasmine/rules/osgi/impl/BundlePackageAdder.java
1,153 → 1,153
/**
* JASMINe
* Copyright (C) 2008-2009 Bull S.A.S.
* Contact: jasmine@ow2.org
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*
* --------------------------------------------------------------------------
* $Id$
* --------------------------------------------------------------------------
*/
package org.ow2.jasmine.rules.osgi.impl;
 
import java.net.URL;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
 
import org.drools.KnowledgeBase;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.ResourceType;
import org.drools.definition.KnowledgePackage;
import org.drools.io.ResourceFactory;
import org.osgi.framework.Bundle;
import org.ow2.jasmine.rules.osgi.api.IPackageAdder;
 
/**
* Implement a service to add {@link KnowledgePackage} into a {@link KnowledgeBase}.
* @author Loris Bouzonnet
*/
public class BundlePackageAdder extends AbstractPackageAdder
implements IPackageAdder {
 
// -------------------------------------------------------------------------
// Variables
// -------------------------------------------------------------------------
 
/**
* Logger
*/
private static Logger logger =
Logger.getLogger(BundlePackageAdder.class.getName());
 
 
// -------------------------------------------------------------------------
// Constructors
// -------------------------------------------------------------------------
 
/**
* Create a new package adder to add rules belonging to a given bundle
* into a working memory.
*
* @param bundle the bundle containing rules
* @param globals the globals
* @param the targeted working memory name
*/
public BundlePackageAdder(final Bundle bundle,
Map<String, Object> globals) {
 
super(bundle, globals);
}
 
/**
* Create a new package adder to add rules belonging to a given bundle
* into the default working memory.
*
* @param bundle the bundle containing rules
* @param globals the globals
*/
public BundlePackageAdder(final Bundle bundle,
Map<String, Object> globals,
final String workingMemoryName) {
 
super(bundle, globals, workingMemoryName);
}
 
 
// -------------------------------------------------------------------------
// Implementation of IPackageAdder
// -------------------------------------------------------------------------
 
/**
* Add some knowledge packages into the provided {@link KnowledgeBase}, by using the
* given {@link KnowledgeBuilder}.
*
* @param builder a knowledge builder
* @param knowledgeBase a knowledge base
*/
public void addPackage(final KnowledgeBuilder builder, final KnowledgeBase ruleBase) {
 
// List all DRL files in the bundle
Enumeration<?> drlPaths = bundle.findEntries("/", "*.drl", true);
 
if(drlPaths != null) {
while(drlPaths.hasMoreElements()) {
URL drlPath = (URL) drlPaths.nextElement();
 
// Search for a domain description file
String externalForm = drlPath.toExternalForm();
URL dslPath = bundle.getEntry(
externalForm.substring(0, externalForm.length() - 4) + ".dsl");
 
if(dslPath != null) {
builder.add(ResourceFactory.newUrlResource(dslPath), ResourceType.DSL);
logger.log(Level.INFO, dslPath.getPath() + " loaded");
}
builder.add(ResourceFactory.newUrlResource(drlPath), ResourceType.DRL);
logger.log(Level.INFO, drlPath.getPath() + " loaded");
}
}
 
// List all DRL XML files in the bundle
Enumeration<?> drlXmlPaths = bundle.findEntries("/", "*.drl.xml", true);
 
if(drlXmlPaths != null) {
while(drlXmlPaths.hasMoreElements()) {
URL drlXmlPath = (URL) drlXmlPaths.nextElement();
builder.add(ResourceFactory.newUrlResource(drlXmlPath), ResourceType.XDRL);
logger.log(Level.INFO, drlXmlPath.getPath() + " loaded");
}
}
 
if (builder.hasErrors()) {
logger.log(Level.WARNING, builder.getErrors().toString());
// TODO We do not exit with an Exception ??
}
 
Collection<KnowledgePackage> packages = builder.getKnowledgePackages();
ruleBase.addKnowledgePackages(packages);
 
// Log installed KnowledgePackage
logger.log(Level.FINE, "Installed KnowledgePackages ({0}):", new Object[] {packages.size()});
for (KnowledgePackage kp : packages) {
logger.log(Level.FINE, " * KnowledgePackage[{0}]", new Object[] {kp});
}
}
 
}
/**
* JASMINe
* Copyright (C) 2008-2009 Bull S.A.S.
* Contact: jasmine@ow2.org
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*
* --------------------------------------------------------------------------
* $Id$
* --------------------------------------------------------------------------
*/
package org.ow2.jasmine.rules.osgi.impl;
 
import java.net.URL;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
 
import org.drools.KnowledgeBase;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.ResourceType;
import org.drools.definition.KnowledgePackage;
import org.drools.io.ResourceFactory;
import org.osgi.framework.Bundle;
import org.ow2.jasmine.rules.osgi.api.IPackageAdder;
 
/**
* Implement a service to add {@link KnowledgePackage} into a {@link KnowledgeBase}.
* @author Loris Bouzonnet
*/
public class BundlePackageAdder extends AbstractPackageAdder
implements IPackageAdder {
 
// -------------------------------------------------------------------------
// Variables
// -------------------------------------------------------------------------
 
/**
* Logger
*/
private static Logger logger =
Logger.getLogger(BundlePackageAdder.class.getName());
 
 
// -------------------------------------------------------------------------
// Constructors
// -------------------------------------------------------------------------
 
/**
* Create a new package adder to add rules belonging to a given bundle
* into a working memory.
*
* @param bundle the bundle containing rules
* @param globals the globals
* @param the targeted working memory name
*/
public BundlePackageAdder(final Bundle bundle,
Map<String, Object> globals) {
 
super(bundle, globals);
}
 
/**
* Create a new package adder to add rules belonging to a given bundle
* into the default working memory.
*
* @param bundle the bundle containing rules
* @param globals the globals
*/
public BundlePackageAdder(final Bundle bundle,
Map<String, Object> globals,
final String workingMemoryName) {
 
super(bundle, globals, workingMemoryName);
}
 
 
// -------------------------------------------------------------------------
// Implementation of IPackageAdder
// -------------------------------------------------------------------------
 
/**
* Add some knowledge packages into the provided {@link KnowledgeBase}, by using the
* given {@link KnowledgeBuilder}.
*
* @param builder a knowledge builder
* @param knowledgeBase a knowledge base
*/
public void addPackage(final KnowledgeBuilder builder, final KnowledgeBase ruleBase) {
 
// List all DRL files in the bundle
Enumeration<?> drlPaths = bundle.findEntries("/", "*.drl", true);
 
if(drlPaths != null) {
while(drlPaths.hasMoreElements()) {
URL drlPath = (URL) drlPaths.nextElement();
 
// Search for a domain description file
String externalForm = drlPath.toExternalForm();
URL dslPath = bundle.getEntry(
externalForm.substring(0, externalForm.length() - 4) + ".dsl");
 
if(dslPath != null) {
builder.add(ResourceFactory.newUrlResource(dslPath), ResourceType.DSL);
logger.log(Level.INFO, dslPath.getPath() + " loaded");
}
builder.add(ResourceFactory.newUrlResource(drlPath), ResourceType.DRL);
logger.log(Level.INFO, drlPath.getPath() + " loaded");
}
}
 
// List all DRL XML files in the bundle
Enumeration<?> drlXmlPaths = bundle.findEntries("/", "*.drl.xml", true);
 
if(drlXmlPaths != null) {
while(drlXmlPaths.hasMoreElements()) {
URL drlXmlPath = (URL) drlXmlPaths.nextElement();
builder.add(ResourceFactory.newUrlResource(drlXmlPath), ResourceType.XDRL);
logger.log(Level.INFO, drlXmlPath.getPath() + " loaded");
}
}
 
if (builder.hasErrors()) {
logger.log(Level.WARNING, builder.getErrors().toString());
// TODO We do not exit with an Exception ??
}
 
Collection<KnowledgePackage> packages = builder.getKnowledgePackages();
ruleBase.addKnowledgePackages(packages);
 
// Log installed KnowledgePackage
logger.log(Level.FINE, "Installed KnowledgePackages ({0}):", new Object[] {packages.size()});
for (KnowledgePackage kp : packages) {
logger.log(Level.FINE, " * KnowledgePackage[{0}]", new Object[] {kp});
}
}
 
}