001package com.pi4j.io.serial;
002
003/*
004 * #%L
005 * **********************************************************************
006 * ORGANIZATION  :  Pi4J
007 * PROJECT       :  Pi4J :: Java Library (Core)
008 * FILENAME      :  SerialFactory.java
009 *
010 * This file is part of the Pi4J project. More information about
011 * this project can be found here:  http://www.pi4j.com/
012 * **********************************************************************
013 * %%
014 * Copyright (C) 2012 - 2016 Pi4J
015 * %%
016 * This program is free software: you can redistribute it and/or modify
017 * it under the terms of the GNU Lesser General Public License as
018 * published by the Free Software Foundation, either version 3 of the
019 * License, or (at your option) any later version.
020 *
021 * This program is distributed in the hope that it will be useful,
022 * but WITHOUT ANY WARRANTY; without even the implied warranty of
023 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
024 * GNU General Lesser Public License for more details.
025 *
026 * You should have received a copy of the GNU General Lesser Public
027 * License along with this program.  If not, see
028 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
029 * #L%
030 */
031
032
033import com.pi4j.concurrent.DefaultExecutorServiceFactory;
034import com.pi4j.concurrent.ExecutorServiceFactory;
035import com.pi4j.io.serial.impl.SerialImpl;
036
037/**
038 * <p> This factory class provide a static method to create new 'Serial' instances. </p>
039 *
040 * <p>
041 * Before using the Pi4J library, you need to ensure that the Java VM in configured with access to
042 * the following system libraries:
043 * <ul>
044 * <li>pi4j</li>
045 * <li>wiringPi</li>
046 * </ul>
047 * <blockquote> This library depends on the wiringPi native system library.</br> (developed by
048 * Gordon Henderson @ <a href="http://wiringpi.com/">http://wiringpi.com/</a>)
049 * </blockquote>
050 * </p>
051 *
052 * @see com.pi4j.io.serial.Serial
053 * @see com.pi4j.io.serial.SerialDataEvent
054 * @see SerialDataEventListener
055 *
056 * @see <a href="http://www.pi4j.com/">http://www.pi4j.com/</a>
057 * @author Robert Savage (<a
058 *         href="http://www.savagehomeautomation.com">http://www.savagehomeautomation.com</a>)
059 */
060public class SerialFactory {
061
062    private static boolean isshutdown = false;
063
064    // we only allow a single default scheduled executor service factory to exists
065    private static ExecutorServiceFactory executorServiceFactory = null;
066
067    // private constructor
068    private SerialFactory() {
069        // forbid object construction
070    }
071
072    /**
073     * Create New Serial instance
074     *
075     * @return Return a new Serial implementation instance.
076     */
077    public static Serial createInstance() {
078        return new SerialImpl();
079    }
080
081    /**
082     * <p>Return instance of {@link ExecutorServiceFactory}.</p>
083     * <p>Note: .</p>
084     *
085     * @return Return a new GpioController impl instance.
086     */
087    public static ExecutorServiceFactory getExecutorServiceFactory() {
088        // if an executor service provider factory has not been created, then create a new default instance
089        if (executorServiceFactory == null) {
090            executorServiceFactory = new DefaultExecutorServiceFactory();
091        }
092        // return the provider instance
093        return executorServiceFactory;
094    }
095
096    /**
097     * Sets default {@link ExecutorServiceFactory}.
098     *
099     * @param executorServiceFactory service factory instance
100     */
101    public static void setExecutorServiceFactory(ExecutorServiceFactory executorServiceFactory) {
102        // set the default factory instance
103        SerialFactory.executorServiceFactory = executorServiceFactory;
104    }
105
106    /**
107     * This method returns TRUE if the serial controller has been shutdown.
108     *
109     * @return shutdown state
110     */
111    public static boolean isShutdown(){
112        return isshutdown;
113    }
114
115
116    /**
117     * This method can be called to forcefully shutdown all serial port
118     * monitoring, listening, and task threads/executors.
119     */
120    public static void shutdown()
121    {
122        // prevent reentrant invocation
123        if(isShutdown())
124            return;
125
126        // shutdown all executor services
127        //
128        // NOTE: we are not permitted to access the shutdown() method of the individual
129        // executor services, we must perform the shutdown with the factory
130        SerialFactory.getExecutorServiceFactory().shutdown();
131
132        // set is shutdown tracking variable
133        isshutdown = true;
134    }
135}