标签:
package patterndemo;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
interface DAO
{
public Connection GetConn(String url) throws SQLException;
public void InsertData();
public void GetData();
public void DeleteData();
}
class myops implements DAO
{
@Override
public void InsertData() {
System.out.println("inserting data...");
}
@Override
public void GetData() {
System.out.println("getting data...");
}
@Override
public void DeleteData() {
System.out.println("deleting data...");
}
@Override
public Connection GetConn(String url) throws SQLException {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection conn = null;
conn = DriverManager.getConnection(
"jdbc:oracle:thin:@hostname:1521:xe",
"user",
"pwd");
return conn;
}
}
class dynamicalproxy implements InvocationHandler
{
Object proxied;
Connection conn;
public dynamicalproxy(Object _proxied,Connection _conn)
{
proxied = _proxied;
conn=_conn;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
conn.setAutoCommit(false);
System.out.println("doing something before what ...");
try
{
method.invoke(proxied, args);
}
catch(Exception ex)
{
conn.rollback();
ex.printStackTrace();
}
finally
{
conn.commit();
System.out.println("commit tran ...");
}
System.out.println("doing something after what ...");
return null;
}
}
public class proxydemo {
public static void main(String[] args) throws IllegalArgumentException, SQLException
{
myops test = new myops();
DAO dao =(DAO) Proxy.newProxyInstance(DAO.class.getClassLoader(),
new Class[]{DAO.class},
new dynamicalproxy(test,test.GetConn(""))
);
dao.InsertData();
dao.GetData();
dao.DeleteData();
}
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
class StreamConsumer extends Thread {
InputStream is;
String type;
StreamConsumer (InputStream is, String type) {
this.is = is;
this.type = type;
}
public void run () {
try {
InputStreamReader isr = new InputStreamReader (is);
BufferedReader br = new BufferedReader (isr);
String line = br.readLine();
while (
line != null
&&
!line.startsWith(type)
)
{
System.out.println (type + ">" + line);
line =br.readLine();
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
public class helloworld {
static File createTempJar(String root) throws IOException {
if (!new File(root).exists()) {
return null;
}
Manifest manifest = new Manifest();
//generate a running jar
manifest.getMainAttributes().putValue("Manifest-Version", "1.0");
manifest.getMainAttributes().putValue("Class-Path", ".");
manifest.getMainAttributes().putValue("Main-Class", helloworld.class.getName());
final File jarFile = File.createTempFile("EJob-", ".jar", new File(System.getProperty("java.io.tmpdir")));
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
jarFile.delete();
}
});
JarOutputStream out = new JarOutputStream(new FileOutputStream(jarFile),manifest);
createTempJarInner(out, new File(root), "");
out.flush();
out.close();
return jarFile;
}
static void createTempJarInner(JarOutputStream out, File f,String base) throws IOException {
if (f.isDirectory()) {
base = base.length()>0?base + "/":base;
for(File _file :f.listFiles())
createTempJarInner(out,_file, base + _file.getName());
} else {
out.putNextEntry(new JarEntry(base));
FileInputStream in = new FileInputStream(f);
byte[] buffer = new byte[1024];
int n = in.read(buffer);
while (n != -1) {
out.write(buffer, 0, n);
n = in.read(buffer);
}
out.closeEntry();
in.close();
}
}
public static void main(String[] args)
{
args=System.getProperty("java.io.tmpdir").split(" ");
for(String arg:args)
System.out.println(arg);
System.out.println("hello world!");
try {
File jarfile = createTempJar("bin");
if(!jarfile.exists()) System.out.print("jar file is not exists");
else
{
String mycmd ="java -jar " + jarfile.toString();
//"java -cp .;" + jarfile.toString() ;
// + " " + helloworld.class.getName();
System.out.println("command:"+ mycmd);
Process proc = Runtime.getRuntime().exec(mycmd);
StreamConsumer sc = new StreamConsumer(proc.getInputStream(),"output");
StreamConsumer scerror = new StreamConsumer(proc.getErrorStream(),"error");
sc.start();
scerror.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
标签:
原文地址:http://www.cnblogs.com/huaxiaoyao/p/4303906.html