001/* 002 * #%L 003 * Netarchivesuite - heritrix 3 monitor 004 * %% 005 * Copyright (C) 2005 - 2018 The Royal Danish Library, 006 * the National Library of France and the Austrian National Library. 007 * %% 008 * This program is free software: you can redistribute it and/or modify 009 * it under the terms of the GNU Lesser General Public License as 010 * published by the Free Software Foundation, either version 2.1 of the 011 * License, or (at your option) any later version. 012 * 013 * This program is distributed in the hope that it will be useful, 014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 016 * GNU General Lesser Public License for more details. 017 * 018 * You should have received a copy of the GNU General Lesser Public 019 * License along with this program. If not, see 020 * <http://www.gnu.org/licenses/lgpl-2.1.html>. 021 * #L% 022 */ 023 024package dk.netarkivet.heritrix3.monitor; 025 026import java.io.IOException; 027import java.util.HashMap; 028import java.util.List; 029import java.util.Map; 030import java.util.TreeMap; 031 032import org.jwat.common.Uri; 033import org.netarchivesuite.heritrix3wrapper.EngineResult; 034import org.netarchivesuite.heritrix3wrapper.Heritrix3Wrapper; 035import org.netarchivesuite.heritrix3wrapper.jaxb.JobShort; 036 037public class Heritrix3WrapperManager { 038 039 protected Heritrix3WrapperManager() { 040 } 041 042 public static Map<String, Heritrix3Wrapper> h3wrapperMap = new HashMap<String, Heritrix3Wrapper>(); 043 044 public static Heritrix3Wrapper getHeritrix3Wrapper(String h3EngineUrl, String username, String password) { 045 Heritrix3Wrapper h3wrapper = null; 046 if (h3EngineUrl != null) { 047 synchronized (h3wrapperMap) { 048 h3wrapper = h3wrapperMap.get(h3EngineUrl); 049 if (h3wrapper == null) { 050 Uri uri = Uri.create(h3EngineUrl); 051 String scheme = uri.getScheme(); 052 String hostname = uri.getHost(); 053 int port = uri.getPort(); 054 if (port == -1) { 055 if ("https".equalsIgnoreCase(scheme)) { 056 port = 443; 057 } else { 058 // Assume schema is http. 059 port = 80; 060 } 061 } 062 h3wrapper = Heritrix3Wrapper.getInstance(hostname, port, null, null, username, password); 063 h3wrapperMap.put(h3EngineUrl, h3wrapper); 064 } 065 } 066 } 067 return h3wrapper; 068 } 069 070 public static Map<Long, String> h3jobnameMap = new TreeMap<Long, String>(); 071 072 public static String getJobname(Heritrix3Wrapper h3wrapper, long jobId) { 073 String jobname; 074 synchronized (h3jobnameMap) { 075 jobname = h3jobnameMap.get(jobId); 076 if (jobname == null) { 077 EngineResult engineResult = h3wrapper.rescanJobDirectory(); 078 JobShort jobShort = null; 079 if (engineResult != null && engineResult.engine != null) { 080 List<JobShort> jobList = engineResult.engine.jobs; 081 JobShort tmpJobShort; 082 String jobPostFix = Long.toString(jobId) + "_"; 083 int idx = 0; 084 while (idx < jobList.size() && jobShort == null) { 085 tmpJobShort = jobList.get(idx++); 086 if (tmpJobShort.shortName.startsWith(jobPostFix)) { 087 jobShort = tmpJobShort; 088 } 089 } 090 } 091 if (jobShort != null) { 092 jobname = jobShort.shortName; 093 h3jobnameMap.put(jobId, jobname); 094 } 095 } 096 } 097 return jobname; 098 } 099 100 public static Map<Long, Heritrix3JobMonitor> h3JobmonitorMap = new TreeMap<Long, Heritrix3JobMonitor>(); 101 102 public static Heritrix3JobMonitor getJobMonitor(long jobId, NASEnvironment environment) throws IOException { 103 Heritrix3JobMonitor jobmonitor; 104 synchronized (h3JobmonitorMap) { 105 jobmonitor = h3JobmonitorMap.get(jobId); 106 if (jobmonitor == null) { 107 jobmonitor = Heritrix3JobMonitor.getInstance(jobId, environment); 108 h3JobmonitorMap.put(jobId, jobmonitor); 109 } 110 } 111 return jobmonitor; 112 } 113 114}