Skip to content

Commit

Permalink
Replace Disposable with Closeable
Browse files Browse the repository at this point in the history
  • Loading branch information
azabost committed May 14, 2024
1 parent 3299f14 commit 427e52b
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 33 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package pl.brightinventions.slf4android;

import org.slf4j.LoggerFactory;

import java.io.Closeable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.logging.Handler;
Expand All @@ -9,33 +13,40 @@
public class LoggerConfiguration implements LoggerPatternConfiguration {
private static boolean initialized = false;
private static LoggerConfiguration configuration;
private final ArrayList<LoggerPattern> loggerPatterns = new ArrayList<LoggerPattern>();
private final ArrayList<Disposable> disposeThingsOnReset = new ArrayList<Disposable>();
private final ArrayList<LoggerPattern> loggerPatterns = new ArrayList<>();
private final ArrayList<Closeable> thingsToCloseOnReset = new ArrayList<>();
private final HandlerFormatterCompiler compiler;

private final org.slf4j.Logger logger;

LoggerConfiguration() {
compiler = new HandlerFormatterCompiler(this);
logger = LoggerFactory.getLogger(getClass().getSimpleName());
}

public static LoggerConfiguration resetConfigurationToDefault() {
if (configuration != null) {
configuration.dispose();
configuration.close();
}
configureDefaults();
return configuration;
}

/**
* Registers a {@link Disposable} that will be disposed upon
* Registers a {@link Closeable} that will be closed upon
* {@link #resetConfigurationToDefault() configuration reset}.
*/
public synchronized void registerDisposable(Disposable disposable) {
disposeThingsOnReset.add(disposable);
public synchronized void registerCloseable(Closeable closeable) {
thingsToCloseOnReset.add(closeable);
}

private synchronized void dispose() {
for (Disposable dispose : disposeThingsOnReset) {
dispose.dispose();
private synchronized void close() {
for (Closeable closeable : thingsToCloseOnReset) {
try {
closeable.close();
} catch (IOException e) {
logger.error("Could not close {}", closeable, e);
}
}
}

Expand Down Expand Up @@ -129,10 +140,6 @@ public LoggerConfiguration removeHandlerFromLogger(String loggerName, Class<? ex
}
return this;
}
/*
*/

/**
* Adds given {@code handler} to logger named {@code loggerName}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
import android.util.Log;
import android.widget.Toast;

import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import pl.brightinventions.slf4android.Disposable;
import pl.brightinventions.slf4android.LogRecord;
import pl.brightinventions.slf4android.MessageValueSupplier;

Expand Down Expand Up @@ -49,7 +50,7 @@ public static AlertDialog showDialogIn(final Context activityContext, LogRecord
return alertDialog;
}

private static AlertDialog showDialogIn(final Context activityContext, final String message, final List<String> emailAddresses, String emailSubject, String emailBody, Iterable<AsyncTask<Context, Void, File>> attachmentTasks, final Disposable onDialogClose) {
private static AlertDialog showDialogIn(final Context activityContext, final String message, final List<String> emailAddresses, String emailSubject, String emailBody, Iterable<AsyncTask<Context, Void, File>> attachmentTasks, final Closeable onDialogClose) {
final EmailErrorReport emailErrorReport = new EmailErrorReport(message, emailAddresses, emailSubject, emailBody);
for (AsyncTask<Context, Void, File> attachment : attachmentTasks) {
startTaskExecution(activityContext, attachment);
Expand All @@ -69,25 +70,31 @@ private static AlertDialog showDialogIn(final Context activityContext, final Str
public void onClick(DialogInterface dialog, int which) {
sendEmailWithError(activityContext, emailErrorReport);
dialog.dismiss();
if (onDialogClose != null) {
onDialogClose.dispose();
}
closeSafely(onDialogClose);
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
if (onDialogClose != null) {
onDialogClose.dispose();
}
closeSafely(onDialogClose);
}
})
.create();
alertDialog.show();
return alertDialog;
}

private static void closeSafely(Closeable closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (IOException e) {
Log.e(TAG, "Could not close " + closeable, e);
}
}
}

private static List<AsyncTask<Context, Void, File>> buildAttachmentFactories(Iterable<String> attachmentsClasses) {
List<AsyncTask<Context, Void, File>> attachmentTasks = new ArrayList<AsyncTask<Context, Void, File>>();
for (String attachmentClassName : attachmentsClasses) {
Expand Down Expand Up @@ -147,9 +154,9 @@ protected void onCreate(Bundle savedInstanceState) {
String emailBody = getIntent().getExtras().getString("email_body");
List<AsyncTask<Context, Void, File>> attachmentTasks = buildAttachmentFactoriesFromIntent();
if (log_record != null) {
dialog = showDialogIn(this, log_record, emailAddresses, emailSubject, emailBody, attachmentTasks, new Disposable() {
dialog = showDialogIn(this, log_record, emailAddresses, emailSubject, emailBody, attachmentTasks, new Closeable() {
@Override
public void dispose() {
public void close() {
finish();
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static NotifyDeveloperHandler create(final Application context, String em
private static ActivityStateListener getStateListener(final Application context) {
final ActivityStateListener stateListener = new ActivityStateListener();

LoggerConfiguration.configuration().registerDisposable(() ->
LoggerConfiguration.configuration().registerCloseable(() ->
context.unregisterActivityLifecycleCallbacks(stateListener)
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

import pl.brightinventions.slf4android.LoggerConfiguration;

public class MainActivity extends Activity {

private static final Logger LOG = LoggerFactory.getLogger(MainActivity.class.getSimpleName());
Expand All @@ -24,5 +28,17 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
LOG.warn("warn");
LOG.error("error");
});
findViewById(R.id.reset).setOnClickListener((view) -> {
LoggerConfiguration.resetConfigurationToDefault();
});
findViewById(R.id.registerCloseable).setOnClickListener((view) -> {
LoggerConfiguration.configuration().registerCloseable(() -> LOG.info("Closing"));
});
findViewById(R.id.registerFailingCloseable).setOnClickListener((view) -> {
LoggerConfiguration.configuration().registerCloseable(() -> {
throw new IOException("Test");
});
});

}
}
21 changes: 17 additions & 4 deletions testapp/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,28 @@
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
<Button
android:id="@+id/log"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello" />
android:text="Log" />

<Button
android:id="@+id/log"
android:id="@+id/reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Log" />
android:text="Reset configuration" />

<Button
android:id="@+id/registerCloseable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Register Closeable" />

<Button
android:id="@+id/registerFailingCloseable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Register failing Closeable" />

</LinearLayout>

0 comments on commit 427e52b

Please sign in to comment.