private void initLog() {
    if (mPaused) return;
    if (mLogWriter != null) return;

    String sep = System.getProperty("file.separator");
    String home = JConfig.getHomeDirectory();

    String doLogging = JConfig.queryConfiguration("logging", "true");
    if (doLogging.equals("true")) {
      if (mLogWriter == null) {
        try {
          File fp;
          String increment = "";
          int stepper = 1;
          do {
            fp = new File(home + sep + "errors" + increment + ".log");
            increment = "." + stepper++;
          } while (fp.exists());
          mFP = fp;
          mLogWriter = new PrintWriter(new FileOutputStream(fp));
        } catch (IOException ioe) {
          System.err.println("FAILED TO OPEN AN ERROR LOG.");
          ioe.printStackTrace();
        }
      }
    }
  }
  private JPanel buildCheckboxPanel() {
    JPanel tp = new JPanel();

    tp.setBorder(BorderFactory.createTitledBorder("Webserver Options"));
    tp.setLayout(new GridLayout(1, 2));
    String doLocalServer = JConfig.queryConfiguration("server.enabled", "false");
    String doAllowSyndication = JConfig.queryConfiguration("allow.syndication", "true");

    localServerBrowseBox = new JCheckBox("Use internal web server");
    localServerBrowseBox.setToolTipText(
        "<html><body>Turning this on enables JBidwatchers internal web server; 'Show in Browser' will go through JBidwatcher<br>first, in order to allow it to show old/deleted auctions,and to avoid the need to log in regularly.<br>The internal web server is password protected with your auction server username/password.</body></html>");
    // localServerBrowseBox.setToolTipText("Turning this on enables JBidwatchers internal web
    // server; 'Show in Browser' will go through JBidwatcher first, in order to allow it to show
    // old/deleted auctions, and to avoid the need to log in regularly.  The internal web server is
    // password protected with your auction server username/password.");
    localServerBrowseBox.setSelected(doLocalServer.equals("true"));
    tp.add(localServerBrowseBox);

    openSyndication = new JCheckBox("Allow syndication to bypass authentication");
    openSyndication.setToolTipText(
        "Allows syndication requests and thumbnail requests to be resolved without requiring a username/password.");
    openSyndication.setSelected(doAllowSyndication.equals("true"));
    tp.add(openSyndication);

    return tp;
  }
示例#3
0
  public int loadAuctionsFromDatabase() {
    int totalCount = AuctionInfo.count();
    int activeCount = AuctionEntry.activeCount();

    MQFactory.getConcrete("splash").enqueue("WIDTH " + activeCount);
    MQFactory.getConcrete("splash").enqueue("SET 0");

    AuctionServer newServer = AuctionServerManager.getInstance().getServer();
    AuctionServerManager.setEntryManager(this);
    if (totalCount == 0) {
      if (JConfig.queryConfiguration("stats.auctions") == null)
        JConfig.setConfiguration("stats.auctions", "0");
      return totalCount;
    }

    AuctionServerManager.getInstance().loadAuctionsFromDB(newServer);
    AuctionStats as = AuctionServerManager.getInstance().getStats();

    //  TODO -- Do something more valuable than just notify, when the auction counts are off.
    int savedCount = Integer.parseInt(JConfig.queryConfiguration("last.auctioncount", "-1"));
    if (as != null) {
      if (as.getCount() != activeCount || (savedCount != -1 && as.getCount() != savedCount)) {
        MQFactory.getConcrete("Swing").enqueue("NOTIFY Failed to load all auctions.");
      }
    }

    return activeCount;
  }
  public void updateValues() {
    String doLocalServer = JConfig.queryConfiguration("server.enabled", "false");
    String doAllowSyndication = JConfig.queryConfiguration("allow.syndication", "true");

    localServerBrowseBox.setSelected(doLocalServer.equals("true"));
    openSyndication.setSelected(doAllowSyndication.equals("true"));
  }
示例#5
0
  /**
   * @brief Save auctions out to the savefile, in XML format.
   *     <p>Similar to the loadAuctions code, this would be nice if it were abstracted to write to
   *     any outputstream, allowing us to write to a remote node to update it with our auctions and
   *     snipes.
   * @return - true if it successfully saved, false if an error occurred.
   */
  public boolean saveAuctions() {
    XMLElement auctionsData = AuctionServerManager.getInstance().toXML();
    String oldSave = JConfig.queryConfiguration("savefile", "auctions.xml");
    String saveFilename =
        JConfig.getCanonicalFile(
            JConfig.queryConfiguration("savefile", "auctions.xml"), "jbidwatcher", false);
    String newSave = saveFilename;

    //  If there's no data to save, then pretend we did it.
    if (auctionsData == null) return true;

    ensureDirectories(saveFilename);

    boolean swapFiles = needSwapSaves(saveFilename);

    if (!saveFilename.equals(oldSave)) {
      JConfig.setConfiguration("savefile", saveFilename);
    }

    //  If we already have a save file, preserve its name, and write
    //  the new one to '.temp'.
    if (swapFiles) {
      newSave = saveFilename + ".temp";
      File newSaveFile = new File(newSave);
      if (newSaveFile.exists()) newSaveFile.delete();
    }

    StringBuffer buf = buildSaveBuffer(auctionsData, null);
    boolean saveDone = true;

    //  Dump the save file out!
    try {
      PrintStream ps = new PrintStream(new FileOutputStream(newSave));
      ps.println(buf);
      ps.close();
    } catch (IOException e) {
      JConfig.log().handleException("Failed to save auctions.", e);
      saveDone = false;
    }

    //  If the save was complete, and we have to swap old/new files,
    //  then [remove prior '.old' file if necessary], save current XML
    //  as '.old', and move most recent save file to be just a normal
    //  save file.
    if (saveDone && swapFiles) {
      preserveFiles(saveFilename);
    }

    return saveDone;
  }
示例#6
0
  private static String makeBackupFilename(String filename, String toInsert) {
    int lastSlash = filename.lastIndexOf(System.getProperty("file.separator"));
    if (lastSlash == -1) {
      JConfig.log().logDebug("Filename has no separators: " + filename);
      lastSlash = 0;
    }
    int firstDot = filename.indexOf('.', lastSlash);
    if (firstDot == -1) {
      JConfig.log().logDebug("Filename has no dot/extension: " + filename);
      firstDot = filename.length();
    }

    return filename.substring(0, firstDot) + '-' + toInsert + filename.substring(firstDot);
  }
  public void save() {
    String outPath = JConfig.queryConfiguration("auctions.savepath");
    if (outPath != null && outPath.length() != 0) {
      if (JConfig.queryConfiguration("store.auctionHTML", "true").equals("true")) {
        String filePath =
            outPath + System.getProperty("file.separator") + getIdentifier() + ".html.gz";

        if (mLoadedPage != null) {
          mLoadedPage.save(filePath);
        }
      }
    }
    mLoadedPage = null;
  }
 public void logFile(String msgtop, StringBuffer dumpsb) {
   initLog();
   String doLogging = JConfig.queryConfiguration("logging", "true");
   if (doLogging.equals("true")) {
     if (JConfig.debugging) {
       if (mLogWriter != null) {
         mLogWriter.println("+------------------------------");
         mLogWriter.println("| " + msgtop);
         mLogWriter.println("+------------------------------");
         if (dumpsb != null) {
           mLogWriter.println(dumpsb);
         } else {
           mLogWriter.println("(null)");
         }
         mLogWriter.println("+------------end---------------");
         mLogWriter.flush();
       }
       for (ErrorHandler handler : sHandlers) {
         handler.addLog("...");
       }
       logMessage("File contents logged with message: " + msgtop);
       for (ErrorHandler handler : sHandlers) {
         handler.addLog("...");
       }
     }
   }
 }
  public void handleException(String sError, Throwable e) {
    initLog();
    Date log_time = new Date();

    if (sError == null || sError.length() == 0) {
      System.err.println("[" + log_time + "]");
    } else {
      System.err.println(log_time + ": " + sError);
    }
    e.printStackTrace();

    String doLogging = JConfig.queryConfiguration("logging", "true");

    String logMsg;
    if (sError == null || sError.length() == 0) {
      logMsg = "[" + log_time + "]";
    } else {
      logMsg = log_time + ": " + sError;
    }

    String trace = getStackTrace(e);
    for (ErrorHandler handler : sHandlers) {
      handler.exception(logMsg, e.getMessage(), trace);
    }

    if (doLogging.equals("true")) {
      if (mLogWriter != null) {
        mLogWriter.println(logMsg);
        if (e.getMessage() != null) mLogWriter.println(e.getMessage());
        e.printStackTrace(mLogWriter);
        mLogWriter.flush();
      }
    }
  }
示例#10
0
 public static void start() {
   if (sTimer == null) {
     sTimer = new TimerHandler(getInstance());
     sTimer.setName("Updates");
     sTimer.start();
   }
   JConfig.registerListener(getInstance());
 }
示例#11
0
  /**
   * @brief Load auctions from a save file, with a pretty splash screen and everything, if
   *     necessary.
   *     <p>I'd like to abstract this, and make it work with arbitrary streams, so that we could
   *     send an XML file of auctions over a network to sync between JBidwatcher instances.
   */
  public void loadAuctions() {
    XMLElement xmlFile = new XMLElement(true);
    String loadFile = JConfig.queryConfiguration("savefile", "auctions.xml");
    String oldLoad = loadFile;

    loadFile = JConfig.getCanonicalFile(loadFile, "jbidwatcher", true);
    if (!loadFile.equals(oldLoad)) {
      JConfig.setConfiguration("savefile", loadFile);
    }

    File toLoad = new File(loadFile);
    if (toLoad.exists() && toLoad.length() != 0) {
      try {
        loadXMLFromFile(loadFile, xmlFile);
      } catch (IOException ioe) {
        JConfig.log()
            .handleException("A serious problem occurred trying to load from auctions.xml.", ioe);
        MQFactory.getConcrete("Swing")
            .enqueue(
                "ERROR Failure to load your saved auctions.  Some or all items may be missing.");
      } catch (XMLParseException xme) {
        JConfig.log().handleException("Trying to load from auctions.xml.", xme);
        MQFactory.getConcrete("Swing")
            .enqueue(
                "ERROR Failure to load your saved auctions.  Some or all items may be missing.");
      }
    } else {
      //  This is a common thing, and we don't want to frighten new
      //  users, who are most likely to see it.
      JConfig.log()
          .logDebug(
              "JBW: Failed to load saved auctions, the auctions file is probably not there yet.");
      JConfig.log().logDebug("JBW: This is not an error, unless you're constantly getting it.");
    }
  }
 File getContentFile() {
   File fp = null;
   String outPath = JConfig.queryConfiguration("auctions.savepath");
   if (outPath != null && outPath.length() != 0) {
     String filePath =
         outPath + System.getProperty("file.separator") + getIdentifier() + ".html.gz";
     fp = new File(filePath);
   }
   return fp;
 }
  protected StringBuffer getContent() {
    StringBuffer sb;

    if (mLoadedPage != null) {
      StringBuffer outSB = mLoadedPage.getUncompressedData(false);
      if (outSB == null) outSB = new StringBuffer("mLoadedPage.getUncompressedData is null");
      sb = outSB;
    } else {
      JConfig.log().logDebug("mLoadedPage is null, returning the 'real' cached copy!");
      GZip gz = getRealContent();
      if (gz != null) {
        sb = gz.getUncompressedData();
        JConfig.log().logDebug("Turned the uncompressed data into a StringBuffer!");
        if (sb == null) JConfig.log().logDebug(" Failed to uncompress for id " + getIdentifier());
      } else {
        sb = new StringBuffer("Error getting real content.");
      }
    }
    return (sb);
  }
示例#14
0
 private static void backupByDate(String filename, File oldest) {
   SimpleDateFormat justDateFmt = new SimpleDateFormat("ddMMMyy");
   String justDate = justDateFmt.format(new Date());
   String oldBackup = makeBackupFilename(filename, justDate);
   File oldDateBackup = new File(oldBackup);
   if (oldDateBackup.exists()) {
     oldDateBackup.delete();
     File newDateBackup = new File(oldBackup);
     oldest.renameTo(newDateBackup);
   } else {
     oldest.renameTo(oldDateBackup);
     String oldestByDate = JConfig.queryConfiguration("save.bydate.4", "");
     for (int i = 4; i > 0; i--) {
       JConfig.setConfiguration(
           "save.bydate." + i, JConfig.queryConfiguration("save.bydate." + (i - 1), ""));
     }
     JConfig.setConfiguration("save.bydate.0", oldBackup);
     File deleteMe = new File(oldestByDate);
     deleteMe.delete();
   }
 }
  private GZip loadFile(File fp) {
    GZip localZip = new GZip();

    if (fp.exists()) {
      //  Okay, I don't allow loading auction data that's over 512K.  Duh.
      if (fp.length() < 512 * 1024) {
        try {
          JConfig.log().logDebug("Loading from backing page (file is " + fp.length() + " bytes)!");
          localZip.load(fp);
        } catch (IOException ioe) {
          JConfig.log().handleException("Couldn't read " + fp.getName(), ioe);
          return null;
        }

        return localZip;
      } else {
        JConfig.log().logDebug("Can't load " + fp.getName() + ", file is too large.");
      }
    }
    return null;
  }
示例#16
0
  private void loadXMLFromFile(String loadFile, XMLElement xmlFile) throws IOException {
    InputStreamReader isr = new InputStreamReader(new FileInputStream(loadFile));
    MQFactory.getConcrete("splash").enqueue("WIDTH " + MAX_PERCENT);
    MQFactory.getConcrete("splash").enqueue("SET " + MAX_PERCENT / 2);

    xmlFile.parseFromReader(isr);
    MQFactory.getConcrete("splash").enqueue("SET " + MAX_PERCENT);

    String formatVersion = xmlFile.getProperty("FORMAT", "0101");
    XMLElement auctionsXML = xmlFile.getChild("auctions");
    JConfig.setConfiguration("savefile.format", formatVersion);
    //  set the width of the splash progress bar based on the number
    //  of auctions that will be loaded!
    if (auctionsXML == null) {
      throw new XMLParseException(
          xmlFile.getTagName(), "AuctionsManager requires an <auctions> tag!");
    }
    String auctionQuantity = auctionsXML.getProperty("COUNT", null);

    int auctionTotal = 0;
    if (auctionQuantity != null) {
      auctionTotal = Integer.parseInt(auctionQuantity);
      MQFactory.getConcrete("splash").enqueue("SET 0");
      MQFactory.getConcrete("splash").enqueue("WIDTH " + auctionTotal);
    }

    AuctionServerManager.setEntryManager(this);
    AuctionServerManager.getInstance().fromXML(auctionsXML);

    AuctionStats as = AuctionServerManager.getInstance().getStats();

    //  TODO -- Do something more valuable than just notify, when the auction counts are off.
    int savedCount = Integer.parseInt(JConfig.queryConfiguration("last.auctioncount", "-1"));
    if (as != null) {
      if (as.getCount() != auctionTotal || (savedCount != -1 && as.getCount() != savedCount)) {
        MQFactory.getConcrete("Swing").enqueue("NOTIFY Failed to load all auctions.");
      }
    }
  }
 @SuppressWarnings({"unchecked"})
 public static List<AuctionInfo> findLostAuctions() {
   List<AuctionInfo> resultSet;
   try {
     resultSet =
         (List<AuctionInfo>)
             findAllBySQL(
                 AuctionInfo.class,
                 "SELECT * FROM auctions WHERE identifier NOT IN (SELECT DISTINCT(identifier) FROM entries)");
   } catch (Exception e) {
     JConfig.log().handleDebugException("Failed to find lost auctions.", e);
     resultSet = null;
   }
   return resultSet;
 }
  void setRealContent(byte[] changedContent, boolean final_data) {
    String outPath = JConfig.queryConfiguration("auctions.savepath");

    if (changedContent != null) {
      mLoadedPage = new GZip();
      mLoadedPage.setData(changedContent);

      if (outPath != null && outPath.length() != 0) {
        if (final_data) {
          String filePath =
              outPath + System.getProperty("file.separator") + getIdentifier() + ".html.gz";
          mLoadedPage.save(filePath);
          mLoadedPage = null;
        }
      }
    }
  }
  /**
   * @param fname - The filename to output to.
   * @param sb - The StringBuffer to dump out.
   * @brief Debugging function to dump a string buffer out to a file.
   *     <p>This is used for 'emergency' debugging efforts.
   */
  public void dump2File(String fname, StringBuffer sb) {
    if (JConfig.queryConfiguration("debug.filedump", "false").equals("false")) return;

    FileWriter fw = null;
    try {
      fw = new FileWriter(fname);

      fw.write(sb.toString());
    } catch (IOException ioe) {
      handleException("Threw exception in dump2File!", ioe);
    } finally {
      if (fw != null)
        try {
          fw.close();
        } catch (IOException ignored) {
          /* I don't care about exceptions on close. */
        }
    }
  }
  public void logMessage(String msg) {
    initLog();
    Date log_time = new Date();

    System.err.println(log_time + ": " + msg);

    String logMsg = log_time + ": ";
    //  This should stop the annoying time duplication in log messages.
    if (msg.startsWith(logMsg)) logMsg = msg;
    else logMsg += msg;
    for (ErrorHandler handler : sHandlers) {
      handler.addLog(logMsg);
    }

    String doLogging = JConfig.queryConfiguration("logging", "true");
    if (doLogging.equals("true")) {
      if (mLogWriter != null) {
        mLogWriter.println(logMsg);
        mLogWriter.flush();
      }
    }
  }
示例#21
0
  private static void preserveFiles(String filename) {
    File oldFile = new File(filename);
    File saveFile = new File(filename + ".temp");
    SimpleDateFormat sdf = new SimpleDateFormat("ddMMMyy_HHmm");
    String nowStr = sdf.format(new Date());
    String retainFilename = makeBackupFilename(filename, nowStr);
    File retainFile = new File(retainFilename);
    if (retainFile.exists()) retainFile.delete();

    String oldestSave = JConfig.queryConfiguration("save.file.4", "");
    if (oldestSave.length() != 0) {
      File oldest = new File(oldestSave);
      if (oldest.exists()) {
        backupByDate(filename, oldest);
      }
    }

    for (int i = 4; i > 0; i--) {
      JConfig.setConfiguration(
          "save.file." + i, JConfig.queryConfiguration("save.file." + (i - 1), ""));
    }

    File keepFile = new File(retainFilename);
    if (!oldFile.renameTo(keepFile)) {
      JConfig.log()
          .logDebug(
              "Renaming the old file ("
                  + oldFile
                  + ") to the retain file ("
                  + keepFile
                  + ") failed!");
    }
    JConfig.setConfiguration("save.file.0", retainFilename);

    File standard = new File(filename);
    if (!saveFile.renameTo(standard)) {
      JConfig.log()
          .logDebug(
              "Renaming the new file ("
                  + saveFile
                  + ") to the standard filename ("
                  + standard
                  + ") failed!");
    }
  }
示例#22
0
 public void updateConfiguration() {
   String newSnipeTime = JConfig.queryConfiguration("snipemilliseconds");
   if (newSnipeTime != null) {
     AuctionEntry.setDefaultSnipeTime(Long.parseLong(newSnipeTime));
   }
 }
 public void logVerboseDebug(String msg) {
   if (JConfig.queryConfiguration("debug.uber", "false").equals("true")) logMessage(msg);
 }
 public void apply() {
   JConfig.setConfiguration(
       "server.enabled", localServerBrowseBox.isSelected() ? "true" : "false");
   JConfig.setConfiguration("allow.syndication", openSyndication.isSelected() ? "true" : "false");
 }