<commit_before>/*
 * $Id: DaemonStatusServiceImpl.java,v 1.2 2014-02-07 21:51:51 tlipkis Exp $
 */

/*

 Copyright (c) 2013 Board of Trustees of Leland Stanford Jr. University,
 all rights reserved.

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 STANFORD UNIVERSITY BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
 IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

 Except as contained in this notice, the name of Stanford University shall not
 be used in advertising or otherwise to promote the sale, use or other dealings
 in this Software without prior written authorization from Stanford University.

 */

/**
 * The DaemonStatus web service implementation.
 */
package org.lockss.ws.status;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collection;
import javax.jws.WebService;
import org.lockss.app.LockssDaemon;
import org.lockss.crawler.CrawlManager;
import org.lockss.daemon.CrawlWindow;
import org.lockss.daemon.TitleConfig;
import org.lockss.plugin.ArchivalUnit;
import org.lockss.plugin.AuUtil;
import org.lockss.plugin.CachedUrlSet;
import org.lockss.plugin.Plugin;
import org.lockss.plugin.PluginManager;
import org.lockss.poller.Poll;
import org.lockss.poller.PollManager;
import org.lockss.repository.LockssRepositoryImpl;
import org.lockss.state.AuState;
import org.lockss.state.NodeManager;
import org.lockss.state.NodeState;
import org.lockss.util.Logger;
import org.lockss.util.StringUtil;
import org.lockss.ws.entities.AuStatus;
import org.lockss.ws.entities.IdNamePair;
import org.lockss.ws.entities.LockssWebServicesFault;
import org.lockss.ws.entities.LockssWebServicesFaultInfo;
import org.lockss.ws.status.DaemonStatusService;

@WebService
public class DaemonStatusServiceImpl implements DaemonStatusService {
  private static Logger log = Logger.getLogger(DaemonStatusServiceImpl.class);

  /**
   * Provides an indication of whether the daemon is ready.
   * 
   * @return a boolean with the indication.
   * @throws LockssWebServicesFault
   */
  @Override
  public boolean isDaemonReady() throws LockssWebServicesFault {
    final String DEBUG_HEADER = "isDaemonReady(): ";

    try {
      log.debug2(DEBUG_HEADER + "Invoked.");
      PluginManager pluginMgr =
	  (PluginManager) LockssDaemon.getManager(LockssDaemon.PLUGIN_MANAGER);
      boolean areAusStarted = pluginMgr.areAusStarted();
      log.debug2(DEBUG_HEADER + "areAusStarted = " + areAusStarted);

      return areAusStarted;
    } catch (Exception e) {
      throw new LockssWebServicesFault(e);
    }
  }

  /**
   * Provides a list of the identifier/name pairs of the archival units in the
   * system.
   * 
   * @return a List<IdNamePair> with the identifier/name pairs of the archival
   *         units in the system.
   * @throws LockssWebServicesFault
   */
  @Override
  public Collection<IdNamePair> getAuIds() throws LockssWebServicesFault {
    final String DEBUG_HEADER = "getAuIds(): ";

    try {
      log.debug2(DEBUG_HEADER + "Invoked.");
      Collection<IdNamePair> result = new ArrayList<IdNamePair>();
      PluginManager pluginMgr =
	  (PluginManager) LockssDaemon.getManager(LockssDaemon.PLUGIN_MANAGER);

      for (ArchivalUnit au : pluginMgr.getAllAus()) {
	log.debug2(DEBUG_HEADER + "au = " + au);
	result.add(new IdNamePair(au.getAuId(), au.getName()));
      }

      log.debug2(DEBUG_HEADER + "result.size() = " + result.size());
      return result;
    } catch (Exception e) {
      throw new LockssWebServicesFault(e);
    }
  }

  /**
   * Provides the status information of an archival unit in the system.
   * 
   * @param auId
   *          A String with the identifier of the archival unit.
   * @return an AuStatus with the status information of the archival unit.
   * @throws LockssWebServicesFault
   */
  @Override
  public AuStatus getAuStatus(String auId) throws LockssWebServicesFault {
    final String DEBUG_HEADER = "getAuStatus(): ";

    try {
      log.debug2(DEBUG_HEADER + "auId = " + auId);
      if (StringUtil.isNullString(auId)) {
	throw new LockssWebServicesFault(
	    new IllegalArgumentException("Invalid Archival Unit identifier"),
	    new LockssWebServicesFaultInfo("Archival Unit identifier = "
		+ auId));
      }

      LockssDaemon theDaemon = LockssDaemon.getLockssDaemon();
      PluginManager pluginMgr = theDaemon.getPluginManager();
      ArchivalUnit au = pluginMgr.getAuFromId(auId);
      AuStatus result = new AuStatus();

      result.setVolume(au.getName());

      TitleConfig tc = au.getTitleConfig();
      if (tc != null) {
	result.setJournalTitle(tc.getJournalTitle());
      }

      Plugin plugin = au.getPlugin();
      result.setPluginName(plugin.getPluginName());

      String yearAsString = null;
      try {
	yearAsString = AuUtil.getTitleAttribute(au, "year");
	if (yearAsString != null) {
	  result.setYear(Integer.valueOf(yearAsString));
	}
      } catch (NumberFormatException nfe) {
	log.warning("Invalid year title attribute '" + yearAsString
	    + "' for auId '" + auId + "'", nfe);
      }

      NodeManager nodeMgr = theDaemon.getNodeManager(au);
      AuState state = nodeMgr.getAuState();
<commit_msg>Provide additional debugging information when status is requested for an AU that does not exist.<commit_after>/*
 * $Id: DaemonStatusServiceImpl.java,v 1.3 2014-02-12 19:43:11 fergaloy-sf Exp $
 */

/*

 Copyright (c) 2013-2014 Board of Trustees of Leland Stanford Jr. University,
 all rights reserved.

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 STANFORD UNIVERSITY BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
 IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

 Except as contained in this notice, the name of Stanford University shall not
 be used in advertising or otherwise to promote the sale, use or other dealings
 in this Software without prior written authorization from Stanford University.

 */

/**
 * The DaemonStatus web service implementation.
 */
package org.lockss.ws.status;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collection;
import javax.jws.WebService;
import org.lockss.app.LockssDaemon;
import org.lockss.crawler.CrawlManager;
import org.lockss.daemon.CrawlWindow;
import org.lockss.daemon.TitleConfig;
import org.lockss.plugin.ArchivalUnit;
import org.lockss.plugin.AuUtil;
import org.lockss.plugin.CachedUrlSet;
import org.lockss.plugin.Plugin;
import org.lockss.plugin.PluginManager;
import org.lockss.poller.Poll;
import org.lockss.poller.PollManager;
import org.lockss.repository.LockssRepositoryImpl;
import org.lockss.state.AuState;
import org.lockss.state.NodeManager;
import org.lockss.state.NodeState;
import org.lockss.util.Logger;
import org.lockss.util.StringUtil;
import org.lockss.ws.entities.AuStatus;
import org.lockss.ws.entities.IdNamePair;
import org.lockss.ws.entities.LockssWebServicesFault;
import org.lockss.ws.entities.LockssWebServicesFaultInfo;
import org.lockss.ws.status.DaemonStatusService;

@WebService
public class DaemonStatusServiceImpl implements DaemonStatusService {
  private static Logger log = Logger.getLogger(DaemonStatusServiceImpl.class);

  /**
   * Provides an indication of whether the daemon is ready.
   * 
   * @return a boolean with the indication.
   * @throws LockssWebServicesFault
   */
  @Override
  public boolean isDaemonReady() throws LockssWebServicesFault {
    final String DEBUG_HEADER = "isDaemonReady(): ";

    try {
      log.debug2(DEBUG_HEADER + "Invoked.");
      PluginManager pluginMgr =
	  (PluginManager) LockssDaemon.getManager(LockssDaemon.PLUGIN_MANAGER);
      boolean areAusStarted = pluginMgr.areAusStarted();
      log.debug2(DEBUG_HEADER + "areAusStarted = " + areAusStarted);

      return areAusStarted;
    } catch (Exception e) {
      throw new LockssWebServicesFault(e);
    }
  }

  /**
   * Provides a list of the identifier/name pairs of the archival units in the
   * system.
   * 
   * @return a List<IdNamePair> with the identifier/name pairs of the archival
   *         units in the system.
   * @throws LockssWebServicesFault
   */
  @Override
  public Collection<IdNamePair> getAuIds() throws LockssWebServicesFault {
    final String DEBUG_HEADER = "getAuIds(): ";

    try {
      log.debug2(DEBUG_HEADER + "Invoked.");
      Collection<IdNamePair> result = new ArrayList<IdNamePair>();
      PluginManager pluginMgr =
	  (PluginManager) LockssDaemon.getManager(LockssDaemon.PLUGIN_MANAGER);

      for (ArchivalUnit au : pluginMgr.getAllAus()) {
	log.debug2(DEBUG_HEADER + "au = " + au);
	result.add(new IdNamePair(au.getAuId(), au.getName()));
      }

      log.debug2(DEBUG_HEADER + "result.size() = " + result.size());
      return result;
    } catch (Exception e) {
      throw new LockssWebServicesFault(e);
    }
  }

  /**
   * Provides the status information of an archival unit in the system.
   * 
   * @param auId
   *          A String with the identifier of the archival unit.
   * @return an AuStatus with the status information of the archival unit.
   * @throws LockssWebServicesFault
   */
  @Override
  public AuStatus getAuStatus(String auId) throws LockssWebServicesFault {
    final String DEBUG_HEADER = "getAuStatus(): ";

    try {
      log.debug2(DEBUG_HEADER + "auId = " + auId);
      if (StringUtil.isNullString(auId)) {
	throw new LockssWebServicesFault(
	    new IllegalArgumentException("Invalid Archival Unit identifier"),
	    new LockssWebServicesFaultInfo("Archival Unit identifier = "
		+ auId));
      }

      LockssDaemon theDaemon = LockssDaemon.getLockssDaemon();
      PluginManager pluginMgr = theDaemon.getPluginManager();
      ArchivalUnit au = pluginMgr.getAuFromId(auId);
      if (au == null) {
	throw new LockssWebServicesFault(
	    "No Archival Unit with provided identifier",
	    new LockssWebServicesFaultInfo("Archival Unit identifier = "
		+ auId));
      }

      AuStatus result = new AuStatus();

      result.setVolume(au.getName());

      TitleConfig tc = au.getTitleConfig();
      if (tc != null) {
	result.setJournalTitle(tc.getJournalTitle());
      }

      Plugin plugin = au.getPlugin();
      result.setPluginName(plugin.getPluginName());

      String yearAsString = null;
      try {
	yearAsString = AuUtil.getTitleAttribute(au, "year");
	if (yearAsString != null) {
	  result.setYear(Integer.valueOf(yearAsString));
	}
      } catch (NumberFormatException nfe) {
	log.warning("Invalid year title attribute '" + yearAsString
	    + "' for auId '" + auId + "'", nfe);
      }

      NodeManager nodeMgr = theDaemon.getNodeManager(au);
      AuState state = nodeMgr.getAuState();

{'fraction_non_alphanumeric': 0.08916938110749185, 'fraction_numerical': 0.004560260586319218, 'mean_word_length': 3.922244488977956, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '9349', 'n_tokens_mistral': 3699, 'n_tokens_neox': 3604, 'n_words': 1085}