środa, 18 czerwca 2014

Get all classes from JAR file - java

Because, sometimes it is not so easy to find a solution of the problem in the Internet I decided to put below a short function which can help in listing all classes from JAR file:

 public List<String> getAllClassesInJarFile(String fileName) {  
     JarFile jarFile;  
     ArrayList<String> list = new ArrayList<String>();  
     try {  
       jarFile = new JarFile(fileName);  
       Enumeration<JarEntry> enums = jarFile.entries();  
       while (enums.hasMoreElements()) {  
         JarEntry jarEntry = enums.nextElement();  
         String entryName = jarEntry.getName();  
         if (!jarEntry.isDirectory() && entryName.endsWith(".class")) {  
           StringBuilder className = new StringBuilder();  
           for (String part : jarEntry.getName().split("/")) {  
             if (className.length() != 0)  
               className.append(".");  
             className.append(part);  
             if (part.endsWith(".class"))  
               className.setLength(className.length()  
                   - ".class".length());  
           }  
           list.add(className.toString());  
         }  
       }  
       jarFile.close();  
     } catch (IOException e) {  
       e.printStackTrace();  
     }  
     return list;  
   }  

This function can be connected with the other one, which checks if the class implement the specific interface:

 @SuppressWarnings({ "rawtypes", "unchecked" })  
   public List<String> getAllClassesImplementingInterface(String fileName,  
       Class interf) {  
     List<String> listOfClassesToCheck = getAllClassesInJarFile(fileName);  
     List<String> listOfClasses = new ArrayList<String>();  
     File file = new File(fileName);  
     URL fileURL;  
     URLClassLoader ucl = null;  
     try {  
       fileURL = file.toURI().toURL();  
       String jarURL = "jar:" + fileURL + "!/";  
       URL urls[] = { new URL(jarURL) };  
       ucl = new URLClassLoader(urls);  
     } catch (MalformedURLException e1) {  
       e1.printStackTrace();  
     }  
     for (String className : listOfClassesToCheck) {  
       Class<?> c;  
       try {  
         c = Class.forName(className, true, ucl);  
         if (interf.isAssignableFrom(c)) {  
           listOfClasses.add(c.getName());  
         }  
       } catch (ClassNotFoundException e) {  
         e.printStackTrace();  
       } catch (SecurityException e) {  
         e.printStackTrace();  
       }  
     }      
     return listOfClasses;  
   }  

niedziela, 17 listopada 2013

Android application for open intelligent building system - short description

The application shown on the picture of the left allows to control basic functionality of the intelligent building system. The Java server (running on the Raspberry Pi), connects to the application through WiFi and sends all changes of the controlled house parameters. User can see those changes and control some elements of the system (lights, blinds). The application dynamically builds the view of the elements based on model received at first connection (the application is independent from the hardware and server configuration)

sobota, 5 października 2013

4 reasons why do homeowners not buy intelligent building systems


connected, home, house, local, network icon

1. Costs:

Probably this is the most common reason. People do not want to spend 40 000 EUR for possibility to control the light...


2. Marketing:


What functionality offers this type of systems. It allows to control light, heat, security or maybe much more ? Where I can buy it ?


3. Lifetime of the equipment:


People build/buy houses and trust that they will live there for more then 30-70 years (or much more). Do the electronic devices installed in walls will live so long ? Will it be possible to buy a new compatible with the old one?


4. Technology:


Is it really an "intelligent building system" ... or maybe it is only a automatic system where you are able to use your smartphone to turn on/off the light ?