comparison src/samer/core_/util/shell/AppShellBase.java @ 0:bf79fb79ee13

Initial Mercurial check in.
author samer
date Tue, 17 Jan 2012 17:50:20 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:bf79fb79ee13
1 /*
2 * AppShell.java
3 *
4 * Copyright (c) 2000, Samer Abdallah, King's College London.
5 * All rights reserved.
6 *
7 * This software is provided AS iS and WITHOUT ANY WARRANTY;
8 * without even the implied warranty of MERCHANTABILITY or
9 * FITNESS FOR A PARTICULAR PURPOSE.
10 */
11
12 package samer.core.util.shell;
13 import samer.core.util.*;
14 import samer.core.*;
15 import java.io.*;
16
17 public abstract class AppShellBase extends Properties
18 implements Shell.Interface, Agent
19 {
20 protected AgentManager am;
21 protected ViewableManager vm;
22 private boolean registerAgents;
23 private boolean registerViewables;
24
25 private static Environment baseEnvironment(String fname)
26 {
27 try {
28 Shell.trace("loading user properties from " +fname);
29 FileInputStream in=new FileInputStream(fname);
30
31 try {
32 Properties uprops=new Properties(Shell.env());
33 uprops.load(in);
34 uprops.setWritable(false);
35 Shell.push(uprops);
36 Shell.trace("user properties successfully loaded");
37 } finally { in.close(); }
38 } catch (Exception ex) {
39 Shell.trace("failed to load user properties: "+ex);
40 }
41 return Shell.env();
42 }
43
44 protected static String getDefaultPropertiesFile() {
45 return System.getProperty("user.home") + File.separator + "user.props";
46 }
47
48 protected AppShellBase() { this(getDefaultPropertiesFile()); }
49 protected AppShellBase(String userPropsFile)
50 {
51 super(baseEnvironment(userPropsFile));
52
53 try { execute("load",Shell.env()); }
54 catch (Exception ex) { Shell.trace("failed to load local properties"); }
55
56 Shell.push(this);
57 // Shell.push(new FilteredEnvironment(new Properties(this), "bounds"));
58 Shell.push(new HashMap(Shell.env()));
59
60 am = new AgentManager(); registerAgent(am);
61 vm = new ViewableManager();
62
63 registerAgents=true;
64 registerViewables=true;
65 }
66
67 public void enableAgentRegistry(boolean f) { registerAgents=f; }
68 public void enableViewableRegistry(boolean f) { registerViewables=f; }
69
70 public void getCommands(Agent.Registry r) {
71 // r.add("set").add("get")
72 r.add("load").add("save")
73 .add("expose").add("exit");
74 }
75
76 public void exit() { am.dispatch("exit",Shell.env()); }
77
78 public void execute(String cmd, Environment env) throws Exception
79 {
80 if (cmd.equals("exit")) {
81 try { execute("save",this); }
82 catch (Exception ex) { Shell.trace(ex.toString()); }
83
84 // not any more. Just exit and be done.
85 // am.exitOnReturn(); // must let other agents respond before System.exit
86 System.exit(0);
87
88 } else if (cmd.equals("expose")) {
89 if (!vm.hasViewerContainer()) {
90 vm.exposeViewables(getViewerContainer());
91 }
92
93 } else if (cmd.equals("get")) { // get property
94 String nm = X.string(env.datum());
95 Shell.print("name: "+nm);
96 try {
97 Object val=X.object(datum(nm));
98 Shell.print("value: "+val);
99 env.add(val);
100 } catch (Exception ex) { Shell.print("value: ** not bound **"); }
101
102 } else if (cmd.equals("set")) {
103 X.store(X.string(env.datum()),X.object(env.datum()));
104
105 } else if (cmd.equals("load")) {
106 String fname=X.string(env.datum("local.props"),"args");
107 Shell.trace("loading properties from "+fname);
108 InputStream in=new FileInputStream(fname);
109 try { props.load(in); }
110 finally { in.close(); }
111
112 } else if (cmd.equals("save")) {
113 String fname=X.string(env.datum("local.props"),"args");
114 Shell.trace("saving properties to "+fname);
115
116 try {
117 File file=new File(fname);
118 File old=new File(fname+".old");
119 try { old.delete(); }
120 catch (Exception ex) {}
121 file.renameTo(old);
122 } catch (Exception ex) {
123 Shell.trace(ex.toString());
124 }
125
126 OutputStream out=new FileOutputStream(fname);
127 try { props.store(out,"properties"); }
128 finally { out.close(); }
129 }
130 }
131
132 protected void put(Class a, Class b) { samer.core.Registry.put(a,b); }
133
134 public void trace(String msg) { System.err.println(msg); }
135 // public void trace(String string) {
136 // System.err.println(" "+(System.currentTimeMillis() % 8192L)+": "+string);
137 // }
138 public void registerAgent(Agent a) { if (registerAgents) am.registerAgent(a); }
139 public void deregisterAgent(Agent a) { am.deregisterAgent(a); }
140 public void deregisterViewable(Viewable v) { vm.deregisterViewable(v); }
141 public void registerViewable(Viewable v) { if (registerViewables) vm.registerViewable(v); }
142
143 protected abstract ViewableManager.ViewerContainer getViewerContainer();
144 }
145