*
* @return The root model.
*/
@Override
protected EnunciateFreemarkerModel getRootModel() throws TemplateModelException {
EnunciateConfiguration config = this.enunciate.getConfig();
EnunciateFreemarkerModel model = (EnunciateFreemarkerModel) super.getRootModel();
model.setEnunciateConfig(config);
//build up the list of all classes to which we are going to apply enunciate.
TypeDeclaration[] additionalApiDefinitions = loadAdditionalApiDefinitions();
AnnotationProcessorEnvironment env = Context.getCurrentEnvironment();
Collection<TypeDeclaration> typeDeclarations = new ArrayList<TypeDeclaration>(env.getTypeDeclarations());
typeDeclarations.addAll(Arrays.asList(additionalApiDefinitions));
//trim the classes that are not "include"d.
trimNotIncludedClasses(typeDeclarations);
//remove any explicitly-excluded classes
removeExcludedClasses(typeDeclarations);
//override any namespace prefix mappings as specified in the config.
for (String ns : config.getNamespacesToPrefixes().keySet()) {
String prefix = config.getNamespacesToPrefixes().get(ns);
model.getNamespacesToPrefixes().put(ns, prefix);
debug("Assigning namespace prefix %s to namespace %s as specified in the config.", prefix, ns);
}
//override any content type ids as specified in the config.
for (String ct : config.getContentTypesToIds().keySet()) {
String id = config.getContentTypesToIds().get(ct);
model.getContentTypesToIds().put(ct, id);
debug("Assigning id '%s' to content type '%s' as specified in the config.", id, ct);
}
if (config.getGeneratedCodeLicenseFile() != null) {
File licenseFile = this.enunciate.resolvePath(config.getGeneratedCodeLicenseFile());
try {
model.put("generatedCodeLicense", this.enunciate.readFile(licenseFile));
}
catch (IOException e) {
warn("Unable to read code license file %s: %s.", licenseFile.getAbsolutePath(), e.getMessage());
}
}
String baseURL = config.getDeploymentProtocol() + "://" + config.getDeploymentHost();
if (config.getDeploymentContext() != null) {
baseURL += config.getDeploymentContext();
}
else if ((config.getLabel() != null) && (!"".equals(config.getLabel()))) {
//we don't have a default context set, so we'll just guess that it's the project label.
baseURL += ("/" + config.getLabel());
}
model.setBaseDeploymentAddress(baseURL);
List<EnunciateTypeDeclarationListener> typeDeclarationListeners = new ArrayList<EnunciateTypeDeclarationListener>();
for (DeploymentModule module : config.getAllModules()) {
if (module instanceof EnunciateTypeDeclarationListener) {
typeDeclarationListeners.add((EnunciateTypeDeclarationListener) module);
}
}
debug("Reading classes to enunciate...");
final List<EndpointInterface> eis = new ArrayList<EndpointInterface>();
for (TypeDeclaration declaration : typeDeclarations) {
final boolean isEndpointInterface = isEndpointInterface(declaration);
final boolean isJAXRSRootResource = isJAXRSRootResource(declaration);
final boolean isJAXRSSupport = isJAXRSSupport(declaration);
if (isEndpointInterface || isJAXRSRootResource || isJAXRSSupport) {
if (isEndpointInterface) {
EndpointInterface endpointInterface = new EndpointInterface(declaration, additionalApiDefinitions);
debug("%s to be considered as an endpoint interface.", declaration.getQualifiedName());
for (EndpointImplementation implementation : endpointInterface.getEndpointImplementations()) {
debug("%s is the implementation of endpoint interface %s.", implementation.getQualifiedName(), endpointInterface.getQualifiedName());
}
eis.add(endpointInterface);
}
if (isJAXRSRootResource) {
RootResource rootResource = new RootResource(declaration);
debug("%s to be considered as a JAX-RS root resource.", declaration.getQualifiedName());
model.add(rootResource);
}
if (isJAXRSSupport) {
if (declaration.getAnnotation(Provider.class) != null) {
debug("%s to be considered as a JAX-RS provider.", declaration.getQualifiedName());
model.addJAXRSProvider(declaration);
}
else {
debug("%s to be considered a JAX-RS support class.", declaration.getQualifiedName());
}
}
}
else if (isRegistry(declaration)) {
debug("%s to be considered as an XML registry.", declaration.getQualifiedName());
Registry registry = new Registry((ClassDeclaration) declaration);
model.add(registry);
}
else if (isJAXRSApplication(declaration)) {
debug("%s is identified as a JAX-RS Application class.", declaration.getQualifiedName());
ApplicationPath applicationPath = declaration.getAnnotation(ApplicationPath.class);
if (applicationPath != null) {
try {
URI uri = URI.create(applicationPath.value());
String path = uri.getPath();
if (config.getDeploymentContext() != null && path.startsWith(config.getDeploymentContext())) {
path = path.substring(config.getDeploymentContext().length());
}
config.setDefaultRestSubcontextConditionally(path);
}
catch (Exception e) {
warn("Invalid URI: %s (%s)", applicationPath.value(), e.getMessage());
}
}