Added ThreadedModule abstract class.

This commit is contained in:
Erik C. Thauvin 2019-04-03 13:34:13 -07:00
parent fbcad6e611
commit d4e9121ca6
3 changed files with 74 additions and 45 deletions

View file

@ -51,7 +51,7 @@ import java.nio.charset.StandardCharsets;
* @created Feb 7, 2004
* @since 1.0
*/
public final class GoogleSearch extends AbstractModule {
public final class GoogleSearch extends ThreadedModule {
/**
* The google command.
*/
@ -74,18 +74,6 @@ public final class GoogleSearch extends AbstractModule {
properties.put(GOOGLE_CSE_KEY_PROP, "");
}
/**
* {@inheritDoc}
*/
@Override
public void commandResponse(final Mobibot bot, final String sender, final String args, final boolean isPrivate) {
if (isEnabled() && args.length() > 0) {
new Thread(() -> run(bot, sender, args)).start();
} else {
helpResponse(bot, sender, args, isPrivate);
}
}
/**
* {@inheritDoc}
*/
@ -99,19 +87,11 @@ public final class GoogleSearch extends AbstractModule {
}
}
/**
* {@inheritDoc}
*/
@Override
public boolean isEnabled() {
return isValidProperties();
}
/**
* Searches Google.
*/
@SuppressFBWarnings(value = {"URLCONNECTION_SSRF_FD", "REC_CATCH_EXCEPTION"})
private void run(final Mobibot bot, final String sender, final String query) {
void run(final Mobibot bot, final String sender, final String query) {
try {
final String q = URLEncoder.encode(query, "UTF-8");

View file

@ -0,0 +1,68 @@
/*
* ThreadedModule.java
*
* Copyright (c) 2004-2019, Erik C. Thauvin (erik@thauvin.net)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of this project nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.thauvin.erik.mobibot.modules;
import net.thauvin.erik.mobibot.Mobibot;
/**
* The <code>ThreadedModule</code> class.
*
* @author <a href="https://erik.thauvin.net/" target="_blank">Erik C. Thauvin</a>
* @created 2019-04-03
* @since 1.0
*/
public abstract class ThreadedModule extends AbstractModule {
/**
* {@inheritDoc}
*/
@Override
public void commandResponse(final Mobibot bot, final String sender, final String args, final boolean isPrivate) {
if (isEnabled() && args.length() > 0) {
new Thread(() -> run(bot, sender, args)).start();
} else {
helpResponse(bot, sender, args, isPrivate);
}
}
/**
* {@inheritDoc}
*/
@Override
public boolean isEnabled() {
return isValidProperties();
}
/**
* Run the thread.
*/
abstract void run(Mobibot bot, String sender, String args);
}

View file

@ -43,7 +43,7 @@ import twitter4j.conf.ConfigurationBuilder;
* @created Sept 10, 2008
* @since 1.0
*/
public final class Twitter extends AbstractModule {
public final class Twitter extends ThreadedModule {
/**
* The twitter command.
*/
@ -66,18 +66,6 @@ public final class Twitter extends AbstractModule {
properties.put(TOKEN_SECRET_PROP, "");
}
/**
* {@inheritDoc}
*/
@Override
public void commandResponse(final Mobibot bot, final String sender, final String args, final boolean isPrivate) {
if (isEnabled() && args.length() > 0) {
new Thread(() -> run(bot, sender, args)).start();
} else {
helpResponse(bot, sender, args, isPrivate);
}
}
/**
* {@inheritDoc}
*/
@ -91,18 +79,11 @@ public final class Twitter extends AbstractModule {
}
}
/**
* {@inheritDoc}
*/
@Override
public boolean isEnabled() {
return isValidProperties();
}
/**
* Posts to twitter.
*/
private void run(final Mobibot bot, final String sender, final String message) {
@Override
void run(final Mobibot bot, final String sender, final String message) {
try {
final ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)