DBVIEW
src/org/dbview/utils/JavaVm.java
00001 /*
00002         DbView - Graph Visualization
00003     Copyright (C) 2012  Denis BEURIVE
00004 
00005     This program is free software: you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation, either version 3 of the License, or
00008     (at your option) any later version.
00009 
00010     This program is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017 */
00018 
00019 /**
00020  * @author Denis Beurive
00021  */
00022 
00023 package org.dbview.utils;
00024 
00025 import java.io.File;
00026 import java.io.FileInputStream;
00027 import java.net.URL;
00028 import java.util.jar.JarEntry;
00029 import java.util.jar.JarInputStream;
00030 import java.lang.Exception;
00031 import java.util.ArrayList;
00032 
00033 /**
00034  * This class implements utilities used to interact with the JAVA run-time environment.
00035  * @author Denis Beurive
00036  */
00037 public class JavaVm
00038 {
00039     /**
00040      * This method lists the content of a JAR file.
00041      * @param in_name Name of the JAR file.
00042      * @return The method returns the list of entries found in the JAR file.
00043      * @throws Exception
00044      */
00045     static public ArrayList<String> listJar(String in_name) throws Exception
00046     {
00047         ArrayList<String> list = new ArrayList<String>(); 
00048         
00049         URL jar_url = JavaVm.locateResource(in_name);
00050         if (null == jar_url) { throw new Exception("The JAR file \"" + in_name + "\" could not be found!"); }
00051         
00052         JarInputStream ji = new JarInputStream(new FileInputStream(new File(jar_url.toURI())));
00053         
00054         while(Boolean.TRUE)
00055         {
00056             JarEntry entry = ji.getNextJarEntry();
00057             if (null == entry) { break; }
00058             list.add(entry.getName());
00059         }
00060         
00061         return list;
00062     }
00063     
00064     /**
00065      * This method tries to locate a resource, given its name.
00066      * @param in_name Name of the resource.
00067      * @return If the method locates the resource, then it returns the resource's URL.
00068      *         Otherwise, it returns the value null.
00069      */
00070     public static URL locateResource(String in_name)
00071     {
00072         return JavaVm.class.getClassLoader().getResource(in_name);
00073     }
00074 }