/[xulu]/trunk/src/appl/util/GeneralUtil.java
ViewVC logotype

Annotation of /trunk/src/appl/util/GeneralUtil.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2 - (hide annotations)
Wed Feb 25 11:54:01 2009 UTC (15 years, 9 months ago) by mojays
File size: 2826 byte(s)
First Commit, corresponds to Revision 1008 of Wikisquare-SVN 
1 mojays 2 package appl.util;
2    
3     import java.io.BufferedInputStream;
4     import java.io.BufferedOutputStream;
5     import java.io.File;
6     import java.io.FileInputStream;
7     import java.io.FileNotFoundException;
8     import java.io.FileOutputStream;
9     import java.io.ObjectInputStream;
10     import java.io.ObjectOutputStream;
11     import java.net.URL;
12    
13     import org.apache.log4j.LogManager;
14     import org.apache.log4j.Logger;
15    
16     import net.jini.loader.pref.PreferredClassLoader;
17    
18     /**
19     * General utility class. See method description for details.
20     *
21     * @author Dominik Appl
22     */
23     public class GeneralUtil {
24    
25     public static Logger LOG = LogManager.getLogger("appl.util.GeneralUtil");
26    
27     /**
28     * Loads a class from the specified URL and returns an instance of this class.
29     * @param location the URL of the rootDirectory/networkLocation
30     * @param classname the fully qualified name of the class (the class must have a
31     *
32     * @return the instance of the class or <code>null</code> if loading fails
33     */
34     public static Object loadClassFromURL(URL location, String classname) {
35     try {
36     URL[] locations = { location };
37     PreferredClassLoader classLoader = new PreferredClassLoader(
38     locations, Thread.currentThread().getContextClassLoader(),
39     null, false);
40     LOG.debug("Try to load " + classname.getClass().getName()
41     + " from " + location);
42     Class classObject = classLoader.loadClass(classname);
43     return classObject.newInstance();
44     } catch (Exception e) {
45     LOG.error("Could not load class for " + classname + " from "
46     + location, e);
47     e.printStackTrace();
48     }
49     return null;
50     }
51    
52     /**
53     * Serializes the given object into the specified file. All exceptions are printed to console.
54     * @param o the object
55     * @param destFile the file to write to
56     */
57     public static void SerializeToFile(Object o, File destFile) {
58     try {
59     destFile.createNewFile();
60     BufferedOutputStream fos = new BufferedOutputStream(
61     new FileOutputStream(destFile));
62     ObjectOutputStream oos = new ObjectOutputStream(fos);
63     oos.writeObject(o);
64     oos.close();
65     fos.close();
66     } catch (Exception e) {
67     e.printStackTrace();
68     }
69     }
70    
71     /**
72     * Reads the first object out of the file
73     * @param loadFile the file to read from
74     * @return the object
75     */
76     public static Object readSerializedFrom(File loadFile) {
77     Object loadedObject = null;
78     try {
79     BufferedInputStream fis = new BufferedInputStream(
80     new FileInputStream(loadFile));
81     ObjectInputStream ois = new ObjectInputStream(fis);
82     loadedObject = ois.readObject();
83     ois.close();
84     fis.close();
85     // remove File from Filesystem
86     } catch (FileNotFoundException e) {
87     LOG.error(e);
88     e.printStackTrace();
89    
90     } catch (Exception e) {
91     LOG.error(e);
92     e.printStackTrace();
93     }
94     return loadedObject;
95     }
96     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26