IMPLEMENTACIÓN COMPLETA EN 3 NIVELES FÍSICOS DEL CASO DE USO: PEDIR BILLETE package ejsA3N; import java.rmi.*; public interface GestorBilletes extends Remote { public Billete getBillete(String nom) throws RemoteException; } package ejsA3N; import java.util.Date; public class Billete implements java.io.Serializable { private int num; private String nomb; private Date fecha; public Billete(int n,String nom){num=n; nomb=nom; fecha=new Date(); } public int getNum(){return num;} public String getNombre(){return nombre;} public Date getFecha(){return fecha;} } package ejsA3N; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.rmi.*; public class PedirBillete extends JFrame { JPanel jPanel1 = new JPanel(); JLabel jLabel1 = new JLabel(); JTextField jTextField1 = new JTextField(); JButton jButton1 = new JButton(); JTextArea jTextArea1 = new JTextArea(); GestorBilletes gestorBilletes; // Objeto con la lógica del negocio public PedirBillete() { super(); try {jbInit();} catch (Exception e) {e.printStackTrace();}} private void jbInit() throws Exception { this.getContentPane().setLayout(null); this.setSize(new Dimension(400, 300)); jPanel1.setLayout(null); jPanel1.setBounds(new Rectangle(0, 0, 392, 273)); jLabel1.setText("Nombre:"); jLabel1.setBounds(new Rectangle(54, 58, 64, 17)); jTextField1.setBounds(new Rectangle(124, 50, 163, 33)); jButton1.setText("Pedir Billete"); jButton1.setBounds(new Rectangle(103, 189, 156, 44)); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { jButton1_actionPerformed(e);} }); jTextArea1.setBounds(new Rectangle(88, 94, 189, 76)); this.setTitle("Pedir Billetes"); this.getContentPane().add(jPanel1, null); jPanel1.add(jLabel1, null); jPanel1.add(jTextField1, null); jPanel1.add(jButton1, null); jPanel1.add(jTextArea1, null); } public void setGestorBilletes(GestorBilletes g) { gestorBilletes=g; } void jButton1_actionPerformed(ActionEvent e) { try{int res = gestorBilletes.getBillete(jTextField1.getText()).getNum(); if (res<0) jTextArea1.append("Error al asignar billete"); else jTextArea1.append("Asignado. \nReferencia: "+res+"\n"); } catch (Exception er) {System.out.println("Error: "+er.toString()); jTextArea1.append("Error al asignar billete");}} public static void main (String []arg) { PedirBillete b = new PedirBillete(); System.setSecurityManager(new RMISecurityManager()); String servicioRemoto = "rmi://localhost:1099/gestorBilletes”; GestorBilletes objetoRemoto; try{objetoRemoto = (GestorBilletes)Naming.lookup(servicioRemoto); b.setGestorBilletes(objetoRemoto); } catch (Exception e) {System.out.println("Error: "+e.toString());} b.setVisible(true); }} package ejsA3N; import java.rmi.*; import java.sql.*; import java.io.*; import java.rmi.server.UnicastRemoteObject; import java.util.*; public class ServidorGestorBilletesBD extends UnicastRemoteObject implements GestorBilletes { private static Connection conexion; private static Statement sentencia; public ServidorGestorBilletesBD() throws RemoteException{ try {Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); conexion=DriverManager.getConnection("jdbc:odbc:Billetes"); conexion.setAutoCommit(false); sentencia=conexion.createStatement(); } catch(Exception e){System.out.println("Error:"+e.toString());}} public Billete getBillete(String nom) throws RemoteException { // Devuelve billete con nº billete, -1 si no hay, -2 problemas String pregSQL = "SELECT NUMERO FROM BILLETES"+ " WHERE ESTADO=\'LIBRE\'"; try{ ResultSet rs = sentencia.executeQuery(pregSQL); if (rs.next()) { String num = rs.getString("NUMERO"); int act = sentencia.executeUpdate("UPDATE BILLETES"+ " SET ESTADO='OCUPADO', NOMBRE = '"+nom+ "' WHERE NUMERO="+num+" AND ESTADO='LIBRE'"); conexion.commit(); int n= Integer.parseInt(num); if (act>0) return new Billete(n,nom); // N. bill asignado return new Billete(-2,""); } // Otro ha OCUPADO billete else return new Billete(-1,"");; } // No había libre catch (SQLException e){System.out.println(e.toString());} return new Billete(-2,""); } // Que prueben otra vez a llamar public static void main(String[] args) { System.setSecurityManager(new RMISecurityManager()); try { java.rmi.registry.LocateRegistry.createRegistry(numPuerto); } catch (Exception e) {System.out.println(Rmiregistry exist");} try { ServidorGestorBilletesBD objetoServidor = new ServidorGestorBilletesBD(); String servicio="//localhost:1099/gestorBilletes"; Naming.rebind(servicio,objetoServidor); } catch (Exception e){System.out.println(e.toString());}}}