1 package fr.in2p3.jsaga.command;
2
3 import fr.in2p3.jsaga.EngineProperties;
4 import fr.in2p3.jsaga.engine.session.SessionConfiguration;
5 import fr.in2p3.jsaga.impl.context.ConfiguredContext;
6 import fr.in2p3.jsaga.impl.context.ConfigurableContextFactory;
7 import fr.in2p3.jsaga.impl.context.ContextImpl;
8
9 import org.apache.commons.cli.*;
10 import org.ogf.saga.context.Context;
11 import org.ogf.saga.context.ContextFactory;
12 import org.ogf.saga.error.DoesNotExistException;
13 import org.ogf.saga.session.Session;
14 import org.ogf.saga.session.SessionFactory;
15 import java.io.*;
16 import java.util.regex.Pattern;
17
18
19
20
21
22
23
24
25
26
27
28
29
30 public class ContextInit extends AbstractCommand {
31 private static final String OPT_HELP = "h", LONGOPT_HELP = "help";
32
33 public ContextInit() {
34 super("jsaga-context-init", new String[]{"contextId"}, null);
35 }
36
37 public static void main(String[] args) throws Exception {
38 ContextInit command = new ContextInit();
39 CommandLine line = command.parse(args);
40 if (line.hasOption(OPT_HELP))
41 {
42 command.printHelpAndExit(null);
43 }
44 else {
45
46 Session session = SessionFactory.createSession(false);
47
48
49 ConfiguredContext[] configContexts = ConfigurableContextFactory.listConfiguredContext();
50 for (int i=0; i<configContexts.length; i++) {
51 if (command.m_nonOptionValues.length==0
52 || command.m_nonOptionValues[0].equals(configContexts[i].getUrlPrefix())
53 || command.m_nonOptionValues[0].equals(configContexts[i].getType()))
54 {
55
56 Context context = ConfigurableContextFactory.createContext(configContexts[i]);
57
58 setUserPass(context);
59
60
61 session.addContext(context);
62 }
63 }
64 session.close();
65 }
66 }
67
68 private static void setUserPass(Context context) throws Exception {
69
70 try {
71 context.getAttribute(Context.USERPASS);
72 } catch (DoesNotExistException e) {
73
74 System.out.println("Enter UserPass for security context: "+ContextInfo.getLabel(context));
75 String userPass = getUserInput();
76
77
78 if (userPass != null) {
79 context.setAttribute(Context.USERPASS, userPass);
80 }
81 }
82 }
83
84 private static volatile boolean s_stopped;
85 private static String getUserInput() throws IOException {
86 BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
87 s_stopped = false;
88 new Thread() {
89 public void run() {
90 while(!s_stopped) {
91 System.out.print("\b ");
92 try {
93 sleep(1);
94 } catch(InterruptedException e) {}
95 }
96 }
97 }.start();
98 try {
99 String line = in.readLine();
100 if (line!=null && line.trim().length() > 0) {
101 return line;
102 } else {
103 return null;
104 }
105 } catch(IOException e) {
106 throw e;
107 } finally {
108 s_stopped = true;
109 }
110 }
111
112 protected Options createOptions() {
113 Options opt = new Options();
114
115
116 opt.addOption(OptionBuilder.withDescription("Display this help and exit")
117 .withLongOpt(LONGOPT_HELP)
118 .create(OPT_HELP));
119
120 return opt;
121 }
122 }