|
Cám ơn bạn đã chia sẻ ý tưởng, và tôi đã thử như ý tưởng của bạn nhưng javascript vẫn tiếp tục chạy các bước tiếp theo, điều đó có nghĩa applet đã chạy xong ngay lúc đấy, và hàm markDone đã trả về giá trị true.
Vậy có nghĩa là applet của tôi có vấn đề. Tôi xin mô tả applet của tôi hy vọng bạn có thể nhìn ra điều gì đó không bình thường:
applet chính là SmartCardSignerApplet:
public static String inData;
public static int error = 0;
public static boolean con = false;
public JFrame f;
private boolean isDone;
public static void setInData(String inData) {
SmartCardSignerApplet.inData = inData;
}
public void setCon() {
con = false;
}
public boolean isCon() {
return con;
}
public void markDone(boolean b) {
isDone = b;
}
public synchronized void sign(final String inData) {
markDone(false);
AccessController.doPrivileged(new PrivilegedAction() {
@Override
public synchronized Object run() {
try {
setInData(inData);
VNPT_CA_Certificate choisecert = new VNPT_CA_Certificate();
choisecert.ListCert_Initialize();
//
} catch (Exception ex) {
ex.printStackTrace();
}
markDone(true);
return null;
}
});
while (isDone == false) {
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
Logger.getLogger(SmartCardSignerApplet.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public int getError() {
return error;
}
public String getCert() {
String cert = null;
try {
cert = Base64Utils.base64Encode(VNPT_CA_Library.vecCert.get(VNPT_CA_Certificate.idChoise - 1).getEncoded());
} catch (CertificateEncodingException ex) {
Logger.getLogger(SmartCardSignerApplet.class.getName()).log(Level.SEVERE, null, ex);
}
return cert;
}
public String getSignedData() {
String signedData = null;
signedData = Base64Utils.base64Encode(VNPT_CA_Certificate.outData);
return signedData;
}
}
Từ class trên tôi gọi ra một form:
public class VNPT_CA_Certificate extends JDialog {
private JFrame frame_Sign;
private String[] headerName = {"Cấp cho", "Cấp bởi", "Ngày cấp", "Ngày hết hạn"};
public int status = 0;
public static int idChoise = -1;
private JTable table;
private JScrollPane scroll;
private JButton btnSign, btnView, btnCancel;
private JTextField txtCer, txtSigned;
private JLabel lbCer, lbSigned, lbTop, lbBot;
private int w, h;
private JPanel p1, p2, p3, pTop, pBot;
private VNPT_CA_Library libStore;
public static byte[] outData;
private Base64Utils baseUtils = new Base64Utils();
public static boolean flag = false;
public synchronized void ListCert_Initialize() {
try {
frame_Sign = new JFrame("VNPT CA - Chương trình ứng dụng PKI");
frame_Sign.setVisible(true);
frame_Sign.setLayout(new GridLayout(4, 1));
frame_Sign.setSize(800, 370);
frame_Sign.setResizable(false);
w = frame_Sign.getWidth();
h = frame_Sign.getHeight();
//
pTop = new JPanel();
pBot = new JPanel();
p1 = new JPanel();
p2 = new JPanel();
p3 = new JPanel();
frame_Sign.add(pTop);
frame_Sign.add(p1);
frame_Sign.add(p2);
frame_Sign.add(pBot);
pTop.setBackground(Color.white);
p1.setBackground(Color.white);
p2.setBackground(Color.white);
p3.setBackground(Color.white);
pBot.setBackground(Color.white);
//Get list certificate
libStore = new VNPT_CA_Library();
int iCheck = 0;
iCheck = libStore.VNPT_CA_Initialize();
if (iCheck != 0) {
SmartCardSignerApplet.error = iCheck;
// quitApplet();
}
Vector<X509Certificate> Certificates = new Vector<X509Certificate>();
iCheck = libStore.VNPT_CA_ListCertificate(Certificates);
if (iCheck != 0) {
SmartCardSignerApplet.error = iCheck;
// quitApplet();
}
idChoise = 0;
// Top Image
lbTop = new JLabel();
lbTop.setIcon(new ImageIcon(getClass().getResource("/img/top.jpg")));
pTop.add(lbTop);
// Bot Image
lbBot = new JLabel();
lbBot.setIcon(new ImageIcon(getClass().getResource("/img/bot.jpg")));
pBot.add(lbBot);
//
String abc = new String();
X509Certificate CertBuf;
int iList = Certificates.size();
Object[][] data = new Object[iList][4];
for (int i = 0; i < iList; i++) {
CertBuf = Certificates.get(i);
data[i][0] = baseUtils.tachChuoi(CertBuf.getSubjectDN().getName());
data[i][1] = baseUtils.tachChuoi(CertBuf.getIssuerDN().getName());
data[i][2] = Base64Utils.formatDate(CertBuf.getNotBefore());
data[i][3] = Base64Utils.formatDate(CertBuf.getNotAfter());
}
table = new JTable(data, headerName);
table.setVisible(true);
table.setPreferredScrollableViewportSize(new Dimension(600, 60));
p1.add(table);
//
for (int i = 0; i < 4; i++) {
alignRight(table, i);
}
//
scroll = new JScrollPane(table);
table.setFillsViewportHeight(true);
p1.add(scroll);
// button
btnSign = new JButton("Ký");
btnSign.setLocation(w / 6, h - 50);
p2.add(btnSign);
btnSign.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
idChoise = table.getSelectedRow() + 1;
if (idChoise <= 0) {
idChoise = 1;
}
//Signature
//VNPT_CA_Library.pinCode = JOptionPane.showInputDialog(null, "Enter the PIN code:", "PIN code", 1);
JLabel jPassword = new JLabel("Enter the PIN code:");
JTextField password = new JPasswordField();
Object[] ob = {jPassword, password};
int result = JOptionPane.showConfirmDialog(null, ob, "PIN code", JOptionPane.OK_CANCEL_OPTION);
if (result == JOptionPane.OK_OPTION) {
VNPT_CA_Library.pinCode = password.getText();
}
if (VNPT_CA_Library.pinCode != null) {
String isData = SmartCardSignerApplet.inData;
byte[] a1 = Base64Utils.toByteArray(isData);
int len = a1.length;
outData = new byte[256];
int iCheck = libStore.VNPT_CA_SignData(idChoise, len, a1, outData);
if (iCheck != 0) {
SmartCardSignerApplet.error = iCheck;
}
SmartCardSignerApplet.con = true;
}
flag = true;
}
});
// button
btnView = new JButton("Xem");
btnView.setLocation(w / 6, h - 50);
p2.add(btnView);
btnView.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
SmartCardSignerApplet.con = true;
quitApplet();
}
});
// button
btnCancel = new JButton("Thoát");
btnCancel.setLocation(w / 6, h - 50);
p2.add(btnCancel);
btnCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
SmartCardSignerApplet.con = true;
quitApplet();
}
});
frame_Sign.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
Form này có tác dụng hiển thị các thông tin cho người dùng biết và xử lý các sự kiện của người dùng. Class này sẽ dùng các hàm trong 1 class thư viện mà tôi không thể post lên đây được nhưng tôi nghĩ vẫn đề không phải ở thư viện, chỉ là 1 trong 2 class mà tôi đưa ra.
|