欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

web服务器监控(二) 博客分类: java综合 SwingJava监控 

程序员文章站 2024-03-17 12:25:16
...

接上文,工具类源码

 

ConsoleUtil

 

package com.gary.util;

/**
 * 控制台工具
 * @author gary
 */
public class ConsoleUtil {
    private static LimitQueue<String> queue = new LimitQueue<String>(35);
    
    /**
     * 获取控制台文本
     * @return
     */
    public static String get(){
    	StringBuilder sb = new StringBuilder();
    	for (String str : queue) {
            sb.append(str);
            sb.append("\n");
        }
        return sb.toString();
    }
    
    /**
     * 输出到控制台
     * @param str
     */
    public static void write(String str){
    	queue.offer(TimeUtil.getCurrentTime() + ": " + str);
    }
    
    /**
     * 清空控制台
     */
    public static void clear(){
    	queue.clear();
    }
}

 LimitQueue

 

package com.gary.util;

import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;

/**
 * 固定长度队列
 * @author gary
 *
 */
public class LimitQueue<E> implements Queue<E> {
    //队列长度
    private int limit;
    Queue<E> queue = new LinkedList<E>();

    public LimitQueue(int limit) {
        this.limit = limit;
    }

    /**
     * 入队
     * @param e
     */
    @Override
    public boolean offer(E e) {
        if (queue.size() >= limit) {
            //如果超出长度,入队时,先出队
            queue.poll();
        }
        return queue.offer(e);
    }

    /**
     * 出队
     * @return
     */
    @Override
    public E poll() {
        return queue.poll();
    }

    /**
     * 获取队列
     * @return
     */
    public Queue<E> getQueue() {
        return queue;
    }

    /**
     * 获取限制大小
     * @return
     */
    public int getLimit() {
        return limit;
    }

    @Override
    public boolean add(E e) {
        return queue.add(e);
    }

    @Override
    public E element() {
        return queue.element();
    }

    @Override
    public E peek() {
        return queue.peek();
    }

    @Override
    public boolean isEmpty() {
        return queue.size() == 0 ? true : false;
    }

    @Override
    public int size() {
        return queue.size();
    }

    @Override
    public E remove() {
        return queue.remove();
    }

    @Override
    public boolean addAll(Collection<? extends E> c) {
        return queue.addAll(c);
    }

    @Override
    public void clear() {
        queue.clear();
    }

    @Override
    public boolean contains(Object o) {
        return queue.contains(o);
    }

    @Override
    public boolean containsAll(Collection<?> c) {
        return queue.containsAll(c);
    }

    @Override
    public Iterator<E> iterator() {
        return queue.iterator();
    }

    @Override
    public boolean remove(Object o) {
        return queue.remove(o);
    }

    @Override
    public boolean removeAll(Collection<?> c) {
        return queue.removeAll(c);
    }

    @Override
    public boolean retainAll(Collection<?> c) {
        return queue.retainAll(c);
    }

    @Override
    public Object[] toArray() {
        return queue.toArray();
    }

    @Override
    public <T> T[] toArray(T[] a) {
        return queue.toArray(a);
    }
}

 MailAutherticator

 

package com.gary.util;

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;

/**
 * javamail身份验证
 * @author gary
 */
public class MailAutherticator extends Authenticator {

	private String userName;
	private String password;
	

    public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public MailAutherticator(String userName, String password) {
		super();
		this.userName = userName;
		this.password = password;
	}

	@Override
    public PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(userName, password);
    }
}

 MailUtil

 

package com.gary.util;

import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.Multipart;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

/**
 * 发送E-mail
 * @author gary
 * @version 1.0
 */
public class MailUtil {

    private String smtp = null;
    private String userName = null;
    private String password = null;

    public MailUtil(String smtp, String userName, String password) {
        this.smtp = smtp;
        this.userName = userName;
        this.password = password;
    }
    
    /**
     * 发送简单邮件
     * @param subject
     * @param from
     * @param to
     * @param content
     * @throws Exception 
     */
    public void sendSimpleMail(String from, String[] to, String subject, String content) throws Exception {
        sendMail(subject, from , to, content, null, "text/html;charset=utf-8");
    }

    /**
     * 发送邮件
     * 
     * @param subject
     * 				邮件主题
     * @param from
     * 				邮件发送人
     * 				"a@qq.com"
     * @param to
     * 				邮件接收人(数组),可以有多个接收人即群发
     *				String sendTo[] = { "b@qq.com", "c@qq.com" };
     * @param text	
     * 				邮件内容
     * @param fileNames
     * 				附件详细地址及重命名
     * 				String fileNames[] = { "C:\\a.txt,a.txt", "C:\\b.txt,b.txt" };
     * @param mimeType
     * 				文本类型,如:"text/html;charset=utf-8"
     * @throws Exception
     */
    @SuppressWarnings("static-access")
    public void sendMail(String subject, String from, String[] to, String text, String[] fileNames,
            String mimeType) throws Exception {
        //邮件会话对象
        javax.mail.Session mailSession;
        //MIME邮件对象 
        javax.mail.internet.MimeMessage mimeMsg;

        //获得系统属性对象 
        Properties props = java.lang.System.getProperties();
        //设置SMTP主机
        props.put("mail.smtp.host", smtp);
        //是否到服务器用户名和密码验证
        props.put("mail.smtp.auth", "true");
        //到服务器验证发送的用户名和密码是否正确 
        MailAutherticator myEmailAuther = new MailAutherticator(userName, password);
        //设置邮件会话,将认证信息放进了Session的创建参数里
        mailSession = javax.mail.Session.getInstance(props, (Authenticator) myEmailAuther);
        //设置传输协议 
        javax.mail.Transport transport = mailSession.getTransport("smtp");
        //设置from、to等信息 
        mimeMsg = new javax.mail.internet.MimeMessage(mailSession);
        if (!(from == null || from.equals(""))) {
            InternetAddress sentFrom = new InternetAddress(from);
            //设置发送人地址 
            mimeMsg.setFrom(sentFrom);
        }

        InternetAddress[] sendTo = new InternetAddress[to.length];
        for (int i = 0; i < to.length; i++) {
            sendTo[i] = new InternetAddress(to[i]);
        }

        mimeMsg.setRecipients(javax.mail.internet.MimeMessage.RecipientType.TO, sendTo);
        mimeMsg.setSubject(subject, "utf-8");

        MimeBodyPart messageBodyPart1 = new MimeBodyPart();
        messageBodyPart1.setContent(text, mimeType);

        //附件传输格式 
        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(messageBodyPart1);

        if (fileNames != null) {
            for (int i = 0; i < fileNames.length; i++) {
                MimeBodyPart messageBodyPart2 = new MimeBodyPart();
                //选择出每一个附件名 
                String fileName = fileNames[i].split(",")[0];
                String displayName = fileNames[i].split(",")[1];
                //得到数据源 
                FileDataSource fds = new FileDataSource(fileName);
                //得到附件本身并至入BodyPart 
                messageBodyPart2.setDataHandler(new DataHandler(fds));
                //得到文件名同样至入BodyPart 
                //messageBodyPart2.setFileName(displayname); 
                // messageBodyPart2.setFileName(fds.getName()); 
                messageBodyPart2.setFileName(MimeUtility.encodeText(displayName));
                multipart.addBodyPart(messageBodyPart2);
            }
        }
        mimeMsg.setContent(multipart);
        //设置信件头的发送日期 
        mimeMsg.setSentDate(new Date());
        mimeMsg.saveChanges();
        //发送邮件 
        transport.send(mimeMsg);
        transport.close();
    }

}

 StringUtil

 

package com.gary.util;

/**
 * 字符串工具
 * @author gary
 */
public class StringUtil {

    /**
     * 不为空
     * @param string
     */
    public static boolean isNotBlank(String str) {
        return !isBlank(str);
    }

    /**
     * 为空
     * @param string
     */
    public static boolean isBlank(String str) {
        int strLen;
        if ((str == null) || ((strLen = str.length()) == 0)) {
            return true;
        }
        for (int i = 0; i < strLen; ++i) {
            if (!Character.isWhitespace(str.charAt(i))) {
                return false;
            }
        }
        return true;
    }
}

 SwingUtil

 

package com.gary.util;

import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JDialog;

/**
 * Swing工具
 * @author gary
 */
public class SwingUtil {

    /**
     * 居中显示Dialog
     */
    public static void show(JDialog jd) {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frameSize = jd.getSize();
        if (frameSize.height > screenSize.height) {
            frameSize.height = screenSize.height;
        }
        if (frameSize.width > screenSize.width) {
            frameSize.width = screenSize.width;
        }
        jd.setLocation((screenSize.width - frameSize.width) / 2,
                (screenSize.height - frameSize.height) / 2);
        jd.setModal(true);
        jd.setVisible(true);
    }
    
}

 TimeUtil

 

package com.gary.util;

import java.util.Calendar;

/**
 * 时间工具
 * @author gary
 *
 */
public class TimeUtil {
    /**
     * 获取当前时间
     * @return
     */
    public static String getCurrentTime(){
    	return new java.sql.Timestamp(new java.util.Date().getTime()).toString();
    }
    
    
    /**
     * 返回星期几
     * @author gary
     * @return
     * 		1-星期日
     * 		2-星期一
     * 		3-星期二
     * 		4-星期三
     * 		5-星期四
     * 		6-星期五
     * 		7-星期六
     */
    public static int getDayOfWeek(){
    	Calendar cal = Calendar.getInstance();
    	return cal.get(Calendar.DAY_OF_WEEK);
    }
    
}

 URLUtil

 

package com.gary.util;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
 * URL工具
 * @author gary
 *
 */
public class URLUtil {

    /**
     * 连接
     * @param urlString
     * @throws MalformedURLException
     * @throws IOException 
     */
    public void connect(String urlString) throws MalformedURLException, IOException {
        URL url = new URL(urlString);
        URLConnection c = url.openConnection();
        c.connect();
    }
}
相关标签: Swing Java 监控