DefaultGriffonServiceClass.java
01 /*
02  * Copyright 2010-2012 the original author or authors.
03  
04  * Licensed under the Apache License, Version 2.0 (the "License");
05  * you may not use this file except in compliance with the License.
06  * You may obtain a copy of the License at
07  
08  *      http://www.apache.org/licenses/LICENSE-2.0
09  
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.griffon.runtime.core;
17 
18 import griffon.core.GriffonApplication;
19 import griffon.core.GriffonServiceClass;
20 import griffon.util.GriffonClassUtils;
21 import griffon.util.GriffonNameUtils;
22 import groovy.lang.Closure;
23 import groovy.lang.MetaMethod;
24 import groovy.lang.MetaProperty;
25 
26 import java.lang.reflect.Method;
27 import java.util.LinkedHashSet;
28 import java.util.Set;
29 
30 /**
31  @author Andres Almiray
32  @since 0.9.1
33  */
34 public class DefaultGriffonServiceClass extends DefaultGriffonClass implements GriffonServiceClass {
35     protected final Set<String> serviceCache = new LinkedHashSet<String>();
36 
37     public DefaultGriffonServiceClass(GriffonApplication app, Class<?> clazz) {
38         super(app, clazz, TYPE, TRAILING);
39     }
40 
41     public void resetCaches() {
42         super.resetCaches();
43         serviceCache.clear();
44     }
45 
46     public String[] getServiceNames() {
47         if (serviceCache.isEmpty()) {
48             for (String propertyName : getPropertiesWithFields()) {
49                 if (!STANDARD_PROPERTIES.contains(propertyName&&
50                         !serviceCache.contains(propertyName&&
51                         !GriffonClassUtils.isEventHandler(propertyName&&
52                         getPropertyValue(propertyName, Closure.class!= null) {
53                     serviceCache.add(propertyName);
54                 }
55             }
56             for (Method method : getClazz().getMethods()) {
57                 String methodName = method.getName();
58                 if (!serviceCache.contains(methodName&&
59                         GriffonClassUtils.isPlainMethod(method&&
60                         !GriffonClassUtils.isEventHandler(methodName)) {
61                     serviceCache.add(methodName);
62                 }
63             }
64             for (MetaProperty p : getMetaProperties()) {
65                 String propertyName = p.getName();
66                 if (GriffonClassUtils.isGetter(p, true)) {
67                     propertyName = GriffonNameUtils.uncapitalize(propertyName.substring(3));
68                 }
69                 if (!STANDARD_PROPERTIES.contains(propertyName&&
70                         !serviceCache.contains(propertyName&&
71                         !GriffonClassUtils.isEventHandler(propertyName&&
72                         isClosureMetaProperty(p)) {
73                     serviceCache.add(propertyName);
74                 }
75             }
76             for (MetaMethod method : getMetaClass().getMethods()) {
77                 String methodName = method.getName();
78                 if (!serviceCache.contains(methodName&&
79                         GriffonClassUtils.isPlainMethod(method&&
80                         !GriffonClassUtils.isEventHandler(methodName)) {
81                     serviceCache.add(methodName);
82                 }
83             }
84         }
85 
86         return serviceCache.toArray(new String[serviceCache.size()]);
87     }
88 }