KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > controls > runtime > assembly > BaseAssemblyContext


1 package org.apache.beehive.controls.runtime.assembly;
2
3 /*
4  * Copyright 2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * $Header:$
19  */

20
21 import org.apache.beehive.controls.api.bean.ControlInterface;
22 import org.apache.beehive.controls.api.assembly.ControlAssemblyContext;
23 import org.apache.beehive.controls.api.assembly.ControlAssemblyException;
24 import org.apache.beehive.controls.runtime.bean.ControlBeanContext;
25
26 import java.io.File JavaDoc;
27 import java.lang.annotation.Annotation JavaDoc;
28 import java.lang.reflect.Method JavaDoc;
29 import java.util.*;
30
31 import com.sun.mirror.apt.Messager;
32 import com.sun.mirror.util.SourcePosition;
33
34 /**
35  * Abstract ControlAssemblyContext implementation. Provides a basic implementation of most non-module-specific
36  * APIs, meant to be extended by module-specific types.
37  */

38 public abstract class BaseAssemblyContext implements ControlAssemblyContext
39 {
40     protected BaseAssemblyContext( Class JavaDoc controlIntfOrExt, Map<String JavaDoc,String JavaDoc> bindings,
41                                    Set<String JavaDoc> clients, File JavaDoc moduleRoot,
42                                    String JavaDoc moduleName, File JavaDoc srcOutputRoot )
43         throws ControlAssemblyException
44     {
45         _controlIntfOrExt = controlIntfOrExt;
46         _bindings = bindings;
47         _clients = clients;
48         _moduleRoot = moduleRoot;
49         _moduleName = moduleName;
50         _srcOutputRoot = srcOutputRoot;
51         _messager = new DefaultAssemblyMessager();
52
53         // Compute and cache "most derived ControlInterface"
54
Queue<Class JavaDoc> q = new LinkedList<Class JavaDoc>();
55         Class JavaDoc ci = controlIntfOrExt;
56
57         while ( ci != null )
58         {
59             if ( ci.isAnnotationPresent(ControlInterface.class) )
60             {
61                 _controlMostDerivedIntf = ci;
62                 break;
63             }
64
65             Class JavaDoc[] supers = ci.getInterfaces();
66             for ( Class JavaDoc s : supers )
67                 q.offer( s );
68
69             ci = q.poll();
70         }
71
72         if ( _controlMostDerivedIntf == null )
73             throw new ControlAssemblyException( "Invalid control type: " + controlIntfOrExt.getName() );
74     }
75
76     public Class JavaDoc getControlType()
77     {
78         return _controlIntfOrExt;
79     }
80
81     public Class JavaDoc getMostDerivedControlInterface()
82     {
83         return _controlMostDerivedIntf;
84     }
85
86     // TODO - if we want to override class annotations on instance
87
// then here is where we will do it
88
public <T extends Annotation JavaDoc> T
89         getControlAnnotation(Class JavaDoc<T> annotationClass)
90     {
91         Class JavaDoc controlInterface = getControlType();
92         return (T)controlInterface.getAnnotation(annotationClass);
93     }
94
95     public <T extends Annotation JavaDoc> T
96         getControlMethodAnnotation(Class JavaDoc<T> annotationClass, Method JavaDoc m)
97             throws NoSuchMethodException JavaDoc
98     {
99         Class JavaDoc controlInterface = getControlType();
100         Method JavaDoc controlMethod = controlInterface.getMethod(
101                 m.getName(), m.getParameterTypes());
102
103         return (T)controlMethod.getAnnotation(annotationClass);
104     }
105
106     public String JavaDoc getDefaultImplClassName()
107     {
108         Class JavaDoc ci = getMostDerivedControlInterface();
109         ControlInterface a = (ControlInterface)
110             ci.getAnnotation(ControlInterface.class);
111
112         return ControlBeanContext.resolveDefaultBinding( a.defaultBinding(), ci.getName() );
113     }
114
115     public File JavaDoc getSrcOutputDir()
116     {
117         return _srcOutputRoot;
118     }
119
120     public File JavaDoc getModuleDir()
121     {
122         return _moduleRoot;
123     }
124
125     public String JavaDoc getModuleName()
126     {
127         return _moduleName;
128     }
129
130     public Set<String JavaDoc> getClients()
131     {
132         return _clients;
133     }
134
135     public Messager getMessager()
136     {
137         return _messager;
138     }
139
140     public boolean hasErrors()
141     {
142         return _nErrors > 0;
143     }
144
145     private class DefaultAssemblyMessager implements Messager
146     {
147         public void printError( SourcePosition pos, String JavaDoc msg )
148         {
149             printDiagnostic( "Error", pos, msg );
150             _nErrors++;
151         }
152         public void printError( String JavaDoc msg )
153         {
154             printError( null, msg );
155         }
156
157         public void printNotice( SourcePosition pos, String JavaDoc msg )
158         {
159             printDiagnostic( "Notice", pos, msg );
160         }
161         public void printNotice( String JavaDoc msg )
162         {
163             printNotice( null, msg );
164         }
165
166         public void printWarning( SourcePosition pos, String JavaDoc msg )
167         {
168             printDiagnostic( "Warning", pos, msg );
169         }
170         public void printWarning( String JavaDoc msg )
171         {
172             printWarning( null, msg );
173         }
174
175         protected void printDiagnostic( String JavaDoc type, SourcePosition pos, String JavaDoc msg )
176         {
177             String JavaDoc fn = "<not available>";
178             int line = 0;
179             int column = 0;
180
181             if ( pos != null )
182             {
183                 fn = pos.file().getName();
184                 line = pos.line();
185                 column = pos.column();
186             }
187
188             System.out.println( type + ": (" + fn + ":" + line + ":" + column + ") " + msg );
189         }
190     }
191
192     private File JavaDoc _moduleRoot;
193     private String JavaDoc _moduleName;
194     private File JavaDoc _srcOutputRoot;
195     private Class JavaDoc _controlIntfOrExt;
196     private Map<String JavaDoc,String JavaDoc> _bindings;
197     private Set<String JavaDoc> _clients;
198     private Messager _messager;
199     private int _nErrors = 0;
200
201     private Class JavaDoc _controlMostDerivedIntf;
202 }
203
Popular Tags