Merge pull request #2632 from mwoodiupui/DS-4409

[DS-4409] Remove GeoIP download Ant target, reconfigure for external provision
This commit is contained in:
Tim Donohue
2020-01-23 11:23:09 -06:00
committed by GitHub
6 changed files with 55 additions and 90 deletions

View File

@@ -10,20 +10,20 @@ package org.dspace.statistics;
import java.io.File; import java.io.File;
import com.maxmind.geoip2.DatabaseReader; import com.maxmind.geoip2.DatabaseReader;
import org.dspace.services.ConfigurationService;
import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
/** /**
* Mock service that uses an embedded SOLR server for the statistics core. * Mock service that uses an embedded SOLR server for the statistics core.
* <p>
* <strong>NOTE:</strong> this class is overridden by one <em>of the same name</em>
* defined in dspace-server-webapp and declared as a bean there.
* See {@code config/spring/api/Z-mock-services.xml}. Some kind of classpath
* magic makes this work.
*/ */
public class MockSolrLoggerServiceImpl public class MockSolrLoggerServiceImpl
extends SolrLoggerServiceImpl extends SolrLoggerServiceImpl
implements InitializingBean { implements InitializingBean {
@Autowired(required = true)
private ConfigurationService configurationService;
public MockSolrLoggerServiceImpl() { public MockSolrLoggerServiceImpl() {
} }
@@ -34,9 +34,8 @@ public class MockSolrLoggerServiceImpl
new FakeDatabaseReader(); // Activate fake new FakeDatabaseReader(); // Activate fake
new FakeDatabaseReader.Builder(); // Activate fake new FakeDatabaseReader.Builder(); // Activate fake
String locationDbPath = configurationService.getProperty("usage-statistics.dbfile"); File locationDb = File.createTempFile("GeoIP", ".db");
File locationDb = new File(locationDbPath); locationDb.deleteOnExit();
locationDb.createNewFile();
locationService = new DatabaseReader.Builder(locationDb).build(); locationService = new DatabaseReader.Builder(locationDb).build();
} }

View File

@@ -72,6 +72,7 @@ import org.dspace.core.Constants;
import org.dspace.disseminate.CitationDocumentServiceImpl; import org.dspace.disseminate.CitationDocumentServiceImpl;
import org.dspace.eperson.Group; import org.dspace.eperson.Group;
import org.dspace.services.ConfigurationService; import org.dspace.services.ConfigurationService;
import org.dspace.statistics.FakeDatabaseReader;
import org.dspace.statistics.ObjectCount; import org.dspace.statistics.ObjectCount;
import org.dspace.statistics.SolrLoggerServiceImpl; import org.dspace.statistics.SolrLoggerServiceImpl;
import org.dspace.statistics.factory.StatisticsServiceFactory; import org.dspace.statistics.factory.StatisticsServiceFactory;
@@ -118,6 +119,13 @@ public class BitstreamRestControllerIT extends AbstractControllerIntegrationTest
StatisticsServiceFactory.getInstance().getSolrLoggerService().removeIndex("*:*"); StatisticsServiceFactory.getInstance().getSolrLoggerService().removeIndex("*:*");
} }
@BeforeClass
public static void mockGeoIP()
throws IOException {
// Set up a mock GeoIP implementation so we don't need to d/l a database.
new FakeDatabaseReader.Builder(); // Activate fake
}
@Before @Before
@Override @Override
public void setUp() throws Exception { public void setUp() throws Exception {

View File

@@ -9,6 +9,7 @@ package org.dspace.statistics;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress; import java.net.InetAddress;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
@@ -28,6 +29,8 @@ import com.maxmind.geoip2.record.Traits;
import mockit.Deencapsulation; import mockit.Deencapsulation;
import mockit.Mock; import mockit.Mock;
import mockit.MockUp; import mockit.MockUp;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/** /**
* Mock service to mock the location Lookup Service used by the SOLR statistics * Mock service to mock the location Lookup Service used by the SOLR statistics
@@ -35,14 +38,17 @@ import mockit.MockUp;
*/ */
public class FakeDatabaseReader public class FakeDatabaseReader
extends MockUp<DatabaseReader> { extends MockUp<DatabaseReader> {
private static final Logger LOG = LogManager.getLogger();
FakeDatabaseReader() { FakeDatabaseReader() {
} }
public FakeDatabaseReader(Object object) { private FakeDatabaseReader(Builder builder) {
LOG.debug("constructor({})", () -> builder.toString());
} }
public FakeDatabaseReader $init(Builder builder) { public FakeDatabaseReader $init(Builder builder) {
LOG.debug("$init({})", () -> builder.toString());
return this; return this;
} }
@@ -67,6 +73,8 @@ public class FakeDatabaseReader
@Mock @Mock
public CityResponse city(InetAddress address) { public CityResponse city(InetAddress address) {
LOG.debug("city({})", () -> address.toString());
List<String> names = new ArrayList<>(1); List<String> names = new ArrayList<>(1);
names.add("New York"); names.add("New York");
@@ -101,17 +109,32 @@ public class FakeDatabaseReader
public Builder() {} public Builder() {}
/**
* Fake constructor.
* @param stream ignored.
*/
@Mock
public void $init(InputStream stream) {
LOG.debug("Builder.$init(\"{}\")", () -> stream.toString());
}
/** /**
* Fake constructor. * Fake constructor.
* @param file ignored. * @param file ignored.
*/ */
@Mock @Mock
public void $init(File file) { public void $init(File file) {
try {
LOG.debug("Builder.$init(\"{}\")", file.getCanonicalPath());
} catch (IOException e) {
LOG.warn("Cannot getCanonicalPath(file): {}", e.getMessage(), e);
}
} }
@Mock @Mock
public DatabaseReader build() public DatabaseReader build()
throws IOException { throws IOException {
LOG.debug("build");
return Deencapsulation.newUninitializedInstance(DatabaseReader.class); return Deencapsulation.newUninitializedInstance(DatabaseReader.class);
} }
} }

View File

@@ -8,6 +8,7 @@
package org.dspace.statistics; package org.dspace.statistics;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.List; import java.util.List;
@@ -18,12 +19,10 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.dspace.content.DSpaceObject; import org.dspace.content.DSpaceObject;
import org.dspace.eperson.EPerson; import org.dspace.eperson.EPerson;
import org.dspace.services.ConfigurationService;
import org.dspace.solr.MockSolrServer; import org.dspace.solr.MockSolrServer;
import org.dspace.usage.UsageWorkflowEvent; import org.dspace.usage.UsageWorkflowEvent;
import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
/** /**
@@ -44,9 +43,6 @@ public class MockSolrLoggerServiceImpl
private MockSolrServer mockSolrServer; private MockSolrServer mockSolrServer;
@Autowired(required = true)
private ConfigurationService configurationService;
public MockSolrLoggerServiceImpl() { public MockSolrLoggerServiceImpl() {
} }
@@ -112,11 +108,10 @@ public class MockSolrLoggerServiceImpl
try { try {
new FakeDatabaseReader(); // Activate fake new FakeDatabaseReader(); // Activate fake
new FakeDatabaseReader.Builder(); // Activate fake new FakeDatabaseReader.Builder(); // Activate fake
String locationDbPath = configurationService.getProperty("usage-statistics.dbfile"); File locationDb = File.createTempFile("GeoIP", ".mmdb");
File locationDb = new File(locationDbPath); locationDb.deleteOnExit();
locationDb.createNewFile();
locationService = new DatabaseReader.Builder(locationDb).build(); locationService = new DatabaseReader.Builder(locationDb).build();
} catch (Exception e) { } catch (IOException e) {
log.error("Unable to load FakeDatabaseReader", e); log.error("Unable to load FakeDatabaseReader", e);
} }
} }

View File

@@ -1,13 +1,15 @@
#---------------------------------------------------------------# #---------------------------------------------------------------#
#--------------USAGE STATISTICS CONFIGURATIONS------------------# #--------------USAGE STATISTICS CONFIGURATIONS------------------#
#---------------------------------------------------------------# #---------------------------------------------------------------#
# These configs are only used by the DSpace interfaces which # # These properties are only used by the DSpace interfaces which #
# track usage statistics (Solr) # # track usage statistics (Solr). #
# See also: solr-statistics.cfg # # See also: solr-statistics.cfg #
#---------------------------------------------------------------# #---------------------------------------------------------------#
# The location for the Geo Database retrieved on update/installation # The location for the database used to map client addresses to approximate
usage-statistics.dbfile = ${dspace.dir}/config/GeoLite2-City.mmdb # physical locations. This MUST BE CONFIGURED if you want statistics with
# location data. A typical path is shown:
#usage-statistics.dbfile = /usr/share/GeoIP/GeoLite2-City.mmdb
# Timeout for the resolver in the DNS lookup # Timeout for the resolver in the DNS lookup
# Time in milliseconds, defaults to 200 for backward compatibility # Time in milliseconds, defaults to 200 for backward compatibility

View File

@@ -79,9 +79,6 @@ Common usage:
<format property="build.date" pattern="yyyyMMdd-HHmmss" /> <format property="build.date" pattern="yyyyMMdd-HHmmss" />
</tstamp> </tstamp>
<!-- Default location of GeoLite2 City database to download. This may be overridden, if URL path changes. -->
<property name="geolite" value="http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.tar.gz" />
<!-- ============================================================= --> <!-- ============================================================= -->
<!-- The DSpace class path for executing installation targets --> <!-- The DSpace class path for executing installation targets -->
@@ -104,7 +101,7 @@ Common usage:
<taskdef resource="net/sf/antcontrib/antlib.xml" classpathref="class.path"/> <taskdef resource="net/sf/antcontrib/antlib.xml" classpathref="class.path"/>
<!-- ============================================================= --> <!-- ============================================================= -->
<!-- Print some useful help text --> <!-- Print some useful help text. -->
<!-- ============================================================= --> <!-- ============================================================= -->
<target name="help"> <target name="help">
@@ -117,7 +114,6 @@ Common usage:
<echo message="update --> Update ${dspace.dir} config, etc, lib and web applications without " /> <echo message="update --> Update ${dspace.dir} config, etc, lib and web applications without " />
<echo message=" touching your data" /> <echo message=" touching your data" />
<echo message="update_configs --> Update your configs directory with new configuration files"/> <echo message="update_configs --> Update your configs directory with new configuration files"/>
<echo message="update_geolite --> Dowload and install GeoCity database into ${dspace.dir}/config" />
<echo message="update_code --> Update compiled code (bin, lib, and etc directories)" /> <echo message="update_code --> Update compiled code (bin, lib, and etc directories)" />
<echo message="update_webapps --> Update web applications" /> <echo message="update_webapps --> Update web applications" />
<echo message="" /> <echo message="" />
@@ -147,7 +143,7 @@ Common usage:
<!-- ============================================================= --> <!-- ============================================================= -->
<!-- clean out backup directories --> <!-- clean out backup directories -->
<!-- ============================================================= --> <!-- ============================================================= -->
<target name="clean_backups"> <target name="clean_backups">
<delete includeemptydirs="true"> <delete includeemptydirs="true">
@@ -176,10 +172,11 @@ Common usage:
<!-- ============================================================= --> <!-- ============================================================= -->
<!-- Update the config dir, only adds files, if a file is altered --> <!-- Update the config dir, only adds files, if a file is altered -->
<!-- in the target directory, the new file is suffixed wth *.new --> <!-- in the target directory, the new file is suffixed wth *.new -->
<!-- and should be hand updated afterward . --> <!-- and should be hand updated afterward. -->
<!-- ============================================================= --> <!-- ============================================================= -->
<target name="update_configs" depends="overwrite_configs,overwrite_solr_configs" description="Updates the Configuration Directory"> <target name="update_configs"
<antcall target="init_geolite" /> depends="overwrite_configs,overwrite_solr_configs"
description="Updates the Configuration Directory">
</target> </target>
<target name="overwrite_configs" description="Overwrites a configuration directory." if="${overwrite}" depends="copy_configs_keep"> <target name="overwrite_configs" description="Overwrites a configuration directory." if="${overwrite}" depends="copy_configs_keep">
@@ -832,8 +829,6 @@ Common usage:
<antcall target="copy_webapps" /> <antcall target="copy_webapps" />
<antcall target="init_geolite" />
<echo> <echo>
==================================================================== ====================================================================
The DSpace code has been installed. The DSpace code has been installed.
@@ -863,61 +858,4 @@ Common usage:
</target> </target>
<!-- installs GeoCity resolution database -->
<target name="update_geolite">
<echo>Downloading: ${geolite}</echo>
<trycatch property="geolite.error">
<try>
<get src="${geolite}"
dest="${dspace.dir}/config/GeoLite2-City.tar.gz"
verbose="true"/>
<untar src="${dspace.dir}/config/GeoLite2-City.tar.gz"
dest="${dspace.dir}/config/"
compression='gzip'>
<patternset>
<include name='**/GeoLite2-City.mmdb'/>
</patternset>
<mapper type='flatten'/>
</untar>
<delete file="${dspace.dir}/config/GeoLite2-City.tar.gz"/>
</try>
<catch>
<echo>
====================================================================
WARNING : FAILED TO DOWNLOAD GEOLITE DATABASE FILE
(Used for DSpace Solr Usage Statistics)
Underlying Error: ${geolite.error}
In order to use DSpace Solr Usage Statistics, you will need to
manually re-run:
ant update_geolite
OR
You may manually install this file by following these steps:
(1) Download the latest database archive from ${geolite}
(2) Unpack it.'
(3) Copy the file 'GeoLite2-City.mmdb' to '${usage-statistics.dbfile}'.
====================================================================
</echo>
</catch>
</trycatch>
</target>
<target name="check_geolite">
<condition property="need.geolite">
<not>
<available file="${usage-statistics.dbfile}" />
</not>
</condition>
</target>
<target name="init_geolite" depends="check_geolite" if="need.geolite">
<antcall target="update_geolite" />
</target>
</project> </project>