Problema com acesso a registro de outra classe

1 resposta
V

Pessoal,

Tenho uma classe chessGame que grava alguns dados no registro:

package br.com.cpqd.www;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Gauge;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.ImageItem;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreNotFoundException;

public class chessGame extends MIDlet implements CommandListener {

	private Display display;
	
	private Form presentation; 
	private Command exit; 
	private Command menu; 
	
	private List menuList;
	private Command select;	
	private Command backMenu;
	
	private RecordStore rs = null;
	static final String REC_STORE = "registro";	
	
	private Form createPlayer;
	private Form createGame;
	private Form search;
	private Form search1;
	private Form conf;	
	
	private Command game;
	private Command register;
	private Command save;
	private Command opcao;
	private Command gameView;
	private Command view;
	private Command challenge;	
	
	private TextBox helpText;
	private TextBox creditText;	
	
	private TextField nickName;
	private TextField name;
	private TextField country;
	private TextField email;	
	
	private Gauge volume;		
	
	private RecordStore myRecordStore;
	
	private Image image;
	
	private ImageItem imgItem;
	
	private Alert allTest;
	
	private ChoiceGroup teste;
	private ChoiceGroup teste1;		
	
	private int choiceGroupIndex;	
	
	public chessGame() {

		display = Display.getDisplay(this); 
		
		exit  = new Command("Sair", Command.EXIT, 1); 
		menu = new Command("Menu Principal", Command.SCREEN, 1); 
		
		presentation = new Form("Jogo de Xadrez");
		presentation.addCommand(exit);
		presentation.addCommand(menu);
		presentation.setCommandListener(this);
		
		backMenu = new Command("Voltar", Command.BACK, 1); // botão para voltar ao menu principal
		game = new Command("Jogar", Command.OK, 1); // botão para inicia o jogo
		select = new Command("Selecionar", Command.OK, 1);	
		
		try {
			openRecStore();
			String nickName = new String(rs.getRecord(1));
			String gameKey = new String(rs.getRecord(2));
			System.out.println(nickName);
			System.out.println(gameKey);
			if (nickName.equals("0") && gameKey.equals("0")) {
				String options[] = {"Cadastro"};
				menuList = new List("Menu Principal", List.IMPLICIT, options, null);
				menuList.addCommand(select);
				menuList.addCommand(backMenu);
				menuList.setCommandListener(this);
			} else if (nickName != "0" && gameKey.equals("0")){
				String options[] = {
					"Remover Cadastro",
					"Pesquisa",
					"Classificação",
					"Configuração",
					"Ajuda"
				};
				menuList = new List("Menu Principal", List.IMPLICIT, options, null);
				menuList.addCommand(select);
				menuList.addCommand(backMenu);
				menuList.setCommandListener(this);
			} else if (nickName != "0" && gameKey != "0") {
				String options[] = {
						"Entrar",
						"Classificação",
						"Configuração",
						"Ajuda"
					};
				menuList = new List("Menu Principal", List.IMPLICIT, options, null);
				menuList.addCommand(select);
				menuList.addCommand(backMenu);
				menuList.setCommandListener(this);
			}
			closeRecStore();
		} catch (Exception e) {
			db(e.toString());
		}		
		
		register = new Command("Cadastrar", Command.OK, 1);
		save = new Command("Salvar", Command.OK, 1);
		opcao = new Command("Opções", Command.OK, 1);
		challenge = new Command("Desafiar", Command.OK, 1);
		gameView = new Command("Ver Jogo", Command.OK, 1);
		view = new Command("Ver", Command.OK, 1);
		
		volume = new Gauge("Volume do Som", true, 30, 4);			

		helpText = new TextBox("Ajuda", "Texto de Ajuda...", 25, 0);
		helpText.addCommand(backMenu);		
		helpText.setCommandListener(this);

		creditText = new TextBox("Pesquisa", "Você foi desafiado pelo Jogador X!", 100, 0);
		creditText.addCommand(backMenu);
		creditText.addCommand(game);
		creditText.setCommandListener(this);

		createPlayer = new Form("Cadastro");
		nickName = new TextField("Apelido: ", "", 20, TextField.ANY);
		name = new TextField("Nome Completo: ", "", 20, TextField.ANY);
		country = new TextField("País de Nascimento: ", "", 20, TextField.ANY);
		email = new TextField("E-mail: ", "", 20, TextField.ANY);
		createPlayer.addCommand(register);
		createPlayer.addCommand(backMenu);
		createPlayer.append(nickName);
		createPlayer.append(name);
		createPlayer.append(country);
		createPlayer.append(email);
		createPlayer.setCommandListener(this);	

		conf = new Form("Configurações");
		conf.append(volume);
		conf.addCommand(backMenu);
		conf.addCommand(save);
		conf.setCommandListener(this);	

		//createGame = new Form("Criar Jogo");
		//nickName = new TextField("Apelido: ", "", 10, TextField.ANY);
		//name = new TextField("Nome: ", "", 10, TextField.ANY);
		//country = new TextField("País: ", "", 10, TextField.ANY);
		//email = new TextField("E-mail: ", "", 10, TextField.ANY);
		//createPlayer.addCommand(register);
		//createPlayer.addCommand(back);
		//createPlayer.append(nickName);
		//createPlayer.append(name);
		//createPlayer.append(country);
		//createPlayer.append(email);
		//createPlayer.setCommandListener(this);

		teste = new ChoiceGroup("Jogadores disponíveis:", Choice.EXCLUSIVE);
		teste.append("Jogador X", null);
		teste.append("Jogador Y", null);
		teste.append("Jogador W", null);
		teste.append("Jogador Z", null);
		search = new Form("Opções");
		choiceGroupIndex = search.append(teste);
		search.addCommand(backMenu);
		search.addCommand(challenge);
		search.addCommand(gameView);
		search.setCommandListener(this);

		teste1 = new ChoiceGroup("Jogos disponíveis:", Choice.EXCLUSIVE);
		teste1.append("Jogo 1", null);
		teste1.append("Jogo 2", null);
		teste1.append("Jogo 3", null);
		teste1.append("Jogo 4", null);
		search1 = new Form("Ver Jogo");
		choiceGroupIndex = search1.append(teste1);
		search1.addCommand(backMenu);
		search1.addCommand(view);
		search1.setCommandListener(this);
		
	}

	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
		
	}

	protected void pauseApp() {
		
	}

	protected void startApp() throws MIDletStateChangeException {
		
		display.setCurrent(presentation); // carrega a tela de apresentação do jogo		
		
		try {
			
			//try {
				
			//	RecordStore.deleteRecordStore(REC_STORE);	
				
	        //} catch(RecordStoreNotFoundException e) { }	        
			openRecStore();	        
			if (rs.getNumRecords() < 2) {				
				writeNickNameDefault("0");
				writeGameKeyDefault("0");								
			}
			System.out.println(rs.getNumRecords());
			closeRecStore();			
					
		} catch (Exception e) {
			db(e.toString());
		}	

	}
	
	public String stateVerify() {		
		String state = null;				
		try {
			openRecStore();
			String nickName = readNickName();
			String gameKey = readGameKey(); 			
			if (nickName.equals("0") && gameKey.equals("0")) {
				state = "Anonimo";
			} else if (nickName != "0" && gameKey.equals("0")){
				state = "Repouso";
			} else if (nickName != "0" && gameKey != "0") {
				state = "Jogando";
			}
			closeRecStore();
		} catch (SecurityException e) {
			db(e.toString());
		} return state;		
	}

	public void openRecStore() {
		
		try {
			
			rs = RecordStore.openRecordStore(REC_STORE, true);	
			
		} catch (Exception e) {
			e.toString();
			
		}	
		
	}

	public void closeRecStore() {
		
		try {
			
			rs.closeRecordStore();
			
		} catch (Exception e) {
			db(e.toString());
			
		}	
		
	}

	public void deleteRecStore() {
		
		if (RecordStore.listRecordStores() != null) {
			try {
				
				RecordStore.deleteRecordStore(REC_STORE);
			} catch (Exception e) {
				db(e.toString());
				
			}
			
		}
		
	}

	public void writeNickNameDefault(String nickname) {
		
		byte[] rec = nickname.getBytes();
		
		try {
			
			rs.addRecord(rec, 0, rec.length);
			
		} catch (Exception e) {
			db(e.toString());
			
		}
		
	}

	public void writeGameKeyDefault(String gameKey) {
		
		byte[] rec = gameKey.getBytes();		
		try {
			
			rs.addRecord(rec, 0, rec.length);
			
		} catch (Exception e) {
			db(e.toString());
			
		}
		
	}
	
	
	public void writeNickName(String nickname) {
		
		byte[] rec = nickname.getBytes();
		try {
			
			rs.setRecord(1, rec, 0, rec.length);
			
		} catch (Exception e) {
			db(e.toString());
			
		}
		
	}

	public void writeGameKey(String gameKey) {
		
		byte[] rec = gameKey.getBytes();		
		try {
			
			rs.setRecord(2, rec, 0, rec.length);
			
		} catch (Exception e) {
			
			db(e.toString());
		}
		
	}
	

	public String readNickName() {		
		
		String nickName = null;
		try {
			
			nickName = new String(rs.getRecord(1));
			
		} catch (Exception e) {			
			e.toString();
			
		} 			
		return nickName; 
		
	}

	
	public String readGameKey() {

		String gameKey = null;
		try {
			
			gameKey = new String(rs.getRecord(2));
			
		} catch (Exception e) {
			
			e.toString();
		} 		
		return gameKey;

	}

	private void db(String str) {
		
		System.err.println("Msg: " + str);
		
	}



	public void commandAction(Command arg0, Displayable arg1) {
		
		try {

			if (arg0 == exit) {
				//try {
					//System.out.println(new String(rs.getRecord(1)));
					//System.out.println(new String(rs.getRecord(2)));
					
				//} catch (Exception e) {
			//		e.toString();
			//	}
				destroyApp(false);
				notifyDestroyed();
			}		
			else if (arg0 == menu){
				display.setCurrent(menuList);
			}		
			else if (arg0 == backMenu) {				
				display.setCurrent(presentation);
			}		
			else if (arg0 == select){			
				if (menuList.getSelectedIndex() == 0) {				
					display.setCurrent(createPlayer);			
				}			
				if (menuList.getSelectedIndex() == 1) {				
					display.setCurrent(search);				
				}			
				if (menuList.getSelectedIndex() == 2) {				
					display.setCurrent(conf);				
				}
				if (menuList.getSelectedIndex() == 3) {	
					display.setCurrent(creditText);	
				}
				if (menuList.getSelectedIndex() == 4) {	
					display.setCurrent(helpText);	
				}			
			}	
			else if (arg0 == gameView){
				display.setCurrent(search1);			
			}		
			else if (arg0 == register){
				stateControl t1 = new stateControl("teste");				
				t1.getRegisterData(nickName.getString(), name.getString(), country.getString(), email.getString());
				t1.start();
				try {

					String data1 = nickName.getString();
					openRecStore();
					writeNickName(data1);
					readNickName();
					closeRecStore();

				} catch (Exception e) {
					
					db(e.toString());
					
				}
			}

		} catch (MIDletStateChangeException excep){

		}
		
	}
	
}

E a classe stateControl:

package br.com.cpqd.www;

import java.rmi.RemoteException;

import br.com.cpqd.www.wsdl.chess.ChessPort_Stub;

public class stateControl extends Thread {
	
	String nickName;
	String name;
	String country;
	String email;
	
	public stateControl(String name) {		
		super(name);
	}
	
	public void getRegisterData(String nickName, String name, String country, String email){
		this.nickName = nickName;
		this.name = name;
		this.country = country;
		this.email = email;
	}	
		
	public void run() {
		while(true) {
			chessGame reg = new chessGame();
			if(reg.stateVerify().equals("Anonimo")) {				
				try{				
					ChessPort_Stub stub = new ChessPort_Stub();	
					stub.createPlayer(this.nickName, this.name, this.country, this.email);					
				} catch (RemoteException rse) { 
					rse.printStackTrace(); 
				}			
			}	
		}
	}

}

Quando rodo dá esse erro:
Uncaught exception java/lang/SecurityException: Application not authorized to access the restricted API.

1 Resposta

kamyu2881

O SecurityException ocorre no momento que vc dá um

Porque a API não permite que classe que herde MIDlet seja instaciada manualmente

Criado 17 de setembro de 2006
Ultima resposta 18 de set. de 2006
Respostas 1
Participantes 2