Coverage Report - argos.util.Util
 
Classes in this File Line Coverage Branch Coverage Complexity
Util
0%
0/82
0%
0/38
0
 
 1  
 /*
 2  
 Copyright (c) 2006, University of Tromsø
 3  
 All rights reserved.
 4  
 
 5  
 Redistribution and use in source and binary forms, with or without 
 6  
 modification, are permitted provided that the following conditions are met:
 7  
 
 8  
  * Redistributions of source code must retain the above copyright notice, this list 
 9  
    of conditions and the following disclaimer.
 10  
 
 11  
  * Redistributions in binary form must reproduce the above copyright notice, this 
 12  
    list of conditions and the following disclaimer in the documentation and/or other 
 13  
    materials provided with the distribution.
 14  
 
 15  
  * Neither the name of the University of Tromsø nor the names of its contributors may 
 16  
    be used to endorse or promote products derived from this software without specific 
 17  
    prior written permission.
 18  
 
 19  
 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
 20  
 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
 21  
 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
 22  
 SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
 23  
 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
 24  
 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
 25  
 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 26  
 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
 27  
 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 
 28  
 DAMAGE.
 29  
 */
 30  
 
 31  
 package argos.util;
 32  
 
 33  
 import java.io.BufferedReader;
 34  
 import java.io.File;
 35  
 import java.io.FileInputStream;
 36  
 import java.io.FileOutputStream;
 37  
 import java.io.IOException;
 38  
 import java.io.InputStreamReader;
 39  
 import java.io.PrintWriter;
 40  
 import java.io.Serializable;
 41  
 import java.net.MalformedURLException;
 42  
 import java.util.ArrayList;
 43  
 import java.util.List;
 44  
 import java.util.zip.ZipEntry;
 45  
 import java.util.zip.ZipInputStream;
 46  
 
 47  
 import javax.management.InstanceNotFoundException;
 48  
 import javax.management.MBeanServerConnection;
 49  
 import javax.management.MalformedObjectNameException;
 50  
 import javax.management.NotificationListener;
 51  
 import javax.management.ObjectName;
 52  
 import javax.management.remote.JMXConnector;
 53  
 import javax.management.remote.JMXConnectorFactory;
 54  
 import javax.management.remote.JMXServiceURL;
 55  
 
 56  
 
 57  0
 public class Util {
 58  
         public static final int BUFFER_SIZE = 10 * 1024;
 59  
         
 60  
         public static List<File> extractFromJar(File jar, String fromFolder, String toFolder, boolean removeFirstDir) 
 61  
                          throws IOException {
 62  0
                 List<File> extracted = new ArrayList<File>();
 63  0
                 byte[] buf = new byte[BUFFER_SIZE];
 64  0
                 FileInputStream tempIn = null;
 65  0
                 ZipInputStream stream = null;
 66  0
                 tempIn = new FileInputStream(jar);
 67  0
                 stream = new ZipInputStream(tempIn);
 68  
                 ZipEntry entry;
 69  0
                 while((entry = stream.getNextEntry()) != null) {
 70  0
                         if(entry.getName().startsWith(fromFolder)) {
 71  0
                                 String tempFileName = entry.getName();
 72  0
                                 if(removeFirstDir) {
 73  0
                                         tempFileName = tempFileName.substring(tempFileName.indexOf('/'));
 74  
                                 }
 75  0
                                 File file = new File(toFolder + "/" + tempFileName);
 76  0
                                 if(entry.isDirectory()) {
 77  0
                                         file.mkdir();
 78  
                                 }
 79  
                                 else {
 80  0
                                         FileOutputStream out = new FileOutputStream(file, false);
 81  0
                                         int n = 0;
 82  0
                                         while((n = stream.read(buf, 0, 1024)) > -1) {
 83  0
                                                 out.write(buf, 0, n);
 84  
                                         }
 85  0
                                         out.close();
 86  0
                                         extracted.add(file);
 87  
                                 }
 88  0
                         }
 89  
                 }
 90  0
                 stream.close();
 91  0
                 tempIn.close();
 92  0
                 return extracted;
 93  
         }
 94  
         
 95  
         public static void copyFile(File inFile, File outFile) throws IOException {
 96  0
                 byte[] buf = new byte[BUFFER_SIZE];
 97  0
                 FileInputStream in = new FileInputStream(inFile);
 98  0
                 FileOutputStream out = new FileOutputStream(outFile);
 99  0
                 int n = 0;
 100  0
                 while((n = in.read(buf, 0, 1024)) > -1) {
 101  0
                         out.write(buf, 0, n);
 102  
                 }
 103  0
                 out.close();
 104  0
                 in.close();
 105  0
         }
 106  
         
 107  
         public static void cleanDir(File file) {
 108  0
                 if(file.isDirectory() && !file.getName().equals("CVS")) {
 109  0
                         for(File f : file.listFiles()) {
 110  0
                                 cleanDir(f);
 111  
                         }
 112  0
                         delete(file);
 113  
                 }
 114  
                 else {
 115  0
                         delete(file);
 116  
                 }
 117  0
         }
 118  
         
 119  
         private static void delete(File file) {
 120  0
                 if(!file.getName().equals(".cvsignore") && !file.delete()) {
 121  0
                         file.deleteOnExit();
 122  
                 }
 123  0
         }
 124  
         
 125  
         public static JMXConnector listenTo(String who, String url, NotificationListener me, 
 126  
                         Serializable handback) throws MalformedObjectNameException, MalformedURLException,
 127  
                         InstanceNotFoundException, IOException {
 128  0
                 int rounds = 10;
 129  0
                 for(int i = 0; i < rounds; i++) {
 130  0
                         if(i > 0) {
 131  
                                 try {
 132  0
                                         Thread.sleep(5000);
 133  
                                 }
 134  0
                                 catch (InterruptedException ignore) {}
 135  
                         }
 136  
                         try {
 137  0
                                 JMXConnector connector = JMXConnectorFactory.connect(new JMXServiceURL(url));
 138  0
                                 MBeanServerConnection server = connector.getMBeanServerConnection();
 139  0
                                 server.addNotificationListener(new ObjectName(who), me, 
 140  
                                                 null, handback);
 141  0
                                 return connector;
 142  
                         }
 143  0
                         catch(InstanceNotFoundException e) {
 144  0
                                 if(i == rounds - 1) {
 145  0
                                         throw e;
 146  
                                 }
 147  
                         }
 148  0
                         catch(IOException e) {
 149  0
                                 if(i == rounds - 1) {
 150  0
                                         throw e;
 151  
                                 }
 152  0
                         }
 153  
                 }
 154  0
                 return null;
 155  
         }
 156  
         
 157  
         public static void unlistenTo(String who, String url, NotificationListener me) throws Exception,
 158  
                         InstanceNotFoundException, IOException {
 159  0
                 int rounds = 10;
 160  0
                 for(int i = 0; i < rounds; i++) {
 161  0
                         if(i > 0) {
 162  
                                 try {
 163  0
                                         Thread.sleep(5000);
 164  
                                 }
 165  0
                                 catch (InterruptedException ignore) {}
 166  
                         }
 167  
                         try {
 168  0
                                 JMXConnector connector = JMXConnectorFactory.connect(new JMXServiceURL(url));
 169  0
                                 MBeanServerConnection server = connector.getMBeanServerConnection();
 170  0
                                 server.removeNotificationListener(new ObjectName(who), me);
 171  0
                                 return;
 172  
                         }
 173  0
                         catch(Exception e) {
 174  0
                                 if(i == rounds - 1) {
 175  0
                                         throw e;
 176  
                                 }
 177  
                         }
 178  
                 }                
 179  0
         }
 180  
 
 181  
     public static void writeResourceToFile(String resource, String outFilename) throws IOException {
 182  0
         BufferedReader reader = new BufferedReader(new InputStreamReader(Util.class.getResourceAsStream(resource)));
 183  0
         PrintWriter writer = new PrintWriter(outFilename);
 184  0
         String line = null;
 185  0
         while((line = reader.readLine()) != null) {
 186  0
             writer.println(line);
 187  
         }
 188  0
         writer.flush();
 189  0
         writer.close();
 190  0
         reader.close();
 191  0
     }
 192  
 }