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