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();
}
}
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;
}
use of org.pentaho.di.repository.RepositoriesMeta in project pentaho-kettle by pentaho.
the class ExecuteJobServlet method openRepository.
@VisibleForTesting
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) {
String message = BaseMessages.getString(PKG, "ExecuteJobServlet.Error.UnableToFindRepository", repositoryName);
throw new KettleRepositoryNotFoundException(message);
}
PluginRegistry registry = PluginRegistry.getInstance();
Repository repository = registry.loadClass(RepositoryPluginType.class, repositoryMeta, Repository.class);
repository.init(repositoryMeta);
repository.connect(user, pass);
return repository;
}
use of org.pentaho.di.repository.RepositoriesMeta in project pentaho-kettle by pentaho.
the class ExecuteTransServletTest method initMocksForTransExecution.
private Trans initMocksForTransExecution(HttpServletRequest mockHttpServletRequest, HttpServletResponse mockHttpServletResponse) throws Exception {
RepositoriesMeta repositoriesMeta = mock(RepositoriesMeta.class);
RepositoryMeta repositoryMeta = mock(RepositoryMeta.class);
Repository repository = mock(Repository.class);
PluginRegistry pluginRegistry = mock(PluginRegistry.class);
RepositoryDirectoryInterface repositoryDirectoryInterface = mock(RepositoryDirectoryInterface.class);
ObjectId objectId = mock(ObjectId.class);
TransMeta transMeta = mock(TransMeta.class);
Trans trans = mock(Trans.class);
KettleLogStore.init();
StringWriter out = new StringWriter();
PrintWriter printWriter = new PrintWriter(out);
when(mockHttpServletRequest.getParameter("rep")).thenReturn("Repo");
when(mockHttpServletRequest.getParameter("trans")).thenReturn("Trans");
when(mockHttpServletRequest.getParameter("user")).thenReturn("user");
when(mockHttpServletRequest.getParameter("pass")).thenReturn("pass");
when(mockHttpServletResponse.getWriter()).thenReturn(printWriter);
whenNew(RepositoriesMeta.class).withNoArguments().thenReturn(repositoriesMeta);
when(repositoriesMeta.findRepository("Repo")).thenReturn(repositoryMeta);
mockStatic(PluginRegistry.class);
when(PluginRegistry.getInstance()).thenReturn(pluginRegistry);
when(pluginRegistry.loadClass(RepositoryPluginType.class, repositoryMeta, Repository.class)).thenReturn(repository);
when(repository.isConnected()).thenReturn(true);
when(repository.loadRepositoryDirectoryTree()).thenReturn(repositoryDirectoryInterface);
when(repositoryDirectoryInterface.findDirectory("/")).thenReturn(repositoryDirectoryInterface);
when(repository.getTransformationID("Trans", repositoryDirectoryInterface)).thenReturn(objectId);
when(repository.loadTransformation(objectId, null)).thenReturn(transMeta);
when(mockHttpServletRequest.getParameterNames()).thenReturn(Collections.enumeration(new ArrayList<>()));
whenNew(Trans.class).withAnyArguments().thenReturn(trans);
setInternalState(executeTransServlet, "socketRepository", mock(SocketRepository.class));
setInternalState(executeTransServlet, "transformationMap", mock(TransformationMap.class));
return trans;
}
use of org.pentaho.di.repository.RepositoriesMeta in project pentaho-kettle by pentaho.
the class Import method main.
public static void main(String[] a) throws KettleException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
KettleEnvironment.init();
Props.init(Props.TYPE_PROPERTIES_SPOON);
List<String> args = pickupCmdArguments(a);
StringBuilder optionRepname, optionUsername, optionPassword, optionDirname;
StringBuilder optionLimitDir, optionFilename, optionRules, optionComment;
StringBuilder optionReplace, optionContinueOnError, optionVersion, optionFileDir, optionNoRules;
CommandLineOption[] options = new CommandLineOption[] { //
createOption("rep", "Import.CmdLine.RepName", optionRepname = new StringBuilder()), createOption("user", "Import.CmdLine.RepUsername", optionUsername = new StringBuilder()), createOption("pass", "Import.CmdLine.RepPassword", optionPassword = new StringBuilder()), createOption("dir", "Import.CmdLine.RepDir", optionDirname = new StringBuilder()), createOption("limitdir", "Import.CmdLine.LimitDir", optionLimitDir = new StringBuilder()), createOption("file", "Import.CmdLine.File", optionFilename = new StringBuilder()), createOption("filedir", "Import.CmdLine.FileDir", optionFileDir = new StringBuilder()), createOption("rules", "Import.CmdLine.RulesFile", optionRules = new StringBuilder()), createOption("norules", "Import.CmdLine.NoRules", optionNoRules = new StringBuilder(), true, false), createOption("comment", "Import.CmdLine.Comment", optionComment = new StringBuilder(), false, false), createOption("replace", "Import.CmdLine.Replace", optionReplace = new StringBuilder(), true, false), createOption("coe", "Import.CmdLine.ContinueOnError", optionContinueOnError = new StringBuilder(), true, false), createOption("version", "Import.CmdLine.Version", optionVersion = new StringBuilder(), true, false), new CommandLineOption("", BaseMessages.getString(PKG, "Import.CmdLine.ExtraFiles"), new StringBuilder(), false, true, true) };
if (args.isEmpty()) {
CommandLineOption.printUsage(options);
exitJVM(9);
}
final LogChannelInterface log = new LogChannel(STRING_IMPORT);
CommandLineOption.parseArguments(args, options, log);
// The arguments that are still left in args are in fact filenames that need to be imported.
// This list is otherwise empty.
// To that we add the normal filename option
//
List<String> filenames = new ArrayList<String>(args);
if (!Utils.isEmpty(optionFilename)) {
filenames.add(optionFilename.toString());
}
String kettleRepname = Const.getEnvironmentVariable("KETTLE_REPOSITORY", null);
String kettleUsername = Const.getEnvironmentVariable("KETTLE_USER", null);
String kettlePassword = Const.getEnvironmentVariable("KETTLE_PASSWORD", null);
if (!Utils.isEmpty(kettleRepname)) {
optionRepname = new StringBuilder(kettleRepname);
}
if (!Utils.isEmpty(kettleUsername)) {
optionUsername = new StringBuilder(kettleUsername);
}
if (!Utils.isEmpty(kettlePassword)) {
optionPassword = new StringBuilder(kettlePassword);
}
if (!Utils.isEmpty(optionVersion)) {
BuildVersion buildVersion = BuildVersion.getInstance();
log.logBasic(BaseMessages.getString(PKG, "Import.Log.KettleVersion", buildVersion.getVersion(), buildVersion.getRevision(), buildVersion.getBuildDate()));
if (a.length == 1) {
exitJVM(6);
}
}
//
if (Utils.isEmpty(optionRepname)) {
log.logError(BaseMessages.getString(PKG, "Import.Error.NoRepProvided"));
exitJVM(1);
}
if (Utils.isEmpty(filenames)) {
log.logError(BaseMessages.getString(PKG, "Import.Error.NoExportFileProvided"));
exitJVM(1);
}
if (Utils.isEmpty(optionDirname)) {
log.logError(BaseMessages.getString(PKG, "Import.Error.NoRepositoryDirectoryProvided"));
exitJVM(1);
}
if (Utils.isEmpty(optionRules) && Utils.isEmpty(optionNoRules) && !"Y".equalsIgnoreCase(optionNoRules.toString())) {
log.logError(BaseMessages.getString(PKG, "Import.Error.NoRulesFileProvided"));
exitJVM(1);
}
// Load the rules file!
//
ImportRules importRules = new ImportRules();
String rulesFile = optionRules.toString();
if (!Utils.isEmpty(rulesFile)) {
try {
Document document = XMLHandler.loadXMLFile(rulesFile);
Node rulesNode = XMLHandler.getSubNode(document, ImportRules.XML_TAG);
importRules.loadXML(rulesNode);
log.logMinimal(BaseMessages.getString(PKG, "Import.Log.RulesLoaded", rulesFile, Integer.toString(importRules.getRules().size())));
for (ImportRuleInterface rule : importRules.getRules()) {
log.logBasic(" - " + rule.toString());
}
} catch (KettleException e) {
log.logError(BaseMessages.getString(PKG, "Import.Log.ExceptionLoadingRules", rulesFile), e);
exitJVM(7);
}
}
// Get the list of limiting source directories
//
List<String> limitDirs;
if (!Utils.isEmpty(optionLimitDir)) {
String[] directories = optionLimitDir.toString().split(",");
limitDirs = Arrays.asList(directories);
} else {
limitDirs = Collections.emptyList();
}
// Find the repository metadata...
//
RepositoriesMeta repsinfo = new RepositoriesMeta();
repsinfo.getLog().setLogLevel(log.getLogLevel());
try {
repsinfo.readData();
} catch (Exception e) {
log.logError(BaseMessages.getString(PKG, "Import.Error.UnableToLoadRepositoryInformation"), e);
exitJVM(1);
}
RepositoryMeta repositoryMeta = repsinfo.findRepository(optionRepname.toString());
if (repositoryMeta == null) {
log.logError(BaseMessages.getString(PKG, "Import.Error.RepositoryCouldNotBeFound", optionRepname.toString()));
exitJVM(1);
}
if (Utils.isEmpty(optionRepname)) {
log.logError(BaseMessages.getString(PKG, "Import.Error.NoRepProvided"));
exitJVM(1);
}
// Load the repository object as a plugin...
//
Repository repository = null;
try {
repository = PluginRegistry.getInstance().loadClass(RepositoryPluginType.class, repositoryMeta, Repository.class);
repository.init(repositoryMeta);
repository.getLog().setLogLevel(log.getLogLevel());
} catch (Exception e) {
log.logError(BaseMessages.getString(PKG, "Import.Error.UnableToLoadOrInitializeRepository"));
exitJVM(1);
}
try {
repository.connect(optionUsername != null ? optionUsername.toString() : null, optionPassword != null ? optionPassword.toString() : null);
} catch (KettleException ke) {
log.logError(ke.getMessage());
exitJVM(1);
} catch (Exception e) {
log.logError(BaseMessages.getString(PKG, "Import.Error.UnableToConnectToRepository"));
exitJVM(1);
}
final boolean replace = Utils.isEmpty(optionReplace) ? false : ValueMetaString.convertStringToBoolean(optionReplace.toString());
final boolean continueOnError = Utils.isEmpty(optionContinueOnError) ? false : ValueMetaString.convertStringToBoolean(optionContinueOnError.toString());
// Start the import!
//
log.logMinimal(BaseMessages.getString(PKG, "Import.Log.Starting"));
Date start, stop;
SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS");
start = new Date();
int returnCode = 0;
try {
RepositoryDirectoryInterface tree = repository.loadRepositoryDirectoryTree();
RepositoryDirectoryInterface targetDirectory = tree.findDirectory(optionDirname.toString());
if (targetDirectory == null) {
log.logError(BaseMessages.getString(PKG, "Import.Error.UnableToFindTargetDirectoryInRepository", optionDirname.toString()));
exitJVM(1);
}
// Perform the actual import
IRepositoryImporter importer = repository.getImporter();
importer.setImportRules(importRules);
if (!limitDirs.isEmpty()) {
if (importer instanceof CanLimitDirs) {
((CanLimitDirs) importer).setLimitDirs(limitDirs);
} else {
throw new KettleException(BaseMessages.getString(PKG, "Import.CouldntLimitDirs", importer.getClass().getCanonicalName()));
}
}
RepositoryImportFeedbackInterface feedbackInterface = new ImportFeedback(log, continueOnError, replace, reader);
// Import files in a certain directory
importer.importAll(feedbackInterface, optionFileDir.toString(), filenames.toArray(new String[filenames.size()]), targetDirectory, replace, continueOnError, optionComment.toString());
// If the importer has exceptions, then our return code is 2
List<Exception> exceptions = importer.getExceptions();
if (exceptions != null && !exceptions.isEmpty()) {
log.logError(BaseMessages.getString(PKG, "Import.Error.UnexpectedErrorDuringImport"), exceptions.get(0));
returnCode = 2;
}
} catch (Exception e) {
log.logError(BaseMessages.getString(PKG, "Import.Error.UnexpectedErrorDuringImport"), e);
exitJVM(2);
}
log.logMinimal(BaseMessages.getString(PKG, "Import.Log.Finished"));
stop = new Date();
String begin = df.format(start);
String end = df.format(stop);
log.logMinimal(BaseMessages.getString(PKG, "Import.Log.StartStop", begin, end));
long seconds = (stop.getTime() - start.getTime()) / 1000;
if (seconds <= 60) {
log.logMinimal(BaseMessages.getString(PKG, "Import.Log.ProcessEndAfter", String.valueOf(seconds)));
} else if (seconds <= 60 * 60) {
int min = (int) (seconds / 60);
int rem = (int) (seconds % 60);
log.logMinimal(BaseMessages.getString(PKG, "Import.Log.ProcessEndAfterLong", String.valueOf(min), String.valueOf(rem), String.valueOf(seconds)));
} else if (seconds <= 60 * 60 * 24) {
int rem;
int hour = (int) (seconds / (60 * 60));
rem = (int) (seconds % (60 * 60));
int min = rem / 60;
rem = rem % 60;
log.logMinimal(BaseMessages.getString(PKG, "Import.Log.ProcessEndAfterLonger", String.valueOf(hour), String.valueOf(min), String.valueOf(rem), String.valueOf(seconds)));
} else {
int rem;
int days = (int) (seconds / (60 * 60 * 24));
rem = (int) (seconds % (60 * 60 * 24));
int hour = rem / (60 * 60);
rem = rem % (60 * 60);
int min = rem / 60;
rem = rem % 60;
log.logMinimal(BaseMessages.getString(PKG, "Import.Log.ProcessEndAfterLongest", String.valueOf(days), String.valueOf(hour), String.valueOf(min), String.valueOf(rem), String.valueOf(seconds)));
}
exitJVM(returnCode);
}
Aggregations