KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > model > RegistryLoader


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
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  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.core.internal.model;
13
14 import java.io.*;
15 import java.net.MalformedURLException JavaDoc;
16 import java.net.URL JavaDoc;
17 import java.util.Properties JavaDoc;
18 import org.eclipse.core.internal.runtime.InternalPlatform;
19 import org.eclipse.core.internal.runtime.Messages;
20 import org.eclipse.core.runtime.*;
21 import org.eclipse.core.runtime.model.*;
22 import org.eclipse.osgi.util.NLS;
23 import org.xml.sax.InputSource JavaDoc;
24 import org.xml.sax.SAXParseException JavaDoc;
25
26 public class RegistryLoader {
27     private Factory factory;
28
29     // debug support
30
private boolean debug = false;
31     private long lastTick = System.currentTimeMillis();//used for performance timing
32

33     private RegistryLoader(Factory factory, boolean debug) {
34         super();
35         this.debug = debug;
36         this.factory = factory;
37     }
38
39     private void debug(String JavaDoc msg) {
40         long thisTick = System.currentTimeMillis();
41         System.out.println("RegistryLoader: " + msg + " [+" + (thisTick - lastTick) + "ms]"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
42
lastTick = thisTick;
43     }
44
45     private String JavaDoc[] getPathMembers(URL JavaDoc path) {
46         String JavaDoc[] list = null;
47         String JavaDoc protocol = path.getProtocol();
48         if (protocol.equals("file")) { //$NON-NLS-1$
49
list = (new File(path.getFile())).list();
50         } else {
51             // XXX: attempt to read URL and see if we got html dir page
52
}
53         return list == null ? new String JavaDoc[0] : list;
54     }
55
56     /**
57      * Reports an error and returns true.
58      */

59     private boolean parseProblem(String JavaDoc message) {
60         factory.error(new Status(IStatus.WARNING, Platform.PI_RUNTIME, Platform.PARSE_PROBLEM, message, null));
61         return true;
62     }
63
64     private PluginRegistryModel parseRegistry(URL JavaDoc[] pluginPath) {
65         long startTick = System.currentTimeMillis();
66         PluginRegistryModel result = processManifestFiles(pluginPath);
67         if (InternalPlatform.DEBUG) {
68             long endTick = System.currentTimeMillis();
69             debug("Parsed Registry: " + (endTick - startTick) + "ms"); //$NON-NLS-1$ //$NON-NLS-2$
70
}
71         return result;
72     }
73
74     public static PluginRegistryModel parseRegistry(URL JavaDoc[] pluginPath, Factory factory, boolean debug) {
75         return new RegistryLoader(factory, debug).parseRegistry(pluginPath);
76     }
77
78     private PluginModel processManifestFile(URL JavaDoc manifest) {
79         InputStream is = null;
80         try {
81             is = manifest.openStream();
82         } catch (IOException e) {
83             if (debug)
84                 debug("No plugin found for: " + manifest); //$NON-NLS-1$
85
return null;
86         }
87         PluginModel result = null;
88         try {
89             try {
90                 InputSource JavaDoc in = new InputSource JavaDoc(is);
91                 // Give the system id a value in case we want it for
92
// error reporting within the parser.
93
in.setSystemId(manifest.getFile());
94                 result = new PluginParser((Factory) factory).parsePlugin(in);
95             } finally {
96                 is.close();
97             }
98         } catch (SAXParseException JavaDoc se) {
99             /* exception details logged by parser */
100             factory.error(new Status(IStatus.WARNING, Platform.PI_RUNTIME, Platform.PARSE_PROBLEM, NLS.bind(Messages.parse_errorProcessing, manifest), null));
101         } catch (Exception JavaDoc e) {
102             factory.error(new Status(IStatus.WARNING, Platform.PI_RUNTIME, Platform.PARSE_PROBLEM, NLS.bind(Messages.parse_errorProcessing, manifest + ": " + e.getMessage()), null)); //$NON-NLS-1$
103
}
104         return result;
105     }
106
107     private PluginRegistryModel processManifestFiles(URL JavaDoc[] pluginPath) {
108         PluginRegistryModel result = factory.createPluginRegistry();
109         for (int i = 0; i < pluginPath.length; i++)
110             processPluginPathEntry(result, pluginPath[i]);
111         return result;
112     }
113
114     private void processPluginPathEntry(PluginRegistryModel registry, URL JavaDoc location) {
115         if (debug)
116             debug("Path - " + location); //$NON-NLS-1$
117
if (location.getFile().endsWith("/")) { //$NON-NLS-1$
118
// directory entry - search for plugins
119
String JavaDoc[] members = getPathMembers(location);
120             for (int j = 0; j < members.length; j++) {
121                 boolean found = false;
122                 try {
123                     found = processPluginPathFile(registry, new URL JavaDoc(location, members[j] + "/plugin.xml")); //$NON-NLS-1$
124
if (!found)
125                         found = processPluginPathFile(registry, new URL JavaDoc(location, members[j] + "/fragment.xml")); //$NON-NLS-1$
126
} catch (MalformedURLException JavaDoc e) {
127                     // Skip bad URLs
128
}
129                 if (debug)
130                     debug(found ? "Processed - " : "Processed (not found) - " + members[j]); //$NON-NLS-1$ //$NON-NLS-2$
131
}
132         } else {
133             // specific file entry - load the given file
134
boolean found = processPluginPathFile(registry, location);
135             if (debug)
136                 debug(found ? "Processed - " : "Processed (not found) - " + location); //$NON-NLS-1$ //$NON-NLS-2$
137
}
138     }
139
140     /**
141      * @return true if a file was found at the given location, and false otherwise.
142      */

143     private boolean processPluginPathFile(PluginRegistryModel registry, URL JavaDoc location) {
144         PluginModel entry = processManifestFile(location);
145         if (entry == null)
146             return false;
147         // Make sure all the required fields are here.
148
// This prevents us from things like NullPointerExceptions
149
// when we are assuming a field exists.
150
if (!requiredPluginModel(entry, location)) {
151             entry = null;
152             return false;
153         }
154         entry.setVersion(getQualifiedVersion(entry, location)); // check for version qualifier
155
if (entry instanceof PluginDescriptorModel) {
156             if (entry.getId() == null || entry.getVersion() == null) {
157                 return parseProblem(NLS.bind(Messages.parse_nullPluginIdentifier, location));
158             }
159             //skip duplicate entries
160
if (registry.getPlugin(entry.getId(), entry.getVersion()) != null) {
161                 return parseProblem(NLS.bind(Messages.parse_duplicatePlugin, entry.getId(), location));
162             }
163             registry.addPlugin((PluginDescriptorModel) entry);
164         } else {
165             if (entry.getId() == null || entry.getVersion() == null) {
166                 return parseProblem(NLS.bind(Messages.parse_nullFragmentIdentifier, location));
167             }
168             if (entry instanceof PluginFragmentModel) {
169                 registry.addFragment((PluginFragmentModel) entry);
170             } else {
171                 return parseProblem(NLS.bind(Messages.parse_unknownEntry, location));
172             }
173         }
174         String JavaDoc url = location.toString();
175         url = url.substring(0, 1 + url.lastIndexOf('/'));
176         entry.setRegistry(registry);
177         entry.setLocation(url);
178         // this is for the registry cache
179
// InternalPlatform.addLastModifiedTime(location.getFile(), new File(location.getFile()).lastModified());
180
return true;
181     }
182
183     private String JavaDoc getQualifiedVersion(PluginModel entry, URL JavaDoc base) {
184         if (entry == null || entry.getVersion() == null || entry.getId() == null)
185             return null;
186
187         InputStream is = null;
188         try {
189             // check to see if we have buildmanifest.properties for this plugin
190
URL JavaDoc manifest = null;
191             manifest = new URL JavaDoc(base, "buildmanifest.properties"); //$NON-NLS-1$
192
Properties JavaDoc props = new Properties JavaDoc();
193             is = manifest.openStream();
194             props.load(is);
195
196             // lookup qualifier for this plugin and "morph" the identifier if needed
197
String JavaDoc key = "plugin@" + entry.getId(); //$NON-NLS-1$
198
String JavaDoc qualifier = props.getProperty(key);
199             if (qualifier == null)
200                 return entry.getVersion();
201             PluginVersionIdentifier v = new PluginVersionIdentifier(entry.getVersion());
202             if (!v.getQualifierComponent().equals("")) //$NON-NLS-1$
203
return entry.getVersion();
204             else
205                 return (new PluginVersionIdentifier(v.getMajorComponent(), v.getMinorComponent(), v.getServiceComponent(), qualifier)).toString();
206         } catch (Exception JavaDoc e) {
207             return entry.getVersion();
208         } finally {
209             if (is != null)
210                 try {
211                     is.close();
212                 } catch (IOException e) {
213                     // Don't throw anything back if the close fails
214
}
215         }
216     }
217
218     private boolean requiredPluginModel(PluginModel plugin, URL JavaDoc location) {
219         String JavaDoc name = plugin.getName();
220         String JavaDoc id = plugin.getId();
221         String JavaDoc version = plugin.getVersion();
222         int nameLength = name == null ? 0 : name.length();
223         int idLength = id == null ? 0 : id.length();
224         int versionLength = version == null ? 0 : version.length();
225
226         if (nameLength <= 0) {
227             parseProblem(NLS.bind(Messages.parse_missingPluginName, location));
228             return false;
229         }
230         if (idLength <= 0) {
231             parseProblem(NLS.bind(Messages.parse_missingPluginId, location));
232             return false;
233         }
234         if (versionLength <= 0) {
235             parseProblem(NLS.bind(Messages.parse_missingPluginVersion, location));
236             return false;
237         }
238
239         if (plugin instanceof PluginFragmentModel) {
240             String JavaDoc pluginName = ((PluginFragmentModel) plugin).getPlugin();
241             String JavaDoc pluginVersion = ((PluginFragmentModel) plugin).getPluginVersion();
242             int pNameLength = pluginName == null ? 0 : pluginName.length();
243             int pNameVersion = pluginVersion == null ? 0 : pluginVersion.length();
244             if (pNameLength <= 0) {
245                 parseProblem(NLS.bind(Messages.parse_missingFPName, location));
246                 return false;
247             }
248             if (pNameVersion <= 0) {
249                 parseProblem(NLS.bind(Messages.parse_missingFPVersion, location));
250                 return false;
251             }
252         }
253         return true;
254     }
255 }
256
Popular Tags