venerdì 8 giugno 2012

Java: riportare in Reflection-Mode una serie di operazioni

import com.ulcjava.container.servlet.client.ConnectorCommandException;


....


    if ((reason instanceof ConnectorException) && (reason.getCause() instanceof ConnectorCommandException) ) {
        ConnectorCommandException cause = (ConnectorCommandException) reason.getCause();
        if (cause.getResponseInfo().getResponseCode() == 500) {
        JOptionPane.showMessageDialog(getOwner(), "La sessione non è più attiva.","La sessione non è più attiva.", JOptionPane.ERROR_MESSAGE);
        return;
        }
       
    }




-------------------- Con Reflection ----------------
Nota...senza import!!

try {
        Class connector_command_exception = Class.forName(CONNECTOR_COMMAND_EXCEPTION);

        if ((reason instanceof ConnectorException) && (connector_command_exception.isInstance(reason.getCause())) ) {
        Object causeInstance = reason.getCause();
        Method responseInfoMethod = causeInstance.getClass().getMethod("getResponseInfo");
        Object responseCodeInstance = responseInfoMethod.invoke(causeInstance, new Object[]{});
        Method responseCodeMethod = responseCodeInstance.getClass().getMethod("getResponseCode");
        int cause=((Integer)responseCodeMethod.invoke(responseCodeInstance, new Object[]{})).intValue();
        if (cause == 500) {
            JOptionPane.showMessageDialog(getOwner(), "Sessione utente terminata.","La sessione utente è terminata.", JOptionPane.ERROR_MESSAGE);
            return;
        }
        }
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    }

lunedì 4 giugno 2012

Installare SDK android su ubuntu 12.04

1) Sono partito da un'installazione pulita di eclipse (la indigo)
  • la cartella l'ho scompattata nella mia home
2) installare android sdk
  • scaricare http://developer.android.com/sdk/index.html
  • ho scompattato la cartella nella mia home
  • nella cartella /tools/ ho lanciato ./android
3) installare ADT plugin for Eclipse
  • Start Eclipse, then select Help > Install New Software....
  • Click Add, in the top-right corner.
  • In the Add Repository dialog that appears, enter "ADT Plugin" for the Name and the following URL for the Location: https://dl-ssl.google.com/android/eclipse/

mercoledì 14 marzo 2012

Creare directory in JAVA

Per creare una o più directory, es. temporanea, in java bisogna fare un giochetto:

 private  File createTempDirectories(String prefix) throws Error {
    File parentDir;
    try {
        parentDir = File.createTempFile(prefix, "");
        parentDir.delete();
        System.out.println("result mkdir di " + parentDir.getAbsolutePath() + ":" +   parentDir.mkdir());   

        File child = new File(parentDir, "it/ibttn/test.txt");
        System.out.println("result mkdirs di " + child.getAbsolutePath() + ":" + child.mkdirs());
    } catch (IOException e) {
        throw new Error("Failed to create temporary directory: " + e);
    }
    return parentDir;
    }



Per eliminare + directory c'è bisogno di un metodo ricorsivo:

  public boolean deleteDirectories(File path) {
        if( path.exists() ) {
          for (File file : path.listFiles()) {
             if(file.isDirectory())
                deleteDirectories(file);
                else
                file.delete();
          }
        }
        return( path.delete() );
      }
   

giovedì 1 marzo 2012

Leggere file in progetti JAVA

HelpDialog.class.getResourceAsStream("/directory/nomefile")

Retrieving Resources from JAR files

// Get current classloader
   ClassLoader cl = this.getClass().getClassLoader();
   // Create icons
   Icon saveIcon  = new ImageIcon(cl.getResource("images/save.gif"));

mercoledì 25 gennaio 2012

Da Runtime.exec() a ProcessBuilder

Il Runtime.exec chiama direttamente la shell o il processo cmd.exe. Dalla JDK 5 invece è possibile eseguire un comando in un processo separato del sistema operativo, con la classe ProcessBuilder. Vedi http://docs.oracle.com/javase/6/docs/api/java/lang/ProcessBuilder.html

venerdì 9 dicembre 2011

Eclipse, Windows e la JDK



Sotto windows, per forzare eclipse ad utilizzare la JDK anzichè la JRE bisogna modificare il file eclipse.ini ed aggiungere la seguente riga:

 -vm
C:\Program Files\Java\jdk1.6.0_29\bin\javaw


Nell'esempio sto utilizzando una jdk 1.6.0_29


Di seguito un esempio completo del file:

-startup
plugins/org.eclipse.equinox.launcher_1.2.0.v20110502.jar
--launcher.library
plugins/org.eclipse.equinox.launcher.win32.win32.x86_1.1.100.v20110502
-product
org.eclipse.epp.package.jee.product
--launcher.defaultAction
openFile
--launcher.XXMaxPermSize
256M
-showsplash
org.eclipse.platform
--launcher.XXMaxPermSize
256m
--launcher.defaultAction
openFile
-vm
C:\Program Files\Java\jdk1.6.0_29\bin\javaw
-vmargs
-Dosgi.requiredJavaVersion=1.5
-Xms40m
-Xmx512m

martedì 25 ottobre 2011

Utenti in tomcat

Su Ubuntu, per configurare l'accesso alla sezione web di Tomcat, bisogna editare il file tomcat-users.xml nella cartella /conf