|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Member
Iscritto dal: Jul 2005
Messaggi: 291
|
[JAVA] Passarsi un Class tramite Object(Out/In)putStream
Salve, avrei bisogno di passare un oggetto di tipo Class tra un server e un client. ho provato prima di tutto a fare un esempio banale
Codice:
import java.net.*;
import java.io.*;
public class TestClassLoader{
public static void main(String[] args) throws Exception{
final String className=args[0];
ServerSocket ss=new ServerSocket(333);
Thread t1=new Thread(new Runnable(){
public void run(){
try{
Class<?> cc=Class.forName(className);
System.out.println("CLIENT:"+cc.toString());
Socket s=new Socket("localhost",333);
ObjectOutputStream oos=new ObjectOutputStream(s.getOutputStream());
System.out.println("CLIENT:ObjectOutputStream aperto");
oos.writeObject(cc);
oos.flush();
System.out.println("CLIENT:Oggetto mandato");
oos.close();
s.close();
}catch(Exception ex) {System.out.println("NONONO "+ex.toString());}
}
});
t1.start();
Socket s=ss.accept();
ObjectInputStream ois=new ObjectInputStream(s.getInputStream());
System.out.println("SERVER:ObjectInputStream aperto");
Class<?> received=(Class<?>)ois.readObject();
ois.close();
s.close();
ss.close();
System.out.println("SERVER:Classe ricevuta, nome:"+received.getName());
}
}
Codice:
java.rmi.UnmarshalException: error unmarshalling return Codice:
private Class<?> transferFromRemote(String name,InetAddress addr) throws ClassNotFoundException{
try {
Socket s = new Socket(addr, 666);
PrintWriter pw = new PrintWriter(s.getOutputStream());
pw.println(name); //trasferisco il nome della classe
pw.flush();
InputStreamReader isr=new InputStreamReader(s.getInputStream());
BufferedReader br=new BufferedReader(isr);
if (!br.readLine().equals("SI")){ //MODIFICATO
br.close();
isr.close();
s.close();
System.out.println("il Client non conosce la classe");
throw new ClassNotFoundException(name);
}
System.out.println("Il Client conosce la classe");
ObjectInputStream ois=new ObjectInputStream(s.getInputStream());
System.out.println("ObjectInputStream creato");
Class<?> ret=(Class<?>) ois.readObject();
System.out.println("Classe ricevuta, addio");
pw.close();
br.close();
isr.close();
ois.close();
s.close();
return ret;
} catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
throw new ClassNotFoundException(name);
}
}
Codice:
public void run(){
try {
System.out.println("Richiesta Classe ricevuta");
InputStreamReader isr = new InputStreamReader(this.s.getInputStream());
BufferedReader br = new BufferedReader(isr);
String nomeClasse = br.readLine(); //mi prendo il nome della classe
System.out.println("Nome classe Ricevuto (" + nomeClasse + ")");
PrintWriter pw=new PrintWriter(s.getOutputStream());
try{
Class<?> toSend=Class.forName(nomeClasse);
pw.println("SI");
pw.flush();
System.out.println("Conosco la calsse, la mando");
ObjectOutputStream oos=new ObjectOutputStream(s.getOutputStream());
System.out.println("ObjectOutputStream creato, classe ("+toSend.getName()+")");
oos.writeObject(toSend);
oos.flush();
System.out.println("Classe mandata, chiudo tutto");
oos.close();
pw.close();
br.close();
isr.close();
s.close();
} catch (ClassNotFoundException ex){
pw.println("NO");
pw.flush();
pw.close();
br.close();
isr.close();
s.close();
}
} catch (IOException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
}
Altra cosa che non mi spiego è il java.rmi.UnmarshalException , il codice del server è messo dentro un ClassLoader moddato che dovrebbe caricare la classe non locale (e correttamente settato come ClassLoader) e il metodo che andrebbe a chiamare il classloader (indirettamente off course) è chiamato tramite RMI, qualche lume?
__________________
CPU: Intel Core 2 Quad Q6600 - Mobo: Asus P5E - RAM:4x2GB DDR2 - sk video: Power Color ATI Radeon HD3870 - HD:Western Digital 750GB |
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Dec 2005
Messaggi: 7258
|
gli oggetti che passi devono implementare l'interfaccia Serializable e quindi essere serializzabili
immagino sia questo il problema |
|
|
|
|
|
#3 |
|
Member
Iscritto dal: Jul 2005
Messaggi: 291
|
Class, come riporta javadoc, implementa l'interfaccia Serializable, e poi non si spiegherebbe perchè col programma di prova funziona
__________________
CPU: Intel Core 2 Quad Q6600 - Mobo: Asus P5E - RAM:4x2GB DDR2 - sk video: Power Color ATI Radeon HD3870 - HD:Western Digital 750GB |
|
|
|
|
|
#4 |
|
Senior Member
Iscritto dal: Dec 2005
Messaggi: 7258
|
qual'è esattamente la riga di codice che genera l'eccezione?
è proprio oos.writeObject(toSend); ?
|
|
|
|
|
|
#5 | |
|
Member
Iscritto dal: Jul 2005
Messaggi: 291
|
Quote:
Codice:
Class<?> ret=(Class<?>) ois.readObject();
__________________
CPU: Intel Core 2 Quad Q6600 - Mobo: Asus P5E - RAM:4x2GB DDR2 - sk video: Power Color ATI Radeon HD3870 - HD:Western Digital 750GB |
|
|
|
|
|
|
#6 |
|
Senior Member
Iscritto dal: Jul 2002
Città: Reggio Calabria -> London
Messaggi: 12112
|
così ad occhio, dato che stai usando java > 5, direi che non hai impostato correttamente la codebase del server.
Poteva anche essere che non avevi compilato gli stub, ma da java 5 in poi vengono generati automaticamente quindi il problema è quello che ho appena scritto Dai un occhio alla configurazione del server (o registro o come cavolo si chiama che non mi ricordo
__________________
|
|
|
|
|
|
#7 |
|
Member
Iscritto dal: Jul 2005
Messaggi: 291
|
Anche se non prettamente legato all'inizio del thread lo ripropongo perchè il problema da risolvere è lo stesso.
Un'altra idea che mi è venuta in mente è da parte Client esportare un metodo (RMI) Codice:
public byte[] getServerUnknowClass(String className) throws RemoteException,ClassNotFoundException {
try {
System.out.println("Il server non conosce la classe " + className);
Class<?> classe = Class.forName(className);
System.out.println("Io conosco la classe");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(classe);
oos.flush();
oos.close();
byte[] ret = baos.toByteArray();
System.out.println("Generato l'array di bytes contenente la def della classe, mando");
return ret;
} catch (IOException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
throw new ClassNotFoundException(className);
}
}
Codice:
public Class<?> findClass(String name) throws ClassNotFoundException{
{
try {
System.out.println("Non conosco la classe " + name + ", la chiedo al client");
byte[] classeDaClient = this.client.getServerUnknowClass(name);
System.out.println("Il client mi ha dato il flusso di bytes");
ByteArrayInputStream bais = new ByteArrayInputStream(classeDaClient);
ObjectInputStream ois = new ObjectInputStream(bais);
Class<?> ret = defineClass(name, classeDaClient, 0, classeDaClient.length);;
System.out.println("Classe acquisita");
ois.close();
bais.close();
System.out.println("Classe ricevuta dal client");
return ret;
} catch (RemoteException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
throw new ClassNotFoundException(name);
}catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
throw new ClassNotFoundException(name);
}
}
}
Però facendo un piccolo programma di esempio Codice:
public class TestClassLoader{
public static void main(String[] args) throws Exception{
String nomeClasse=args[0];
String nomeFile=args[1];
Class<?> cc=Class.forName(nomeClasse);
ByteArrayOutputStream baos=new ByteArrayOutputStream();
ObjectOutputStream oos=new ObjectOutputStream(baos);
oos.writeObject(cc);
oos.close();
baos.close();
byte[] byteDaBaos=baos.toByteArray();
FileInputStream fis=new FileInputStream(nomeFile);
List<Byte> listaBytes=new ArrayList<Byte>();
int read=fis.read();
while (read!=-1){
listaBytes.add(new Byte((byte)read));
read=fis.read();
}
fis.close();
Object[] byteDaFile=listaBytes.toArray();
System.out.println("Bytes dalla classe\n"+Arrays.toString(byteDaBaos));
System.out.println("Bytes dal file\n"+Arrays.toString(byteDaFile));
}
}
Codice:
Bytes dalla classe [-84, -19, 0, 5, 118, 114, 0, 9, 88, 109, 108, 80, 97, 114, 115, 101, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 120, 112] Bytes dal file [-54, -2, -70, -66, 0, 0, 0, 50, 0, 120, 10, 0, 29, 0, 51, 10, 0, 52, 0, 53, 10, 0, 52, 0, 54, 10, 0, 55, 0, 56, 9, 0, 22, 0, 57, 7, 0, 58, 10, 0, 6, 0, 59, 11, 0, 60, 0, 61, 8, 0, 62, 10, 0, 22, 0, 63, 7, 0, 64, 8, 0, 65, 10, 0, 11, 0, 66, 8, 0, 67, 8, 0, 68, 11, 0, 69, 0, 70, 11, 0, 69, 0, 71, 10, 0, 72, 0, 73, 11, 0, 69, 0, 74, 11, 0, 75, 0, 76, 11, 0, 75, 0, 77, 7, 0, 78, 7, 0, 79, 10, 0, 23, 0, 66, 10, 0, 22, 0, 80, 9, 0, 81, 0, 82, 10, 0, 22, 0, 83, 10, 0, 84, 0, 85, 7, 0, 86, 1, 0, 8, 100, 111, 99, 117, 109, 101, 110, 116, 1, 0, 22, 76, 111, 114, 103, 47, 119, 51, 99, 47, 100, 111, 109, 47, 68, 111, 99, 117, 109, 101, 110, 116, 59, 1, 0, 6, 60, 105, 110, 105, 116, 62, 1, 0, 17, 40, 76, 106, 97, 118, 97, 47, 105, 111, 47, 70, 105, 108, 101, 59, 41, 86, 1, 0, 4, 67, 111, 100, 101, 1, 0, 15, 76, 105, 110, 101, 78, 117, 109, 98, 101, 114, 84, 97, 98, 108, 101, 1, 0, 13, 83, 116, 97, 99, 107, 77, 97, 112, 84, 97, 98, 108, 101, 7, 0, 78, 7, 0, 79, 7, 0, 58, 1, 0, 16, 103, 101, 116, 78, 97, 109, 105, 110, 103, 83, 101, 114, 118, 105, 99, 101, 1, 0, 20, 40, 41, 76, 106, 97, 118, 97, 47, 108, 97, 110, 103, 47, 83, 116, 114, 105, 110, 103, 59, 7, 0, 87, 7, 0, 88, 1, 0, 5, 103, 101, 116, 78, 83, 1, 0, 56, 40, 76, 111, 114, 103, 47, 119, 51, 99, 47, 100, 111, 109, 47, 78, 111, 100, 101, 59, 76, 106, 97, 118, 97, 47, 108, 97, 110, 103, 47, 83, 116, 114, 105, 110, 103, 59, 41, 76, 111, 114, 103, 47, 119, 51, 99, 47, 100, 111, 109, 47, 78, 111, 100, 101, 59, 7, 0, 89, 1, 0, 4, 109, 97, 105, 110, 1, 0, 22, 40, 91, 76, 106, 97, 118, 97, 47, 108, 97, 110, 103, 47, 83, 116, 114, 105, 110, 103, 59, 41, 86, 1, 0, 10, 83, 111, 117, 114, 99, 101, 70, 105, 108, 101, 1, 0, 14, 88, 109, 108, 80, 97, 114, 115, 101, 114, 46, 106, 97, 118, 97, 12, 0, 32, 0, 90, 7, 0, 91, 12, 0, 92, 0, 93, 12, 0, 94, 0, 95, 7, 0, 96, 12, 0, 97, 0, 98, 12, 0, 30, 0, 31, 1, 0, 19, 106, 97, 118, 97, 47, 108, 97, 110, 103, 47, 69, 120, 99, 101, 112, 116, 105, 111, 110, 12, 0, 99, 0, 90, 7, 0, 100, 12, 0, 101, 0, 102, 1, 0, 6, 67, 111, 110, 102, 105, 103, 12, 0, 44, 0, 45, 1, 0, 26, 106, 97, 118, 97, 47, 108, 97, 110, 103, 47, 82, 117, 110, 116, 105, 109, 101, 69, 120, 99, 101, 112, 116, 105, 111, 110, 1, 0, 18, 78, 111, 110, 32, 116, 114, 111, 118, 111, 32, 67, 111, 110, 102, 105, 103, 33, 33, 12, 0, 32, 0, 103, 1, 0, 13, 78, 97, 109, 105, 110, 103, 83, 101, 114, 118, 105, 99, 101, 1, 0, 25, 78, 111, 110, 32, 116, 114, 111, 118, 111, 32, 110, 97, 109, 105, 110, 103, 83, 101, 114, 118, 105, 99, 101, 33, 33, 7, 0, 88, 12, 0, 104, 0, 41, 12, 0, 105, 0, 41, 7, 0, 106, 12, 0, 107, 0, 108, 12, 0, 109, 0, 110, 7, 0, 89, 12, 0, 111, 0, 112, 12, 0, 113, 0, 114, 1, 0, 9, 88, 109, 108, 80, 97, 114, 115, 101, 114, 1, 0, 12, 106, 97, 118, 97, 47, 105, 111, 47, 70, 105, 108, 101, 12, 0, 32, 0, 33, 7, 0, 115, 12, 0, 116, 0, 117, 12, 0, 40, 0, 41, 7, 0, 118, 12, 0, 119, 0, 103, 1, 0, 16, 106, 97, 118, 97, 47, 108, 97, 110, 103, 47, 79, 98, 106, 101, 99, 116, 1, 0, 19, 111, 114, 103, 47, 119, 51, 99, 47, 100, 111, 109, 47, 69, 108, 101, 109, 101, 110, 116, 1, 0, 16, 111, 114, 103, 47, 119, 51, 99, 47, 100, 111, 109, 47, 78, 111, 100, 101, 1, 0, 20, 111, 114, 103, 47, 119, 51, 99, 47, 100, 111, 109, 47, 78, 111, 100, 101, 76, 105, 115, 116, 1, 0, 3, 40, 41, 86, 1, 0, 40, 106, 97, 118, 97, 120, 47, 120, 109, 108, 47, 112, 97, 114, 115, 101, 114, 115, 47, 68, 111, 99, 117, 109, 101, 110, 116, 66, 117, 105, 108, 100, 101, 114, 70, 97, 99, 116, 111, 114, 121, 1, 0, 11, 110, 101, 119, 73, 110, 115, 116, 97, 110, 99, 101, 1, 0, 44, 40, 41, 76, 106, 97, 118, 97, 120, 47, 120, 109, 108, 47, 112, 97, 114, 115, 101, 114, 115, 47, 68, 111, 99, 117, 109, 101, 110, 116, 66, 117, 105, 108, 100, 101, 114, 70, 97, 99, 116, 111, 114, 121, 59, 1, 0, 18, 110, 101, 119, 68, 111, 99, 117, 109, 101, 110, 116, 66, 117, 105, 108, 100, 101, 114, 1, 0, 37, 40, 41, 76, 106, 97, 118, 97, 120, 47, 120, 109, 108, 47, 112, 97, 114, 115, 101, 114, 115, 47, 68, 111, 99, 117, 109, 101, 110, 116, 66, 117, 105, 108, 100, 101, 114, 59, 1, 0, 33, 106, 97, 118, 97, 120, 47, 120, 109, 108, 47, 112, 97, 114, 115, 101, 114, 115, 47, 68, 111, 99, 117, 109, 101, 110, 116, 66, 117, 105, 108, 100, 101, 114, 1, 0, 5, 112, 97, 114, 115, 101, 1, 0, 38, 40, 76, 106, 97, 118, 97, 47, 105, 111, 47, 70, 105, 108, 101, 59, 41, 76, 111, 114, 103, 47, 119, 51, 99, 47, 100, 111, 109, 47, 68, 111, 99, 117, 109, 101, 110, 116, 59, 1, 0, 15, 112, 114, 105, 110, 116, 83, 116, 97, 99, 107, 84, 114, 97, 99, 101, 1, 0, 20, 111, 114, 103, 47, 119, 51, 99, 47, 100, 111, 109, 47, 68, 111, 99, 117, 109, 101, 110, 116, 1, 0, 18, 103, 101, 116, 68, 111, 99, 117, 109, 101, 110, 116, 69, 108, 101, 109, 101, 110, 116, 1, 0, 23, 40, 41, 76, 111, 114, 103, 47, 119, 51, 99, 47, 100, 111, 109, 47, 69, 108, 101, 109, 101, 110, 116, 59, 1, 0, 21, 40, 76, 106, 97, 118, 97, 47, 108, 97, 110, 103, 47, 83, 116, 114, 105, 110, 103, 59, 41, 86, 1, 0, 14, 103, 101, 116, 84, 101, 120, 116, 67, 111, 110, 116, 101, 110, 116, 1, 0, 11, 103, 101, 116, 78, 111, 100, 101, 78, 97, 109, 101, 1, 0, 16, 106, 97, 118, 97, 47, 108, 97, 110, 103, 47, 83, 116, 114, 105, 110, 103, 1, 0, 6, 101, 113, 117, 97, 108, 115, 1, 0, 21, 40, 76, 106, 97, 118, 97, 47, 108, 97, 110, 103, 47, 79, 98, 106, 101, 99, 116, 59, 41, 90, 1, 0, 13, 103, 101, 116, 67, 104, 105, 108, 100, 78, 111, 100, 101, 115, 1, 0, 24, 40, 41, 76, 111, 114, 103, 47, 119, 51, 99, 47, 100, 111, 109, 47, 78, 111, 100, 101, 76, 105, 115, 116, 59, 1, 0, 9, 103, 101, 116, 76, 101, 110, 103, 116, 104, 1, 0, 3, 40, 41, 73, 1, 0, 4, 105, 116, 101, 109, 1, 0, 21, 40, 73, 41, 76, 111, 114, 103, 47, 119, 51, 99, 47, 100, 111, 109, 47, 78, 111, 100, 101, 59, 1, 0, 16, 106, 97, 118, 97, 47, 108, 97, 110, 103, 47, 83, 121, 115, 116, 101, 109, 1, 0, 3, 111, 117, 116, 1, 0, 21, 76, 106, 97, 118, 97, 47, 105, 111, 47, 80, 114, 105, 110, 116, 83, 116, 114, 101, 97, 109, 59, 1, 0, 19, 106, 97, 118, 97, 47, 105, 111, 47, 80, 114, 105, 110, 116, 83, 116, 114, 101, 97, 109, 1, 0, 7, 112, 114, 105, 110, 116, 108, 110, 0, 33, 0, 22, 0, 29, 0, 0, 0, 1, 0, 2, 0, 30, 0, 31, 0, 0, 0, 4, 0, 1, 0, 32, 0, 33, 0, 1, 0, 34, 0, 0, 0, 118, 0, 3, 0, 5, 0, 0, 0, 33, 42, -73, 0, 1, -72, 0, 2, 77, 44, -74, 0, 3, 78, 42, 45, 43, -74, 0, 4, -75, 0, 5, -89, 0, 10, 58, 4, 25, 4, -74, 0, 7, -79, 0, 1, 0, 4, 0, 22, 0, 25, 0, 6, 0, 2, 0, 35, 0, 0, 0, 34, 0, 8, 0, 0, 0, 8, 0, 4, 0, 12, 0, 8, 0, 13, 0, 13, 0, 14, 0, 22, 0, 18, 0, 25, 0, 15, 0, 27, 0, 16, 0, 32, 0, 19, 0, 36, 0, 0, 0, 19, 0, 2, -1, 0, 25, 0, 2, 7, 0, 37, 7, 0, 38, 0, 1, 7, 0, 39, 6, 0, 1, 0, 40, 0, 41, 0, 1, 0, 34, 0, 0, 0, 126, 0, 3, 0, 4, 0, 0, 0, 59, 42, -76, 0, 5, -71, 0, 8, 1, 0, 76, 43, 18, 9, -72, 0, 10, 77, 44, -57, 0, 13, -69, 0, 11, 89, 18, 12, -73, 0, 13, -65, 44, 18, 14, -72, 0, 10, 78, 45, -57, 0, 13, -69, 0, 11, 89, 18, 15, -73, 0, 13, -65, 45, -71, 0, 16, 1, 0, -80, 0, 0, 0, 2, 0, 35, 0, 0, 0, 26, 0, 6, 0, 0, 0, 29, 0, 10, 0, 30, 0, 17, 0, 31, 0, 31, 0, 32, 0, 38, 0, 33, 0, 52, 0, 34, 0, 36, 0, 0, 0, 17, 0, 2, -3, 0, 31, 7, 0, 42, 7, 0, 43, -4, 0, 20, 7, 0, 43, 0, 10, 0, 44, 0, 45, 0, 1, 0, 34, 0, 0, 0, -92, 0, 2, 0, 5, 0, 0, 0, 83, 42, -57, 0, 5, 1, -80, 42, -71, 0, 17, 1, 0, 43, -74, 0, 18, -103, 0, 5, 42, -80, 42, -71, 0, 19, 1, 0, 77, 3, 62, 29, 44, -71, 0, 20, 1, 0, -94, 0, 44, 44, 29, -71, 0, 21, 2, 0, 43, -72, 0, 10, 58, 4, 25, 4, -58, 0, 20, 25, 4, -71, 0, 17, 1, 0, 43, -74, 0, 18, -103, 0, 6, 25, 4, -80, -124, 3, 1, -89, -1, -48, 1, -80, 0, 0, 0, 2, 0, 35, 0, 0, 0, 42, 0, 10, 0, 0, 0, 38, 0, 6, 0, 39, 0, 19, 0, 40, 0, 21, 0, 42, 0, 28, 0, 43, 0, 40, 0, 44, 0, 53, 0, 45, 0, 72, 0, 46, 0, 75, 0, 43, 0, 81, 0, 49, 0, 36, 0, 0, 0, 15, 0, 5, 6, 14, -3, 0, 8, 7, 0, 46, 1, 44, -6, 0, 5, 0, 9, 0, 47, 0, 48, 0, 1, 0, 34, 0, 0, 0, 61, 0, 6, 0, 2, 0, 0, 0, 29, -69, 0, 22, 89, -69, 0, 23, 89, 42, 3, 50, -73, 0, 24, -73, 0, 25, 76, -78, 0, 26, 43, -74, 0, 27, -74, 0, 28, -79, 0, 0, 0, 1, 0, 35, 0, 0, 0, 14, 0, 3, 0, 0, 0, 54, 0, 18, 0, 55, 0, 28, 0, 56, 0, 1, 0, 49, 0, 0, 0, 2, 0, 50] Non c'è magari un metodo che dato il nome di una classe mi ritorni il file .class cui è definita?????
__________________
CPU: Intel Core 2 Quad Q6600 - Mobo: Asus P5E - RAM:4x2GB DDR2 - sk video: Power Color ATI Radeon HD3870 - HD:Western Digital 750GB Ultima modifica di morskott : 25-02-2008 alle 20:53. |
|
|
|
|
|
#8 |
|
Junior Member
Iscritto dal: Mar 2008
Messaggi: 1
|
ciao, volevo chiederti se avevi trovato una soluzione per il tuo caso. io ho un problema simile. devo instanziare una classe (usando il defineClass) partendo da un'array di byte che recupero da un file xml, ma lancia un'eccezione del tipo: "java.lang.ClassFormatError: Incompatible magic value 1668246830 in class file MiaClasse"
|
|
|
|
|
|
#9 |
|
Member
Iscritto dal: Jul 2005
Messaggi: 291
|
Il problema l'ho risolto passando dal server al client la string del nome del file, poi il client cerca il file contenente quella classe (sostituisco ad ogni punto della stringa "\" e ci postpongo ".class" e cerco questo file nella cartella corrente o nel classpath), lo apre e passa al server i bytes del file, il server manda il tutto in pasto al defineClass()
Codice:
public byte[] getServerUnknowClass(String className) throws RemoteException,ClassNotFoundException {
try {
System.out.println("Richiesta della classe:"+className);
String path=sostituisci(className,".",File.separator)+".class";
System.out.println("Nome del file class:"+path);
File f=new File(path);
if (f.exists()){
System.out.println("Il file esiste, lo mando");
RandomAccessFile raf=new RandomAccessFile(f,"r");
byte[] ret=new byte[(int)raf.length()];
raf.read(ret);
System.out.println("Il flusso di bytes è\n"+Arrays.toString(ret));
raf.close();
System.out.println("Mando tutto");
return ret;
}else{
System.out.println("Il file non esiste, cerco nel classpath");
String classPath=System.getProperty("CLASSPATH");
StringTokenizer st=new StringTokenizer(classPath,File.pathSeparator);
while (st.hasMoreTokens()){
File theFile=new File(st.nextToken()+File.separator+path);
System.out.println("Provo il file:"+theFile.getPath());
if (theFile.exists()){
System.out.println("Il file esiste, lo mando");
RandomAccessFile raf=new RandomAccessFile(f,"r");
byte[] ret=new byte[(int)raf.length()];
raf.read(ret);
System.out.println("Il flusso di bytes è\n"+Arrays.toString(ret));
raf.close();
return ret;
}
}
}
System.out.println("Non trovo la classe");
throw new ClassNotFoundException(className);
} catch (IOException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
throw new ClassNotFoundException(className);
}
}
private String sostituisci(String className, String string, String separator) {
StringTokenizer st=new StringTokenizer(className,string);
String ret=st.nextToken();
while (st.hasMoreTokens()){
ret+=separator+st.nextToken();
}
return ret;
}
Codice:
public Class<?> findClass(String name) throws ClassNotFoundException{
{
try {
System.out.println("Non conosco la classe " + name + ", la chiedo al client");
byte[] classeDaClient = this.client.getServerUnknowClass(name);
System.out.println("Il client mi ha dato il flusso di bytes\n"+Arrays.toString(classeDaClient));
Class<?> ret = defineClass(name, classeDaClient, 0, classeDaClient.length);
System.out.println("Classe acquisita");
return ret;
} catch (RemoteException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
throw new ClassNotFoundException(name);
}catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
throw new ClassNotFoundException(name);
}
}
}
__________________
CPU: Intel Core 2 Quad Q6600 - Mobo: Asus P5E - RAM:4x2GB DDR2 - sk video: Power Color ATI Radeon HD3870 - HD:Western Digital 750GB |
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 15:39.




















