Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,24 @@ public class InlongConstants {

public static final String AMPERSAND = "&";

public static final String ASTERISK = "*";

public static final String NEW_LINE = "\n";

public static final String REGEX_WHITESPACE = "\\s";

public static final char SINGLE_QUOTE_CHAR = '\'';

public static final char DOUBLE_QUOTE_CHAR = '"';

public static final char NEW_LINE_CHAR = '\n';

public static final char CARRIAGE_RETURN_CHAR = '\r';

public static final char SEMICOLON_CHAR = ';';

public static final char PIPE_CHAR = '|';

public static final String ADMIN_USER = "admin";

public static final Integer AFFECTED_ONE_ROW = 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ public enum ErrorCodeEnum {

MODULE_NOT_FOUND(6001, "Module does not exist/no operation authority"),
MODULE_INFO_INCORRECT(6002, "Module info was incorrect"),
MODULE_COMMAND_NOT_IN_WHITELIST(6003,
"Module command not in whitelist: '%s'"),

PACKAGE_NOT_FOUND(7001, "Package does not exist/no operation authority"),
PACKAGE_INFO_INCORRECT(7002, "Package info was incorrect"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.apache.inlong.manager.service.module;

/**
* The five command fields validated by {@link ModuleCommandValidator}. Only carries the
* display label used in error messages; how to read the value from a given carrier
* (DTO / Request / ...) is supplied by {@link ModuleCommandAccessors}, so this enum is
* decoupled from any concrete POJO type.
*
* <p>Package-private on purpose — it is an implementation detail shared between
* {@link ModuleCommandValidator} and {@link ModuleCommandAccessors} and should not leak
* to callers.
*/
enum CommandField {

START("startCommand"),
STOP("stopCommand"),
CHECK("checkCommand"),
INSTALL("installCommand"),
UNINSTALL("uninstallCommand");

final String label;

CommandField(String label) {
this.label = label;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.apache.inlong.manager.service.module;

import org.apache.inlong.manager.pojo.module.ModuleDTO;
import org.apache.inlong.manager.pojo.module.ModuleRequest;

import java.util.EnumMap;
import java.util.Map;
import java.util.function.Function;

/**
* The single place in this package that knows how to read the five command fields off a
* concrete POJO. Given a carrier ({@link ModuleRequest} / {@link ModuleDTO} / ...), it
* returns a {@code Function<CommandField, String>} that {@link ModuleCommandValidator}
* can drive uniformly.
*
* <p>Design notes:
* <ul>
* <li>The field \u2192 getter mapping is registered <b>once</b> per carrier type in a
* {@link EnumMap} \u2014 no more per-carrier {@code switch} statements.</li>
* <li>Adding a new carrier (e.g. {@code ModuleResponse}) = one extra static map + one
* extra {@code of(...)} overload. Nothing in {@link ModuleCommandValidator}
* changes.</li>
* <li>Package-private on purpose \u2014 this is an internal helper, not part of any
* public API.</li>
* </ul>
*/
public final class ModuleCommandAccessors {

private static final Map<CommandField, Function<ModuleRequest, String>> REQUEST_GETTERS =
new EnumMap<>(CommandField.class);
private static final Map<CommandField, Function<ModuleDTO, String>> DTO_GETTERS =
new EnumMap<>(CommandField.class);

static {
REQUEST_GETTERS.put(CommandField.START, ModuleRequest::getStartCommand);
REQUEST_GETTERS.put(CommandField.STOP, ModuleRequest::getStopCommand);
REQUEST_GETTERS.put(CommandField.CHECK, ModuleRequest::getCheckCommand);
REQUEST_GETTERS.put(CommandField.INSTALL, ModuleRequest::getInstallCommand);
REQUEST_GETTERS.put(CommandField.UNINSTALL, ModuleRequest::getUninstallCommand);

DTO_GETTERS.put(CommandField.START, ModuleDTO::getStartCommand);
DTO_GETTERS.put(CommandField.STOP, ModuleDTO::getStopCommand);
DTO_GETTERS.put(CommandField.CHECK, ModuleDTO::getCheckCommand);
DTO_GETTERS.put(CommandField.INSTALL, ModuleDTO::getInstallCommand);
DTO_GETTERS.put(CommandField.UNINSTALL, ModuleDTO::getUninstallCommand);
}

private ModuleCommandAccessors() {
}

/**
* Return a reader over the command fields of the given {@link ModuleRequest}.
*/
static Function<CommandField, String> of(ModuleRequest request) {
return readerFor(request, REQUEST_GETTERS);
}

/**
* Return a reader over the command fields of the given {@link ModuleDTO}.
*/
static Function<CommandField, String> of(ModuleDTO dto) {
return readerFor(dto, DTO_GETTERS);
}

private static <T> Function<CommandField, String> readerFor(T carrier,
Map<CommandField, Function<T, String>> getters) {
return field -> {
Function<T, String> getter = getters.get(field);
if (getter == null) {
throw new IllegalStateException("No getter registered for command field: " + field);
}
return getter.apply(carrier);
};
}
}
Loading
Loading