KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > startup > NbProblemDisplayer


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
20 package org.netbeans.core.startup;
21
22 import java.io.IOException JavaDoc;
23 import java.text.Collator JavaDoc;
24 import java.util.HashSet JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Set JavaDoc;
27 import java.util.SortedSet JavaDoc;
28 import java.util.TreeSet JavaDoc;
29 import org.netbeans.InvalidException;
30 import org.netbeans.Module;
31 import org.netbeans.Util;
32 import org.openide.modules.Dependency;
33 import org.openide.modules.SpecificationVersion;
34 import org.openide.util.NbBundle;
35
36 /**
37  * Utility class to provide localized messages explaining problems
38  * that modules had during attempted installation.
39  * Used by both {@link org.netbeans.core.startup.NbEvents} and autoupdate's ModuleBean.
40  * @author Jesse Glick
41  * @see "#16636"
42  */

43 public final class NbProblemDisplayer {
44     
45     private NbProblemDisplayer() {}
46     
47     /**
48      * Provide a localized explanation of some installation problem.
49      * Problem may be either an InvalidException or a Dependency.
50      * Structure of message can assume that the module failing its
51      * dependencies is already being displayed, and concentrate
52      * on the problem.
53      * @param m the module which cannot be installed
54      * @param problem either an {@link InvalidException} or {@link Dependency} as returned from {@link Module#getProblems}
55      * @return an explanation of the problem in the most human-friendly format available
56      */

57     public static String JavaDoc messageForProblem(Module m, Object JavaDoc problem) {
58         if (problem instanceof InvalidException) {
59             return Util.findLocalizedMessage((InvalidException)problem, true);
60         } else {
61             Dependency dep = (Dependency)problem;
62             switch (dep.getType()) {
63             case Dependency.TYPE_MODULE:
64                 String JavaDoc polite = (String JavaDoc)m.getLocalizedAttribute("OpenIDE-Module-Module-Dependency-Message"); // NOI18N
65
if (polite != null) {
66                     return polite;
67                 } else {
68                     String JavaDoc name = dep.getName();
69                     // Find code name base:
70
int idx = name.lastIndexOf('/');
71                     if (idx != -1) {
72                         name = name.substring(0, idx);
73                     }
74                     Module other = m.getManager().get(name);
75                     if (other != null && other.getCodeName().equals(dep.getName())) {
76                         switch (dep.getComparison()) {
77                         case Dependency.COMPARE_ANY:
78                             // Just disabled (probably had its own problems).
79
return NbBundle.getMessage(NbProblemDisplayer.class, "MSG_problem_other_disabled", other.getDisplayName());
80                         case Dependency.COMPARE_IMPL:
81                             String JavaDoc requestedI = dep.getVersion();
82                             String JavaDoc actualI = (other.getImplementationVersion() != null) ?
83                                 other.getImplementationVersion() :
84                                 NbBundle.getMessage(NbProblemDisplayer.class, "LBL_no_impl_version");
85                             if (requestedI.equals(actualI)) {
86                                 // Just disabled (probably had its own problems).
87
return NbBundle.getMessage(NbProblemDisplayer.class, "MSG_problem_other_disabled", other.getDisplayName());
88                             } else {
89                                 // Wrong version.
90
return NbBundle.getMessage(NbProblemDisplayer.class, "MSG_problem_other_wrong_version", other.getDisplayName(), requestedI, actualI);
91                             }
92                         case Dependency.COMPARE_SPEC:
93                             SpecificationVersion requestedS = new SpecificationVersion(dep.getVersion());
94                             SpecificationVersion actualS = (other.getSpecificationVersion() != null) ?
95                                 other.getSpecificationVersion() :
96                                 new SpecificationVersion("0"); // NOI18N
97
if (actualS.compareTo(requestedS) >= 0) {
98                                 // Just disabled (probably had its own problems).
99
return NbBundle.getMessage(NbProblemDisplayer.class, "MSG_problem_other_disabled", other.getDisplayName());
100                             } else {
101                                 // Too old.
102
return NbBundle.getMessage(NbProblemDisplayer.class, "MSG_problem_other_too_old", other.getDisplayName(), requestedS, actualS);
103                             }
104                         default:
105                             throw new IllegalStateException JavaDoc();
106                         }
107                     } else {
108                         // Keep the release version info in this case.
109
// XXX would be nice to have a special message for mismatched major release
110
// version - i.e. other != null
111
return NbBundle.getMessage(NbProblemDisplayer.class, "MSG_problem_other_needed_not_found", dep.getName());
112                     }
113                 }
114             case Dependency.TYPE_REQUIRES:
115             case Dependency.TYPE_NEEDS:
116                 polite = (String JavaDoc)m.getLocalizedAttribute("OpenIDE-Module-Requires-Message"); // NOI18N
117
if (polite != null) {
118                     return polite;
119                 } else {
120                     Set JavaDoc others = m.getManager().getModules();
121                     Iterator JavaDoc it = others.iterator();
122                     while (it.hasNext()) {
123                         Module other = (Module)it.next();
124                         if (other.provides(dep.getName())) {
125                             return NbBundle.getMessage(NbProblemDisplayer.class, "MSG_problem_require_disabled", dep.getName());
126                         }
127                     }
128                     return NbBundle.getMessage(NbProblemDisplayer.class, "MSG_problem_require_not_found", dep.getName());
129                 }
130             case Dependency.TYPE_PACKAGE:
131                 polite = (String JavaDoc)m.getLocalizedAttribute("OpenIDE-Module-Package-Dependency-Message"); // NOI18N
132
if (polite != null) {
133                     return polite;
134                 } else {
135                     String JavaDoc name = dep.getName();
136                     // Find package name or qualified name of probe class:
137
int idx = name.lastIndexOf('[');
138                     if (idx == 0) {
139                         // Probed class. [javax.television.Antenna]
140
return NbBundle.getMessage(NbProblemDisplayer.class, "MSG_problem_class_not_loaded", name.substring(1, name.length() - 1));
141                     } else if (idx != -1) {
142                         // Package plus sample class. javax.television[Antenna]
143
return NbBundle.getMessage(NbProblemDisplayer.class, "MSG_problem_package_not_loaded_or_old", name.substring(0, idx));
144                     } else {
145                         return NbBundle.getMessage(NbProblemDisplayer.class, "MSG_problem_package_not_loaded_or_old", name);
146                     }
147                 }
148             case Dependency.TYPE_JAVA:
149                 // XXX would OpenIDE-Module-Java-Dependency-Message be useful?
150
if (dep.getName().equals(Dependency.JAVA_NAME) && dep.getComparison() == Dependency.COMPARE_SPEC) {
151                     return NbBundle.getMessage(NbProblemDisplayer.class, "MSG_problem_java_too_old", dep.getVersion(), Dependency.JAVA_SPEC);
152                 } else {
153                     // All other usages unlikely, don't bother making pretty.
154
return dep.toString();
155                 }
156             default:
157                 throw new IllegalArgumentException JavaDoc(dep.toString());
158             }
159         }
160     }
161
162     static void problemMessagesForModules(Appendable JavaDoc writeTo, Set JavaDoc<? extends Module> modules, boolean justRootCause) {
163         try {
164             HashSet JavaDoc<String JavaDoc> names = new HashSet JavaDoc<String JavaDoc>();
165             for (Module m : modules) {
166                 names.add(m.getCodeName());
167             }
168
169             HashSet JavaDoc<String JavaDoc> dependantModules = new HashSet JavaDoc<String JavaDoc>();
170             for (Module m : modules) {
171                 SortedSet JavaDoc<String JavaDoc> problemTexts = new TreeSet JavaDoc<String JavaDoc>(Collator.getInstance());
172                 Iterator JavaDoc pit = m.getProblems().iterator();
173                 if (pit.hasNext()) {
174                     while (pit.hasNext()) {
175                         Object JavaDoc problem = pit.next();
176                         if (problem instanceof Dependency && justRootCause) {
177                             Dependency d = (Dependency)problem;
178                             if (
179                                 d.getType() == Dependency.TYPE_MODULE &&
180                                 names.contains(d.getName())
181                             ) {
182                                 dependantModules.add(m.getCodeName());
183                                 continue;
184                             }
185                         }
186
187                         problemTexts.add(m.getDisplayName() + " - " + // NOI18N
188
NbProblemDisplayer.messageForProblem(m, problem));
189                     }
190                 } else {
191                     throw new IllegalStateException JavaDoc("Module " + m + " could not be installed but had no problems"); // NOI18N
192
}
193                 for (String JavaDoc s: problemTexts) {
194                     writeTo.append("\n\t").append(s); // NOI18N
195
}
196             }
197             if (!dependantModules.isEmpty()) {
198                 writeTo.append("\n\t").append(NbBundle.getMessage(NbProblemDisplayer.class, "MSG_also_dep_modules", dependantModules.size()));
199             }
200         } catch (IOException JavaDoc ex) {
201             throw (IllegalStateException JavaDoc)new IllegalStateException JavaDoc().initCause(ex);
202         }
203     }
204     
205 }
206
Popular Tags