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.plugins;
17 
18 import groovy.lang.GroovyObjectSupport;
19 import groovy.lang.MissingPropertyException;
20 
21 import java.io.IOException;
22 import java.util.HashMap;
23 import java.util.Map;
24 import java.util.concurrent.ConcurrentHashMap;
25 
26 import org.springframework.core.io.Resource;
27 
28 /**
29  * Simple Javabean implementation of the GrailsPluginInfo interface.
30  *
31  * @author Graeme Rocher
32  * @since 1.3
33  */
34 public class BasicGrailsPluginInfo extends GroovyObjectSupport implements GrailsPluginInfo {
35 
36     private String name;
37     private String version;
38     private Map<String,Object> attributes = new ConcurrentHashMap<String,Object>();
39     private Resource descriptor;
40 
BasicGrailsPluginInfo(Resource pluginLocation)41     public BasicGrailsPluginInfo(Resource pluginLocation) {
42         descriptor = pluginLocation;
43     }
44 
getName()45     public String getName() {
46         return name;
47     }
48 
setName(String name)49     public void setName(String name) {
50         this.name = name;
51     }
52 
getVersion()53     public String getVersion() {
54         return version;
55     }
56 
setVersion(String version)57     public void setVersion(String version) {
58         this.version = version;
59     }
60 
61     @Override
setProperty(String property, Object newValue)62     public void setProperty(String property, Object newValue) {
63         try {
64             super.setProperty(property, newValue);
65         }
66         catch (MissingPropertyException e) {
67             attributes.put(property, newValue);
68         }
69     }
70 
71     @Override
getProperty(String property)72     public Object getProperty(String property) {
73         try {
74             return super.getProperty(property);
75         }
76         catch (MissingPropertyException e) {
77             return attributes.get(property);
78         }
79     }
80 
getFullName()81     public String getFullName() {
82         return name + '-' + version;
83     }
84 
getDescriptor()85     public Resource getDescriptor() {
86         return this.descriptor;
87     }
88 
getPluginDir()89     public Resource getPluginDir() {
90         try {
91             return this.descriptor.createRelative(".");
92         }
93         catch (IOException e) {
94             return null;
95         }
96     }
97 
98     @SuppressWarnings({ "unchecked", "rawtypes" })
getProperties()99     public Map getProperties() {
100         Map props = new HashMap();
101         props.putAll(attributes);
102         props.put(NAME, name);
103         props.put(VERSION, version);
104         return props;
105     }
106 }
107