1 package org.apache.beehive.controls.runtime.assembly; 2 3 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 ; 27 import java.lang.annotation.Annotation ; 28 import java.lang.reflect.Method ; 29 import java.util.*; 30 31 import com.sun.mirror.apt.Messager; 32 import com.sun.mirror.util.SourcePosition; 33 34 38 public abstract class BaseAssemblyContext implements ControlAssemblyContext 39 { 40 protected BaseAssemblyContext( Class controlIntfOrExt, Map<String ,String > bindings, 41 Set<String > clients, File moduleRoot, 42 String moduleName, File 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 Queue<Class > q = new LinkedList<Class >(); 55 Class ci = controlIntfOrExt; 56 57 while ( ci != null ) 58 { 59 if ( ci.isAnnotationPresent(ControlInterface.class) ) 60 { 61 _controlMostDerivedIntf = ci; 62 break; 63 } 64 65 Class [] supers = ci.getInterfaces(); 66 for ( Class 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 getControlType() 77 { 78 return _controlIntfOrExt; 79 } 80 81 public Class getMostDerivedControlInterface() 82 { 83 return _controlMostDerivedIntf; 84 } 85 86 public <T extends Annotation > T 89 getControlAnnotation(Class <T> annotationClass) 90 { 91 Class controlInterface = getControlType(); 92 return (T)controlInterface.getAnnotation(annotationClass); 93 } 94 95 public <T extends Annotation > T 96 getControlMethodAnnotation(Class <T> annotationClass, Method m) 97 throws NoSuchMethodException 98 { 99 Class controlInterface = getControlType(); 100 Method controlMethod = controlInterface.getMethod( 101 m.getName(), m.getParameterTypes()); 102 103 return (T)controlMethod.getAnnotation(annotationClass); 104 } 105 106 public String getDefaultImplClassName() 107 { 108 Class 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 getSrcOutputDir() 116 { 117 return _srcOutputRoot; 118 } 119 120 public File getModuleDir() 121 { 122 return _moduleRoot; 123 } 124 125 public String getModuleName() 126 { 127 return _moduleName; 128 } 129 130 public Set<String > 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 msg ) 148 { 149 printDiagnostic( "Error", pos, msg ); 150 _nErrors++; 151 } 152 public void printError( String msg ) 153 { 154 printError( null, msg ); 155 } 156 157 public void printNotice( SourcePosition pos, String msg ) 158 { 159 printDiagnostic( "Notice", pos, msg ); 160 } 161 public void printNotice( String msg ) 162 { 163 printNotice( null, msg ); 164 } 165 166 public void printWarning( SourcePosition pos, String msg ) 167 { 168 printDiagnostic( "Warning", pos, msg ); 169 } 170 public void printWarning( String msg ) 171 { 172 printWarning( null, msg ); 173 } 174 175 protected void printDiagnostic( String type, SourcePosition pos, String msg ) 176 { 177 String 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 _moduleRoot; 193 private String _moduleName; 194 private File _srcOutputRoot; 195 private Class _controlIntfOrExt; 196 private Map<String ,String > _bindings; 197 private Set<String > _clients; 198 private Messager _messager; 199 private int _nErrors = 0; 200 201 private Class _controlMostDerivedIntf; 202 } 203 | Popular Tags |