Skip to content

Commit

Permalink
ninja: main/top-level code cleanup + message fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
virgo47 committed Dec 3, 2021
1 parent 6343f26 commit f2ac288
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 66 deletions.
28 changes: 17 additions & 11 deletions tools/ninja/src/main/java/com/evolveum/midpoint/ninja/Main.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
/*
* Copyright (c) 2010-2018 Evolveum and contributors
* Copyright (C) 2010-2021 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/
package com.evolveum.midpoint.ninja;

import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Objects;

import com.beust.jcommander.JCommander;
import com.beust.jcommander.ParameterException;
Expand All @@ -27,7 +29,7 @@ public static void main(String[] args) {
new Main().run(args);
}

protected void run(String[] args) {
protected <T> void run(String[] args) {
JCommander jc = NinjaUtils.setupCommandLineParser();

try {
Expand All @@ -39,14 +41,18 @@ protected void run(String[] args) {

String parsedCommand = jc.getParsedCommand();

BaseOptions base = NinjaUtils.getOptions(jc, BaseOptions.class);
BaseOptions base = Objects.requireNonNull(
NinjaUtils.getOptions(jc, BaseOptions.class));

if (base.isVersion()) {
try {
Path path = Paths.get(Main.class.getResource("/version").toURI());
URL versionResource = Objects.requireNonNull(
Main.class.getResource("/version"));
Path path = Paths.get(versionResource.toURI());
String version = FileUtils.readFileToString(path.toFile(), StandardCharsets.UTF_8);
System.out.println(version);
} catch (Exception ex) {
// ignored
}
return;
}
Expand All @@ -65,8 +71,9 @@ protected void run(String[] args) {

NinjaContext context = null;
try {
ConnectionOptions connection = NinjaUtils.getOptions(jc, ConnectionOptions.class);
Action action;
ConnectionOptions connection = Objects.requireNonNull(
NinjaUtils.getOptions(jc, ConnectionOptions.class));
Action<T> action;
if (connection.isUseWebservice()) {
action = Command.createRestAction(parsedCommand);
} else {
Expand All @@ -80,7 +87,8 @@ protected void run(String[] args) {
return;
}

Object options = jc.getCommands().get(parsedCommand).getObjects().get(0);
//noinspection unchecked
T options = (T) jc.getCommands().get(parsedCommand).getObjects().get(0);

context = new NinjaContext(jc);

Expand Down Expand Up @@ -140,11 +148,9 @@ private void handleException(BaseOptions opts, Exception ex) {
}

private void printHelp(JCommander jc, String parsedCommand) {
if (parsedCommand == null) {
jc.usage();
} else {
if (parsedCommand != null) {
jc.getUsageFormatter().usage(parsedCommand);
jc.usage();
}
jc.usage();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010-2018 Evolveum and contributors
* Copyright (C) 2010-2021 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
Expand All @@ -9,13 +9,12 @@
import java.util.concurrent.BlockingQueue;

import com.evolveum.midpoint.ninja.action.worker.ExportConsumerWorker;
import com.evolveum.midpoint.ninja.impl.NinjaContext;
import com.evolveum.midpoint.ninja.opts.ExportOptions;
import com.evolveum.midpoint.ninja.util.OperationStatus;
import com.evolveum.midpoint.prism.PrismObject;

/**
* Created by Viliam Repan (lazyman).
* Ninja action realizing "export" command.
*/
public class ExportRepositoryAction extends AbstractRepositorySearchAction<ExportOptions> {

Expand All @@ -28,5 +27,4 @@ protected String getOperationShortName() {
protected Runnable createConsumer(BlockingQueue<PrismObject> queue, OperationStatus operation) {
return new ExportConsumerWorker(context, options, queue, operation);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010-2019 Evolveum and contributors
* Copyright (C) 2010-2021 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
Expand All @@ -25,32 +25,22 @@ public enum Command {

VERIFY("verify", VerifyOptions.class, VerifyRepositoryAction.class, null),

// PASSWORD_RESET("password", PasswordResetOptions.class, PasswordResetRepositoryAction.class, null),
//
// UNLOCK("unlock", UnlockOptions.class, UnlockRepositoryAction.class, null),
//
// TEST("test", TestResourceOptions.class, null, TestResourceRestAction.class),
//
KEYS("keys", ListKeysOptions.class, ListKeysRepositoryAction.class, null),

TRACE("trace", EditTraceOptions.class, EditTraceAction.class, null);
//
// TRANSFORM("transform", TransformOptions.class, TransformRepositoryAction.class, null),
//
// SCHEMA("schema", SchemaOptions.class, SchemaRepositoryAction.class, null);

// todo reencrypt, modify, bulk, etc

private String commandName;
private final String commandName;

private Class options;
private final Class<?> options;

private Class<? extends RepositoryAction> repositoryAction;
private final Class<? extends RepositoryAction<?>> repositoryAction;

private Class<? extends RestAction> restAction;
private final Class<? extends RestAction<?>> restAction;

Command(String commandName, Class options, Class<? extends RepositoryAction> repositoryAction,
Class<? extends RestAction> restAction) {
<T> Command(String commandName, Class<T> options, Class<? extends RepositoryAction<T>> repositoryAction,
Class<? extends RestAction<T>> restAction) {
this.commandName = commandName;
this.options = options;
this.repositoryAction = repositoryAction;
Expand All @@ -63,13 +53,13 @@ public String getCommandName() {

public Object createOptions() {
try {
return options.newInstance();
return options.getDeclaredConstructor().newInstance();
} catch (Exception ex) {
throw new IllegalStateException(ex);
}
}

public static RepositoryAction createRepositoryAction(String command) {
public static <T> RepositoryAction<T> createRepositoryAction(String command) {
Command cmd = findCommand(command);
if (cmd == null) {
return null;
Expand All @@ -80,13 +70,14 @@ public static RepositoryAction createRepositoryAction(String command) {
return null;
}

return cmd.repositoryAction.newInstance();
//noinspection unchecked
return (RepositoryAction<T>) cmd.repositoryAction.getDeclaredConstructor().newInstance();
} catch (Exception ex) {
throw new IllegalStateException(ex);
}
}

public static RestAction createRestAction(String command) {
public static <T> RestAction<T> createRestAction(String command) {
Command cmd = findCommand(command);
if (cmd == null) {
return null;
Expand All @@ -97,7 +88,8 @@ public static RestAction createRestAction(String command) {
return null;
}

return cmd.restAction.newInstance();
//noinspection unchecked
return (RestAction<T>) cmd.restAction.getDeclaredConstructor().newInstance();
} catch (Exception ex) {
throw new IllegalStateException(ex);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
/*
* Copyright (c) 2010-2018 Evolveum and contributors
* Copyright (C) 2010-2021 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/
package com.evolveum.midpoint.ninja.opts;

import java.io.File;

import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;

import java.io.File;

/**
* Created by Viliam Repan (lazyman).
*/
Expand All @@ -29,18 +29,15 @@ public class ExportOptions extends BaseImportExportOptions {
public static final String P_NO_IDS = "-ni";
public static final String P_NO_IDS_LONG = "--no-container-ids";

@Parameter(names = {P_OUTPUT, P_OUTPUT_LONG}, descriptionKey = "export.output")
@Parameter(names = { P_OUTPUT, P_OUTPUT_LONG }, descriptionKey = "export.output")
private File output;

@Parameter(names = {P_OVERWRITE, P_OVERWRITE_LONG}, descriptionKey = "export.overwrite")
@Parameter(names = { P_OVERWRITE, P_OVERWRITE_LONG }, descriptionKey = "export.overwrite")
private boolean overwrite;

@Parameter(names = {P_NO_IDS, P_NO_IDS_LONG}, descriptionKey = "export.skipids")
@Parameter(names = { P_NO_IDS, P_NO_IDS_LONG }, descriptionKey = "export.skipids")
private boolean skipIds;

// @Parameter(names = {P_SPLIT, P_SPLIT_LONG}, descriptionKey = "export.split")
// private boolean split;

public File getOutput() {
return output;
}
Expand All @@ -52,8 +49,4 @@ public boolean isOverwrite() {
public boolean isSkipContainerIds() {
return skipIds;
}

// public boolean isSplit() {
// return split;
// }
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
/*
* Copyright (c) 2010-2019 Evolveum and contributors
* Copyright (C) 2010-2021 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/
package com.evolveum.midpoint.ninja.util;

import java.io.*;
import java.lang.reflect.Modifier;
import java.nio.charset.Charset;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import com.beust.jcommander.JCommander;
import org.apache.commons.io.FileUtils;

import com.evolveum.midpoint.ninja.impl.Command;
import com.evolveum.midpoint.ninja.impl.NinjaContext;
import com.evolveum.midpoint.ninja.impl.NinjaException;
Expand All @@ -19,23 +32,14 @@
import com.evolveum.midpoint.prism.query.ObjectFilter;
import com.evolveum.midpoint.prism.query.ObjectQuery;
import com.evolveum.midpoint.prism.xnode.RootXNode;
import com.evolveum.midpoint.schema.*;
import com.evolveum.midpoint.schema.GetOperationOptionsBuilder;
import com.evolveum.midpoint.schema.constants.ObjectTypes;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.xml.ns._public.common.common_3.AccessCertificationCampaignType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.LookupTableType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.UserType;
import org.apache.commons.io.FileUtils;

import java.io.*;
import java.lang.reflect.Modifier;
import java.nio.charset.Charset;
import java.text.DecimalFormat;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
* Created by Viliam Repan (lazyman).
Expand All @@ -49,7 +53,7 @@ public class NinjaUtils {

public static final String XML_OBJECTS_SUFFIX = "</c:objects>";

public static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat(".##");
public static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("#.##");

public static final long COUNT_STATUS_LOG_INTERVAL = 2 * 1000; // two seconds

Expand Down Expand Up @@ -80,6 +84,7 @@ public static <T> T getOptions(JCommander jc, Class<T> type) {
List<Object> objects = jc.getObjects();
for (Object object : objects) {
if (type.equals(object.getClass())) {
//noinspection unchecked
return (T) object;
}
}
Expand Down Expand Up @@ -185,7 +190,7 @@ public static GetOperationOptionsBuilder addIncludeOptionsForExport(GetOperation
public static List<ObjectTypes> getTypes(Set<ObjectTypes> selected) {
List<ObjectTypes> types = new ArrayList<>();

if (selected != null && ! selected.isEmpty()) {
if (selected != null && !selected.isEmpty()) {
types.addAll(selected);
} else {
for (ObjectTypes type : ObjectTypes.values()) {
Expand Down
5 changes: 2 additions & 3 deletions tools/ninja/src/main/resources/messages.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright (c) 2010-2017 Evolveum and contributors
# Copyright (C) 2010-2021 Evolveum and contributors
#
# This work is dual-licensed under the Apache License 2.0
# and European Union Public License. See LICENSE file for details.
Expand Down Expand Up @@ -42,10 +42,9 @@ delete.filter=
export=Exports objects from midPoint
export.output=
export.overwrite=
export.split=
export.skipids=Skips container ids
verify=Verify objects in midPoint repository
verify.warn=List of displayed varning categories, e.g. deprecated,plannedRemoval, uuid
verify.warn=List of displayed warning categories, e.g. deprecated, plannedRemoval, uuid
passwordReset=Command will reset password of user specified by oid
passwordReset.oid=
testResource=Test resource
Expand Down

0 comments on commit f2ac288

Please sign in to comment.