KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > apt > core > internal > LoadFailureHandler


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 BEA Systems, Inc.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * jgarms@bea.com - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.jdt.apt.core.internal;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.resources.IMarker;
18 import org.eclipse.core.resources.IProject;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.jdt.core.IJavaProject;
22
23 /**
24  * Stores factories or libraries that were inaccessible when
25  * attempting to load annotation processors, and then handles
26  * reporting those errors to the user as markers in the problems pane.
27  * <p>
28  * This class is necessary due to deadlock possibilities in
29  * {@link AnnotationProcessorFactoryLoader}. We need to gather up
30  * the errors while holding a lock in that class,
31  * and then later report them outside the lock, via the
32  * reportFailureMarkers() method.
33  */

34 public class LoadFailureHandler {
35     
36     private final IProject _project;
37     private final List JavaDoc<String JavaDoc> _missingLibraries = new ArrayList JavaDoc<String JavaDoc>();
38     private final List JavaDoc<String JavaDoc> _failedFactories = new ArrayList JavaDoc<String JavaDoc>();
39     
40     public LoadFailureHandler(IJavaProject proj) {
41         _project = proj.getProject();
42     }
43     
44     public void addMissingLibrary(String JavaDoc lib) {
45         _missingLibraries.add(lib);
46     }
47     
48     public void addFailedFactory(String JavaDoc factory) {
49         _failedFactories.add(factory);
50     }
51     
52     public void reportFailureMarkers() {
53         reportFailureToLoadFactories();
54         reportMissingLibraries();
55     }
56     
57     /**
58      * Enter problem markers for factory containers that could not be found on
59      * disk. This routine does not check whether markers already exist.
60      * See {@link AnnotationProcessorFactoryLoader} for information about
61      * the lifecycle of these markers.
62      */

63     private void reportMissingLibraries() {
64         for (String JavaDoc fc : _missingLibraries) {
65             try {
66                 String JavaDoc message = Messages.bind(
67                         Messages.AnnotationProcessorFactoryLoader_factorypath_missingLibrary,
68                         new String JavaDoc[] {fc, _project.getName()});
69                 IMarker marker = _project.createMarker(AptPlugin.APT_LOADER_PROBLEM_MARKER);
70                 marker.setAttributes(
71                         new String JavaDoc[] {
72                             IMarker.MESSAGE,
73                             IMarker.SEVERITY,
74                             IMarker.LOCATION
75                         },
76                         new Object JavaDoc[] {
77                             message,
78                             IMarker.SEVERITY_ERROR,
79                             Messages.AnnotationProcessorFactoryLoader_factorypath
80                         }
81                     );
82             } catch (CoreException e) {
83                 AptPlugin.log(e, "Unable to create APT build problem marker on project " + _project.getName()); //$NON-NLS-1$
84
}
85         }
86     }
87     
88     /**
89      * Enter a marker for a factory class that could not be loaded.
90      * Note that if a jar is missing, we won't be able to load its factory
91      * names, and thus we won't even try loading its factory classes; but
92      * we can still fail to load a factory class if, for instance, the
93      * jar is corrupted or the factory constructor throws an exception.
94      * See {@link AnnotationProcessorFactoryLoader} for information about
95      * the lifecycle of these markers.
96      */

97     private void reportFailureToLoadFactories() {
98         for (String JavaDoc factoryName : _failedFactories) {
99             try {
100                 String JavaDoc message = Messages.bind(
101                         Messages.AnnotationProcessorFactoryLoader_unableToLoadFactoryClass,
102                         new String JavaDoc[] {factoryName, _project.getName()});
103                 IMarker marker = _project.createMarker(AptPlugin.APT_LOADER_PROBLEM_MARKER);
104                 marker.setAttributes(
105                         new String JavaDoc[] {
106                             IMarker.MESSAGE,
107                             IMarker.SEVERITY,
108                             IMarker.LOCATION
109                         },
110                         new Object JavaDoc[] {
111                             message,
112                             IStatus.ERROR,
113                             Messages.AnnotationProcessorFactoryLoader_factorypath
114                         }
115                     );
116             } catch (CoreException e) {
117                 AptPlugin.log(e, "Unable to create build problem marker"); //$NON-NLS-1$
118
}
119         }
120     }
121     
122     @Override JavaDoc
123     public int hashCode() {
124         return _project.hashCode();
125     }
126     
127     @Override JavaDoc
128     public boolean equals(Object JavaDoc o) {
129         if (!(o instanceof LoadFailureHandler)) return false;
130         LoadFailureHandler otherHandler = (LoadFailureHandler)o;
131         return _project.equals(otherHandler._project);
132     }
133
134 }
135
Popular Tags