Skip to content

Commit

Permalink
Code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
k-tamura committed Jun 15, 2017
1 parent 579102e commit 31f20e4
Show file tree
Hide file tree
Showing 38 changed files with 268 additions and 326 deletions.
4 changes: 3 additions & 1 deletion src/main/java/org/t246osslab/easybuggy/core/model/User.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package org.t246osslab.easybuggy.core.model;

import java.io.Serializable;
import java.util.Date;

public class User {
public class User implements Serializable{

private static final long serialVersionUID = 1L;
private String userId = null;
private String name = null;
private String password = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,69 +47,21 @@ public final class ApplicationUtils {
private static String adminAddress = null;

static {
ResourceBundle bundle = null;
try {
bundle = ResourceBundle.getBundle("application");
ResourceBundle bundle = ResourceBundle.getBundle("application");
databaseURL = getProperty(bundle, "database.url", databaseURL);
databaseDriver = getProperty(bundle, "database.driver", databaseDriver);
accountLockTime = getProperty(bundle, "account.lock.time", accountLockTime);
accountLockCount = getProperty(bundle, "account.lock.count", accountLockCount);
smtpHost = getProperty(bundle, "mail.smtp.host", smtpHost);
smtpPort = getProperty(bundle, "mail.smtp.port", smtpPort);
smtpAuth = getProperty(bundle, "mail.smtp.auth", smtpAuth);
smtpStarttlsEnable = getProperty(bundle, "mail.smtp.starttls.enable", smtpStarttlsEnable);
smtpUser = getProperty(bundle, "mail.user", smtpUser);
smtpPass = getProperty(bundle, "mail.password", smtpPass);
adminAddress = getProperty(bundle, "mail.admin.address", adminAddress);
} catch (MissingResourceException e) {
log.error("Exception occurs: ", e);
}
if (bundle != null) {
try {
databaseURL = bundle.getString("database.url");
} catch (Exception e) {
log.error("Exception occurs: ", e);
}
try {
databaseDriver = bundle.getString("database.driver");
} catch (Exception e) {
log.error("Exception occurs: ", e);
}
try {
accountLockTime = Long.parseLong(bundle.getString("account.lock.time"));
} catch (Exception e) {
log.error("Exception occurs: ", e);
}
try {
accountLockCount = Integer.parseInt(bundle.getString("account.lock.count"));
} catch (Exception e) {
log.error("Exception occurs: ", e);
}
try {
smtpHost = bundle.getString("mail.smtp.host");
} catch (Exception e) {
log.error("Exception occurs: ", e);
}
try {
smtpPort = bundle.getString("mail.smtp.port");
} catch (Exception e) {
log.error("Exception occurs: ", e);
}
try {
smtpAuth = bundle.getString("mail.smtp.auth");
} catch (Exception e) {
log.error("Exception occurs: ", e);
}
try {
smtpStarttlsEnable = bundle.getString("mail.smtp.starttls.enable");
} catch (Exception e) {
log.error("Exception occurs: ", e);
}
try {
smtpUser = bundle.getString("mail.user");
} catch (Exception e) {
log.error("Exception occurs: ", e);
}
try {
smtpPass = bundle.getString("mail.password");
} catch (Exception e) {
log.error("Exception occurs: ", e);
}

try {
adminAddress = bundle.getString("mail.admin.address");
} catch (Exception e) {
log.error("Exception occurs: ", e);
}
log.error("MissingResourceException occurs: ", e);
}
}

Expand Down Expand Up @@ -216,4 +168,31 @@ public static String getSmtpPass() {
public static String getAdminAddress() {
return adminAddress;
}

private static String getProperty(ResourceBundle bundle, String key, String defaultValue) {
try {
return bundle.getString(key);
} catch (Exception e) {
log.error("Exception occurs: ", e);
}
return defaultValue;
}

private static int getProperty(ResourceBundle bundle, String key, int defaultValue) {
try {
return Integer.parseInt(bundle.getString(key));
} catch (Exception e) {
log.error("Exception occurs: ", e);
}
return defaultValue;
}

private static long getProperty(ResourceBundle bundle, String key, long defaultValue) {
try {
return Long.parseLong(bundle.getString(key));
} catch (Exception e) {
log.error("Exception occurs: ", e);
}
return defaultValue;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package org.t246osslab.easybuggy.core.utils;

public class DeleteClassWhileMavenBuild {

// this class is removed during Maven build processing
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
Expand Down Expand Up @@ -48,9 +47,8 @@ public static boolean isReadyToSendEmail() {
/**
* Sends an e-mail message from a SMTP host with a list of attached files.
*/
public static void sendEmailWithAttachment(
String subject, String message, List<File> attachedFiles)
throws AddressException, MessagingException {
public static void sendEmailWithAttachment(String subject, String message, List<File> attachedFiles)
throws MessagingException {
// sets SMTP server properties
Properties properties = new Properties();
properties.put("mail.smtp.host", ApplicationUtils.getSmtpHost());
Expand Down Expand Up @@ -87,17 +85,15 @@ public PasswordAuthentication getPasswordAuthentication() {
multipart.addBodyPart(messageBodyPart);

// adds attachments
if (attachedFiles != null && attachedFiles.size() > 0) {
if (attachedFiles != null && !attachedFiles.isEmpty()) {
for (File aFile : attachedFiles) {
MimeBodyPart attachPart = new MimeBodyPart();

try {
try {
attachPart.attachFile(aFile);
} catch (IOException e) {
log.error("IOException occurs: ", e);
}

multipart.addBodyPart(attachPart);
multipart.addBodyPart(attachPart);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ protected void doGet(HttpServletRequest req, HttpServletResponse res) throws Ser

class InitializerErrorThrower {
static {
int i = 1 / 0;
LoggerFactory.getLogger(InitializerErrorThrower.class).debug("clinit" + 1 / 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
public class OutOfMemoryErrorServlet2 extends HttpServlet {

protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
byte[] buffer = new byte[Integer.MAX_VALUE];
req.setAttribute("oome2", new byte[Integer.MAX_VALUE]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
public class OutOfMemoryErrorServlet6 extends HttpServlet {

protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

ByteBuffer buffer = ByteBuffer.allocateDirect(99999999);
req.setAttribute("oome2", ByteBuffer.allocateDirect(99999999));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
@SuppressWarnings("serial")
public class UnsatisfiedLinkErrorServlet extends HttpServlet {

private native static NetworkInterface getByName0(String name) throws SocketException;
private static native NetworkInterface getByName0(String name) throws SocketException;

protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
getByName0("");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
public class ArithmeticExceptionServlet extends HttpServlet {

protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
int i = 1 / 0;
res.addIntHeader("ae", 1 / 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
public class ArrayIndexOutOfBoundsExceptionServlet extends HttpServlet {

protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
int i = (new int[] { 1 })[1];
req.setAttribute("aioobe", (new int[] { 1 })[1]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ public class ArrayStoreExceptionServlet extends HttpServlet {

protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
Object[] objects = new String[1];
objects[0] = new Integer(0);
objects[0] = Integer.valueOf(1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.t246osslab.easybuggy.core.utils.Closer;

@SuppressWarnings("serial")
@WebServlet(urlPatterns = { "/boe" })
Expand All @@ -24,9 +25,10 @@ public class BufferOverflowExceptionServlet extends HttpServlet {
private static final Logger log = LoggerFactory.getLogger(BufferOverflowExceptionServlet.class);

protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
RandomAccessFile raf = null;
try {
File f = new File("test.txt");
RandomAccessFile raf = new RandomAccessFile(f, "rw");
raf = new RandomAccessFile(f, "rw");
FileChannel ch = raf.getChannel();
MappedByteBuffer buf = ch.map(MapMode.READ_WRITE, 0, f.length());
final byte[] src = new byte[10];
Expand All @@ -35,6 +37,8 @@ protected void doGet(HttpServletRequest req, HttpServletResponse res) throws Ser
log.error("FileNotFoundException occurs: ", e);
} catch (IOException e) {
log.error("IOException occurs: ", e);
} finally {
Closer.close(raf);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ public class ClassCastExceptionServlet extends HttpServlet {

protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
req.setAttribute("param1", "value1");
String[] s = (String[]) req.getAttribute("param1");
req.setAttribute("param2", (String[]) req.getAttribute("param1"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public class EmptyStackExceptionServlet extends HttpServlet {

protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
Stack<String> stack = new Stack<String>();
String tmp = null;
while (null != (tmp = (String) stack.pop())) {
String tmp;
while (null != (tmp = stack.pop())) {
log.debug("Stack.pop(): " + tmp);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.t246osslab.easybuggy.exceptions;

import java.io.IOException;
import java.util.Date;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
Expand All @@ -14,6 +14,6 @@
public class IllegalArgumentExceptionServlet extends HttpServlet {

protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
new Date(null);
req.setAttribute("iae", new ArrayList<Object>(-1));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
public class NegativeArraySizeExceptionServlet extends HttpServlet {

protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
int[] intArray = new int[-1];
req.setAttribute("nase", new int[-1]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
public class NullPointerExceptionServlet extends HttpServlet {

protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
Integer.decode(null);
req.setAttribute("npe", Integer.decode(null));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
public class NumberFormatExceptionServlet extends HttpServlet {

protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
Integer.valueOf("");
req.setAttribute("nfe", Integer.valueOf(""));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
public class UnsupportedCharsetExceptionServlet extends HttpServlet {

protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
new String("str".getBytes(Charset.defaultCharset()), Charset.forName("test"));
req.setAttribute("uce", new String("str".getBytes(Charset.defaultCharset()), Charset.forName("test")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ protected void doGet(HttpServletRequest req, HttpServletResponse res) throws Ser
Iterator<String> i = alphabet.iterator();
while(i.hasNext()){
String name = i.next();
if(!name.equals("a")){
if(!"a".equals(name)){
i.remove();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.t246osslab.easybuggy.core.utils.HTTPResponseCreator;
Expand All @@ -21,17 +22,10 @@ public class CreatingUnnecessaryObjectsServlet extends HttpServlet {
private static final Logger log = LoggerFactory.getLogger(CreatingUnnecessaryObjectsServlet.class);

protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
int number = -1;
try {
Locale locale = req.getLocale();
String strNumber = req.getParameter("number");
if (strNumber != null) {
try {
number = Integer.parseInt(strNumber);
} catch (NumberFormatException e) {
// ignore
}
}
int number = NumberUtils.toInt(strNumber, -1);
StringBuilder bodyHtml = new StringBuilder();
bodyHtml.append("<form action=\"createobjects\" method=\"post\">");
bodyHtml.append(MessageUtils.getMsg("msg.calc.sym.natural.numbers", locale));
Expand Down Expand Up @@ -64,19 +58,17 @@ protected void service(HttpServletRequest req, HttpServletResponse res) throws S
bodyHtml.append("\\(\\begin{eqnarray}\\sum_{ k = 1 }^{ " + number + " } k\\end{eqnarray}\\) = ");
}
} else {
bodyHtml.append("1 + 2 + 3 + + n = ");
bodyHtml.append("1 + 2 + 3 + ... + n = ");
bodyHtml.append("\\(\\begin{eqnarray}\\sum_{ k = 1 }^{ n } k\\end{eqnarray}\\) = ");
}
if (number >= 1) {
long start = System.nanoTime();
bodyHtml.append(calcSum1(number));
log.info((System.nanoTime() - start) / 1000000f + " ms");
}
bodyHtml.append("<br>");
bodyHtml.append("<br>");
bodyHtml.append("<br><br>");
bodyHtml.append("<input type=\"submit\" value=\"" + MessageUtils.getMsg("label.calculate", locale) + "\">");
bodyHtml.append("<br>");
bodyHtml.append("<br>");
bodyHtml.append("<br><br>");
bodyHtml.append(MessageUtils.getInfoMsg("msg.note.enter.large.number", locale));
bodyHtml.append("</form>");

Expand Down
Loading

0 comments on commit 31f20e4

Please sign in to comment.