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;

    
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("sys""secret");
             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.

Parameters:
user user id
pass password of user
Returns:
new JCR session
Throws:
javax.jcr.RepositoryException if fails or user credentials are not correct
    protected Session createSession(String userString passthrows RepositoryException {
        char[] password = pass.toCharArray();
        SimpleCredentials sc = new SimpleCredentials(userpassword);
        Session session = .login(sc);
        return session;
    }
    // ------ 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 = null;
        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 ;
    }
    protected RRepository createRepository() throws RRepositoryException {
        try {
            // FIXME: do not hardcode credential info
            Session session = createSession("user""pass");
            RTransactionManager transactionManager = getTrasactionManager(session);
            JcrRepository jri = new JcrRepository(sessiontransactionManager,
                    .getValue(), .getValue());
            return jri;
        } catch (RepositoryException e) {
            throw new RRepositoryException("Failed to get Repository Instance"e);
        }
    }

    
    public void initialize(ConfigSet confSetthrows RRepositoryException {
        // 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 {
        getRepositoryInstance().release();
    }

    
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);
New to GrepCode? Check out our FAQ X