KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > filesystems > ExternalUtil


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.openide.filesystems;
20
21 import java.util.logging.Level JavaDoc;
22 import java.util.logging.Logger JavaDoc;
23 import org.openide.util.Exceptions;
24 import org.openide.util.Lookup;
25
26
27 /** Contains utility methods to deal with repository and error manager,
28 * so we do not need to directly contact
29 *
30 * @author Jaroslav Tulach
31 */

32 final class ExternalUtil extends Object JavaDoc {
33     /** value for the repository & error manager */
34     private static Repository repository;
35
36     /** Static method to find the Repository to use.
37      * @return Repository instance
38      */

39     public static Repository getRepository() {
40         initialize();
41
42         return repository;
43     }
44
45     /** Notifies an exception.
46      */

47     public static void exception(Exception JavaDoc ex) {
48         Logger.getLogger(ExternalUtil.class.getName()).log(Level.WARNING, null, ex);
49     }
50
51     /** Copies anotation.
52      * @param newEx new exception to annotate
53      * @param oldEx old exception to take annotation from
54      * @return newEx
55      */

56     public static Throwable JavaDoc copyAnnotation(Throwable JavaDoc newEx, Throwable JavaDoc oldEx) {
57         return newEx.initCause(oldEx);
58     }
59
60     /** Annotates the exception with a message.
61      */

62     public static void annotate(Throwable JavaDoc ex, String JavaDoc msg) {
63         Exceptions.attachLocalizedMessage(ex, msg);
64     }
65
66     /** Annotates the exception with a message.
67      */

68     public static Throwable JavaDoc annotate(Throwable JavaDoc ex, Throwable JavaDoc stack) {
69         Throwable JavaDoc orig = ex;
70         while (ex.getCause() != null) {
71             ex = ex.getCause();
72         }
73         ex.initCause(stack);
74         return orig;
75     }
76
77     private static Logger JavaDoc LOG = Logger.getLogger("org.openide.filesystems"); // NOI18N
78
/** Logs a text.
79      */

80     public static void log(String JavaDoc msg) {
81         LOG.fine(msg);
82     }
83
84     /** Loads a class of given name
85      * @param name name of the class
86      * @return the class
87      * @exception ClassNotFoundException if class was not found
88      */

89     public static Class JavaDoc findClass(String JavaDoc name) throws ClassNotFoundException JavaDoc {
90         initialize();
91
92         ClassLoader JavaDoc c = Lookup.getDefault().lookup(ClassLoader JavaDoc.class);
93
94         if (c == null) {
95             return Class.forName(name);
96         } else {
97             return Class.forName(name, true, c);
98         }
99     }
100
101     /** Initializes the context and errManager
102      */

103     private static void initialize() {
104         if (!isInitialized()) {
105             Lookup l = Lookup.getDefault();
106             Repository rep = l.lookup(org.openide.filesystems.Repository.class);
107             setRepository(rep);
108         }
109     }
110
111     private static synchronized boolean isInitialized() {
112         return repository != null;
113     }
114
115     /**
116      * @param rep may be null
117      */

118     private static synchronized void setRepository(Repository rep) {
119         repository = rep;
120
121         if (repository == null) {
122             // if not provided use default one
123
repository = new Repository(FileUtil.createMemoryFileSystem());
124         }
125     }
126 }
127
Popular Tags