Skip to content

Commit

Permalink
Remove generic exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
car031 committed May 25, 2023
1 parent 91b026f commit 894563e
Show file tree
Hide file tree
Showing 63 changed files with 973 additions and 641 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import com.logicaldoc.core.security.Session;
import com.logicaldoc.core.security.SessionManager;
import com.logicaldoc.core.security.Tenant;
import com.logicaldoc.util.plugin.PluginException;
import com.logicaldoc.util.plugin.PluginRegistry;

public class LDRepositoryTest extends AbstractCmisTCase {
Expand All @@ -63,7 +64,7 @@ public void setUp() throws Exception {
fdao = (FolderDAO) context.getBean("FolderDAO");
}

private void activateCorePlugin() throws JpfException, IOException {
private void activateCorePlugin() throws JpfException, IOException, PluginException {
File pluginsDir = new File("target/tests-plugins");
pluginsDir.mkdir();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,7 @@ public void setDateFormat(String dateFormat) {
this.dateFormat = dateFormat;
}

@Override
public TimeZone getTimeZone() {
return timeZone;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ private Session newMailSession() {
try {
if (!StringUtils.isEmpty(username))
sess = Session.getInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
Expand All @@ -384,6 +385,7 @@ protected PasswordAuthentication getPasswordAuthentication() {
} catch (SecurityException e) {
if (!StringUtils.isEmpty(username))
sess = Session.getInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,16 @@ public String getFullName() {
return firstName != null ? firstName : (lastName != null ? " " + lastName : "");
}

public String toString() {
return getFullName() + (email != null ? " - " + email : "");
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

@Override
public String toString() {
return getFullName() + (email != null ? " - " + email : "");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.apache.commons.lang.StringUtils;
import org.apache.http.Consts;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
Expand Down Expand Up @@ -294,13 +293,11 @@ protected void runTask() throws TaskException {
/*
* General usage
*/
SimpleDateFormat isoDf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");

Date lastLogin = findLastLogin();
postParams.add(new BasicNameValuePair("last_login", lastLogin != null ? isoDf.format(lastLogin) : ""));
postParams.add(new BasicNameValuePair("last_login", formatDate(lastLogin)));

Date lastCreation = findLastCreation();
postParams.add(new BasicNameValuePair("last_creation", lastCreation != null ? isoDf.format(lastCreation) : ""));
postParams.add(new BasicNameValuePair("last_creation", formatDate(lastCreation)));

/*
* Quotas
Expand All @@ -318,33 +315,38 @@ protected void runTask() throws TaskException {
postParams.add(new BasicNameValuePair("reg_organization", regOrganization != null ? regOrganization : ""));
postParams.add(new BasicNameValuePair("reg_website", regWebsite != null ? regWebsite : ""));

try {
postStatistics(postParams);
} catch (IOException e) {
log.warn("Troubles sending the statistics");
log.debug("Unable to send statistics", e);
postStatistics(postParams);

}
next();
}

private String formatDate(Date lastLogin) {
SimpleDateFormat isoDf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
return lastLogin != null ? isoDf.format(lastLogin) : "";
}

private void postStatistics(List<NameValuePair> postParams) throws IOException, ClientProtocolException {
HttpPost post = new HttpPost("http://stat.logicaldoc.com/stats/collect");
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(postParams, Consts.UTF_8);
post.setEntity(entity);
private void postStatistics(List<NameValuePair> postParams) {
try {
HttpPost post = new HttpPost("http://stat.logicaldoc.com/stats/collect");
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(postParams, Consts.UTF_8);
post.setEntity(entity);

CloseableHttpClient httpclient = HttpUtil.getNotValidatingClient(60);

CloseableHttpClient httpclient = HttpUtil.getNotValidatingClient(60);
// Execute request
try (CloseableHttpResponse response = httpclient.execute(post)) {
int responseStatusCode = response.getStatusLine().getStatusCode();
// log status code
log.debug("Response status code: {}", responseStatusCode);
if (responseStatusCode != 200)
throw new IOException(HttpUtil.getBodyString(response));
}
log.info("Statistics packaged");
} catch (IOException e) {
log.warn("Troubles sending the statistics");
log.debug("Unable to send statistics", e);

// Execute request
try (CloseableHttpResponse response = httpclient.execute(post)) {
int responseStatusCode = response.getStatusLine().getStatusCode();
// log status code
log.debug("Response status code: {}", responseStatusCode);
if (responseStatusCode != 200)
throw new IOException(HttpUtil.getBodyString(response));
}
log.info("Statistics packaged");
}

private Date findLastLogin() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,15 @@ public static ThreadPools get() {
*
* @return the created pool
*
* @throws InterruptedException Raised in case the pool has been shutdown
* @throws ThreadPoolNotAvailableException Raised in case the pool has been
* shutdown
*/
public synchronized ExecutorService getPool(String name) throws InterruptedException {
public synchronized ExecutorService getPool(String name) throws ThreadPoolNotAvailableException {
ExecutorService pool = pools.get(name);

if (pool != null) {
if (pools.get(name).isShutdown())
throw new InterruptedException(name + " pool was shutdown");
throw new ThreadPoolNotAvailableException(name);
} else {
int core = config.getInt(THREADPOOL + name + ".core", 5);
int max = config.getInt(THREADPOOL + name + ".max", 10);
Expand Down Expand Up @@ -102,7 +103,7 @@ public void schedule(Runnable task, String poolName, long delay) {
log.debug("Pool {} does not support scheduling so the task has been started immediately", poolName);
execute(task, poolName);
}
} catch (InterruptedException e) {
} catch (ThreadPoolNotAvailableException e) {
log.error(e.getMessage());
}
}
Expand All @@ -117,7 +118,7 @@ public void execute(Runnable task, String poolName) {
try {
ExecutorService pool = getPool(poolName);
pool.execute(task);
} catch (InterruptedException e) {
} catch (ThreadPoolNotAvailableException e) {
log.error(e.getMessage());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ public int importDocuments(long targetFolder, String[] paths) throws ServerExcep
}

private void importEntry(Session session, Dropbox dbox, Folder root, Set<String> imported, Metadata entry)
throws Exception, DbxException, PersistenceException {
throws DbxException, PersistenceException, IOException {
if (entry instanceof FileMetadata) {
importDocument(root, (FileMetadata) entry, dbox, session);
imported.add(entry.getPathDisplay());
Expand Down Expand Up @@ -327,7 +327,8 @@ private void importEntry(Session session, Dropbox dbox, Folder root, Set<String>
}
}

private void importDocument(Folder root, FileMetadata src, Dropbox dbox, Session session) throws Exception {
private void importDocument(Folder root, FileMetadata src, Dropbox dbox, Session session)
throws IOException, PersistenceException {
DocumentDAO ddao = (DocumentDAO) Context.get().getBean(DocumentDAO.class);
DocumentManager manager = (DocumentManager) Context.get().getBean(DocumentManager.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ public interface InfoService extends RemoteService {
* @param tenant name of the tenant
* @param login if the informations are asked by the login form
*
* @throws ServerException raised in case of error in the server
*
* @return the User Inteface's informations
*/
public GUIInfo getInfo(String locale, String tenant, boolean login);
public GUIInfo getInfo(String locale, String tenant, boolean login) throws ServerException;

/**
* Retrieves all the settings for the current session
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public abstract class SystemEventListener {

protected SystemEventListener(SystemEventStatus status) {
if(status == null)
throw new RuntimeException("A valid SystemEventStatus must be registered");
throw new IllegalArgumentException("A valid SystemEventStatus must be registered");

this.systemStatus = status;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ public class JavaLauncher {
* @param jvmargs - arguments for the jvm
* @param properties - any system properties
*
* @throws Exception An error happened during execution
*
* @return the launched process
*
* @throws ExecException Error in executing the given command
*/
public static Process exec(String mainClass, String classpath, String[] jvmargs, String[] properties)
throws Exception {
throws ExecException {

// get a jvm to execute with
String jvm = findJVM();
Expand Down Expand Up @@ -63,7 +64,7 @@ public static Process exec(String mainClass, String classpath, String[] jvmargs,
wholeCommand.append(command[i] + " ");
}

log.info("Executing Command: " + wholeCommand);
log.info("Executing Command: {}", wholeCommand);

try {

Expand All @@ -76,7 +77,7 @@ public static Process exec(String mainClass, String classpath, String[] jvmargs,

} catch (Exception e) {
log.error("Failed to launch java program: {}", e.getMessage());
throw new Exception(FAILED_TO_LAUNCH_JAVA_PROGRAM + e.getMessage());
throw new ExecException(wholeCommand.toString(), e);
}

}
Expand All @@ -88,9 +89,9 @@ public static Process exec(String mainClass, String classpath, String[] jvmargs,
* @param jvmargs - arguments for the java virtual machine
* @return Process The launched process
*
* @throws Exception An error happened during execution
* @throws ExecException An error happened during execution
*/
public static Process execJar(String pathToJar, String[] jvmargs) throws Exception {
public static Process execJar(String pathToJar, String[] jvmargs) throws ExecException {
String jvm = findJVM();

String[] command = new String[jvmargs.length + 3];
Expand Down Expand Up @@ -120,7 +121,7 @@ public static Process execJar(String pathToJar, String[] jvmargs) throws Excepti
return proc;

} catch (Exception e) {
throw new Exception(FAILED_TO_LAUNCH_JAVA_PROGRAM + e.getMessage());
throw new ExecException(wholeCommand.toString(), e);
}

}
Expand All @@ -129,7 +130,8 @@ public static Process execJar(String pathToJar, String[] jvmargs) throws Excepti
* Monitor an execute java program for errors and exit status.
*
* @param proc
* @throws java.io.IOException
*
* @throws IOException I/O error
*/
private static void monitorProcess(Process proc) throws IOException {
proc.getInputStream().close();
Expand All @@ -142,17 +144,13 @@ private static void monitorProcess(Process proc) throws IOException {

// read the output
String line;
while ((line = bufferedreader.readLine()) != null) {
while ((line = bufferedreader.readLine()) != null)
log.info(line);
}

// check for failure
try {

if (proc.waitFor() != 0) {
if (proc.waitFor() != 0)
log.info("exit value = " + proc.exitValue());
}

} catch (InterruptedException e) {
log.error(e.getMessage());
Thread.currentThread().interrupt();
Expand All @@ -165,24 +163,19 @@ private static void monitorProcess(Process proc) throws IOException {
* @return - path to java binary
*/
public static String findJVM() {

String jvm = null;
jvm = System.getProperty("java.home");

// handle property not set
if (jvm == null) {

log.warn("Java home property not set, just guessing with a general java call, and will probably fail.");

// just take a guess an hope it's in the classpath
jvm = "java";

}

// add binary folder
jvm = jvm + File.separator + "bin" + File.separator + "java";

return jvm;
// add binary folders
return jvm + File.separator + "bin" + File.separator + "java";
}

/**
Expand All @@ -191,9 +184,7 @@ public static String findJVM() {
* @param args the invocation arguments
*/
public static void main(String[] args) {

try {

// things you want the JVM to get. NOT program arguments.
String[] jvmargs = { "-Xms256m", "-Xmx1024m", "-Ddebug=true" };

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.logicaldoc.util.http;

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

import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
Expand Down Expand Up @@ -35,7 +37,7 @@ public class HttpUpload {

private FileBodyCounter.ProgressListener listener;

public void upload() throws Exception {
public void upload() throws ClientProtocolException, IOException {
HttpPost filePost = new HttpPost(url);

String f = fileName;
Expand Down Expand Up @@ -68,7 +70,7 @@ public void upload() throws Exception {
} else {
String message = "Upload failed, response= " + status;
System.out.println(message);
throw new Exception(message);
throw new IOException(message);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public static void writeFile(InputStream in, String filepath) throws FileNotFoun
}
}

public static void writeFile(byte[] in, String filepath) throws Exception {
public static void writeFile(byte[] in, String filepath) throws IOException {
try (InputStream inStream = new ByteArrayInputStream(in);) {
writeFile(inStream, filepath);
}
Expand Down Expand Up @@ -589,7 +589,7 @@ private static void copy(RandomAccessFile input, OutputStream output, long start
}
}

public static void replaceInFile(String sourcePath, String token, String newValue) throws Exception {
public static void replaceInFile(String sourcePath, String token, String newValue) {
String oldContent = "";

File tmp = new File(sourcePath + ".tmp");
Expand Down
Loading

0 comments on commit 894563e

Please sign in to comment.