PageRenderTime 82ms CodeModel.GetById 40ms app.highlight 9ms RepoModel.GetById 31ms app.codeStats 0ms

/src/mpv5/webshopinterface/WSDaemon.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 153 lines | 71 code | 24 blank | 58 comment | 6 complexity | 32ff1a5c04243558c4bcee64516e5d68 MD5 | raw file
  1
  2/*
  3*  This file is part of YaBS.
  4*
  5*      YaBS is free software: you can redistribute it and/or modify
  6*      it under the terms of the GNU General Public License as published by
  7*      the Free Software Foundation, either version 3 of the License, or
  8*      (at your option) any later version.
  9*
 10*      YaBS is distributed in the hope that it will be useful,
 11*      but WITHOUT ANY WARRANTY; without even the implied warranty of
 12*      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13*      GNU General Public License for more details.
 14*
 15*      You should have received a copy of the GNU General Public License
 16*      along with YaBS.  If not, see <http://www.gnu.org/licenses/>.
 17 */
 18package mpv5.webshopinterface;
 19
 20//~--- non-JDK imports --------------------------------------------------------
 21
 22import mpv5.db.objects.WebShop;
 23
 24import mpv5.logging.Log;
 25
 26//~--- JDK imports ------------------------------------------------------------
 27
 28import java.net.MalformedURLException;
 29import java.net.URL;
 30
 31import java.util.List;
 32import java.util.Vector;
 33
 34/**
 35 * This service runs in background and polls the registered web shops in a defined interval
 36 */
 37public class WSDaemon extends Thread {
 38    private long               waitTime = 300;
 39    private boolean            running  = false;
 40    private List<WSDaemonJob>  jobs     = new Vector<WSDaemonJob>();
 41    private WSConnectionClient client;
 42    private WebShop            wes;
 43
 44    /**
 45     *
 46     * @param webShop
 47     * @throws NoCompatibleHostFoundException
 48     * @throws MalformedURLException
 49     */
 50    public WSDaemon(WebShop webShop) throws NoCompatibleHostFoundException, MalformedURLException {
 51        if (webShop.__getIsauthenticated()) {
 52            client = new WSConnectionClient(new URL(webShop.__getUrl()), webShop.__getIsrequestCompression(),
 53                                            webShop.__getUsername(), webShop.__getPassw());
 54        } else {
 55            client = new WSConnectionClient(new URL(webShop.__getUrl()), webShop.__getIsrequestCompression(), null,
 56                                            null);
 57        }
 58
 59        setWaitTime(webShop.__getInterv());
 60        wes     = webShop;
 61        running = true;
 62    }
 63
 64    /**
 65     * Create a new background service
 66     * @param url The web shop url
 67     * @param requCompression
 68     * @throws NoCompatibleHostFoundException
 69     */
 70    public WSDaemon(URL url, boolean requCompression, String user, String pw) throws NoCompatibleHostFoundException {
 71        client  = new WSConnectionClient(url, requCompression, user, pw);
 72        wes     = new WebShop();
 73        running = true;
 74    }
 75
 76    /**
 77     * Returns the unique id of the web shop used
 78     * @return
 79     */
 80    public int getWebShopID() {
 81        return getWebShop().__getIDS();
 82    }
 83
 84    /**
 85     * Adds a job
 86     * @param job
 87     */
 88    public void addJob(WSDaemonJob job) {
 89        jobs.add(job);
 90    }
 91
 92    /**
 93     * Stop this deamon
 94     */
 95    public void kill() {
 96        running = false;
 97    }
 98
 99    @Override
100    public void run() {
101        do {
102            Log.Debug(this, "Polling WebShop: " + client);
103            checkForWork();
104
105            try {
106                Thread.sleep(getWaitTime() * 1000);
107            } catch (InterruptedException ex) {}
108        } while (running);
109    }
110
111    /**
112     * @return the waitTime
113     */
114    public long getWaitTime() {
115        return waitTime;
116    }
117
118    /**
119     * @param waitTime the waitTime to set
120     */
121    public void setWaitTime(long waitTime) {
122        this.waitTime = waitTime;
123    }
124
125    private void checkForWork() {
126        for (int i = 0; i < jobs.size(); i++) {
127            jobs.get(i).work(client);
128
129            if (jobs.get(i).isOneTimeJob() || jobs.get(i).isDone()) {
130                jobs.remove(jobs.get(i));
131            }
132        }
133    }
134
135    /**
136     * Run the the {@link WSDaemon#run()} method only once
137     * @param runOnce
138     */
139    public void start(boolean runOnce) {
140        running = !runOnce;
141        super.start();
142    }
143
144    /**
145     * @return the wes
146     */
147    public WebShop getWebShop() {
148        return wes;
149    }
150}
151
152
153//~ Formatted by Jindent --- http://www.jindent.com