I am working on a Spring Shell project. The tool is a command line tool to manipulate data in a database. There are commands like add user (which adds a record to a table in database). In order to execute any commands the user of the tool has to be connected to the database. I would like to be able to run this all in one line. The user of my tool should be able to write a command like the following.
--database connection string xyz --username abc --password mno add user --username bob --role AA_ADMIN --company Microsoft
Here the three parameters database connection string, username and password are required to run the add user command.
Below I have included some sample code it is from the spring shell reference docs
package commands;
import org.springframework.shell.core.CommandMarker;
import org.springframework.shell.core.annotation.CliAvailabilityIndicator;
import org.springframework.shell.core.annotation.CliCommand;
import org.springframework.shell.core.annotation.CliOption;
import org.springframework.stereotype.Component;
#Component
public class UserManipulation implements CommandMarker {
private boolean simpleCommandExecuted = false;
#CliAvailabilityIndicator({"hw simple"})
public boolean isSimpleAvailable() {
//always available
return true;
}
#CliAvailabilityIndicator({"hw complex", "hw enum"})
public boolean isComplexAvailable() {
if (simpleCommandExecuted) {
return true;
} else {
return false;
}
}
#CliCommand(value = "hw simple", help = "Print a simple hello world message")
public String simple(
#CliOption(key = { "message" }, mandatory = true, help = "The hello world message") final String message,
#CliOption(key = { "location" }, mandatory = false, help = "Where you are saying hello", specifiedDefaultValue="At work") final String location) {
simpleCommandExecuted = true;
return "Message = [" + message + "] Location = [" + location + "]";
}
#CliCommand(value = "hw complex", help = "Print a complex hello world message")
public String hello(
#CliOption(key = { "message" }, mandatory = true, help = "The hello world message") final String message,
#CliOption(key = { "name1"}, mandatory = true, help = "Say hello to the first name") final String name1,
#CliOption(key = { "name2" }, mandatory = true, help = "Say hello to a second name") final String name2,
#CliOption(key = { "time" }, mandatory = false, specifiedDefaultValue="now", help = "When you are saying hello") final String time,
#CliOption(key = { "location" }, mandatory = false, help = "Where you are saying hello") final String location) {
return "Hello " + name1 + " and " + name2 + ". Your special message is " + message + ". time=[" + time + "] location=[" + location + "]";
}
#CliCommand(value = "hw enum", help = "Print a simple hello world message from an enumerated value")
public String eenum(
#CliOption(key = { "message" }, mandatory = true, help = "The hello world message") final MessageType message){
return "Hello. Your special enumerated message is " + message;
}
enum MessageType {
Type1("type1"),
Type2("type2"),
Type3("type3");
private String type;
private MessageType(String type){
this.type = type;
}
public String getType(){
return type;
}
}
}
So currently, hw simple is a command that is required to be executed before running hw complex or hw enum command. I do not want hw simple to be a command instead it the message parameter within the hw simple command should be a parameter that is required as a prerequisite to run hw complex or hw enum. So for example the command that I would like to run is.
--message hw complex --message abc --name1 def --name2 ghi --time 7:98 --location: Seattle
Does anyone know how to do this? If it is not possible to do this I would like to hear that or any alternative ideas if possible.
You have 2 options here:
either make those 3 additional parameters (database, username, password) parameters of each and every command that require them (note that in your particular example, you would need to rename one of those username parameters [the one to connect to the DB, or the one that represents the user to add] as you can't have 2 parameters with the same name obviously).
Use the #CliAvailabilityIndicator approach, similar to what is described in the example, where a first command (maybe named use or connect) first tests the connection with the 3 given parameters and stores them somewhere, so that any further "real" command (such as add user) can use those values.
Also note that you can actually use a combination of the two (i.e. use solution 2 to provide defaults, that may be overridden on a case by case basis by solution 1).
Lastly, please note that you'll never be able to have something like what you describe at the beginning of your question, as command names must be at the beginning and they can't contain -- (options do)
Related
I am setting up the Backend code of a chat messaging system for an app my group is creating using WebSockets. My goal is for our app to be able to send and receive messages in a public group chat, and also specifically Direct Message (DM) specific people with an # symbol, in front of the recipient's username.
I managed to get the public group messaging component working perfectly fine. However, I am running into an issue with the DM functionality. Let's say for example a person named "test" wrote a message to someone named "teacher" and here was what they typed: "#teacher testMessage". Ideally, I would want the program to send the message "testMessage" to "teacher" only. However, every time I would test the program using Postman, I would end up with the following error:
java.lang.NullPointerException: Cannot invoke "javax.websocket.Session.getBasicRemote()" because the return value of "java.util.Map.get(Object)" is null
From my understanding, the error means that the variable type the method is supposed to receive (in this case a String), is not what it is actually getting.
Here is the code below:
private static Map < Session, String > sessionUsernameMap = new Hashtable<>();
private static Map < String, Session > usernameSessionMap = new Hashtable<>();
#OnMessage
public void onMessage(Session session, String message) throws IOException { //The message is the the entire thing that the person types (ex: #teacher testMessage)
logger.info("Message Received: " + message); //String message = #teacher testMessage
String username = sessionUsernameMap.get(session); //String username = "test" This is the username of the person who wrote the message
if (message.startsWith("#")) {
String destUsername = message.split(" ")[0].substring(1); //destUsername = "teacher"
String realMessage = message.substring(message.lastIndexOf(" ") + 1); //realMessage = "testMessage"
sendMessageToParticularUser(destUsername, "[DM] " + username + ": " + realMessage); //puts in "teacher" for destUsername and "testMessage" for realMessage
sendMessageToParticularUser(username, "[DM] " + username + ": " + message);
}
else {
broadcast(username + ": " + message);
}
msgRepo.save(new Message(username, message));
}
private void sendMessageToParticularUser(String username, String message) {
System.out.println(message);
try {
usernameSessionMap.get(username).getBasicRemote().sendText(message); //PROBLEM RIGHT HERE WITH .get(username)
} catch (IOException e) {
logger.info("Exception: " + e.getMessage().toString());
e.printStackTrace();
}
}
I have been working on this issue for a few hours now with no luck. I would very much appreciate any help or input on this. Thank you.
I am making a bot for discord and I want to get the username of the owner of the server and put it into a string.
Code of my ProcessCommand Class where I want it:
#SuppressWarnings("unused")
public static void proccessCommand(IMessage message, String prefix) {
IUser sender = message.getAuthor();
IChannel channel = message.getChannel();
IGuild guild = message.getGuild();
String[] command = message.getContent().toLowerCase().replaceFirst(prefix, "").split(" ");
if(command[0].equals("rules")) {
channel.sendMessage("ImConor's Discord Server Rules!" + "\r\n" + "No Racism, No Alts, No Disrespect to ANYONE, No Ban Evasion" );
}
else if(command[0].equals("restart")) {
message.delete();
channel.sendMessage("#everyone, I am restarting! Please do NOT use any ModBot commands!");
}else if(command[0].equals("ping")) {
channel.sendMessage("Pong!");
}else if(command[0].equals("start")) {
if (message.getAuthor() == *string of username here*) {
message.delete();
channel.sendMessage("#everyone, I have returned!");
}
}
}
Looking at this documentation here.
You already have a IGuild variable named guild from your message.getGuild() method.
And if you search into the documentation for owner (Ctrl + F helps), you will see a getOwner() method for IGuild class, which results a IUser and that object basically contains the details about the server's owner.
Finally, IUser object have a getName() method.
So in short, you can do String ownerName = guild.getOwner().getName();
Or simply a IUser ownerObject = guild.getOwner() and do whatever you want with the user object of the owner next.
so as part of some work I've been doing I was given a file with WebServices that are being used in a Swift application. I have zero familiarity with WebServices and only know Java through syntax understanding. I need to call one of these gets with a parameter from the swift application. What I'm trying to figure out first and foremost is how I can call one of these webservices with a parameter from the URL it's associated with. For example down below I want to call the method
http://localhost:9000/ListVehicleByPlateNumber
and I want to specify the parameter through the URL say something like
http://localhost:9000/ListVehicleByPlateNumber?para="123"
But this doesn't assign any value to the parameter and I'm not getting results. If I hardcode so that the string used in the function is = "123" it gives me the results I'm looking for. I just need to know how I can pass this parameter through the url, syntax-wise.
Routes file
GET /ListVehicleByPlateNumber controllers.NewVehicle.listVehicleByPlateNumber(para: String ?="")
Controller
public Result listVehicleByPlateNumber(String para){
NewVehicleModel v = new NewVehicleModel();
List<NewVehicleModel> vehiclesC = v.searchByPlateVehicle(para);
ObjectNode wrapper = Json.newObject();
ObjectNode msg = Json.newObject();
if(vehiclesC != null) {
msg.set("VehicleList", toJson(vehiclesC));
wrapper.set("success", msg);
return ok(wrapper);
}else{
msg.put("error", "There are no vehicles with the plate number");
wrapper.set("error", msg);
return badRequest(wrapper);
}
}
Where it's called
public List<NewVehicleModel> searchByPlateVehicle(String plateNumber){
Transaction t = Ebean.beginTransaction();
List<NewVehicleModel> vehicles = new ArrayList<>();
try {
String sql = "SELECT V.idNewVehicle, V.VehicleType,V.PlateNumber,V.VehicleJurisdiction,V.State,V.Vin,V.Year, " +
"V.Make,V.modelos,V.RegistrationNumber,V.InsuranceCompany,V.PurchaseDate,V.ExpirationDate,V.idPersonaFK " +
"FROM NewVehicle V " +
"WHERE V.PlateNumber = :plateNumber";
RawSql rawSql = RawSqlBuilder.parse(sql)
.columnMapping("V.idNewVehicle", "idNewVehicle")
.columnMapping("V.State", "state")
.columnMapping("V.VehicleType", "vehicleType")
.columnMapping("V.PlateNumber", "plateNumber")
.columnMapping("V.VehicleJurisdiction", "vehicleJurisdiction")
.columnMapping("V.Vin", "vin")
.columnMapping("V.Year", "year")
.columnMapping("V.Make", "make")
.columnMapping("V.modelos", "modelos")
.columnMapping("V.RegistrationNumber", "registrationNumber")
.columnMapping("V.InsuranceCompany", "insuranceCompany")
.columnMapping("V.PurchaseDate", "purchaseDate")
.columnMapping("V.ExpirationDate", "expirationDate")
.columnMapping("V.idPersonaFK", "idPersonaFK")
.create();
Query<NewVehicleModel> query = Ebean.find(NewVehicleModel.class);
query.setRawSql(rawSql)
.setParameter("plateNumber", plateNumber);
vehicles = query.findList();
t.commit();
}
catch (Exception e){
System.out.println(e.getMessage());
}finally {
t.end();
}
return vehicles;
}
Found my own answer. I ended up casting from Integer to String here's how it looks in routes
GET /ListVehicleByPlateNumber/:para controllers.NewVehicle.listVehicleByPlateNumber(para: Integer )
Controller
public Result listVehicleByPlateNumber(int para){
String p = String.valueOf(para);
URI Format for value 123 example.
http://localhost:9000/ListVehicleByPlateNumber/123
I'm currently working on an application that will be used to power on/off a device (server) over SSH. Now I use a property file with the following settings:
command.power_on.name = Power on
command.power_on.host = host.com
command.power_on.user = user
command.power_on.password = password
command.power_on.port = 22
command.power_on.timeout = 10000
command.power_on.command = power on command
command.power_off.name = Power off
command.power_off.host = host.com
command.power_off.user = user
command.power_off.password = password
command.power_off.port = 22
command.power_off.timeout = 10000
command.power_off.command = power off command
I want to loop through all the commands and put them into List, Array, Object, Custom Object or whatever is possible.
PSEUDO Code:
String[] commands = propertiesConfiguration.getKeys("command");
for (String command : commands) {
CommandModel[] commandModel = command;
/* Will return two command models with the power_on and power_off attributes.
(name, host, user, password, port, timeout and command) */
}
And as result I want to get something like this (like in XML):
Command[] command; // Array with all the commands.
System.out.println(command[0].toString()); // Will print the "power_on" attributes.
System.out.println(command[1].toString()); // Will print the "power_off" attributes.
CommandModel commandModel = new CommandModel(); // The command model with queries (select, select all and update).
Command powerOn = commandModel.getCommand("power_on"); // Will return the command "power_on" with their attributes (name, host, user, password, port, timeout, command).
Command powerOff = commandModel.getCommand("power_off"); // Will return the command "power_off" with their attributes (name, host, user, password, port, timeout, command).
Command[] command = commandModel.getAll(); // Will return an array of all the commands.
I know this is possible with XML based structures, but is this also possible with a property based file? I have only 2 commands, I thought a property file will be enough for this. Or should I use a XML file?
All feedback, suggestions etc are welcome. Thank you in advance!
You can loop all of your properties using the Properties class.
Have a look at ResourceBundle class here.
The getKeys() method will return a Enumeration of String.
You can also get the values by using getString(key) method. It accepts a key as a parameter.
The following code sample will add the requested output objects in a map, having as key the name of the two wanted commands (poweroff and poweron). The fields are set using reflection.
Taking into consideration that the properties file is in the same folder where the application is run the following code sample will read all properties defined in the properties file.
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
public class Main {
public static void main(String[] args) throws IOException, NoSuchFieldException, IllegalAccessException {
InputStream is = Main.class.getResourceAsStream("command.properties");
Properties properties = new Properties();
// the output map that will contain the results
// the poweroff related content for key "poweroff"
// the poweron related content for key "poweron"
Map<String, Command> commands = new HashMap<>();
properties.load(is);
// parse the properties file
for (Map.Entry<Object, Object> e :properties.entrySet()) {
// extract the name of the object (poweroff or poweron)
String name = name((String)e.getKey());
// extract the name of the property to be set (timeout, host etc.)
String property = property((String)e.getKey());
// get the object where to set the outcome
Command command = commands.get(name);
// create the object if it wasn't already created
// and add it to the map
if (command == null) {
command = new Command();
commands.put(name, command);
}
// set the value of the given property on the corresponding object
setValue(Command.class, command, property, e.getValue());
}
System.out.println(commands);
}
static String name(String input) {
return input.split("\\.")[1];
}
static String property(String input) {
return input.split("\\.")[2];
}
static void setValue(Class<?> clazz, Object object, String propertyName, Object value) throws NoSuchFieldException, IllegalAccessException {
Field field = clazz.getDeclaredField(propertyName);
field.setAccessible(true);
field.set(object, value);
}
static class Command {
String name;
String host;
String user;
String password;
String port;
String timeout;
String command;
#Override
public String toString() {
return name + " " + host + " " + user + " " + password + " " + port + " " + timeout + " " + command;
}
}
Output: {power_off=Power off host.com user password 22 10000 power off command, power_on=Power on host.com user password 22 10000 power on command}
Create a class called
public class MyProperties{
private String key;
private String value;
//get/set() methods of key/value OR Create Constructor with Varaibles
}
Now Write this logic
private List<MyProperties> createPropertyList(){
Properties proObj = new Properties();
proObj.load(new FileInputStream("PathOfPropertiesFile"));
List<MyProperties> propertyList = new ArrayList<MyProperties>();
for(String propertyKey: proObj.stringPropertyNames()) {
MyProperties myProperties = new MyProperties();
myProperties.setKey(propertyKey);
myProperties.setValue(properties.getProperty(propertyKey));
propertyList.add(myProperties);
}
return propertyList;
}
could someone help me. Here is my problem :
I try to send an object with jms (this part works) and receive it with jms.
My object is quite simple. 3 String, 3 int, and a boolean.
There are no problem of connexion or anything like this. I receive the object but it's as if I received every things one by one.
Here is my MessageListener :
MessageListener listner = new MessageListener() {
public void onMessage(Message message) {
ObectToSend yo=null;
try {
if (message instanceof ObjectMessage) {
ObjectMessage myMessage = (ObjectMessage) message;
System.err.println("test");
yo = (ObectToSend) myMessage.getObject();
System.err.println("test2");
System.err.println(yo.entite + " " + yo.error + " " + yo.idGloreg + " " + yo.indPerso + " " + yo.nom + " " + yo.prenom + " " + yo.nom);
}
} catch (JMSException e) {
System.out.println("Caught:" + e);
e.printStackTrace();
}
}
};
And that my sending part :
Serializable ObectTest = new ObectToSend("pro", "enc", 134, 10, true, "yayaya", 0);
MessageProducer producer = session.createProducer(topic);
producer.setDeliveryMode(DeliveryMode.PERSISTENT);
ObjectMessage message = session.createObjectMessage();
message.setObject(ObectTest);
connection.start();
producer.send(message);
Finally here is what I want to send (in receiver.java and sender.java) :
public static class ObectToSend implements Serializable{
private static final long serialVersionUID = 1L;
String prenom;
String nom;
int idGloreg;
int indPerso;
boolean ok;
String entite;
int error;
ObectToSend(String prenomP, String nomP, int idGloregP, int indPersoP, boolean okP, String entiteP, int errorP){
prenom = prenomP ;
nom= nomP;
idGloreg = idGloregP;
indPerso = indPersoP;
ok = okP;
entite= entiteP;
error = errorP;
}
}
My console :
test
test
test
test
test
test
test
If someone could tell me what's the problem that would be great. I don't get it. My textmessage with topic/queue/sync/async are working so nicely. It comes to object and....
It seems the problem is here :
yo = (ObectToSend) myMessage.getObject();
but.....
For future users of JMS I will answer my own question.
It was really hard to find any information as it's not explained in JMS documentation.
I found a lot of people asking how to do it but never had any answer. That's because it's not releated to JMS but to Java.
So here it goes :
If you want to use a same classe (object) like
ObectToSend yo = (ObectToSend) myMessage.getObject();
My first object (yo) is from the class ObectToSend.java in the package com.test.jms and my second object (myMessage.getObject() ) is from the package com.test.jms2. So I have an exception like "notfoundclass". And cannot cast objects.
The class ObectToSend.java should be in both projects. But you can't just copy paste with the same name (what I stupidly did).
You need to create a jar of the class used in both projects/packages and add it to both projects.
That way you use EXACTLY the same class and not a copy of it. And your 2 objects are exactly the same.
You could also use a map message. Indeed, you only have strings, ints, and booleans. You actually don't need an object. MapMessage is here for you and is much much simpler.
Here is an exemple :
// create mapMessage
message = session.createMapMessage();
// Here insert variables in properties of the message
// This can be filtred with selector
message.setStringProperty("entity", entity);
message.setStringProperty("messageFrom", messageFrom);
// Here insert variables in body of the message
//This CAN'T be filtred (what you want I think)
message.setString("title", title);
message.setString("description", description);
// Get map message
MapMessage mapMessage = (MapMessage) message;
// Here get variables of your message
String title = mapMessage.getString("title");
String description = mapMessage.getString("description");
See how simple it is ?