segunda-feira, setembro 26, 2005

NC Persistência Java Local


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;

import org.prevayler.Prevayler;
import org.prevayler.PrevaylerFactory;
import org.prevayler.Transaction;

class Pessoa implements Serializable {
public static final long serialVersionUID = 1L;

private int iCpf = 0;
private String strNome = "ninguém";
private byte bytIdade = 0;

Pessoa(String strNomeTemp) {
iCpf = 0;
strNome = strNomeTemp;
bytIdade = 0;
}

public byte getBytIdade() {
return bytIdade;
}

public void setBytIdade(byte bytIdade) {
this.bytIdade = bytIdade;
}

public int getICpf() {
return iCpf;
}

public void setICpf(int cpf) {
iCpf = cpf;
}

public String getStrNome() {
return strNome;
}

public void setStrNome(String strNome) {
this.strNome = strNome;
}
}

class ListaPessoas implements Serializable {
public static final long serialVersionUID = 1L;

private ArrayList arrPessoa = new ArrayList();

public void add(Pessoa opessoaTemp) {
arrPessoa.add(opessoaTemp);
}

public Pessoa get(int iIdTemp) {
return (Pessoa) arrPessoa.get(iIdTemp);
}

public void remove(int iIdTemp) {
arrPessoa.remove(iIdTemp);
}

public int size() {
return arrPessoa.size();
}
}

class AdicionaPessoa implements Transaction {
private static final long serialVersionUID = 2L;

private String nome;

public AdicionaPessoa(String nome) {
super();
this.nome = nome;
}

public void executeOn(Object system, Date arg1) {
((ListaPessoas)system).add(new Pessoa(nome));
}
}

class SnapshotTimer extends Thread {

private boolean emExecucao = true;
private Prevayler prevayler;

public SnapshotTimer(Prevayler prevayler) {
this.prevayler = prevayler;
}

public void run() {
super.run();

try {
while (emExecucao) {
Thread.sleep(1000 * 60 * 10);
prevayler.takeSnapshot();
}
} catch (InterruptedException e) {
} catch (IOException e) {
e.printStackTrace();
}
}

public void setemExcucao(boolean b_Temp) {
emExecucao = b_Temp;
}
}

public class Teste {

public static void main(String[] args) {
System.out.println("Iniciando Prevayler...");

Prevayler prevayler = null;
SnapshotTimer snapshot = null;

try {
prevayler = getPrevayler();
snapshot = new SnapshotTimer(prevayler);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}

snapshot.start();
System.out.print("Digite o nome da pessoa ou FIM para sair: ");
String nome = lerTeclado();

while (!nome.equals("FIM")) {
try {
prevayler.execute(new AdicionaPessoa(nome));
} catch (Exception e1) {
e1.printStackTrace();
}

System.out.println("Pessoa armazenada.\n");
System.out.print("Digite o nome da pessoa ou FIM para sair: ");

nome = lerTeclado();
}

snapshot.setemExcucao(false);
if (snapshot.isAlive()) snapshot.interrupt();

System.out.println("\n Imprimindo pessoas persistidas.\n");
ListaPessoas lista = ((ListaPessoas)prevayler.prevalentSystem());
for (int i=0; i System.out.println(lista.get(i).getStrNome());
}

}

public static String lerTeclado() {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
try {
return reader.readLine();
} catch (IOException e1) {
return "FIM";
}
}


public static Prevayler getPrevayler() throws IOException, ClassNotFoundException {
PrevaylerFactory factory = new PrevaylerFactory();
factory.configurePrevalentSystem(new ListaPessoas());
factory.configurePrevalenceBase("Base");
System.out.println("Instanciando Prevayler normal");
return factory.create();
}

}


This page is powered by Blogger. Isn't yours?