01: /*******************************************************************************
02: * Copyright (c) 2005, 2007 BEA Systems, Inc.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * mkaufman@bea.com - initial API and implementation
10: *******************************************************************************/package org.eclipse.jdt.apt.core.internal.util;
11:
12: import java.io.IOException;
13: import java.util.Map;
14:
15: /**
16: * An entry on the processor factory path. Typically a jar, plug-in,
17: * etc. that contains annotation processors. It may contain Java 5
18: * processors, Java 6 processors, both or neither.
19: */
20: public abstract class FactoryContainer {
21: public enum FactoryType {
22: PLUGIN, // Eclipse plugin
23: EXTJAR, // external jar file (not in workspace)
24: WKSPJAR, // jar file within workspace
25: VARJAR; // external jar file referenced by classpath variable
26: }
27:
28: /**
29: * Returns an ID that is guaranteed to be sufficiently unique for this container --
30: * that is, all necessary state can be reconstructed from just the id and FactoryType.
31: * For plugins, it's the plugin id, for jar files, the path to the jar, etc.
32: */
33: public abstract String getId();
34:
35: /**
36: * This method is used to display the container in the UI.
37: * If this default implementation is not adequate for a particular
38: * container, that container should provide an override.
39: */
40: @Override
41: public String toString() {
42: return getId();
43: }
44:
45: public abstract FactoryType getType();
46:
47: /**
48: * Test whether the resource that backs this container exists,
49: * can be located, and is (at least in principle) accessible for
50: * factories to be loaded from. For instance, a plugin exists if
51: * the plugin is loaded in Eclipse; a jar exists if the jar file
52: * can be found on disk. The test is not required to be perfect:
53: * for instance, a jar file might exist but be corrupted and
54: * therefore not really readable, but this method would still return
55: * true.
56: * @return true if the resource backing the container exists.
57: */
58: public abstract boolean exists();
59:
60: /**
61: * Subclasses must return a map of implementation name to service
62: * name, for all the processor services this container provides.
63: * @throws IOException
64: */
65: protected abstract Map<String, String> loadFactoryNames()
66: throws IOException;
67:
68: /**
69: * Map of implementation name to service name. For instance,
70: * "org.xyz.FooProcessor" -> "javax.annotation.processing.Processor".
71: */
72: protected Map<String, String> _factoryNames;
73:
74: public Map<String, String> getFactoryNames() throws IOException {
75: if (_factoryNames == null)
76: _factoryNames = loadFactoryNames();
77: return _factoryNames;
78: }
79:
80: @Override
81: public int hashCode() {
82: return getType().hashCode() ^ getId().hashCode();
83: }
84:
85: @Override
86: public boolean equals(Object o) {
87: if (!(o instanceof FactoryContainer)) {
88: return false;
89: }
90:
91: FactoryContainer other = (FactoryContainer) o;
92: return other.getType() == getType()
93: && other.getId().equals(getId());
94: }
95: }
|