KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > apt > core > internal > util > FactoryContainer


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  * mkaufman@bea.com - initial API and implementation
10  *******************************************************************************/

11
12
13 package org.eclipse.jdt.apt.core.internal.util;
14
15 import java.io.IOException JavaDoc;
16 import java.util.Map JavaDoc;
17
18 /**
19  * An entry on the processor factory path. Typically a jar, plug-in,
20  * etc. that contains annotation processors. It may contain Java 5
21  * processors, Java 6 processors, both or neither.
22  */

23 public abstract class FactoryContainer
24 {
25     public enum FactoryType {
26         PLUGIN, // Eclipse plugin
27
EXTJAR, // external jar file (not in workspace)
28
WKSPJAR, // jar file within workspace
29
VARJAR; // external jar file referenced by classpath variable
30
}
31     
32     /**
33      * Returns an ID that is guaranteed to be sufficiently unique for this container --
34      * that is, all necessary state can be reconstructed from just the id and FactoryType.
35      * For plugins, it's the plugin id, for jar files, the path to the jar, etc.
36      */

37     public abstract String JavaDoc getId();
38     
39     /**
40      * This method is used to display the container in the UI.
41      * If this default implementation is not adequate for a particular
42      * container, that container should provide an override.
43      */

44     @Override JavaDoc
45     public String JavaDoc toString() {
46         return getId();
47     }
48     
49     public abstract FactoryType getType();
50     
51     /**
52      * Test whether the resource that backs this container exists,
53      * can be located, and is (at least in principle) accessible for
54      * factories to be loaded from. For instance, a plugin exists if
55      * the plugin is loaded in Eclipse; a jar exists if the jar file
56      * can be found on disk. The test is not required to be perfect:
57      * for instance, a jar file might exist but be corrupted and
58      * therefore not really readable, but this method would still return
59      * true.
60      * @return true if the resource backing the container exists.
61      */

62     public abstract boolean exists();
63     
64     /**
65      * Subclasses must return a map of implementation name to service
66      * name, for all the processor services this container provides.
67      * @throws IOException
68      */

69     protected abstract Map JavaDoc<String JavaDoc, String JavaDoc> loadFactoryNames() throws IOException JavaDoc;
70     
71     /**
72      * Map of implementation name to service name. For instance,
73      * "org.xyz.FooProcessor" -> "javax.annotation.processing.Processor".
74      */

75     protected Map JavaDoc<String JavaDoc, String JavaDoc> _factoryNames;
76     
77     public Map JavaDoc<String JavaDoc, String JavaDoc> getFactoryNames() throws IOException JavaDoc
78     {
79         if ( _factoryNames == null )
80             _factoryNames = loadFactoryNames();
81         return _factoryNames;
82     }
83     
84     @Override JavaDoc
85     public int hashCode() {
86         return getType().hashCode() ^ getId().hashCode();
87     }
88
89     @Override JavaDoc
90     public boolean equals(Object JavaDoc o) {
91         if (!(o instanceof FactoryContainer)) {
92             return false;
93         }
94
95         FactoryContainer other = (FactoryContainer) o;
96         return other.getType() == getType() && other.getId().equals(getId());
97     }
98 }
99
100
Popular Tags