Tuesday, May 30, 2017

adding bulk of users GUI interface using Java swing in windows

Now, continuing talking about providing GUI interface to systems operations.

We will dive more into Java and start using the jar file command-factory.jar I mentioned in my previous article to show how useful is to provide GUI interface whether it's for delegation, e.g. you want to delegate specific operation, give access to specific team to extract report(from system not database), enforce standards for the operations conventions/unifying passwords, integrate multiple services together..etc, the list is too long to be mentioned here.

To demonstrate the idea, I will use what I would like to call OAM Object-Administrational Mapping (I just invented that ^_^) which is equivelant to the ORM Object-Relational Mapping. So, I will use command-factory.jar to create small app as "helloworld" to demonstrate the idea of "extending administration capabilities using Java development".

Let us start, after googling for 10 minutes I found the commands to create and delete windows user(normal windows user not a domain user).

in windows cmd:

-net user USERNAME PASSWORD /add

e.g.

-net user testuser Pass_123 /add

to delete:

net user testuser /del

Keep in mind in windows this works in the external commands only(check windows internal vs. external commands), for internal commands it didn't work I guess you need to use "shell" flag somewhere, Windows Powershell or even write python script to do the job for you. Even though with the external commands, probably you will need to use the full executable path (just put the full path it will save you some head scratches).

in windows cmd:

where net

output:
C:\Windows\System32\net.exe

After becoming familiar with the command, go to your IDE netbeans, eclipse..etc. Here, I thought it is better to demonstrate with desktop app instead of web based as it is faster to be used, you know, web app needs application server.

We will follow MVC design pattern which will include Model(user class/entity), View (main console/java swing) and controller(java class to link model and view).

create Java desktop application and give it a name call it UserManager for example, and for the sake of testing we will create main class without GUI/swing. Once application is created add command-factory.jar to the project libraries.


Project includes:

-User.java

package entity;

public class User {
 
    private String name;
    private String password;

    public User() {
    }

    public User(String name, String password) {
        this.name = name;
        this.password = password;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "User{" + "name=" + name + ", password=" + password + '}';
    }  
}

-Controller.java
package controller;

import UserSAO.UserSAO;
import entity.User;

public class Controller {

    private UserSAO userSAO;
    private User user;

    public Controller() {
        userSAO = new UserSAO();
    }

    public UserSAO getUserSAO() {
        return userSAO;
    }
}

-UserSAO.java

package UserSAO;

import commandfactory.CommandFactory;
import entity.User;
import java.util.ArrayList;
import java.util.List;

public class UserSAO {

    CommandFactory commandFactory;

    public CommandFactory getCommandFactory() {
        return commandFactory;
    }

    public void createUser(User user) {
        List<String> cmd = new ArrayList<>();
        cmd.add("C:\\Windows\\System32\\net.exe");
        cmd.add("user");
        cmd.add(user.getName());
        cmd.add(user.getPassword());
        cmd.add("/add");
        commandFactory = new CommandFactory();
        commandFactory.setCmdList(cmd);
        commandFactory.execCommand();
    }

    public void deleteUser(String name) {
        List<String> cmd = new ArrayList<>();
        cmd.add("C:\\Windows\\System32\\net.exe");
        cmd.add("user");
        cmd.add(name);
        cmd.add("/delete");
        commandFactory = new CommandFactory();
        commandFactory.setCmdList(cmd);
        commandFactory.execCommand();
    }
}

-Main.java

package usermanager;

import controller.Controller;
import entity.User;

public class Main {

    public static void main(String[] args) {

        User user = new User("testuser", "Pass_123");
        Controller controller = new Controller();
        controller.getUserSAO().createUser(user);
        if (controller.getUserSAO().getCommandFactory().getExitCode() == 0) {
            System.out.println(controller.getUserSAO().getCommandFactory().getStdOutPut());
        }
//        controller.getUserSAO().deleteUser("testuser");
//        if (controller.getUserSAO().getCommandFactory().getExitCode() == 0) {
//            System.out.println(controller.getUserSAO().getCommandFactory().getStdOutPut());
//        }
    }
}


The code above is to create a user with password (testuser/Pass_123). To delete a user, you can use the below..

-Main.java

package usermanager;

import controller.Controller;

public class Main {

    public static void main(String[] args) {

//        User user = new User("testuser", "Pass_123");
        Controller controller = new Controller();
//        controller.getUserSAO().createUser(user);
//        if (controller.getUserSAO().getCommandFactory().getExitCode() == 0) {
//            System.out.println(controller.getUserSAO().getCommandFactory().getStdOutPut());
//        }
        controller.getUserSAO().deleteUser("testuser");
        if (controller.getUserSAO().getCommandFactory().getExitCode() == 0) {
            System.out.println(controller.getUserSAO().getCommandFactory().getStdOutPut());
        }
    }
}

Now, I will replace the main class with java swing class and have it so basic to accept user names in TextArea component then create users based on that along with their passwords..

-create JFrame class call it MainFrame

-from swing palette just add by drag and drop two text areas and 2 labels and 1 button
-Declare as instance variable and initialize it in the constructor

Controller controller;
public MainFrame() {
controller = new Controller();
..omitted

it will look like the snapshot

In the actionevent of the Add command you will add the below code

  for (String s: userTextArea1.getText().split("\\n+")) {
            controller.getUserSAO().deleteUser(s);
            if (controller.getUserSAO().getCommandFactory().getExitCode() == 0)
            logArea.append(s+" user is deleted\n");
        }

on windows cmd:
-net user (to check users were created)

while in the action event of Delete command you will put the below code

for (String s: userTextArea1.getText().split("\\n+")) {
            controller.getUserSAO().deleteUser(s);
            if (controller.getUserSAO().getCommandFactory().getExitCode() == 0)
            logArea.append(s+" user is deleted\n");
        }


after creation
-net user (to check users)
after deletion
-net user



jar url https://github.com/samir82show/devops/blob/master/Usertest.zip
use java 8
Samir











Saturday, May 27, 2017

jar file to execute external system commands

Since I like to use command line in the system administration,I realized that having GUI interface is becoming highly required more and more, so, I decided to use Java in my applications whether they are desktop (swing) or web based.

In order to achieve this(execute external scripts/commands), java provides two ways, Runtime and ProcessBuilder.

I decided to use ProcessBuilder to make jar file which one you add it to your library will offer you executing external commands as well as getting the Standard Output, Standard Error and the exit status.

Download here.

Here is how to use it..
  • Add the jar command-factory.jar file to the project library
  • Declare and initialize the class e.g. 
    • CommandFactory processFactory = new CommandFactory();
  • Declare and initialize list of string e.g. 
    • List<String> cmd = new ArrayList<>();
    • cmd.add("python"); 
    • cmd.add("c:\\test.py");
  • Set the command to be executed e.g. 
    • processFactory.setCmdList(cmd);
  • Call execute method e.g. 
    • processFactory.execCommand();
  • You can check the exit status 
    • e.g. processFactory.getExitCode();
  • Standard output e.g. 
    • processFactory.getStdOutPut();
  • Standard error e.g. 
    • processFactory.getStdError();
  • Check the executed command e.g. 
    • processFactory.getCmdList();