Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.openl.rules.repository.factories;
  
  import javax.jcr.Session;
  import javax.jcr.Value;
 
This is Abstract class with common code for Local and RMI methods of accessing any JCR-170 compliant instance.

It performs basic insanity checks. For example, it verifies that OpenL node types are registered in using JCR.

Author(s):
Aleh Bykhavets
 
 public abstract class AbstractJcrRepositoryFactory implements RRepositoryFactory {
 
     public static final String DEFAULT_NODETYPE_FILE = "/org/openl/rules/repository/openl_nodetypes.xml";

    
Default path where new project should be created
 
             "design-repository.rules.path""/rules");
             "design-repository.deployments.path""/deployments");
 
     private Repository repository;
     protected String repositoryName;
     private RRepository rulesRepository;
 
     private SimpleCredentials credencials;
 
     private ConfigPropertyString login;
     private ConfigPropertyString password;
     private ConfigPropertyString repoConfigFile;

    
Checks whether the JCR instance is prepared for OpenL. If it is the first time, then there are no openL node types, yet.

 
     protected void checkOnStart() throws RepositoryException {
         Session systemSession = null;
         try {
             // FIXME: do not hardcode system credentials
             systemSession = createSession();
             NodeTypeManager ntm = systemSession.getWorkspace().getNodeTypeManager();
 
             boolean initNodeTypes = false;
             try {
                 // Does JCR know anything about OpenL?
                 ntm.getNodeType(.);
             } catch (NoSuchNodeTypeException e) {
                 // No, it doesn't.
                 initNodeTypes = true;
             }
 
             if (initNodeTypes) {
                 // Add OpenL node definitions
                 initNodeTypes(ntm);
             } else {
                 checkSchemaVersion(ntm);
             }
         } finally {
             if (systemSession != null) {
                 systemSession.logout();
             }
         }
     }

    
Checks whether schema version of the repository is valid. If check failed then it throws exception.

Parameters:
ntm Node Type Manager
Throws:
javax.jcr.RepositoryException if check failed
 
     protected void checkSchemaVersion(NodeTypeManager ntmthrows RepositoryException {
         String schemaVersion = getCurrentSchemaVersion(ntm);
         // compare expected and repository schema versions
        String expectedVersion = getExpectedSchemaVersion();
        if (!expectedVersion.equals(schemaVersion)) {
            throw new RepositoryException("Schema version is different. Has (" + schemaVersion + ") when ("
                    + expectedVersion + ") expected.");
        }
    }
    
    
Creates JCR Session.

Returns:
new JCR session
Throws:
javax.jcr.RepositoryException if fails or user credentials are not correct
    protected Session createSession() throws RepositoryException {
        return .login();
    }
    // ------ protected methods ------
    protected String getExpectedSchemaVersion() throws RepositoryException {
        String xPathQ = "/nodeTypes/nodeType[@name = 'openl:repository']"
                + "/propertyDefinition[@name = 'schema-version']/defaultValues/defaultValue[1]";
        XPathFactory factory = XPathFactory.newInstance();
        XPath xPath = factory.newXPath();
        String file = ;
        try {
            InputSource source = new InputSource(this.getClass().getResourceAsStream(file));
            String result = xPath.evaluate(xPathQsource);
            if (result == null || result.length() == 0) {
                throw new Exception("Cannot find node.");
            }
            return result;
        } catch (Exception e) {
            throw new RepositoryException("Cannot read schema version from '" + file + "': " + e.getMessage());
        }
    }
        String schemaVersion = null;
        // check special node
        NodeType nodeType;
        try {
            nodeType = ntm.getNodeType(.);
        } catch (NoSuchNodeTypeException e) {
            throw new RepositoryException("Cannot determine scheme version: " + e.getMessage());
        }
        PropertyDefinition[] propDefs = nodeType.getPropertyDefinitions();
        // retrieve value of schema version
        for (PropertyDefinition definition : propDefs) {
            if ("schema-version".equals(definition.getName())) {
                Value[] defValues = definition.getDefaultValues();
                if (defValues != null && defValues.length > 0) {
                    // take first only
                    // Note: multiply values are not supported
                    schemaVersion = defValues[0].getString();
                }
                break;
            }
        }
        if (schemaVersion == null) {
            throw new RepositoryException("Cannot determine scheme version: no special property or value!");
        }
        return schemaVersion;
    }

    
        if( == null){
             = createRepository();
        }
        return ;
    }
        try {
            Session session = createSession();
            RTransactionManager transactionManager = getTrasactionManager(session);
            return new JcrRepository(sessiontransactionManager,
                    .getValue(), .getValue());
        } catch (RepositoryException e) {
            throw new RRepositoryException("Failed to get Repository Instance"e);
        }
    }

    
    public void initialize(ConfigSet confSetthrows RRepositoryException {
        confSet.updateProperty(); 
        confSet.updateProperty();
        confSet.updatePasswordProperty();
        confSet.updateProperty();
        // TODO: add default path support
        // 1. check path -- create if absent
        // 2. pass as parameter or property to JcrRepository
    }

    
Registers OpenL node types in JCR.

Usually it can be done on local JCR instance.

This operation may not be supported via RMI.

Parameters:
ntm node type manager
Throws:
javax.jcr.RepositoryException if failed
    protected abstract void initNodeTypes(NodeTypeManager ntmthrows RepositoryException;
    public void release() throws RRepositoryException {
        // If rulesRepository is not created, we don't need to create it and then release it
        if ( != null) {
            .release();
             = null;
        }
    }

    
Sets repository reference. Must be called before invoking getRepositoryInstance() method.

Parameters:
rep implementation specific repository
Throws:
javax.jcr.RepositoryException if fails to check first start
    protected void setRepository(Repository repString namethrows RepositoryException {
         = rep;
         = name;
        checkOnStart();
    }
    public abstract RTransactionManager getTrasactionManager(Session session);
        return ;
    }
    public void setLogin(ConfigPropertyString login) {
        this. = login;
    }
    public void setPassword(ConfigPropertyString password) {
        this. = password;
    }
    public void setRepoConfigFile(ConfigPropertyString repoConfigFile) {
        this. = repoConfigFile;
    }
New to GrepCode? Check out our FAQ X