1 /* 2 * Copyright 2004-2005 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package org.codehaus.groovy.grails.commons; 17 18 import grails.util.Environment; 19 20 import java.util.concurrent.ConcurrentHashMap; 21 22 /** 23 * @author Marc Palmer (marc@anyware.co.uk) 24 */ 25 public class ControllerArtefactHandler extends ArtefactHandlerAdapter { 26 27 public static final String TYPE = "Controller"; 28 public static final String PLUGIN_NAME = "controllers"; 29 private ConcurrentHashMap<String, GrailsClass> uriToControllerClassCache; 30 private ArtefactInfo artefactInfo; 31 ControllerArtefactHandler()32 public ControllerArtefactHandler() { 33 super(TYPE, GrailsControllerClass.class, DefaultGrailsControllerClass.class, 34 DefaultGrailsControllerClass.CONTROLLER, false); 35 } 36 37 @Override initialize(ArtefactInfo artefacts)38 public void initialize(ArtefactInfo artefacts) { 39 uriToControllerClassCache = new ConcurrentHashMap<String, GrailsClass>(); 40 artefactInfo = artefacts; 41 } 42 43 @Override getPluginName()44 public String getPluginName() { 45 return PLUGIN_NAME; 46 } 47 48 @Override getArtefactForFeature(Object feature)49 public GrailsClass getArtefactForFeature(Object feature) { 50 if (artefactInfo == null) { 51 return null; 52 } 53 54 String uri = feature.toString(); 55 GrailsClass controllerClass = uriToControllerClassCache.get(uri); 56 if (controllerClass == null) { 57 final GrailsClass[] controllerClasses = artefactInfo.getGrailsClasses(); 58 // iterate in reverse in order to pick up application classes first 59 for (int i = (controllerClasses.length-1); i >= 0; i--) { 60 GrailsClass c = controllerClasses[i]; 61 if (((GrailsControllerClass) c).mapsToURI(uri)) { 62 controllerClass = c; 63 break; 64 } 65 } 66 if (controllerClass != null) { 67 // don't cache for dev environment 68 if (Environment.getCurrent() != Environment.DEVELOPMENT) { 69 uriToControllerClassCache.putIfAbsent(uri, controllerClass); 70 } 71 } 72 } 73 return controllerClass; 74 } 75 } 76