📄 mbenjamin.java
字号:
package net.jumperz.app.MBenjamin;
import java.net.*;
import java.io.*;
import java.util.*;
import net.jumperz.util.*;
import net.jumperz.app.MBenjamin.command.*;
public class MBenjamin
implements MObserver1
{
private static final String CONTROL = "control";
private static final String COMMAND = "command";
private static MBenjamin instance;
private File configureDir;
private long timeOut;
private int threadCount;
private MThreadPool threadPool;
private List commandList;
private String smtp;
private String from;
private String to;
private int bindPort;
private ServerSocket sSocket;
//-------------------------------------------------------------------------------------
public static void main( String[] args )
throws IOException
{
if( args.length != 1 )
{
System.out.println( "Usage: java net.jumperz.app.MBenjamin.MBenjamin CONFIGURE_DIR" );
return;
}
else
{
instance = new MBenjamin( args[ 0 ] );
instance.start();
}
}
//-------------------------------------------------------------------------------------
public static MBenjamin getInstance()
{
return instance;
}
//-------------------------------------------------------------------------------------
public MBenjamin( String configureDirName )
throws IOException
{
MStandardLogger.getInstance().addStream( System.out );
commandList = new ArrayList();
loadConfig( configureDirName );
}
//-------------------------------------------------------------------------------------
public synchronized void start()
throws IOException
{
bind();
threadPool = new MThreadPool( threadCount );
MTimer timer = new MTimer( timeOut, 1 );
timer.register1( instance );
threadPool.addCommand( timer );
int count = commandList.size();
for( int i = 0; i < count; ++i )
{
MCommand command = ( MCommand )commandList.get( i );
MSubject1 subject = ( MSubject1 )commandList.get( i );
MBenjaminObserver observer = new MBenjaminObserver( command );
subject.register1( observer );
threadPool.addCommand( command );
}
}
//-------------------------------------------------------------------------------------
private void bind()
throws IOException
{
sSocket = new ServerSocket( bindPort, 1, InetAddress.getByName( "127.0.0.1" ) );
}
//-------------------------------------------------------------------------------------
private void loadConfig( String configureDirName )
throws IOException
{
checkDir( configureDirName );
loadControl();
loadCommands();
}
//-------------------------------------------------------------------------------------
private void loadControl()
throws IOException
{
Properties prop = new Properties();
prop.load( new FileInputStream( configureDir.getCanonicalPath() + "/" + CONTROL ) );
timeOut = Long.parseLong( prop.getProperty( "timeOut" ) );
threadCount = Integer.parseInt( prop.getProperty( "threadCount" ) );
smtp = prop.getProperty( "smtp" );
from = prop.getProperty( "from" );
to = prop.getProperty( "to" );
bindPort = Integer.parseInt( prop.getProperty( "bindPort" ) );
}
//-------------------------------------------------------------------------------------
private void checkDir( String configureDirName )
throws IOException
{
configureDir = new File( configureDirName );
if( !configureDir.exists() )
{
throw new IOException( configureDirName + " does not exist." );
}
if( !configureDir.isDirectory() )
{
throw new IOException( configureDirName + " is not a directory." );
}
}
//-------------------------------------------------------------------------------------
public void update()
{
if( commandList.size() > 0 )
{
System.out.println( commandList );
try
{
sendMail();
}
catch( Exception e )
{
e.printStackTrace();
}
}
exit();
}
//-------------------------------------------------------------------------------------
private void sendMail()
throws IOException
{
Socket socket = new Socket( smtp, 25 );
InputStream in = socket.getInputStream();
OutputStream out = socket.getOutputStream();
BufferedReader reader = new BufferedReader( new InputStreamReader( in, MCharset.CS_ISO_8859_1 ) );
reader.readLine(); // 220 hoge
out.write( "HELO localhost\r\n".getBytes( MCharset.CS_ISO_8859_1 ) );
reader.readLine(); // 250 hoge
out.write( ( "MAIL FROM:<" + from + ">\r\n" ).getBytes( MCharset.CS_ISO_8859_1 ) );
reader.readLine(); // 250 hoge
out.write( ( "RCPT TO:<" + to + ">\r\n" ).getBytes( MCharset.CS_ISO_8859_1 ) );
reader.readLine(); // 250 hoge
out.write( "DATA\r\n".getBytes( MCharset.CS_ISO_8859_1 ) );
reader.readLine(); // 354 hoge
out.write( "Subject: Alert from Benjamin@JUMPERZ.NET\r\n".getBytes( MCharset.CS_ISO_8859_1 ) );
out.write( ( "From: " + from + "\r\n" ).getBytes( MCharset.CS_ISO_8859_1 ) );
out.write( ( "To: " + to + "\r\n\r\n" ).getBytes( MCharset.CS_ISO_8859_1 ) );
out.write( commandList.toString().getBytes( MCharset.CS_ISO_8859_1 ) );
out.write( "\r\n.\r\n".getBytes( MCharset.CS_ISO_8859_1 ) );
reader.readLine(); // 250 hoge
out.write( "QUIT\r\n".getBytes( MCharset.CS_ISO_8859_1 ) );
reader.readLine(); // 221 hoge
socket.close();
}
//-------------------------------------------------------------------------------------
public synchronized void removeCommand( MCommand command )
{
commandList.remove( command );
if( commandList.size() == 0 )
{
System.out.println( "AOK :)" );
}
}
//-------------------------------------------------------------------------------------
private void exit()
{
try
{
sSocket.close();
}
catch( IOException e )
{
}
threadPool.stop();
}
//-------------------------------------------------------------------------------------
private void loadCommands()
throws IOException
{
BufferedReader reader = new BufferedReader( new InputStreamReader( new FileInputStream( configureDir.getCanonicalPath() + "/" + COMMAND ), MCharset.CS_ISO_8859_1 ) );
String line = null;
while( true )
{
line = reader.readLine();
if( line == null )
{
break;
}
if( !MStringUtil.isComment( line ) )
{
String[] array = line.split( "," );
if( array[ 0 ].equals( "HTTP" ) )
{
if( array.length != 3 )
{
throw new IOException( "Syntax error: " + line );
}
commandList.add( new MBenjaminHttpCommand( array[ 1 ], array[ 2 ] ) );
}
else if( array[ 0 ].equals( "HTTPS" ) )
{
if( array.length != 3 )
{
throw new IOException( "Syntax error: " + line );
}
commandList.add( new MBenjaminHttpsCommand( array[ 1 ], array[ 2 ] ) );
}
else if( array[ 0 ].equals( "TCP" ) )
{
if( array.length != 4 )
{
throw new IOException( "Syntax error: " + line );
}
commandList.add( new MBenjaminTcpCommand( array[ 1 ], Integer.parseInt( array[ 2 ] ), array[ 3 ] ) );
}
}
}
}
//-------------------------------------------------------------------------------------
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -