Skip to content

Commit

Permalink
* 增强 close
Browse files Browse the repository at this point in the history
  • Loading branch information
fjn committed Sep 12, 2022
1 parent 97196b6 commit ab58010
Show file tree
Hide file tree
Showing 14 changed files with 259 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.jn.langx.lifecycle;

import com.jn.langx.util.collection.Collects;
import com.jn.langx.util.io.close.AbstractCloser;

import java.util.List;

public class DestroyableCloser extends AbstractCloser<Destroyable> {
@Override
public List<Class> applyTo() {
return Collects.<Class>newArrayList(Destroyable.class);
}

@Override
protected void doClose(Destroyable destroyable) throws Throwable {
destroyable.destroy();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.jn.langx.lifecycle;

import com.jn.langx.util.collection.Collects;
import com.jn.langx.util.io.close.AbstractCloser;

import java.util.List;

public class LifecycleCloser extends AbstractCloser<Lifecycle> {
@Override
public List<Class> applyTo() {
return Collects.<Class>newArrayList(Lifecycle.class);
}

@Override
protected void doClose(Lifecycle lifecycle) throws Throwable {
lifecycle.shutdown();
}
}
8 changes: 8 additions & 0 deletions langx-java/src/main/java/com/jn/langx/util/io/Closer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.jn.langx.util.io;

import java.util.List;

public interface Closer<I> {
void close(I i);
List<Class> applyTo();
}
42 changes: 3 additions & 39 deletions langx-java/src/main/java/com/jn/langx/util/io/IOs.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,20 @@
import com.jn.langx.annotation.NonNull;
import com.jn.langx.io.stream.ByteArrayOutputStream;
import com.jn.langx.io.stream.StringBuilderWriter;
import com.jn.langx.lifecycle.Destroyable;
import com.jn.langx.lifecycle.Lifecycle;
import com.jn.langx.util.Emptys;
import com.jn.langx.util.Maths;
import com.jn.langx.util.Preconditions;
import com.jn.langx.util.Throwables;
import com.jn.langx.util.logging.Loggers;
import com.jn.langx.util.reflect.Reflects;
import org.slf4j.Logger;
import com.jn.langx.util.io.close.ObjectCloser;

import java.io.*;
import java.net.*;
import java.nio.ByteBuffer;
import java.nio.channels.ReadableByteChannel;
import java.nio.charset.Charset;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Scanner;

/**
* @author jinuo.fang
Expand Down Expand Up @@ -58,39 +50,11 @@ public class IOs {
private static char[] SKIP_CHAR_BUFFER;
private static byte[] SKIP_BYTE_BUFFER;

public static void close(Object target) {
public static void close(final Object target) {
if (target == null) {
return;
}

try {
if (target instanceof Closeable) {
((Closeable) target).close();
} else if (target instanceof Lifecycle) {
((Lifecycle) target).shutdown();
} else if (target instanceof Destroyable) {
((Destroyable) target).destroy();
} else if (target instanceof ResultSet) {
((ResultSet) target).close();
} else if (target instanceof Statement) {
((Statement) target).close();
} else if (target instanceof Connection) {
((Connection) target).close();
} else if (target instanceof Scanner) {
((Scanner) target).close();
} else if (target instanceof Socket) {
((Socket) target).close();
} else if (target instanceof ServerSocket) {
((ServerSocket) target).close();
} else if (target instanceof DatagramSocket) {
((DatagramSocket) target).close();
} else {
Reflects.invokeAnyMethodForcedIfPresent(target, "close", null, null);
}
} catch (Throwable ex) {
Logger logger = Loggers.getLogger(IOs.class);
logger.warn(ex.getMessage(), ex);
}
ObjectCloser.close(target);
}

public static String readAsString(Reader reader) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.jn.langx.util.io.close;

import com.jn.langx.annotation.NonNull;
import com.jn.langx.util.io.Closer;
import com.jn.langx.util.logging.Loggers;
import com.jn.langx.util.reflect.Reflects;


public abstract class AbstractCloser<I> implements Closer<I> {

@Override
public void close(I i) {
if (i != null) {
try {
doClose(i);
} catch (Throwable e) {
Loggers.getLogger(getClass()).warn("error occur when close {}, error: {}", Reflects.getFQNClassName(i.getClass()), e.getMessage(), e);
}
}
}

protected abstract void doClose(@NonNull I i) throws Throwable;


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.jn.langx.util.io.close;


import com.jn.langx.util.collection.Collects;

import java.io.Closeable;
import java.util.List;

public class CloseableCloser extends AbstractCloser<Closeable> {
@Override
protected void doClose(Closeable closeable) throws Throwable{
closeable.close();
}

@Override
public List<Class> applyTo() {
return Collects.<Class>newArrayList(Closeable.class);
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.jn.langx.util.io.close;

import com.jn.langx.util.collection.Collects;

import java.io.Closeable;
import java.sql.Connection;
import java.util.List;

public class ConnectionCloser extends AbstractCloser<Connection> {
@Override
protected void doClose(Connection connection) throws Throwable {
connection.close();
}

@Override
public List<Class> applyTo() {
return Collects.<Class>newArrayList(Connection.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.jn.langx.util.io.close;

import com.jn.langx.util.collection.Collects;
import com.jn.langx.util.reflect.Reflects;

import java.util.List;

public class ForceCloser extends AbstractCloser<Object> {
@Override
public List<Class> applyTo() {
return Collects.<Class>newArrayList(Object.class);
}

@Override
protected void doClose(Object o) throws Throwable {
Reflects.invokeAnyMethodForcedIfPresent(o, "close", null, null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.jn.langx.util.io.close;

import com.jn.langx.util.Preconditions;
import com.jn.langx.util.collection.Collects;
import com.jn.langx.util.function.Consumer;
import com.jn.langx.util.function.Predicate;
import com.jn.langx.util.io.Closer;
import com.jn.langx.util.reflect.Reflects;

import java.util.Map;
import java.util.ServiceLoader;

public class ObjectCloser {
private static Map<Class, Closer> closerMap = Collects.emptyHashMap(true);
private static ForceCloser forceCloser = new ForceCloser();

static {
Collects.forEach(ServiceLoader.load(Closer.class), new Consumer<Closer>() {
@Override
public void accept(final Closer closer) {
register(closer);
}
});
}

public static void register(final Closer closer) {
if (closer != null) {
Collects.forEach(closer.applyTo(), new Consumer<Class>() {
@Override
public void accept(Class aClass) {
closerMap.put(aClass, closer);
}
});
}
}

public static void close(Object obj) {
if (obj == null) {
return;
}
Class type = obj.getClass();
findCloser(type).close(obj);
}

private static Closer findCloser(final Class type) {
Preconditions.checkNotNull(type);
Closer closer = closerMap.get(type);
if (closer == null) {
Class t = Collects.findFirst(closerMap.keySet(), new Predicate<Class>() {
@Override
public boolean test(Class expectClass) {
return Reflects.isSubClassOrEquals(expectClass, type);
}
});

if (t != null) {
closer = closerMap.get(t);
}
}

if (closer == null) {
closer = forceCloser;
}

return closer;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.jn.langx.util.io.close;

import com.jn.langx.util.collection.Collects;

import java.sql.ResultSet;
import java.util.List;

public class ResultSetCloser extends AbstractCloser<ResultSet> {
@Override
protected void doClose(ResultSet resultSet) throws Throwable {
resultSet.close();
}

@Override
public List<Class> applyTo() {
return Collects.<Class>newArrayList(ResultSet.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.jn.langx.util.io.close;

import com.jn.langx.util.collection.Collects;

import java.sql.Statement;
import java.util.List;

public class StatementCloser extends AbstractCloser<Statement>{
@Override
public List<Class> applyTo() {
return Collects.<Class>newArrayList(Statement.class);
}

@Override
protected void doClose(Statement statement) throws Throwable {
statement.close();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
com.jn.langx.util.io.close.CloseableCloser
com.jn.langx.lifecycle.LifecycleCloser
com.jn.langx.lifecycle.DestroyableCloser
com.jn.langx.util.io.close.ConnectionCloser
com.jn.langx.util.io.close.ResultSetCloser
com.jn.langx.util.io.close.StatementCloser
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.jn.langx.java8.util.io;

import com.jn.langx.util.collection.Collects;
import com.jn.langx.util.io.close.AbstractCloser;

import java.util.List;

public class AutoCloseableCloser extends AbstractCloser<AutoCloseable> {
@Override
public List<Class> applyTo() {
return Collects.<Class>newArrayList(AutoCloseable.class);
}

@Override
protected void doClose(AutoCloseable autoCloseable) throws Throwable {
autoCloseable.close();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.jn.langx.java8.util.io.AutoCloseableCloser

0 comments on commit ab58010

Please sign in to comment.