Lanlan
5 years ago
37 changed files with 400 additions and 88 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,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(); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,30 @@ |
|||||||
|
package com.fr.start; |
||||||
|
|
||||||
|
import junit.framework.TestCase; |
||||||
|
import org.easymock.EasyMock; |
||||||
|
import org.junit.Assert; |
||||||
|
import org.junit.runner.RunWith; |
||||||
|
import org.powermock.api.easymock.PowerMock; |
||||||
|
import org.powermock.core.classloader.annotations.PrepareForTest; |
||||||
|
import org.powermock.modules.junit4.PowerMockRunner; |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* @author hades |
||||||
|
* @version 10.0 |
||||||
|
* Created by hades on 2020/3/10 |
||||||
|
*/ |
||||||
|
@RunWith(PowerMockRunner.class) |
||||||
|
@PrepareForTest({DesignerJavaRuntime.class}) |
||||||
|
public class DesignerJavaRuntimeTest extends TestCase { |
||||||
|
|
||||||
|
public void testIsInValidVmOptions() { |
||||||
|
PowerMock.mockStatic(DesignerJavaRuntime.class); |
||||||
|
DesignerJavaRuntime designerJavaRuntime = PowerMock.createPartialMock(DesignerJavaRuntime.class, "isInstallVersion", "getJvmOptions"); |
||||||
|
String[] options = new String[]{"-Dfile.encoding=UTF-8", "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8000"}; |
||||||
|
EasyMock.expect(designerJavaRuntime.getJvmOptions()).andReturn(options).anyTimes(); |
||||||
|
EasyMock.replay(designerJavaRuntime); |
||||||
|
PowerMock.replay(DesignerJavaRuntime.class); |
||||||
|
Assert.assertTrue(designerJavaRuntime.isInValidVmOptions()); |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue