KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > common > queries > api > InjectionTargetQuery


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.modules.j2ee.common.queries.api;
21
22 import java.io.IOException JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.List JavaDoc;
25 import javax.lang.model.element.TypeElement;
26 import org.netbeans.api.java.source.CompilationController;
27 import org.netbeans.api.java.source.JavaSource;
28 import org.netbeans.modules.j2ee.common.queries.spi.InjectionTargetQueryImplementation;
29 import org.netbeans.modules.j2ee.common.source.AbstractTask;
30 import org.openide.filesystems.FileObject;
31 import org.openide.util.Lookup;
32 import org.openide.util.LookupEvent;
33 import org.openide.util.LookupListener;
34
35 /**
36  * Ask whether it is possible to use dependency injection in some class
37  * @author Martin Adamek, Milan Kuchtiak
38  */

39 public class InjectionTargetQuery {
40     
41     private static Lookup.Result<InjectionTargetQueryImplementation> implementations;
42     /** Cache of all available InjectionTargetQueryImplementation instances. */
43     private static List JavaDoc<InjectionTargetQueryImplementation> cache;
44
45     private InjectionTargetQuery() {
46     }
47     
48     /**
49      * Decide if dependency injection can be used in given class
50      * @param controller CompilationController related to JavaSource
51      * @param typeElement class where annotated field or method should be inserted,
52      * if null is provided, main public class from file is taken
53      * @return true if any container or environment is able to inject resources in given class, false otherwise
54      */

55     public static boolean isInjectionTarget(CompilationController controller, TypeElement typeElement) {
56         if (typeElement == null || controller==null) {
57             throw new NullPointerException JavaDoc("Passed null to InjectionTargetQuery.isInjectionTarget(CompilationController, TypeElement)"); // NOI18N
58
}
59         for (InjectionTargetQueryImplementation elem : getInstances()) {
60             if (elem.isInjectionTarget(controller, typeElement)) {
61                 return true;
62             }
63         }
64         return false;
65     }
66     
67     public static boolean isInjectionTarget(FileObject fileObject, final String JavaDoc className) throws IOException JavaDoc {
68         JavaSource javaSource = JavaSource.forFileObject(fileObject);
69         final boolean[] result = new boolean[1];
70         javaSource.runUserActionTask(new AbstractTask<CompilationController>() {
71             public void run(CompilationController controller) throws Exception JavaDoc {
72                 TypeElement typeElement = controller.getElements().getTypeElement(className);
73                 result[0] = isInjectionTarget(controller, typeElement);
74             }
75         }, true);
76         return result[0];
77     }
78     
79     /**
80      * Decide if injected reference must be static in given class.
81      * For example, in application client injection can be used only in class with main method and all
82      * injected fields must be static<br>
83      * Implementation
84      * @param controller CompilationController related to JavaSource
85      * @param typeElement class where annotated field or method should be inserted,rted,
86      * if null is provided, main public class from file is taken
87      * @return true if static reference is required in given class, false otherwise
88      */

89     public static boolean isStaticReferenceRequired(CompilationController controller, TypeElement typeElement) {
90         if (typeElement == null || controller==null) {
91             throw new NullPointerException JavaDoc("Passed null to InjectionTargetQuery.isStaticReferenceRequired(CompilationController, TypeElement)"); // NOI18N
92
}
93         for (InjectionTargetQueryImplementation elem : getInstances()) {
94             if (elem.isStaticReferenceRequired(controller, typeElement)) {
95                 return true;
96             }
97         }
98         return false;
99     }
100     
101     public static boolean isStaticReferenceRequired(FileObject fileObject, final String JavaDoc className) throws IOException JavaDoc {
102         JavaSource javaSource = JavaSource.forFileObject(fileObject);
103         final boolean[] result = new boolean[1];
104         javaSource.runUserActionTask(new AbstractTask<CompilationController>() {
105             public void run(CompilationController controller) throws Exception JavaDoc {
106                 TypeElement typeElement = controller.getElements().getTypeElement(className);
107                 result[0] = isStaticReferenceRequired(controller, typeElement);
108             }
109         }, true);
110         return result[0];
111     }
112     
113     private static synchronized List JavaDoc<InjectionTargetQueryImplementation> getInstances() {
114         if (implementations == null) {
115             implementations = Lookup.getDefault().lookup(new Lookup.Template<InjectionTargetQueryImplementation>(InjectionTargetQueryImplementation.class));
116             implementations.addLookupListener(new LookupListener() {
117                 public void resultChanged (LookupEvent ev) {
118                     synchronized (InjectionTargetQuery.class) {
119                         cache = null;
120                     }
121                 }});
122         }
123         if (cache == null) {
124             cache = new ArrayList JavaDoc<InjectionTargetQueryImplementation>(implementations.allInstances());
125         }
126         return cache;
127     }
128
129 }
130
Popular Tags