modelimpl.java

来自「这是一个以JAVA编写的程序,本人还没有试过,是一个简单的温度控制系统」· Java 代码 · 共 220 行

JAVA
220
字号
package net.sf.dz;import java.io.FileNotFoundException;import java.net.URL;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.Set;import org.freehold.jukebox.conf.Configuration;import org.freehold.jukebox.conf.ConfigurationFactory;import org.freehold.jukebox.conf.XmlConfiguration;import org.freehold.jukebox.logger.LogAware;import org.freehold.jukebox.logger.LogChannel;import org.freehold.jukebox.sem.SemaphoreGroup;import org.freehold.jukebox.service.PassiveService;import org.freehold.jukebox.service.RunnableService;import net.sf.dz.device.model.Unit;import net.sf.dz.device.model.impl.UnitModel;import net.sf.dz.util.ObjectFactory;import net.sf.dz.view.View;/** * The DIY Zoning model implementation. * * This object bootstraps the whole system based on the configuration file. * * @author Copyright &copy; <a href="mailto:vt@freehold.crocodile.org">Vadim Tkachenko</a> 2001-2002 * @version $Id: ModelImpl.java,v 1.4 2004/02/18 23:09:38 vtt Exp $ */public class ModelImpl extends PassiveService implements Model {    public static final LogChannel CH_CORE = new LogChannel("Model");        /**     * The keyword to use look up the configuration URL from the     * environment.     */    public static final String ENV_CF_URL = "dz.conf.URL";        /**     * The configuration root to extract the unit list from the     * configuration.     */    public static final String CF_UNIT = "dz.unit";    /**     * The configuration root to extract the view list from the     * configuration.     */    public static final String CF_VIEW = "dz.view";    /**     * Master configuration.     */    private Configuration conf;        /**     * Set of {@link net.sf.dz.device.model.Unit units} controlled by the     * core.     */    private Set unitSet = new HashSet();        /**     * Set of views representing the system.     */    private Set viewSet = new HashSet();        protected void startup() throws Throwable {            String confURL = System.getProperty(ENV_CF_URL);                if ( confURL == null ) {                    throw new IllegalArgumentException("Please define " + ENV_CF_URL + " environment property");        }                try {                    conf = (new ConfigurationFactory()).getConfiguration(new URL(confURL));                    } catch ( FileNotFoundException fnfex ) {                    complain(LOG_ERR, CH_CORE, "Can't load the master configuration from " + confURL + ", cause:", fnfex);            complain(LOG_CRIT, CH_CORE, "If this is a new installation, no configuration exists.");            complain(LOG_CRIT, CH_CORE, "Go to http://diy-zoning.sourceforge.net/docs/HOWTO/");            complain(LOG_CRIT, CH_CORE, "to find out how to create it.");        }            if ( conf == null ) {                    throw new IllegalArgumentException("Configuration couldn't be created from " + confURL);        }                complain(LOG_INFO, CH_CORE, "Configuring from " + confURL);        configure("dz", conf);                startUnits();        startViews();    }        protected void configure() throws Throwable {            List unitList = getConfiguration().getList(CF_UNIT);                complain(LOG_INFO, CH_CORE, "Units: " + unitList);                for ( Iterator ui = unitList.iterator(); ui.hasNext(); ) {                    unitSet.add(createUnit(ui.next().toString()));        }                if ( unitSet.isEmpty() ) {                    throw new IllegalStateException("Check configuration, no units found");        }                List viewList = getConfiguration().getList(CF_VIEW);            complain(LOG_INFO, CH_CORE, "Views: " + viewList);                for ( Iterator vi = viewList.iterator(); vi.hasNext(); ) {                    viewSet.add(createView(vi.next().toString()));        }                if ( viewSet.isEmpty() ) {                    complain(LOG_WARNING, CH_CORE, "No views were created (per configuration), you will not be able to control the application");        }    }        /**     * Create a unit instance from the configuration.     *     * @param unitName Name of the unit to create.     */    private Unit createUnit(String unitName) {            Unit unit = new UnitModel();                ((LogAware)unit).setLogger(getLogger());        unit.configure(CF_UNIT + "." + unitName, getConfiguration());                return unit;    }    /**     * Create a view instance from the configuration.     *     * @param viewName Name of the view to create.     */    private Object createView(String viewName) throws Throwable {            String cfroot = "dz.view." + viewName;        String viewClass = getConfiguration().getString(cfroot + ".class");                View view = (View)ObjectFactory.instantiate(viewClass, View.class);                ((LogAware)view).setLogger(getLogger());        view.attach(this);        view.configure(cfroot, getConfiguration());                return view;    }        private void startUnits() throws Throwable {            SemaphoreGroup units = new SemaphoreGroup();            for ( Iterator i = unitSet.iterator(); i.hasNext(); ) {                    Unit u = (Unit)i.next();                        if ( u instanceof RunnableService) {                            units.add(((RunnableService)u).start());            }        }                if ( units.waitForOne(true) == null ) {                    throw new IllegalStateException("Not a single unit started successfully, check the logs");        }                complain(LOG_INFO, CH_CORE, "Started units");    }        private void startViews() throws Throwable {            for ( Iterator i = viewSet.iterator(); i.hasNext(); ) {                    View v = (View)i.next();                        if ( v instanceof RunnableService) {                            ((RunnableService)v).start().waitFor();            }        }                // VT: FIXME: Check if there's at least one view running. If not,        // fail.                complain(LOG_INFO, CH_CORE, "Started views");    }        protected void shutdown(Throwable cause) throws Throwable {        }        public Iterator iterator() {            // VT: FIXME: Check if this is called at the right time - can't        // happen before all the units have started                return unitSet.iterator();    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?