Search in sources :

Example 6 with RepositoriesMeta

use of org.pentaho.di.repository.RepositoriesMeta in project pentaho-kettle by pentaho.

the class JobExecutionConfigurationTest method testConnectRepository.

@Test
public void testConnectRepository() throws KettleException {
    JobExecutionConfiguration jobExecutionConfiguration = new JobExecutionConfiguration();
    final RepositoriesMeta repositoriesMeta = mock(RepositoriesMeta.class);
    final RepositoryMeta repositoryMeta = mock(RepositoryMeta.class);
    final Repository repository = mock(Repository.class);
    final String mockRepo = "mockRepo";
    final boolean[] connectionSuccess = { false };
    Repository initialRepo = mock(Repository.class);
    jobExecutionConfiguration.setRepository(initialRepo);
    KettleLogStore.init();
    // Create mock repository plugin
    MockRepositoryPlugin mockRepositoryPlugin = mock(MockRepositoryPlugin.class);
    when(mockRepositoryPlugin.getIds()).thenReturn(new String[] { "mockRepo" });
    when(mockRepositoryPlugin.matches("mockRepo")).thenReturn(true);
    when(mockRepositoryPlugin.getName()).thenReturn("mock-repository");
    when(mockRepositoryPlugin.getClassMap()).thenAnswer(new Answer<Map<Class<?>, String>>() {

        @Override
        public Map<Class<?>, String> answer(InvocationOnMock invocation) throws Throwable {
            Map<Class<?>, String> dbMap = new HashMap<Class<?>, String>();
            dbMap.put(Repository.class, repositoryMeta.getClass().getName());
            return dbMap;
        }
    });
    PluginRegistry.getInstance().registerPlugin(RepositoryPluginType.class, mockRepositoryPlugin);
    // Define valid connection criteria
    when(repositoriesMeta.findRepository(anyString())).thenAnswer(new Answer<RepositoryMeta>() {

        @Override
        public RepositoryMeta answer(InvocationOnMock invocation) throws Throwable {
            return mockRepo.equals(invocation.getArguments()[0]) ? repositoryMeta : null;
        }
    });
    when(mockRepositoryPlugin.loadClass(Repository.class)).thenReturn(repository);
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            if ("username".equals(invocation.getArguments()[0]) && "password".equals(invocation.getArguments()[1])) {
                connectionSuccess[0] = true;
            } else {
                connectionSuccess[0] = false;
                throw new KettleException("Mock Repository connection failed");
            }
            return null;
        }
    }).when(repository).connect(anyString(), anyString());
    // Ignore repository not found in RepositoriesMeta
    jobExecutionConfiguration.connectRepository(repositoriesMeta, "notFound", "username", "password");
    assertEquals("Repository Changed", initialRepo, jobExecutionConfiguration.getRepository());
    // Ignore failed attempt to connect
    jobExecutionConfiguration.connectRepository(repositoriesMeta, mockRepo, "username", "");
    assertEquals("Repository Changed", initialRepo, jobExecutionConfiguration.getRepository());
    // Save repository if connection passes
    jobExecutionConfiguration.connectRepository(repositoriesMeta, mockRepo, "username", "password");
    assertEquals("Repository didn't change", repository, jobExecutionConfiguration.getRepository());
    assertTrue("Repository not connected", connectionSuccess[0]);
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) Matchers.anyString(org.mockito.Matchers.anyString) RepositoryMeta(org.pentaho.di.repository.RepositoryMeta) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Repository(org.pentaho.di.repository.Repository) InvocationOnMock(org.mockito.invocation.InvocationOnMock) RepositoriesMeta(org.pentaho.di.repository.RepositoriesMeta) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 7 with RepositoriesMeta

use of org.pentaho.di.repository.RepositoriesMeta in project pentaho-kettle by pentaho.

the class ExecuteJobServlet method openRepository.

private Repository openRepository(String repositoryName, String user, String pass) throws KettleException {
    if (Utils.isEmpty(repositoryName)) {
        return null;
    }
    RepositoriesMeta repositoriesMeta = new RepositoriesMeta();
    repositoriesMeta.readData();
    RepositoryMeta repositoryMeta = repositoriesMeta.findRepository(repositoryName);
    if (repositoryMeta == null) {
        throw new KettleException("Unable to find repository: " + repositoryName);
    }
    PluginRegistry registry = PluginRegistry.getInstance();
    Repository repository = registry.loadClass(RepositoryPluginType.class, repositoryMeta, Repository.class);
    repository.init(repositoryMeta);
    repository.connect(user, pass);
    return repository;
}
Also used : RepositoryMeta(org.pentaho.di.repository.RepositoryMeta) KettleException(org.pentaho.di.core.exception.KettleException) Repository(org.pentaho.di.repository.Repository) PluginRegistry(org.pentaho.di.core.plugins.PluginRegistry) RepositoriesMeta(org.pentaho.di.repository.RepositoriesMeta)

Example 8 with RepositoriesMeta

use of org.pentaho.di.repository.RepositoriesMeta in project pentaho-platform by pentaho.

the class RepositorySyncWebService method sync.

public RepositorySyncStatus sync(String repositoryId, String repositoryUrl) throws RepositorySyncException {
    boolean singleDiServerInstance = // $NON-NLS-1$ //$NON-NLS-2$
    "true".equals(PentahoSystem.getSystemSetting(SINGLE_DI_SERVER_INSTANCE, "true"));
    if (singleDiServerInstance) {
        return RepositorySyncStatus.SINGLE_DI_SERVER_INSTANCE;
    }
    RepositoriesMeta repositoriesMeta = new RepositoriesMeta();
    try {
        repositoriesMeta.readData();
    } catch (Exception e) {
        // $NON-NLS-1$
        log.error(Messages.getInstance().getString("RepositorySyncWebService.UNABLE_TO_READ_DATA"), e);
        throw new RepositorySyncException(Messages.getInstance().getString("RepositorySyncWebService.UNABLE_TO_READ_DATA"), // $NON-NLS-1$
        e);
    }
    RepositoryMeta repositoryMeta = repositoriesMeta.findRepository(repositoryId);
    if (repositoryMeta == null) {
        try {
            repositoryMeta = getRepositoryMeta(repositoryId, repositoryUrl);
            if (repositoryMeta == null) {
                // $NON-NLS-1$
                log.error(Messages.getInstance().getString("RepositorySyncWebService.UNABLE_TO_LOAD_PLUGIN"));
                throw new RepositorySyncException(Messages.getInstance().getString(// $NON-NLS-1$
                "RepositorySyncWebService.UNABLE_TO_LOAD_PLUGIN"));
            }
            repositoriesMeta.addRepository(repositoryMeta);
            repositoriesMeta.writeData();
            return RepositorySyncStatus.REGISTERED;
        } catch (KettleException e) {
            log.error(Messages.getInstance().getString("RepositorySyncWebService.UNABLE_TO_REGISTER_REPOSITORY", repositoryId), // $NON-NLS-1$
            e);
            throw new RepositorySyncException(Messages.getInstance().getString("RepositorySyncWebService.UNABLE_TO_REGISTER_REPOSITORY", repositoryId), // $NON-NLS-1$
            e);
        }
    } else {
        String xml = repositoryMeta.getXML();
        Element node;
        try {
            node = XMLParserFactoryProducer.createSecureDocBuilderFactory().newDocumentBuilder().parse(new StringBufferInputStream(xml)).getDocumentElement();
        } catch (Exception e) {
            node = null;
        }
        if (node != null) {
            // $NON-NLS-1$
            NodeList list = node.getElementsByTagName("repository_location_url");
            if (list != null && list.getLength() == 1) {
                String url = list.item(0).getTextContent();
                if (url.equals(repositoryUrl)) {
                    // now test base URL
                    String fullyQualifiedServerUrl = null;
                    if (PentahoSystem.getApplicationContext().getFullyQualifiedServerURL() != null) {
                        fullyQualifiedServerUrl = PentahoSystem.getApplicationContext().getFullyQualifiedServerURL();
                        if (url.endsWith("/")) {
                            // $NON-NLS-1$
                            url = url.substring(0, url.length() - 2);
                        }
                        if (fullyQualifiedServerUrl.endsWith("/")) {
                            // $NON-NLS-1$
                            fullyQualifiedServerUrl = fullyQualifiedServerUrl.substring(0, fullyQualifiedServerUrl.length() - 2);
                        }
                        if (url.startsWith(fullyQualifiedServerUrl)) {
                            return RepositorySyncStatus.ALREADY_REGISTERED;
                        }
                    }
                    log.error(Messages.getInstance().getString("RepositorySyncWebService.FULLY_QUALIFIED_SERVER_URL_SYNC_PROBLEM", fullyQualifiedServerUrl, // $NON-NLS-1$
                    url));
                    throw new RepositorySyncException(Messages.getInstance().getString("RepositorySyncWebService.FULLY_QUALIFIED_SERVER_URL_SYNC_PROBLEM", fullyQualifiedServerUrl, // $NON-NLS-1$
                    url));
                } else {
                    log.error(Messages.getInstance().getString("RepositorySyncWebService.REPOSITORY_URL_SYNC_PROBLEM", repositoryId, url, // $NON-NLS-1$
                    repositoryUrl));
                    throw new RepositorySyncException(Messages.getInstance().getString("RepositorySyncWebService.REPOSITORY_URL_SYNC_PROBLEM", repositoryId, url, // $NON-NLS-1$
                    repositoryUrl));
                }
            }
        }
        log.error(Messages.getInstance().getString("RepositorySyncWebService.REPOSITORY_URL_XML_PARSING_PROBLEM", repositoryId, // $NON-NLS-1$
        xml));
        throw new RepositorySyncException(Messages.getInstance().getString("RepositorySyncWebService.REPOSITORY_URL_XML_PARSING_PROBLEM_CLIENT_MESSAGE", // $NON-NLS-1$
        repositoryId));
    }
}
Also used : RepositoryMeta(org.pentaho.di.repository.RepositoryMeta) KettleException(org.pentaho.di.core.exception.KettleException) StringBufferInputStream(java.io.StringBufferInputStream) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) RepositoriesMeta(org.pentaho.di.repository.RepositoriesMeta) KettleException(org.pentaho.di.core.exception.KettleException)

Example 9 with RepositoriesMeta

use of org.pentaho.di.repository.RepositoriesMeta in project pdi-platform-plugin by pentaho.

the class EngineMetaLoader method dumpRepositoryNames.

private void dumpRepositoryNames() {
    try {
        RepositoriesMeta repositoriesMeta = new RepositoriesMeta();
        // Read from the default $HOME/.kettle/repositories.xml file.
        repositoriesMeta.readData();
        for (int i = 0; i < repositoriesMeta.nrRepositories(); i++) {
            RepositoryMeta repoMeta = repositoriesMeta.getRepository(i);
            // $NON-NLS-1$//$NON-NLS-2$
            log.debug("Found repo: " + repoMeta.getName() + " type: " + repoMeta.getClass().getName());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : RepositoryMeta(org.pentaho.di.repository.RepositoryMeta) RepositoriesMeta(org.pentaho.di.repository.RepositoriesMeta) KettleException(org.pentaho.di.core.exception.KettleException) FileNotFoundException(java.io.FileNotFoundException)

Example 10 with RepositoriesMeta

use of org.pentaho.di.repository.RepositoriesMeta in project pdi-platform-plugin by pentaho.

the class PdiAction method connectToRepository.

/**
 * Connects to the PDI repository
 *
 * @param logWriter
 * @return
 * @throws KettleException
 * @throws KettleSecurityException
 * @throws ActionExecutionException
 */
protected Repository connectToRepository(final LogWriter logWriter) throws KettleSecurityException, KettleException, ActionExecutionException {
    if (log.isDebugEnabled()) {
        // $NON-NLS-1$
        log.debug(Messages.getInstance().getString("Kettle.DEBUG_META_REPOSITORY"));
    }
    RepositoriesMeta repositoriesMeta = new RepositoriesMeta();
    if (log.isDebugEnabled()) {
        // $NON-NLS-1$
        log.debug(Messages.getInstance().getString("Kettle.DEBUG_POPULATING_META"));
    }
    boolean singleDiServerInstance = // $NON-NLS-1$ //$NON-NLS-2$
    "true".equals(PentahoSystem.getSystemSetting(SINGLE_DI_SERVER_INSTANCE, "true"));
    try {
        if (singleDiServerInstance) {
            if (log.isDebugEnabled()) {
                // $NON-NLS-1$
                log.debug("singleDiServerInstance=true, loading default repository");
            }
            // only load a default enterprise repository. If this option is set, then you cannot load
            // transformations or jobs from anywhere but the local server.
            String repositoriesXml = // $NON-NLS-1$
            "<?xml version=\"1.0\" encoding=\"UTF-8\"?><repositories>" + // $NON-NLS-1$
            "<repository><id>PentahoEnterpriseRepository</id>" + "<name>" + SINGLE_DI_SERVER_INSTANCE + // $NON-NLS-1$ //$NON-NLS-2$
            "</name>" + "<description>" + SINGLE_DI_SERVER_INSTANCE + // $NON-NLS-1$ //$NON-NLS-2$
            "</description>" + "<repository_location_url>" + PentahoSystem.getApplicationContext().getFullyQualifiedServerURL() + // $NON-NLS-1$ //$NON-NLS-2$
            "</repository_location_url>" + // $NON-NLS-1$
            "<version_comment_mandatory>N</version_comment_mandatory>" + // $NON-NLS-1$
            "</repository>" + // $NON-NLS-1$
            "</repositories>";
            ByteArrayInputStream sbis = new ByteArrayInputStream(repositoriesXml.getBytes("UTF8"));
            repositoriesMeta.readDataFromInputStream(sbis);
        } else {
            // TODO: add support for specified repositories.xml files...
            // Read from the default $HOME/.kettle/repositories.xml file.
            repositoriesMeta.readData();
        }
    } catch (Exception e) {
        throw new ActionExecutionException(Messages.getInstance().getErrorString("Kettle.ERROR_0018_META_REPOSITORY_NOT_POPULATED"), // $NON-NLS-1$
        e);
    }
    if (log.isDebugEnabled()) {
        // $NON-NLS-1$
        log.debug(Messages.getInstance().getString("Kettle.DEBUG_FINDING_REPOSITORY"));
    }
    // Find the specified repository.
    RepositoryMeta repositoryMeta = null;
    try {
        if (singleDiServerInstance) {
            repositoryMeta = repositoriesMeta.findRepository(SINGLE_DI_SERVER_INSTANCE);
        } else {
            repositoryMeta = repositoriesMeta.findRepository(repositoryName);
        }
    } catch (Exception e) {
        throw new ActionExecutionException(Messages.getInstance().getErrorString("Kettle.ERROR_0004_REPOSITORY_NOT_FOUND", repositoryName), // $NON-NLS-1$
        e);
    }
    if (repositoryMeta == null) {
        if (log.isDebugEnabled()) {
            log.debug(pdiUserAppender.getBuffer().toString());
        }
        throw new ActionExecutionException(Messages.getInstance().getErrorString("Kettle.ERROR_0004_REPOSITORY_NOT_FOUND", // $NON-NLS-1$
        repositoryName));
    }
    if (log.isDebugEnabled()) {
        // $NON-NLS-1$
        log.debug(Messages.getInstance().getString("Kettle.DEBUG_GETTING_REPOSITORY"));
    }
    Repository repository = null;
    try {
        repository = PluginRegistry.getInstance().loadClass(RepositoryPluginType.class, repositoryMeta.getId(), Repository.class);
        repository.init(repositoryMeta);
    } catch (Exception e) {
        throw new ActionExecutionException(Messages.getInstance().getErrorString("Kettle.ERROR_0016_COULD_NOT_GET_REPOSITORY_INSTANCE"), // $NON-NLS-1$
        e);
    }
    // OK, now try the username and password
    if (log.isDebugEnabled()) {
        // $NON-NLS-1$
        log.debug(Messages.getInstance().getString("Kettle.DEBUG_CONNECTING"));
    }
    // Two scenarios here: internal to server or external to server. If internal, you are already authenticated. If
    // external, you must provide a username and additionally specify that the IP address of the machine running this
    // code is trusted.
    repository.connect(PentahoSessionHolder.getSession().getName(), "password");
    // OK, the repository is open and ready to use.
    if (log.isDebugEnabled()) {
        // $NON-NLS-1$
        log.debug(Messages.getInstance().getString("Kettle.DEBUG_FINDING_DIRECTORY"));
    }
    return repository;
}
Also used : RepositoryMeta(org.pentaho.di.repository.RepositoryMeta) IUnifiedRepository(org.pentaho.platform.api.repository2.unified.IUnifiedRepository) Repository(org.pentaho.di.repository.Repository) ByteArrayInputStream(java.io.ByteArrayInputStream) RepositoryPluginType(org.pentaho.di.core.plugins.RepositoryPluginType) RepositoriesMeta(org.pentaho.di.repository.RepositoriesMeta) ActionExecutionException(org.pentaho.platform.api.engine.ActionExecutionException) ActionExecutionException(org.pentaho.platform.api.engine.ActionExecutionException) ActionValidationException(org.pentaho.platform.api.engine.ActionValidationException) UnknownParamException(org.pentaho.di.core.parameters.UnknownParamException) FileNotFoundException(java.io.FileNotFoundException) KettleValueException(org.pentaho.di.core.exception.KettleValueException) KettleStepException(org.pentaho.di.core.exception.KettleStepException) KettleException(org.pentaho.di.core.exception.KettleException) KettleSecurityException(org.pentaho.di.core.exception.KettleSecurityException)

Aggregations

RepositoriesMeta (org.pentaho.di.repository.RepositoriesMeta)19 KettleException (org.pentaho.di.core.exception.KettleException)17 RepositoryMeta (org.pentaho.di.repository.RepositoryMeta)14 Repository (org.pentaho.di.repository.Repository)12 RepositoryPluginType (org.pentaho.di.core.plugins.RepositoryPluginType)8 PluginRegistry (org.pentaho.di.core.plugins.PluginRegistry)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Test (org.junit.Test)2 Mockito.doAnswer (org.mockito.Mockito.doAnswer)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Answer (org.mockito.stubbing.Answer)2 KettleSecurityException (org.pentaho.di.core.exception.KettleSecurityException)2 KettleStepException (org.pentaho.di.core.exception.KettleStepException)2 KettleValueException (org.pentaho.di.core.exception.KettleValueException)2 KettleXMLException (org.pentaho.di.core.exception.KettleXMLException)2 UnknownParamException (org.pentaho.di.core.parameters.UnknownParamException)2