org.pentaho.di.www.SlaveServerConfig Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kettle-engine Show documentation
Show all versions of kettle-engine Show documentation
Container pom for Pentaho Data Integration modules
The newest version!
/*! ******************************************************************************
*
* Pentaho Data Integration
*
* Copyright (C) 2002-2017 by Hitachi Vantara : 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.www;
import org.pentaho.di.cluster.SlaveServer;
import org.pentaho.di.core.Const;
import org.pentaho.di.core.util.Utils;
import org.pentaho.di.core.database.Database;
import org.pentaho.di.core.database.DatabaseMeta;
import org.pentaho.di.core.encryption.Encr;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.core.exception.KettleXMLException;
import org.pentaho.di.core.logging.LogChannel;
import org.pentaho.di.core.logging.LogChannelInterface;
import org.pentaho.di.core.logging.LoggingObjectInterface;
import org.pentaho.di.core.logging.LoggingObjectType;
import org.pentaho.di.core.logging.SimpleLoggingObject;
import org.pentaho.di.core.plugins.PluginRegistry;
import org.pentaho.di.core.plugins.RepositoryPluginType;
import org.pentaho.di.core.row.RowMetaInterface;
import org.pentaho.di.core.xml.XMLHandler;
import org.pentaho.di.metastore.MetaStoreConst;
import org.pentaho.di.repository.RepositoriesMeta;
import org.pentaho.di.repository.Repository;
import org.pentaho.di.repository.RepositoryMeta;
import org.pentaho.metastore.api.exceptions.MetaStoreException;
import org.pentaho.metastore.stores.delegate.DelegatingMetaStore;
import org.pentaho.metastore.stores.memory.MemoryMetaStore;
import org.pentaho.metastore.stores.xml.XmlMetaStore;
import org.w3c.dom.Node;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public class SlaveServerConfig {
public static final String XML_TAG = "slave_config";
public static final String XML_TAG_MASTERS = "masters";
public static final String XML_TAG_REPOSITORY = "repository";
public static final String XML_TAG_SEQUENCES = "sequences";
public static final String XML_TAG_AUTOSEQUENCE = "autosequence";
public static final String XML_TAG_AUTO_CREATE = "autocreate";
public static final String XML_TAG_JETTY_OPTIONS = "jetty_options";
public static final String XML_TAG_ACCEPTORS = "acceptors";
public static final String XML_TAG_ACCEPT_QUEUE_SIZE = "acceptQueueSize";
public static final String XML_TAG_LOW_RES_MAX_IDLE_TIME = "lowResourcesMaxIdleTime";
private List masters;
private SlaveServer slaveServer;
private boolean reportingToMasters;
private boolean joining;
private int maxLogLines;
private int maxLogTimeoutMinutes;
private int objectTimeoutMinutes;
private String filename;
private List databases;
private List slaveSequences;
private SlaveSequence autoSequence;
private boolean automaticCreationAllowed;
private Repository repository;
private RepositoryMeta repositoryMeta;
private String repositoryId;
private String repositoryUsername;
private String repositoryPassword;
private DelegatingMetaStore metaStore;
private String passwordFile;
public SlaveServerConfig() {
masters = new ArrayList();
databases = new ArrayList();
slaveSequences = new ArrayList();
automaticCreationAllowed = false;
metaStore = new DelegatingMetaStore();
// Add the local Hitachi Vantara MetaStore to the delegation.
// This sets it as the active one.
//
try {
XmlMetaStore localStore = new XmlMetaStore( MetaStoreConst.getDefaultPentahoMetaStoreLocation() );
metaStore.addMetaStore( localStore );
metaStore.setActiveMetaStoreName( localStore.getName() );
} catch ( MetaStoreException e ) {
LogChannel.GENERAL.logError( "Unable to open local Pentaho meta store from [" + MetaStoreConst.getDefaultPentahoMetaStoreLocation() + "]", e );
// now replace this with an in memory metastore.
//
try {
MemoryMetaStore memoryStore = new MemoryMetaStore();
memoryStore.setName( "Memory metastore" );
metaStore.addMetaStore( memoryStore );
metaStore.setActiveMetaStoreName( memoryStore.getName() );
} catch ( MetaStoreException e2 ) {
throw new RuntimeException( "Unable to add a default memory metastore to the delegating store", e );
}
}
passwordFile = null; // force lookup by server in ~/.kettle or local folder
}
public SlaveServerConfig( SlaveServer slaveServer ) {
this();
this.slaveServer = slaveServer;
}
public SlaveServerConfig( List masters, boolean reportingToMasters, SlaveServer slaveServer ) {
this.masters = masters;
this.reportingToMasters = reportingToMasters;
this.slaveServer = slaveServer;
}
public String getXML() {
StringBuilder xml = new StringBuilder();
xml.append( XMLHandler.openTag( XML_TAG ) );
for ( SlaveServer slaveServer : masters ) {
xml.append( slaveServer.getXML() );
}
XMLHandler.addTagValue( "report_to_masters", reportingToMasters );
if ( slaveServer != null ) {
xml.append( slaveServer.getXML() );
}
XMLHandler.addTagValue( "joining", joining );
XMLHandler.addTagValue( "max_log_lines", maxLogLines );
XMLHandler.addTagValue( "max_log_timeout_minutes", maxLogTimeoutMinutes );
XMLHandler.addTagValue( "object_timeout_minutes", objectTimeoutMinutes );
xml.append( XMLHandler.openTag( XML_TAG_SEQUENCES ) );
for ( SlaveSequence slaveSequence : slaveSequences ) {
xml.append( XMLHandler.openTag( SlaveSequence.XML_TAG ) );
xml.append( slaveSequence.getXML() );
xml.append( XMLHandler.closeTag( SlaveSequence.XML_TAG ) );
}
xml.append( XMLHandler.closeTag( XML_TAG_SEQUENCES ) );
if ( autoSequence != null ) {
xml.append( XMLHandler.openTag( XML_TAG_AUTOSEQUENCE ) );
xml.append( autoSequence.getXML() );
xml.append( XMLHandler.addTagValue( XML_TAG_AUTO_CREATE, automaticCreationAllowed ) );
xml.append( XMLHandler.closeTag( XML_TAG_AUTOSEQUENCE ) );
}
if ( repositoryMeta != null ) {
xml.append( XMLHandler.openTag( XML_TAG_REPOSITORY ) );
xml.append( " " ).append( XMLHandler.addTagValue( "id", repositoryMeta.getId() ) );
xml.append( " " ).append( XMLHandler.addTagValue( "username", repositoryUsername ) );
xml.append( " " ).append(
XMLHandler.addTagValue( "password", Encr.encryptPasswordIfNotUsingVariables( repositoryPassword ) ) );
xml.append( XMLHandler.closeTag( XML_TAG_REPOSITORY ) );
}
xml.append( XMLHandler.closeTag( XML_TAG ) );
return xml.toString();
}
public SlaveServerConfig( LogChannelInterface log, Node node ) throws KettleXMLException {
this();
Node slaveNode = XMLHandler.getSubNode( node, SlaveServer.XML_TAG );
if ( slaveNode != null ) {
slaveServer = new SlaveServer( slaveNode );
checkNetworkInterfaceSetting( log, slaveNode, slaveServer );
}
Node mastersNode = XMLHandler.getSubNode( node, XML_TAG_MASTERS );
int nrMasters = XMLHandler.countNodes( mastersNode, SlaveServer.XML_TAG );
for ( int i = 0; i < nrMasters; i++ ) {
Node masterSlaveNode = XMLHandler.getSubNodeByNr( mastersNode, SlaveServer.XML_TAG, i );
SlaveServer masterSlaveServer = new SlaveServer( masterSlaveNode );
checkNetworkInterfaceSetting( log, masterSlaveNode, masterSlaveServer );
masterSlaveServer.setSslMode( slaveServer.isSslMode() );
masters.add( masterSlaveServer );
}
reportingToMasters = "Y".equalsIgnoreCase( XMLHandler.getTagValue( node, "report_to_masters" ) );
joining = "Y".equalsIgnoreCase( XMLHandler.getTagValue( node, "joining" ) );
maxLogLines = Const.toInt( XMLHandler.getTagValue( node, "max_log_lines" ), 0 );
maxLogTimeoutMinutes = Const.toInt( XMLHandler.getTagValue( node, "max_log_timeout_minutes" ), 0 );
objectTimeoutMinutes = Const.toInt( XMLHandler.getTagValue( node, "object_timeout_minutes" ), 0 );
// Read sequence information
//
List dbNodes = XMLHandler.getNodes( node, DatabaseMeta.XML_TAG );
for ( Node dbNode : dbNodes ) {
databases.add( new DatabaseMeta( dbNode ) );
}
Node sequencesNode = XMLHandler.getSubNode( node, "sequences" );
List seqNodes = XMLHandler.getNodes( sequencesNode, SlaveSequence.XML_TAG );
for ( Node seqNode : seqNodes ) {
slaveSequences.add( new SlaveSequence( seqNode, databases ) );
}
Node autoSequenceNode = XMLHandler.getSubNode( node, XML_TAG_AUTOSEQUENCE );
if ( autoSequenceNode != null ) {
autoSequence = new SlaveSequence( autoSequenceNode, databases );
automaticCreationAllowed =
"Y".equalsIgnoreCase( XMLHandler.getTagValue( autoSequenceNode, XML_TAG_AUTO_CREATE ) );
}
// Set Jetty Options
setUpJettyOptions( node );
Node repositoryNode = XMLHandler.getSubNode( node, XML_TAG_REPOSITORY );
repositoryId = XMLHandler.getTagValue( repositoryNode, "name" );
repositoryUsername = XMLHandler.getTagValue( repositoryNode, "username" );
repositoryPassword = XMLHandler.getTagValue( repositoryNode, "password" );
}
/** Set up jetty options to the system properties
* @param node
*/
protected void setUpJettyOptions( Node node ) {
Map jettyOptions = parseJettyOptions( node );
if ( jettyOptions != null && jettyOptions.size() > 0 ) {
for ( Entry jettyOption : jettyOptions.entrySet() ) {
System.setProperty( jettyOption.getKey(), jettyOption.getValue() );
}
}
}
/**
* Read and parse jetty options
*
* @param node
* that contains jetty options nodes
* @return map of not empty jetty options
*/
protected Map parseJettyOptions( Node node ) {
Map jettyOptions = null;
Node jettyOptionsNode = XMLHandler.getSubNode( node, XML_TAG_JETTY_OPTIONS );
if ( jettyOptionsNode != null ) {
jettyOptions = new HashMap();
if ( XMLHandler.getTagValue( jettyOptionsNode, XML_TAG_ACCEPTORS ) != null ) {
jettyOptions.put( Const.KETTLE_CARTE_JETTY_ACCEPTORS, XMLHandler.getTagValue( jettyOptionsNode, XML_TAG_ACCEPTORS ) );
}
if ( XMLHandler.getTagValue( jettyOptionsNode, XML_TAG_ACCEPT_QUEUE_SIZE ) != null ) {
jettyOptions.put( Const.KETTLE_CARTE_JETTY_ACCEPT_QUEUE_SIZE, XMLHandler.getTagValue( jettyOptionsNode,
XML_TAG_ACCEPT_QUEUE_SIZE ) );
}
if ( XMLHandler.getTagValue( jettyOptionsNode, XML_TAG_LOW_RES_MAX_IDLE_TIME ) != null ) {
jettyOptions.put( Const.KETTLE_CARTE_JETTY_RES_MAX_IDLE_TIME, XMLHandler.getTagValue( jettyOptionsNode,
XML_TAG_LOW_RES_MAX_IDLE_TIME ) );
}
}
return jettyOptions;
}
private void openRepository( String repositoryId ) throws KettleException {
try {
RepositoriesMeta repositoriesMeta = new RepositoriesMeta();
repositoriesMeta.readData();
repositoryMeta = repositoriesMeta.findRepository( repositoryId );
if ( repositoryMeta == null ) {
throw new KettleException( "Unable to find repository: " + repositoryId );
}
PluginRegistry registry = PluginRegistry.getInstance();
repository = registry.loadClass( RepositoryPluginType.class, repositoryMeta, Repository.class );
repository.init( repositoryMeta );
repository.connect( repositoryUsername, repositoryPassword );
// Add the repository MetaStore to the delegation as well.
// Set this one as active with the highest priority
//
if ( repository.getMetaStore() != null ) {
metaStore.addMetaStore( 0, repository.getMetaStore() );
metaStore.setActiveMetaStoreName( repository.getMetaStore().getName() );
}
LogChannel.GENERAL.logBasic( "Connected to repository '" + repository.getName() + "'" );
} catch ( Exception e ) {
throw new KettleException( "Unable to open repository connection", e );
}
}
public void readAutoSequences() throws KettleException {
if ( autoSequence == null ) {
return;
}
Database database = null;
try {
DatabaseMeta databaseMeta = autoSequence.getDatabaseMeta();
LoggingObjectInterface loggingInterface =
new SimpleLoggingObject( "auto-sequence", LoggingObjectType.GENERAL, null );
database = new Database( loggingInterface, databaseMeta );
database.connect();
String schemaTable =
databaseMeta.getQuotedSchemaTableCombination( autoSequence.getSchemaName(), autoSequence.getTableName() );
String seqField = databaseMeta.quoteField( autoSequence.getSequenceNameField() );
String valueField = databaseMeta.quoteField( autoSequence.getValueField() );
String sql = "SELECT " + seqField + ", " + valueField + " FROM " + schemaTable;
List