Mercurial > hg > jslab
view src/samer/core_/util/shell/AppShellBase.java @ 8:5e3cbbf173aa tip
Reorganise some more
author | samer |
---|---|
date | Fri, 05 Apr 2019 22:41:58 +0100 |
parents | bf79fb79ee13 |
children |
line wrap: on
line source
/* * AppShell.java * * Copyright (c) 2000, Samer Abdallah, King's College London. * All rights reserved. * * This software is provided AS iS and WITHOUT ANY WARRANTY; * without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. */ package samer.core.util.shell; import samer.core.util.*; import samer.core.*; import java.io.*; public abstract class AppShellBase extends Properties implements Shell.Interface, Agent { protected AgentManager am; protected ViewableManager vm; private boolean registerAgents; private boolean registerViewables; private static Environment baseEnvironment(String fname) { try { Shell.trace("loading user properties from " +fname); FileInputStream in=new FileInputStream(fname); try { Properties uprops=new Properties(Shell.env()); uprops.load(in); uprops.setWritable(false); Shell.push(uprops); Shell.trace("user properties successfully loaded"); } finally { in.close(); } } catch (Exception ex) { Shell.trace("failed to load user properties: "+ex); } return Shell.env(); } protected static String getDefaultPropertiesFile() { return System.getProperty("user.home") + File.separator + "user.props"; } protected AppShellBase() { this(getDefaultPropertiesFile()); } protected AppShellBase(String userPropsFile) { super(baseEnvironment(userPropsFile)); try { execute("load",Shell.env()); } catch (Exception ex) { Shell.trace("failed to load local properties"); } Shell.push(this); // Shell.push(new FilteredEnvironment(new Properties(this), "bounds")); Shell.push(new HashMap(Shell.env())); am = new AgentManager(); registerAgent(am); vm = new ViewableManager(); registerAgents=true; registerViewables=true; } public void enableAgentRegistry(boolean f) { registerAgents=f; } public void enableViewableRegistry(boolean f) { registerViewables=f; } public void getCommands(Agent.Registry r) { // r.add("set").add("get") r.add("load").add("save") .add("expose").add("exit"); } public void exit() { am.dispatch("exit",Shell.env()); } public void execute(String cmd, Environment env) throws Exception { if (cmd.equals("exit")) { try { execute("save",this); } catch (Exception ex) { Shell.trace(ex.toString()); } // not any more. Just exit and be done. // am.exitOnReturn(); // must let other agents respond before System.exit System.exit(0); } else if (cmd.equals("expose")) { if (!vm.hasViewerContainer()) { vm.exposeViewables(getViewerContainer()); } } else if (cmd.equals("get")) { // get property String nm = X.string(env.datum()); Shell.print("name: "+nm); try { Object val=X.object(datum(nm)); Shell.print("value: "+val); env.add(val); } catch (Exception ex) { Shell.print("value: ** not bound **"); } } else if (cmd.equals("set")) { X.store(X.string(env.datum()),X.object(env.datum())); } else if (cmd.equals("load")) { String fname=X.string(env.datum("local.props"),"args"); Shell.trace("loading properties from "+fname); InputStream in=new FileInputStream(fname); try { props.load(in); } finally { in.close(); } } else if (cmd.equals("save")) { String fname=X.string(env.datum("local.props"),"args"); Shell.trace("saving properties to "+fname); try { File file=new File(fname); File old=new File(fname+".old"); try { old.delete(); } catch (Exception ex) {} file.renameTo(old); } catch (Exception ex) { Shell.trace(ex.toString()); } OutputStream out=new FileOutputStream(fname); try { props.store(out,"properties"); } finally { out.close(); } } } protected void put(Class a, Class b) { samer.core.Registry.put(a,b); } public void trace(String msg) { System.err.println(msg); } // public void trace(String string) { // System.err.println(" "+(System.currentTimeMillis() % 8192L)+": "+string); // } public void registerAgent(Agent a) { if (registerAgents) am.registerAgent(a); } public void deregisterAgent(Agent a) { am.deregisterAgent(a); } public void deregisterViewable(Viewable v) { vm.deregisterViewable(v); } public void registerViewable(Viewable v) { if (registerViewables) vm.registerViewable(v); } protected abstract ViewableManager.ViewerContainer getViewerContainer(); }