This discussion is archived
6 Replies Latest reply: Sep 29, 2011 2:06 AM by 452196 RSS

Maintaining user context from CLI

890988 Newbie
Currently Being Moderated
Hi,

I am working on CLI for a client-server application. The problem I am facing is that when I call a CLI command from the shell, I have to pass the user credential for each request. At the end of each request, the java program terminates and I lose the user context.
Please suggest what can be done.

One of the suggestion that i got was to cache the object of the user on the client machine and the client can keep using this cached user. Is it advisable to do so. If yes, how can i do this? Does java provide APIs for caching objects?

looking forward to your reply,

regards,
Anshuman
  • 1. Re: Maintaining user context from CLI
    Kayaman Guru
    Currently Being Moderated
    You shouldn't terminate the program after each request. That's unnecessary and the reason for your problem.
  • 2. Re: Maintaining user context from CLI
    890988 Newbie
    Currently Being Moderated
    It is not an interactive CLI. Unless the execution of the first request terminates, i can't fire the next request.

    I invoke the CLI from the terminal like this
    #> <myShellFile>.sh <operation_i_want_to_perform> <parameters>

    Internally the shell file, <myShellFile>.sh, invokes the main class of the java program. The operations and parameters are passed as command line parameters.
    Unless the program terminates i cannot make another request from the terminal.
  • 3. Re: Maintaining user context from CLI
    Kayaman Guru
    Currently Being Moderated
    887985 wrote:
    It is not an interactive CLI.
    Why not, when you clearly need one?
    Unless the execution of the first request terminates, i can't fire the next request.
    Modify the program so it reads the user input instead of re-running the program every time.
  • 4. Re: Maintaining user context from CLI
    890988 Newbie
    Currently Being Moderated
    I guess you are right ... I was just hoping that there could some other way. Its gonna be a lot of rework to make it interactive.
  • 5. Re: Maintaining user context from CLI
    Kayaman Guru
    Currently Being Moderated
    887985 wrote:
    I guess you are right ... I was just hoping that there could some other way. Its gonna be a lot of rework to make it interactive.
    From what you described, it doesn't sound like it should be a lot of work.

    If it works by specifying the operation and params as parameters to a bash script, it should be quite easy to convert to a single program that takes the inputs separately.

    Of course I don't know what kind of design quagmire the script and the java code are...
  • 6. Re: Maintaining user context from CLI
    452196 Journeyer
    Currently Being Moderated
    887985 wrote:
    Hi,

    One of the suggestion that i got was to cache the object of the user on the client machine and the client can keep using this cached user. Is it advisable to do so. If yes, how can i do this? Does java provide APIs for caching objects?

    As ever the answer would be "it depends". One option would be to have the program store the credentials using java.util.prefs.Preferences. This is a nice simple way of persisting a small number of values without having to worry about implementation. But do you want the credentials stored permanently on the client machine? Or you could set an environmental variable in the shell.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points