Skip to content

Commit

Permalink
[BACKLOG-9374] - Handle Pentaho Repository timeout (#2919)
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreyBurikhin authored and mdamour1976 committed Sep 16, 2016
1 parent 6086bd2 commit b61c3dc
Show file tree
Hide file tree
Showing 7 changed files with 267 additions and 10 deletions.
49 changes: 49 additions & 0 deletions engine/src/org/pentaho/di/repository/ReconnectableRepository.java
@@ -0,0 +1,49 @@
/*! ******************************************************************************
*
* Pentaho Data Integration
*
* Copyright (C) 2002-2016 by Pentaho : http://www.pentaho.com
*
*******************************************************************************
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/

package org.pentaho.di.repository;

import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.core.exception.KettleSecurityException;

/**
* Allows to connect to repository multiple times (in case of timeout and etc).
*
*/
public interface ReconnectableRepository extends Repository {

/**
* Connect to the repository. This repository type allows to connect more than once to the same repository
* object.
*
* @param username
* the username of the user connecting to the repository.
* @param password
* the password of the user connecting to the repository.
* @throws KettleSecurityException
* in case the supplied user or password is incorrect.
* @throws KettleException
* in case there is a general unexpected error.
*/
public void connect( String username, String password ) throws KettleException, KettleSecurityException;

}
Expand Up @@ -65,6 +65,7 @@
import org.pentaho.di.repository.IUser;
import org.pentaho.di.repository.ObjectId;
import org.pentaho.di.repository.ObjectRevision;
import org.pentaho.di.repository.ReconnectableRepository;
import org.pentaho.di.repository.Repository;
import org.pentaho.di.repository.RepositoryDirectory;
import org.pentaho.di.repository.RepositoryDirectoryInterface;
Expand Down Expand Up @@ -115,7 +116,7 @@
@RepositoryPlugin( id = "PentahoEnterpriseRepository", name = "RepositoryType.Name.EnterpriseRepository",
description = "RepositoryType.Description.EnterpriseRepository",
metaClass = "org.pentaho.di.repository.pur.PurRepositoryMeta", i18nPackageName = "org.pentaho.di.repository.pur" )
public class PurRepository extends AbstractRepository implements Repository, RepositoryExtended, java.io.Serializable {
public class PurRepository extends AbstractRepository implements Repository, ReconnectableRepository, RepositoryExtended, java.io.Serializable {

private static final long serialVersionUID = 7460109109707189479L; /* EESOURCE: UPDATE SERIALVERUID */

Expand Down Expand Up @@ -248,6 +249,7 @@ public RootRef getRootRef() {
}

@Override public void connect( final String username, final String password ) throws KettleException {
connected = false;
if ( isTest() ) {
connected = true;
purRepositoryServiceRegistry
Expand Down
Expand Up @@ -22,6 +22,7 @@

package org.pentaho.di.ui.repo;

import org.apache.commons.lang.ClassUtils;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.pentaho.di.core.database.DatabaseMeta;
Expand All @@ -34,12 +35,14 @@
import org.pentaho.di.core.plugins.RepositoryPluginType;
import org.pentaho.di.i18n.BaseMessages;
import org.pentaho.di.repository.AbstractRepository;
import org.pentaho.di.repository.ReconnectableRepository;
import org.pentaho.di.repository.RepositoriesMeta;
import org.pentaho.di.repository.Repository;
import org.pentaho.di.repository.RepositoryMeta;
import org.pentaho.di.ui.core.PropsUI;
import org.pentaho.di.ui.spoon.Spoon;

import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -185,7 +188,10 @@ public String connectToRepository( RepositoryMeta repositoryMeta, String usernam
JSONObject jsonObject = new JSONObject();
try {
Repository repository =
pluginRegistry.loadClass( RepositoryPluginType.class, repositoryMeta.getId(), Repository.class );
pluginRegistry.loadClass( RepositoryPluginType.class, repositoryMeta.getId(), Repository.class );
if ( repository instanceof ReconnectableRepository ) {
repository = wrapWithRepositoryTimeoutHandler( (ReconnectableRepository) repository );
}
repository.init( repositoryMeta );
repository.connect( username, password );
if ( username != null ) {
Expand Down Expand Up @@ -213,6 +219,37 @@ public String connectToRepository( RepositoryMeta repositoryMeta, String usernam
return jsonObject.toString();
}

public String reconnectToRepository( String username, String password ) {
Repository currentRepositoryInstance = getConnectedRepositoryInstance();
return reconnectToRepository( currentRepository, (ReconnectableRepository) currentRepositoryInstance, username,
password );
}

public String reconnectToRepository( RepositoryMeta repositoryMeta, ReconnectableRepository repository,
String username, String password ) {
JSONObject jsonObject = new JSONObject();
try {
if ( username != null ) {
getPropsUI().setLastRepositoryLogin( username );
}
if (repository.isConnected()) {
repository.disconnect();
}
repository.init( repositoryMeta );
repository.connect( username, password );
jsonObject.put( "success", true );
} catch ( KettleException ke ) {
if ( ke.getMessage().contains( ERROR_401 ) ) {
jsonObject.put( ERROR_MESSAGE, BaseMessages.getString( PKG, "RepositoryConnection.Error.InvalidCredentials" ) );
} else {
jsonObject.put( ERROR_MESSAGE, BaseMessages.getString( PKG, "RepositoryConnection.Error.InvalidServer" ) );
}
jsonObject.put( "success", false );
log.logError( "Unable to connect to repository", ke );
}
return jsonObject.toString();
}

public boolean deleteRepository( String name ) {
RepositoryMeta repositoryMeta = repositoriesMeta.findRepository( name );
int index = repositoriesMeta.indexOfRepository( repositoryMeta );
Expand Down Expand Up @@ -312,6 +349,10 @@ public boolean isConnected() {
return getSpoon().rep != null;
}

public Repository getConnectedRepositoryInstance() {
return getSpoon().rep;
}

public void save() {
try {
repositoriesMeta.writeData();
Expand All @@ -326,6 +367,14 @@ private Spoon getSpoon() {
}
return spoon;
}

@SuppressWarnings( "unchecked" )
private Repository wrapWithRepositoryTimeoutHandler( ReconnectableRepository repository ) {
List<Class<?>> repositoryIntrerfaces = ClassUtils.getAllInterfaces( repository.getClass() );
Class<?>[] repositoryIntrerfacesArray = repositoryIntrerfaces.toArray( new Class<?>[repositoryIntrerfaces.size()] );
return (Repository) Proxy.newProxyInstance( repository.getClass().getClassLoader(), repositoryIntrerfacesArray,
new RepositorySessionTimeoutHandler( repository, this ) );
}

public PropsUI getPropsUI() {
return PropsUI.getInstance();
Expand Down
Expand Up @@ -22,6 +22,8 @@

package org.pentaho.di.ui.repo;

import java.util.HashMap;

import org.codehaus.jackson.map.ObjectMapper;
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.BrowserFunction;
Expand All @@ -40,8 +42,6 @@
import org.pentaho.platform.settings.ServerPort;
import org.pentaho.platform.settings.ServerPortRegistry;

import java.util.HashMap;

/**
* Created by bmorrise on 2/21/16.
*/
Expand All @@ -68,18 +68,23 @@ public class RepositoryDialog extends ThinDialog {

private RepositoryConnectController controller;
private Shell shell;
private boolean result = false;

public RepositoryDialog( Shell shell, RepositoryConnectController controller ) {
super( shell, WIDTH, HEIGHT );
this.controller = controller;
this.shell = shell;
}

private void open() {
open( null );
private boolean open() {
return open( null );
}

private void open( RepositoryMeta repositoryMeta ) {
private boolean open( RepositoryMeta repositoryMeta ) {
return open( repositoryMeta, false, null );
}

private boolean open( RepositoryMeta repositoryMeta, boolean relogin, String errorMessage ) {

new BrowserFunction( browser, "close" ) {
@Override public Object function( Object[] arguments ) {
Expand All @@ -89,6 +94,19 @@ private void open( RepositoryMeta repositoryMeta ) {
return true;
}
};

new BrowserFunction( browser, "setResult" ) {
@Override public Object function( Object[] arguments ) {
setResult( (boolean) arguments[0] );
return true;
}
};

new BrowserFunction( browser, "getErrorMessage" ) {
@Override public Object function( Object[] objects ) {
return errorMessage == null ? "" : errorMessage;
}
};

new BrowserFunction( browser, "help" ) {
@Override public Object function( Object[] objects ) {
Expand Down Expand Up @@ -152,9 +170,13 @@ private void open( RepositoryMeta repositoryMeta ) {

new BrowserFunction( browser, "loginToRepository" ) {
@Override public Object function( Object[] objects ) {
String username = (String) objects[ 0 ];
String password = (String) objects[ 1 ];
return controller.connectToRepository( username, password );
String username = (String) objects[0];
String password = (String) objects[1];
if ( relogin ) {
return controller.reconnectToRepository( username, password );
} else {
return controller.connectToRepository( username, password );
}
}
};

Expand Down Expand Up @@ -232,6 +254,7 @@ public Object function( Object[] objects ) {
display.sleep();
}
}
return result;
}

public void openManager() {
Expand All @@ -243,11 +266,20 @@ public void openCreation() {
super.createDialog( CREATION_TITLE, getRepoURL( CREATION_WEB_CLIENT_PATH ), OPTIONS, LOGO );
open();
}

public boolean openRelogin( RepositoryMeta repositoryMeta, String errorMessage ) {
super.createDialog( LOGIN_TITLE, getRepoURL( LOGIN_WEB_CLIENT_PATH ), OPTIONS, LOGO );
return open( repositoryMeta, true, errorMessage );
}

public void openLogin( RepositoryMeta repositoryMeta ) {
super.createDialog( LOGIN_TITLE, getRepoURL( LOGIN_WEB_CLIENT_PATH ), OPTIONS, LOGO );
open( repositoryMeta );
}

private void setResult( boolean result ) {
this.result = result;
}

private static Integer getOsgiServicePort() {
// if no service port is specified try getting it from
Expand Down

0 comments on commit b61c3dc

Please sign in to comment.