forked from fanruan/design
Maksim
5 years ago
18 changed files with 313 additions and 93 deletions
@ -0,0 +1,59 @@
|
||||
package com.fr.common.detect; |
||||
|
||||
import com.fr.concurrent.NamedThreadFactory; |
||||
import com.fr.design.DesignerEnvManager; |
||||
import com.fr.log.FineLoggerFactory; |
||||
import com.fr.module.ModuleContext; |
||||
import com.fr.web.WebSocketConfig; |
||||
|
||||
import java.net.Socket; |
||||
import java.util.concurrent.ExecutorService; |
||||
|
||||
/** |
||||
* @author hades |
||||
* @version 10.0 |
||||
* Created by hades on 2020/3/10 |
||||
*/ |
||||
public class CommonPortDetector { |
||||
|
||||
private static final CommonPortDetector INSTANCE = new CommonPortDetector(); |
||||
private ExecutorService service = ModuleContext.getExecutor().newSingleThreadExecutor(new NamedThreadFactory("CommonPortDetector")); |
||||
|
||||
public static CommonPortDetector getInstance() { |
||||
return INSTANCE; |
||||
} |
||||
|
||||
public void execute() { |
||||
service.submit(new Runnable() { |
||||
@Override |
||||
public void run() { |
||||
detectTomcatPort(); |
||||
detectWebSocketPort(); |
||||
} |
||||
}); |
||||
} |
||||
|
||||
private void detectTomcatPort() { |
||||
int port = DesignerEnvManager.getEnvManager().getEmbedServerPort(); |
||||
if (checkPort(port)) { |
||||
FineLoggerFactory.getLogger().error("EmbedTomcat Port: {} is not available, maybe occupied by other programs, please check it!", port); |
||||
} |
||||
} |
||||
|
||||
private void detectWebSocketPort() { |
||||
Integer[] ports = WebSocketConfig.getInstance().getPort(); |
||||
for (int port : ports) { |
||||
if (checkPort(port)) { |
||||
FineLoggerFactory.getLogger().error("WebSocKet Port: {} is not available, maybe occupied by other programs, please check it!", port); |
||||
} |
||||
} |
||||
} |
||||
|
||||
private boolean checkPort(int port) { |
||||
try (Socket socket = new Socket("localhost", port)) { |
||||
return true; |
||||
} catch (Exception e) { |
||||
return false; |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,41 @@
|
||||
package com.fr.design.os.impl; |
||||
|
||||
import com.fr.design.mainframe.DesignerFrame; |
||||
import com.fr.invoke.Reflect; |
||||
import com.fr.log.FineLoggerFactory; |
||||
import com.fr.stable.os.support.OSBasedAction; |
||||
|
||||
import java.lang.reflect.InvocationHandler; |
||||
import java.lang.reflect.Method; |
||||
import java.lang.reflect.Proxy; |
||||
|
||||
/** |
||||
* @author hades |
||||
* @version 10.0 |
||||
* Created by hades on 2020/3/13 |
||||
*/ |
||||
public class MacOsAddListenerAction implements OSBasedAction { |
||||
|
||||
@Override |
||||
public void execute(final Object... objects) { |
||||
try { |
||||
Class app = Class.forName("com.apple.eawt.Application"); |
||||
Class handler = Class.forName("com.apple.eawt.QuitHandler"); |
||||
Object instance = Proxy.newProxyInstance(handler.getClassLoader(), new Class[]{handler}, |
||||
new InvocationHandler() { |
||||
@Override |
||||
public Object invoke(Object proxy, Method method, |
||||
Object[] args) throws Throwable { |
||||
if ("handleQuitRequestWith".equals(method.getName())) { |
||||
DesignerFrame designerFrame = (DesignerFrame) objects[0]; |
||||
designerFrame.exit(); |
||||
} |
||||
return null; |
||||
} |
||||
}); |
||||
Reflect.on(Reflect.on(app).call("getApplication").get()).call("setQuitHandler", instance); |
||||
} catch (ClassNotFoundException e) { |
||||
FineLoggerFactory.getLogger().error(e.getMessage(), e); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,45 @@
|
||||
package com.fr.common.detect; |
||||
|
||||
import com.fr.invoke.Reflect; |
||||
import com.fr.log.FineLoggerFactory; |
||||
import junit.framework.TestCase; |
||||
import org.junit.Assert; |
||||
|
||||
import java.io.IOException; |
||||
import java.net.ServerSocket; |
||||
|
||||
/** |
||||
* @author hades |
||||
* @version 10.0 |
||||
* Created by hades on 2020/3/10 |
||||
*/ |
||||
public class CommonPortDetectorTest extends TestCase { |
||||
|
||||
private ServerSocket serverSocket; |
||||
|
||||
@Override |
||||
public void setUp() throws Exception { |
||||
serverSocket = new ServerSocket(55555); |
||||
new Thread(new Runnable() { |
||||
@Override |
||||
public void run() { |
||||
try { |
||||
serverSocket.accept(); |
||||
} catch (IOException e) { |
||||
FineLoggerFactory.getLogger().error(e.getMessage(), e); |
||||
} |
||||
} |
||||
}).start(); |
||||
} |
||||
|
||||
public void testCheckPort() { |
||||
CommonPortDetector detector = CommonPortDetector.getInstance(); |
||||
boolean access = Reflect.on(detector).call("checkPort", 55555).get(); |
||||
Assert.assertTrue(access); |
||||
} |
||||
|
||||
@Override |
||||
public void tearDown() throws Exception { |
||||
serverSocket.close(); |
||||
} |
||||
} |
Loading…
Reference in new issue