Author: mic...@jb... Date: 2005-12-27 19:04:40 -0500 (Tue, 27 Dec 2005) New Revision: 1937 Added: trunk/labs/jbossrules/drools-repository/conf/ trunk/labs/jbossrules/drools-repository/conf/jaas.config trunk/labs/jbossrules/drools-repository/conf/repository.xml trunk/labs/jbossrules/drools-repository/src/main/org/drools/metamodel/DublinCore.java trunk/labs/jbossrules/drools-repository/src/main/org/drools/metamodel/RuleDefinition.java Removed: trunk/labs/jbossrules/drools-repository/src/main/org/drools/metamodel/Asset.java trunk/labs/jbossrules/drools-repository/src/main/org/drools/metamodel/Rule.java trunk/labs/jbossrules/drools-repository/src/main/org/drools/metamodel/RuleBase.java trunk/labs/jbossrules/drools-repository/src/main/org/drools/metamodel/RuleSet.java trunk/labs/jbossrules/drools-repository/src/main/org/drools/metamodel/RuleSetConfig.java trunk/labs/jbossrules/drools-repository/src/main/org/drools/metamodel/RuleSetFile.java trunk/labs/jbossrules/drools-repository/src/main/org/drools/metamodel/VersionInfo.java Modified: trunk/labs/jbossrules/drools-repository/src/main/org/drools/metamodel/DRLSourceFile.java trunk/labs/jbossrules/drools-repository/src/main/org/drools/repo/RepositoryService.java trunk/labs/jbossrules/drools-repository/src/main/org/drools/repo/jcr/JCRSessionUtil.java trunk/labs/jbossrules/drools-repository/src/main/org/drools/repo/jcr/RepositoryServiceImpl.java trunk/labs/jbossrules/drools-repository/src/test/org/drools/repo/jcr/RepositoryServiceImplTest.java Log: more JCR work Added: trunk/labs/jbossrules/drools-repository/conf/jaas.config =================================================================== --- trunk/labs/jbossrules/drools-repository/conf/jaas.config 2005-12-27 23:35:21 UTC (rev 1936) +++ trunk/labs/jbossrules/drools-repository/conf/jaas.config 2005-12-28 00:04:40 UTC (rev 1937) @@ -0,0 +1,3 @@ +Jackrabbit { + org.apache.jackrabbit.core.security.SimpleLoginModule required anonymousId="anonymous"; +}; \ No newline at end of file Property changes on: trunk/labs/jbossrules/drools-repository/conf/jaas.config ___________________________________________________________________ Name: svn:eol-style + native Added: trunk/labs/jbossrules/drools-repository/conf/repository.xml =================================================================== --- trunk/labs/jbossrules/drools-repository/conf/repository.xml 2005-12-27 23:35:21 UTC (rev 1936) +++ trunk/labs/jbossrules/drools-repository/conf/repository.xml 2005-12-28 00:04:40 UTC (rev 1937) @@ -0,0 +1,25 @@ +<?xml version="1.0" encoding="ISO-8859-1"?> +<Repository> + <FileSystem class="org.apache.jackrabbit.core.fs.local.LocalFileSystem"> + <param name="path" value="${rep.home}/repository"/> + </FileSystem> + <Security appName="Jackrabbit"> + <AccessManager class="org.apache.jackrabbit.core.security.SimpleAccessManager"/> + </Security> + <Workspaces rootPath="${rep.home}/workspaces" defaultWorkspace="default" /> + <Workspace name="${wsp.name}"> + <FileSystem class="org.apache.jackrabbit.core.fs.local.LocalFileSystem"> + <param name="path" value="${wsp.home}"/> + </FileSystem> + <PersistenceManager class="org.apache.jackrabbit.core.state.xml.XMLPersistenceManager" /> + <SearchIndex class="org.apache.jackrabbit.core.query.lucene.SearchIndex"> + <param name="path" value="${wsp.home}/index"/> + </SearchIndex> + </Workspace> + <Versioning rootPath="${rep.home}/versions"> + <FileSystem class="org.apache.jackrabbit.core.fs.local.LocalFileSystem"> + <param name="path" value="${rep.home}/versions"/> + </FileSystem> + <PersistenceManager class="org.apache.jackrabbit.core.state.xml.XMLPersistenceManager" /> + </Versioning> +</Repository> \ No newline at end of file Property changes on: trunk/labs/jbossrules/drools-repository/conf/repository.xml ___________________________________________________________________ Name: svn:eol-style + native Deleted: trunk/labs/jbossrules/drools-repository/src/main/org/drools/metamodel/Asset.java =================================================================== --- trunk/labs/jbossrules/drools-repository/src/main/org/drools/metamodel/Asset.java 2005-12-27 23:35:21 UTC (rev 1936) +++ trunk/labs/jbossrules/drools-repository/src/main/org/drools/metamodel/Asset.java 2005-12-28 00:04:40 UTC (rev 1937) @@ -1,32 +0,0 @@ -package org.drools.metamodel; - -import java.io.Serializable; - -/** - * This is the superclass for all metamodel repository classes. - * They are all assets. Some just have more assets then others. - * @author <a href="mailto:mic...@gm..."> Michael Neale</a> - */ -public class Asset implements Serializable -{ - /* - * This is based on the <a href="http://dublincore.org/documents/dces/"> Dublin Core</a> - * specification, plus a little bit more, and a little bit less. Not all of these fields will be used. - */ - public String name; - public String creator; - public String subject; - public String description; - public String publisher; - public String contributor; - public java.util.Date dateCreated; - public String format; - public String source; - public String language; - public String relation; - public String coverage; - public String rights; - - - -} Modified: trunk/labs/jbossrules/drools-repository/src/main/org/drools/metamodel/DRLSourceFile.java =================================================================== --- trunk/labs/jbossrules/drools-repository/src/main/org/drools/metamodel/DRLSourceFile.java 2005-12-27 23:35:21 UTC (rev 1936) +++ trunk/labs/jbossrules/drools-repository/src/main/org/drools/metamodel/DRLSourceFile.java 2005-12-28 00:04:40 UTC (rev 1937) @@ -5,14 +5,14 @@ * @author <a href="mailto:mic...@gm..."> Michael Neale</a> * */ -public class DRLSourceFile extends RuleSetFile +public class DRLSourceFile { private String content; + private String name; - public DRLSourceFile(String content, String name) { - super.name = name; + this.name = name; this.content = content; } Added: trunk/labs/jbossrules/drools-repository/src/main/org/drools/metamodel/DublinCore.java =================================================================== --- trunk/labs/jbossrules/drools-repository/src/main/org/drools/metamodel/DublinCore.java 2005-12-27 23:35:21 UTC (rev 1936) +++ trunk/labs/jbossrules/drools-repository/src/main/org/drools/metamodel/DublinCore.java 2005-12-28 00:04:40 UTC (rev 1937) @@ -0,0 +1,155 @@ +package org.drools.metamodel; + +/** + * This is based on the <a href="http://dublincore.org/documents/dces/"> Dublin + * Core</a> specification, plus a little bit more, and a little bit less. Not + * all of these fields will be used. + */ +public class DublinCore +{ + + private String title; + private String creator; + private String subject; + private String description; + private String publisher; + private String contributor; + private java.util.Date dateCreated; + private String format; + private String source; + private String language; + private String relation; + private String coverage; + private String rights; + + public String getContributor() + { + return contributor; + } + + public void setContributor(String contributor) + { + this.contributor = contributor; + } + + public String getCoverage() + { + return coverage; + } + + public void setCoverage(String coverage) + { + this.coverage = coverage; + } + + public String getCreator() + { + return creator; + } + + public void setCreator(String creator) + { + this.creator = creator; + } + + public java.util.Date getDateCreated() + { + return dateCreated; + } + + public void setDateCreated(java.util.Date dateCreated) + { + this.dateCreated = dateCreated; + } + + public String getDescription() + { + return description; + } + + public void setDescription(String description) + { + this.description = description; + } + + public String getFormat() + { + return format; + } + + public void setFormat(String format) + { + this.format = format; + } + + public String getLanguage() + { + return language; + } + + public void setLanguage(String language) + { + this.language = language; + } + + public String getName() + { + return title; + } + + public void setName(String name) + { + this.title = name; + } + + public String getPublisher() + { + return publisher; + } + + public void setPublisher(String publisher) + { + this.publisher = publisher; + } + + public String getRelation() + { + return relation; + } + + public void setRelation(String relation) + { + this.relation = relation; + } + + public String getRights() + { + return rights; + } + + public void setRights(String rights) + { + this.rights = rights; + } + + public String getSource() + { + return source; + } + + public void setSource(String source) + { + this.source = source; + } + + public String getSubject() + { + return subject; + } + + public void setSubject(String subject) + { + this.subject = subject; + } + +} Property changes on: trunk/labs/jbossrules/drools-repository/src/main/org/drools/metamodel/DublinCore.java ___________________________________________________________________ Name: svn:eol-style + native Deleted: trunk/labs/jbossrules/drools-repository/src/main/org/drools/metamodel/Rule.java =================================================================== --- trunk/labs/jbossrules/drools-repository/src/main/org/drools/metamodel/Rule.java 2005-12-27 23:35:21 UTC (rev 1936) +++ trunk/labs/jbossrules/drools-repository/src/main/org/drools/metamodel/Rule.java 2005-12-28 00:04:40 UTC (rev 1937) @@ -1,10 +0,0 @@ -package org.drools.metamodel; - -import java.util.Properties; - -public class Rule extends Asset -{ - private Properties properties; - private VersionInfo versionInfo; - private String fragment; -} Deleted: trunk/labs/jbossrules/drools-repository/src/main/org/drools/metamodel/RuleBase.java =================================================================== --- trunk/labs/jbossrules/drools-repository/src/main/org/drools/metamodel/RuleBase.java 2005-12-27 23:35:21 UTC (rev 1936) +++ trunk/labs/jbossrules/drools-repository/src/main/org/drools/metamodel/RuleBase.java 2005-12-28 00:04:40 UTC (rev 1937) @@ -1,38 +0,0 @@ -package org.drools.metamodel; - -import java.util.ArrayList; -import java.util.Collection; - -/** - * A rulebase view of the repository. - * - * @author <a href="mailto:mic...@gm..."> Michael Neale</a> - * - */ -public class RuleBase extends Asset -{ - - protected Collection ruleSets; - protected Collection ruleSetFiles; - - public RuleBase(String name) { - super.name = name; - ruleSets = new ArrayList(); - ruleSetFiles = new ArrayList(); - } - - public void addRuleSet(RuleSet rs) { - ruleSets.add(rs); - } - - public void addRuleSetFile(RuleSetFile rs) { - ruleSetFiles.add(rs); - } - - - - - - - -} Added: trunk/labs/jbossrules/drools-repository/src/main/org/drools/metamodel/RuleDefinition.java =================================================================== --- trunk/labs/jbossrules/drools-repository/src/main/org/drools/metamodel/RuleDefinition.java 2005-12-27 23:35:21 UTC (rev 1936) +++ trunk/labs/jbossrules/drools-repository/src/main/org/drools/metamodel/RuleDefinition.java 2005-12-28 00:04:40 UTC (rev 1937) @@ -0,0 +1,45 @@ +package org.drools.metamodel; + +public class RuleDefinition +{ + private String ruleName; + private String fragment; + + + public String getFragment() + { + return fragment; + } + + public RuleDefinition(String ruleName, + String fragment){ + super( ); + // TODO Auto-generated constructor stub + this.ruleName = ruleName; + this.fragment = fragment; + + } + + public void setFragment(String fragment) + { + this.fragment = fragment; + } + + + + + public String getRuleName() + { + return ruleName; + } + + public void setRuleName(String ruleName) + { + this.ruleName = ruleName; + } + + public RuleDefinition() {} + + + +} Property changes on: trunk/labs/jbossrules/drools-repository/src/main/org/drools/metamodel/RuleDefinition.java ___________________________________________________________________ Name: svn:eol-style + native Deleted: trunk/labs/jbossrules/drools-repository/src/main/org/drools/metamodel/RuleSet.java =================================================================== --- trunk/labs/jbossrules/drools-repository/src/main/org/drools/metamodel/RuleSet.java 2005-12-27 23:35:21 UTC (rev 1936) +++ trunk/labs/jbossrules/drools-repository/src/main/org/drools/metamodel/RuleSet.java 2005-12-28 00:04:40 UTC (rev 1937) @@ -1,12 +0,0 @@ -package org.drools.metamodel; - -import java.util.List; - -public class RuleSet extends Asset -{ - private RuleSetConfig config; - private List rules; - - - -} Deleted: trunk/labs/jbossrules/drools-repository/src/main/org/drools/metamodel/RuleSetConfig.java =================================================================== --- trunk/labs/jbossrules/drools-repository/src/main/org/drools/metamodel/RuleSetConfig.java 2005-12-27 23:35:21 UTC (rev 1936) +++ trunk/labs/jbossrules/drools-repository/src/main/org/drools/metamodel/RuleSetConfig.java 2005-12-28 00:04:40 UTC (rev 1937) @@ -1,16 +0,0 @@ -package org.drools.metamodel; - -import java.util.Properties; - -public class RuleSetConfig extends Asset -{ - private Properties imports; - private String functions; - private Properties applicationData; - private VersionInfo versionInfo; - - - - - -} Deleted: trunk/labs/jbossrules/drools-repository/src/main/org/drools/metamodel/RuleSetFile.java =================================================================== --- trunk/labs/jbossrules/drools-repository/src/main/org/drools/metamodel/RuleSetFile.java 2005-12-27 23:35:21 UTC (rev 1936) +++ trunk/labs/jbossrules/drools-repository/src/main/org/drools/metamodel/RuleSetFile.java 2005-12-28 00:04:40 UTC (rev 1937) @@ -1,11 +0,0 @@ -package org.drools.metamodel; - -public class RuleSetFile extends Asset -{ - - private VersionInfo versionInfo; - - - - -} Deleted: trunk/labs/jbossrules/drools-repository/src/main/org/drools/metamodel/VersionInfo.java =================================================================== --- trunk/labs/jbossrules/drools-repository/src/main/org/drools/metamodel/VersionInfo.java 2005-12-27 23:35:21 UTC (rev 1936) +++ trunk/labs/jbossrules/drools-repository/src/main/org/drools/metamodel/VersionInfo.java 2005-12-28 00:04:40 UTC (rev 1937) @@ -1,43 +0,0 @@ -package org.drools.metamodel; - -/** - * This represents some information about the version, as it was checked in. - * @author <a href="mailto:mic...@gm..."> Michael Neale</a> - */ -public class VersionInfo extends Asset -{ - private String comment; - private long versionNumber; - private String status; - - public VersionInfo(String comment, long versionNumber, String status) { - this.comment = comment; - this.versionNumber = versionNumber; - this.status = status; - } - - public VersionInfo nextVersion(String comment) { - return new VersionInfo(comment, versionNumber + 1, this.status); - } - public VersionInfo nextVersion(String comment, String newStatus) { - return new VersionInfo(comment, versionNumber + 1, newStatus); - } - - - - public String getComment() - { - return comment; - } - public String getStatus() - { - return status; - } - public long getVersionNumber() - { - return versionNumber; - } - - - -} Modified: trunk/labs/jbossrules/drools-repository/src/main/org/drools/repo/RepositoryService.java =================================================================== --- trunk/labs/jbossrules/drools-repository/src/main/org/drools/repo/RepositoryService.java 2005-12-27 23:35:21 UTC (rev 1936) +++ trunk/labs/jbossrules/drools-repository/src/main/org/drools/repo/RepositoryService.java 2005-12-28 00:04:40 UTC (rev 1937) @@ -3,15 +3,10 @@ import java.util.List; import java.util.Properties; -import org.drools.metamodel.Asset; -import org.drools.metamodel.Rule; -import org.drools.metamodel.RuleBase; -import org.drools.metamodel.RuleSet; -import org.drools.metamodel.RuleSetConfig; -import org.drools.metamodel.RuleSetFile; -import org.drools.metamodel.VersionInfo; +import org.drools.metamodel.RuleDefinition; + public interface RepositoryService { /** @@ -30,9 +25,9 @@ * This may also lock the asset, not sure... * Can provide seperate explicit lock methods if needed. */ - public RuleSetFile checkoutFile(String ruleBaseName, String ruleSetName); - public RuleSetConfig checkoutRuleSetConfig(String ruleBaseName, String ruleSetName); - public Rule checkoutRule(String ruleBaseName, String ruleSetName, String ruleName); +// public RuleSetFile checkoutFile(String ruleBaseName, String ruleSetName); +// public RuleSetConfig checkoutRuleSetConfig(String ruleBaseName, String ruleSetName); +// public Rule checkoutRule(String ruleBaseName, String ruleSetName, String ruleName); /** @@ -40,72 +35,99 @@ * Exceptions thrown if you don't fill out the right stuff etc. * The names must be unique of course, or exceptions will be thrown. */ - public RuleSetFile addNewFile(String ruleBaseName, RuleSetFile file); - public RuleSet addNewRuleSet(String ruleBaseName, RuleSet ruleSet); - public Rule addNewRule(String ruleBaseName, String ruleSetName, Rule rule); +// public RuleSetFile addNewFile(String ruleBaseName, RuleSetFile file); +// public RuleSet addNewRuleSet(String ruleBaseName, RuleSet ruleSet); +// public Rule addNewRule(String ruleBaseName, String ruleSetName, Rule rule); - /** - * This method updates the asset in the repository, and - * increment the version number etc. - * If someone else beat you to it, it will chuck a wobbly. Whereby you - * can tell the poor user, they are going to have to find out who it was, - * and throw a phone at their heads, russle crowe style. - * Of course, with checkouts, it should stop anyone else checking it out. - */ - public VersionInfo checkin(Asset asset, String comment); +// /** +// * This method updates the asset in the repository, and +// * increment the version number etc. +// * If someone else beat you to it, it will chuck a wobbly. Whereby you +// * can tell the poor user, they are going to have to find out who it was, +// * and throw a phone at their heads, russle crowe style. +// * Of course, with checkouts, it should stop anyone else checking it out. +// */ +// public VersionInfo checkin(Asset asset, String comment); +// +// /** +// * This will change the status flag on the latest version of the asset. +// * It will make sure that your version is up to date before allowing it. +// */ +// public VersionInfo changeStatus(Asset asset, String newStatus); +// +// +// /** +// * Returns a list of rulebase names in the repository. +// */ +// public List listRuleBaseName(); +// +// +// /** +// * This returns a list of rule-set names for a given rulebase. +// */ +// public List listRuleSetNames(String ruleBase); +// +// +// /** +// * This will deeply load the whole RuleSet. +// */ +// public RuleSet retrieveRuleSet(String ruleBaseName, String ruleSetName); - /** - * This will change the status flag on the latest version of the asset. - * It will make sure that your version is up to date before allowing it. - */ - public VersionInfo changeStatus(Asset asset, String newStatus); +// +// /** +// * This will load a RuleSetFile. +// */ +// public RuleSetFile retrieveRuleSetFile(String ruleBaseName, String ruleSetName); - /** - * Returns a list of rulebase names in the repository. - */ - public List listRuleBaseName(); +// /** +// * Haven't really defined how to search for things, +// * but will be on most fields. +// * Can return a list of assets that match, which could be rules, rulesets etc... +// * Probably need some canned queries, as well as some XPath ones. +// * Can do things like search for rules modified on a certain date, in various states etc. +// * TODO: get a list of basic functionality to support. +// */ +// public List searchRules(Properties searchProperties); + /** - * This returns a list of rule-set names for a given rulebase. + * Sets up a virgin repository. Only needs to be called once. */ - public List listRuleSetNames(String ruleBase); - - + public void createNewRepo(); + /** - * This will deeply load the whole RuleSet. + * This will add a new rule to the repository. + * If it already exists, and exception will be thrown. + * You need to explicitly checkin a new version of a rule. This + * method is only for adding a brand new rule. + * + * The RuleBase and RuleSet will be implicitly created if they don't already exist. */ - public RuleSet retrieveRuleSet(String ruleBaseName, String ruleSetName); + public RuleDefinition addNewRule(String ruleBase, String ruleSet, RuleDefinition rule); - - /** - * This will load a RuleSetFile. + /** + * This will permanently delete a rule. + * Only use when you really don't want it anymore. */ - public RuleSetFile retrieveRuleSetFile(String ruleBaseName, String ruleSetName); + public void removeRule(String ruleBase, String ruleSet, String ruleName); + /** Retrieve a rule */ + public RuleDefinition retrieveRule(String ruleBase, String ruleSet, String ruleName); + /** - * Haven't really defined how to search for things, - * but will be on most fields. - * Can return a list of assets that match, which could be rules, rulesets etc... - * Probably need some canned queries, as well as some XPath ones. - * Can do things like search for rules modified on a certain date, in various states etc. - * TODO: get a list of basic functionality to support. + * This will remove a ruleset, and all the contained rules. */ - public List searchRules(Properties searchProperties); + public void removeRuleSet(String ruleBase, String ruleSet); - - /** - * Sets up a virgin repository. Only needs to be called once. + * Applies changes that were made. + * Some actions have implicit saves, but it is best to */ - public void createNewRepo(); - - - - + public void save(); } Modified: trunk/labs/jbossrules/drools-repository/src/main/org/drools/repo/jcr/JCRSessionUtil.java =================================================================== --- trunk/labs/jbossrules/drools-repository/src/main/org/drools/repo/jcr/JCRSessionUtil.java 2005-12-27 23:35:21 UTC (rev 1936) +++ trunk/labs/jbossrules/drools-repository/src/main/org/drools/repo/jcr/JCRSessionUtil.java 2005-12-28 00:04:40 UTC (rev 1937) @@ -21,11 +21,21 @@ public class JCRSessionUtil { + public static final String REPO_JNDI_NAME = "repo"; + + /** + * At the moment this is all hard coded to be un-authenticated and local + * It is looking for a jaas.config, as well as the repository.xml to setup the repository. + * @return + * @throws NamingException + * @throws RepositoryException + */ public Session getSession() throws NamingException, RepositoryException { + System.setProperty("java.security.auth.login.config", "c:/jaas.config"); - String configFile = "repotest/repository.xml"; - String repHomeDir = "repotest"; + String configFile = "conf/repository.xml"; + String repHomeDir = "drools-repository"; Hashtable env = new Hashtable( ); env.put( Context.INITIAL_CONTEXT_FACTORY, @@ -35,13 +45,10 @@ InitialContext ctx = new InitialContext( env ); - //configure repo, if its not already - RegistryHelper.registerRepository( ctx, - "repo", - configFile, - repHomeDir, - true ); - Repository r = (Repository) ctx.lookup( "repo" ); + registerNewRepoInJNDI( configFile, + repHomeDir, + ctx ); + Repository r = (Repository) ctx.lookup( REPO_JNDI_NAME ); Session session = r.login( new SimpleCredentials( "userid", "".toCharArray( ) ), @@ -49,5 +56,18 @@ return session; } + + private void registerNewRepoInJNDI(String configFile, + String repHomeDir, + InitialContext ctx) throws NamingException, + RepositoryException + { + //configure repo, if its not already + RegistryHelper.registerRepository( ctx, + REPO_JNDI_NAME, + configFile, + repHomeDir, + true ); + } } Modified: trunk/labs/jbossrules/drools-repository/src/main/org/drools/repo/jcr/RepositoryServiceImpl.java =================================================================== --- trunk/labs/jbossrules/drools-repository/src/main/org/drools/repo/jcr/RepositoryServiceImpl.java 2005-12-27 23:35:21 UTC (rev 1936) +++ trunk/labs/jbossrules/drools-repository/src/main/org/drools/repo/jcr/RepositoryServiceImpl.java 2005-12-28 00:04:40 UTC (rev 1937) @@ -2,102 +2,90 @@ import java.util.ArrayList; import java.util.List; -import java.util.Properties; +import javax.jcr.ItemExistsException; import javax.jcr.NamespaceRegistry; import javax.jcr.Node; import javax.jcr.NodeIterator; import javax.jcr.PathNotFoundException; import javax.jcr.RepositoryException; import javax.jcr.Session; +import javax.jcr.lock.LockException; +import javax.jcr.nodetype.ConstraintViolationException; +import javax.jcr.version.VersionException; -import org.drools.metamodel.Asset; import org.drools.metamodel.DRLSourceFile; -import org.drools.metamodel.Rule; -import org.drools.metamodel.RuleSet; -import org.drools.metamodel.RuleSetConfig; -import org.drools.metamodel.RuleSetFile; -import org.drools.metamodel.VersionInfo; +import org.drools.metamodel.RuleDefinition; import org.drools.repo.RepositoryService; import org.drools.repo.RuleRepositoryException; public class RepositoryServiceImpl implements - RepositoryService -{ + RepositoryService { private Session session; - public RepositoryServiceImpl(Session session) - { + public RepositoryServiceImpl(Session session){ this.session = session; } - public void createNamespace() - { - try - { + public void createNamespace(){ + try { NamespaceRegistry reg = this.session.getWorkspace( ).getNamespaceRegistry( ); - if ( nameSpaceExists( reg ) ) - { + if ( nameSpaceExists( reg ) ) { return; } - else - { + else { log( "Registering new Namespace" ); reg.registerNamespace( RepositoryService.RULE_NAMESPACE_PREFIX, RepositoryService.RULE_NAMESPACE ); } } - catch ( RepositoryException e ) - { + catch ( RepositoryException e ) { throw new RuleRepositoryException( e ); } } - public void createNewRepo() - { + /** + * This will create a brand new repository. All rules are stored in the + * drools:repository node. + */ + public void createNewRepo(){ createNamespace( ); createRuleRepoNode( ); - - } - private void createRuleRepoNode() - { - try - { + /** + * This creates the root drools repository node, if it doesn't already + * exist. + * + */ + private void createRuleRepoNode(){ + try { Node rn = this.session.getRootNode( ); - if ( !rn.hasNode( nodeNameOf( "repository" ) ) ) - { + if ( !rn.hasNode( nodeNameOf( "repository" ) ) ) { this.session.getRootNode( ).addNode( nodeNameOf( "repository" ) ); } } - catch ( Exception e ) - { + catch ( Exception e ) { throw new RuleRepositoryException( "Unable to create new repository root node.", e ); } } - private String nodeNameOf(String string) - { + private String nodeNameOf(String string){ return RepositoryService.RULE_NAMESPACE_PREFIX + ":" + string; } - private void log(String string) - { + private void log(String string){ System.out.println( "REPO:" + string ); } - private boolean nameSpaceExists(NamespaceRegistry registry) throws RepositoryException - { + private boolean nameSpaceExists(NamespaceRegistry registry) throws RepositoryException{ String[] uris = registry.getURIs( ); - for ( int i = 0; i < uris.length; i++ ) - { - if ( uris[i].equals( RepositoryService.RULE_NAMESPACE ) ) - { + for ( int i = 0; i < uris.length; i++ ) { + if ( uris[i].equals( RepositoryService.RULE_NAMESPACE ) ) { log( "Name space already exists." ); return true; } @@ -107,189 +95,202 @@ } public void xxxxsaveNewRuleset(String name, - String content) - { + String content){ Node repo = getRepository( ); - try - { - Node ruleset = repo.addNode(nodeNameOf("ruleset")); - ruleset.setProperty("content", content); - ruleset.setProperty("ruleset-name", name); - save(); + try { + Node ruleset = repo.addNode( nodeNameOf( "ruleset" ) ); + ruleset.setProperty( "content", + content ); + ruleset.setProperty( "ruleset-name", + name ); + session.save( ); } catch ( Exception e ) { - throw new RuleRepositoryException("Unable to add a new Ruleset called: " + name, e); + throw new RuleRepositoryException( "Unable to add a new Ruleset called: " + name, + e ); } } - private Node getRepository() - { - try - { - return session.getRootNode().getNode(nodeNameOf("repository")); + private Node getRepository(){ + try { + return session.getRootNode( ).getNode( nodeNameOf( "repository" ) ); } - catch ( PathNotFoundException e ) - { - throw new RuleRepositoryException("Unable to find repository root node. Repository may not have been setup correctly.", e); + catch ( PathNotFoundException e ) { + throw new RuleRepositoryException( "Unable to find repository root node. Repository may not have been setup correctly.", + e ); } - catch ( RepositoryException e ) - { - throw new RuleRepositoryException(e); + catch ( RepositoryException e ) { + throw new RuleRepositoryException( e ); } } - public List xxxxfindAllRuleSetDRL() - { - List rulesets = new ArrayList(); - Node repo = getRepository(); - try - { - NodeIterator nodeIt = repo.getNodes(); - while(nodeIt.hasNext()) { - Node rulesetNode = nodeIt.nextNode(); - String content = rulesetNode.getProperty("content").getString(); - String name = rulesetNode.getProperty("ruleset-name").getString(); - DRLSourceFile drl = new DRLSourceFile(content, name); - rulesets.add(drl); - } + public List xxxxfindAllRuleSetDRL(){ + List rulesets = new ArrayList( ); + Node repo = getRepository( ); + try { + NodeIterator nodeIt = repo.getNodes( ); + while ( nodeIt.hasNext( ) ) { + Node rulesetNode = nodeIt.nextNode( ); + String content = rulesetNode.getProperty( "content" ).getString( ); + String name = rulesetNode.getProperty( "ruleset-name" ).getString( ); + DRLSourceFile drl = new DRLSourceFile( content, + name ); + rulesets.add( drl ); + } } - catch ( RepositoryException e ) - { - throw new RuleRepositoryException("Unable to list rulesets.", e); + catch ( RepositoryException e ) { + throw new RuleRepositoryException( "Unable to list rulesets.", + e ); } return rulesets; } - - public void save() { - try - { - session.save( ); - } - catch ( Exception e ) - { - throw new RuleRepositoryException( "Unable to save session.", - e ); - } - } - public void xxxxeraseAllRuleSetDRL() - { - - Node repo = this.getRepository(); + public void xxxxeraseAllRuleSetDRL(){ + + Node repo = this.getRepository( ); NodeIterator it; - try - { - it = repo.getNodes(); - while (it.hasNext()) { - it.nextNode().remove(); + try { + it = repo.getNodes( ); + while ( it.hasNext( ) ) { + it.nextNode( ).remove( ); } - save(); + session.save( ); } - catch ( RepositoryException e ) - { - throw new RuleRepositoryException("Unable to delete DRL nodes.", e); + catch ( RepositoryException e ) { + throw new RuleRepositoryException( "Unable to delete DRL nodes.", + e ); } } + public RuleDefinition addNewRule(String ruleBase, + String ruleSet, + RuleDefinition rule){ + try { + Node ruleSetNode = getOrCreateRuleSetNode( ruleBase, + ruleSet ); + if ( ruleSetNode.hasNode( nodeNameOf( rule.getRuleName( ) ) ) ) { + throw new RuleRepositoryException( "Rule with that name [" + rule.getRuleName( ) + + "] already exists in ruleset [" + + ruleSet + "]" ); + } + Node newRuleNode = ruleSetNode.addNode( nodeNameOf( rule.getRuleName( ) ) ); + newRuleNode.setProperty( nodeNameOf( "fragment" ), + rule.getFragment( ) ); - public RuleSetFile addNewRulesetFile(RuleSetFile file, - String initialComment) - { - // TODO Auto-generated method stub - return null; - } + } + catch ( PathNotFoundException e ) { + throw new RuleRepositoryException( "Not able to locate ruleset or base to store rule.", + e ); + } + catch ( RepositoryException e ) { + throw new RuleRepositoryException( e ); + } - public RuleSetFile checkoutFile(String ruleBaseName, - String ruleSetName) - { - // TODO Auto-generated method stub - return null; + return rule; } - public RuleSetConfig checkoutRuleSetConfig(String ruleBaseName, - String ruleSetName) - { - // TODO Auto-generated method stub - return null; + private Node getOrCreateRuleSetNode(String ruleBase, + String ruleSet) throws PathNotFoundException, + ItemExistsException, + VersionException, + ConstraintViolationException, + LockException, + RepositoryException{ + Node ruleBaseNode = getOrCreateRuleBaseNode( ruleBase ); + if ( ruleBaseNode.hasNode( nodeNameOf( ruleSet ) ) ) { + return ruleBaseNode.getNode( nodeNameOf( ruleSet ) ); + } + else { + return ruleBaseNode.addNode( nodeNameOf( ruleSet ) ); + } } - public Rule checkoutRule(String ruleBaseName, - String ruleSetName, - String ruleName) - { - // TODO Auto-generated method stub - return null; + private Node getOrCreateRuleBaseNode(String ruleBase) throws PathNotFoundException, + ItemExistsException, + VersionException, + ConstraintViolationException, + LockException, + RepositoryException{ + Node repo = getRepository( ); + String nodeName = nodeNameOf( ruleBase ); + if ( repo.hasNode( nodeName ) ) { + return repo.getNode( nodeName ); + } + else { + return repo.addNode( nodeName ); + } } - public RuleSetFile addNewFile(String ruleBaseName, - RuleSetFile file) - { - // TODO Auto-generated method stub - return null; + public void save(){ + try { + session.save( ); + } + catch ( Exception e ) { + throw new RuleRepositoryException( "Unable to save the session.", + e ); + } } - public RuleSet addNewRuleSet(String ruleBaseName, - RuleSet ruleSet) - { - // TODO Auto-generated method stub - return null; - } + public void removeRule(String ruleBase, + String ruleSet, + String ruleName){ + try { + String path = calcPath( new String[]{ruleBase, ruleSet, ruleName} ); + Node ruleNode = getRepository( ).getNode( path ); + ruleNode.remove( ); + } + catch ( PathNotFoundException e ) { + throw new RuleRepositoryException( "Unable to find rule to remove.", + e ); + } + catch ( RepositoryException e ) { + throw new RuleRepositoryException( e ); + } - public Rule addNewRule(String ruleBaseName, - String ruleSetName, - Rule rule) - { - // TODO Auto-generated method stub - return null; } - public VersionInfo checkin(Asset asset, - String comment) - { - // TODO Auto-generated method stub - return null; + public void removeRuleSet(String ruleBase, + String ruleSet){ + String[] path = new String[] {ruleBase, ruleSet}; + try { + getRepository().getNode(calcPath(path)).remove(); + } + catch ( Exception e ) { + throw new RuleRepositoryException("Unable to remove the ruleset [" + ruleSet + "]", e); + } } - public VersionInfo changeStatus(Asset asset, - String newStatus) - { - // TODO Auto-generated method stub - return null; + /** + * convert a list of names to a path to retrieve. + */ + String calcPath(String[] names){ + StringBuffer buf = new StringBuffer( ); + for ( int i = 0; i < names.length; i++ ) { + buf.append( nodeNameOf( names[i] ) ); + if ( i != names.length - 1 ) { + buf.append( '/' ); + } + } + return buf.toString( ); } - public List listRuleBaseName() - { - // TODO Auto-generated method stub - return null; - } + public RuleDefinition retrieveRule(String ruleBase, + String ruleSet, + String ruleName){ + try { + Node ruleNode = getRepository().getNode(calcPath(new String[] {ruleBase, ruleSet, ruleName})); + RuleDefinition rule = new RuleDefinition(); + rule.setFragment(ruleNode.getProperty(nodeNameOf("fragment")).getString()); - public List listRuleSetNames(String ruleBase) - { - // TODO Auto-generated method stub - return null; + return rule; + } + catch ( PathNotFoundException e ) { + throw new RuleRepositoryException("Rule does not exist.", e); + } + catch ( RepositoryException e ) { + throw new RuleRepositoryException(e); + } } - public RuleSet retrieveRuleSet(String ruleBaseName, - String ruleSetName) - { - // TODO Auto-generated method stub - return null; - } - - public RuleSetFile retrieveRuleSetFile(String ruleBaseName, - String ruleSetName) - { - // TODO Auto-generated method stub - return null; - } - - public List searchRules(Properties searchProperties) - { - // TODO Auto-generated method stub - return null; - } - - - } Modified: trunk/labs/jbossrules/drools-repository/src/test/org/drools/repo/jcr/RepositoryServiceImplTest.java =================================================================== --- trunk/labs/jbossrules/drools-repository/src/test/org/drools/repo/jcr/RepositoryServiceImplTest.java 2005-12-27 23:35:21 UTC (rev 1936) +++ trunk/labs/jbossrules/drools-repository/src/test/org/drools/repo/jcr/RepositoryServiceImplTest.java 2005-12-28 00:04:40 UTC (rev 1937) @@ -1,31 +1,111 @@ package org.drools.repo.jcr; -import java.util.List; - import junit.framework.TestCase; +import org.drools.metamodel.RuleDefinition; +import org.drools.repo.RepositoryService; import org.drools.repo.RepositoryServiceFactory; public class RepositoryServiceImplTest extends TestCase { + private static RepositoryServiceImpl instance; + private long startTime; - public void testCreateNewAndDelete() { - RepositoryServiceImpl repo = (RepositoryServiceImpl) RepositoryServiceFactory.getRepositoryService(); - repo.createNewRepo(); + + + + + public void testAddRemoveNewRuleAndRuleSet() { - repo.xxxxeraseAllRuleSetDRL(); + RuleDefinition rule = new RuleDefinition("nami", "rule blah {}"); + + getRepo().addNewRule("rb", "rs", rule); + getRepo().save(); + RuleDefinition rule2 = getRepo().retrieveRule("rb", "rs", "nami"); + getRepo().removeRule("rb", "rs", rule.getRuleName()); + getRepo().removeRuleSet("rb", "rs"); - repo.xxxxsaveNewRuleset("My Ruleset", "vkahijkhfhdsjkfhdsh"); + assertEquals(rule.getFragment(), rule2.getFragment()); + getRepo().save(); - List rulesets = repo.xxxxfindAllRuleSetDRL(); - - System.out.println(rulesets.size()); + } + + public void testLargeNumberOfRules() { + startTimer(); + //setup 5000 rules in ruleset 1 + RepositoryService repo = getRepo(); + for (int i = 0; i < 5000; i ++) { + RuleDefinition rule = new RuleDefinition("rule" + i, "MyRule_" + i); + repo.addNewRule("rulebase", "ruleset1", rule); + if (i % 10 == 0) repo.save(); + } + System.out.println("Time for first 5000: " + lapTime()); + repo.save(); + + //ruleset2 + for (int i = 0; i < 5000; i ++) { + RuleDefinition rule = new RuleDefinition("rule" + i, "MyRule_" + i); + repo.addNewRule("rulebase", "ruleset2", rule); + if (i % 10 == 0) repo.save(); + } + System.out.println("Time for second 5000: " + lapTime()); + repo.save(); + //rulebase2 + for (int i = 0; i < 5000; i ++) { + RuleDefinition rule = new RuleDefinition("rule" + i, "MyRule_" + i); + repo.addNewRule("rulebase2", "ruleset", rule); + if (i % 10 == 0) repo.save(); + } + System.out.println("Time for third 5000: " + lapTime()); + repo.save(); + startTimer(); + + RuleDefinition rule1 = repo.retrieveRule("rulebase", "ruleset1", "rule1000"); + System.out.println("Retrieve 1 :" + lapTime()); + RuleDefinition rule2 = repo.retrieveRule("rulebase", "ruleset2", "rule1000"); + System.out.println("Retrieve 2 :" + lapTime()); + RuleDefinition rule3 = repo.retrieveRule("rulebase2", "ruleset", "rule1000"); + System.out.println("Retrieve 3 :" + lapTime()); + + repo.removeRuleSet("rulebase", "ruleset1"); + repo.removeRuleSet("rulebase", "ruleset2"); + repo.removeRuleSet("rulebase2", "ruleset"); + repo.save(); + System.out.println("Remove all:" + lapTime()); + + assertEquals("MyRule_1000", rule1.getFragment()); + assertEquals("MyRule_1000", rule2.getFragment()); + assertEquals("MyRule_1000", rule3.getFragment()); + + } + + private long lapTime(){ + long elapsed = System.currentTimeMillis() - startTime; + startTime = System.currentTimeMillis(); + return elapsed; + } + private void startTimer(){ + this.startTime = System.currentTimeMillis(); + + } + + public void testCalcPath() { + String result = getRepo().calcPath(new String[] {"one", "two", "three"}); + assertEquals("drools:one/drools:two/drools:three", result); + } - - + private RepositoryServiceImpl getRepo() { + if (instance == null) { + instance = (RepositoryServiceImpl) RepositoryServiceFactory.getRepositoryService( ); + instance.createNewRepo( ); + instance.save(); + } + return instance; + } + } |