View Javadoc

1   /**
2    * Copyright 2005-2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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.kuali.rice.ksb.api.bus.support;
17  
18  import org.kuali.rice.core.api.util.jaxb.MapStringStringAdapter;
19  
20  import javax.xml.bind.annotation.XmlAccessType;
21  import javax.xml.bind.annotation.XmlAccessorType;
22  import javax.xml.bind.annotation.XmlElement;
23  import javax.xml.bind.annotation.XmlRootElement;
24  import javax.xml.bind.annotation.XmlType;
25  import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
26  import java.util.Collections;
27  import java.util.HashMap;
28  import java.util.Map;
29  
30  @XmlRootElement(name = RestServiceConfiguration.Constants.ROOT_ELEMENT_NAME)
31  @XmlAccessorType(XmlAccessType.NONE)
32  @XmlType(name = RestServiceConfiguration.Constants.TYPE_NAME, propOrder = {
33  		RestServiceConfiguration.Elements.RESOURCE_CLASS,
34  		RestServiceConfiguration.Elements.RESOURCE_TO_CLASS_NAME_MAP
35  })
36  public final class RestServiceConfiguration extends AbstractServiceConfiguration {
37  
38  	private static final long serialVersionUID = -4226512121638441108L;
39  
40  	@XmlElement(name = Elements.RESOURCE_CLASS, required = false)
41  	private final String resourceClass;
42  	
43  	@XmlJavaTypeAdapter(MapStringStringAdapter.class)
44  	@XmlElement(name = Elements.RESOURCE_TO_CLASS_NAME_MAP, required = false)
45  	private final Map<String, String> resourceToClassNameMap;
46  	
47  	/**
48       * Private constructor used only by JAXB.
49       */
50  	private RestServiceConfiguration() {
51  		super();
52  		this.resourceClass = null;
53  		this.resourceToClassNameMap = null;
54  	}
55  	
56  	private RestServiceConfiguration(Builder builder) {
57  		super(builder);
58  		this.resourceClass = builder.getResourceClass();
59  		if (builder.getResourceToClassNameMap() != null) {
60  			this.resourceToClassNameMap = new HashMap<String, String>(builder.getResourceToClassNameMap());
61  		} else {
62  			this.resourceToClassNameMap = Collections.emptyMap();
63  		}
64  	}
65  	
66  	public static RestServiceConfiguration fromServiceDefinition(RestServiceDefinition restServiceDefinition) {
67  		return Builder.create(restServiceDefinition).build();
68  	}
69  		
70  	public String getResourceClass() {
71  		return this.resourceClass;
72  	}
73  	
74  	/**
75  	 * Returns a map of resource names to resource classes.  Can be null if
76  	 * there are no mapped resources on this service configuration.
77  	 * 
78  	 * @return the resource to class name map, or null if no resources have
79  	 * been mapped
80  	 */
81  	public Map<String, String> getResourceToClassNameMap() {
82  		return this.resourceToClassNameMap;
83  	}
84  	
85  	/**
86  	 * @param className
87  	 * @return true if this service contains a resource for the given class name
88  	 */
89  	public boolean hasClass(String className) {
90  		if (resourceToClassNameMap == null) {
91  			return false;
92  		}
93  		return resourceToClassNameMap.containsValue(className);
94  	}
95  	
96  	public static final class Builder extends AbstractServiceConfiguration.Builder<RestServiceConfiguration> {
97  
98  		private static final long serialVersionUID = 4300659121377259098L;
99  
100 		private String resourceClass;
101 		private Map<String, String> resourceToClassNameMap;
102 				
103 		public String getResourceClass() {
104 			return resourceClass;
105 		}
106 
107 		public void setResourceClass(String resourceClass) {
108 			this.resourceClass = resourceClass;
109 		}
110 
111 		public Map<String, String> getResourceToClassNameMap() {
112 			return resourceToClassNameMap;
113 		}
114 
115 		public void setResourceToClassNameMap(Map<String, String> resourceToClassNameMap) {
116 			this.resourceToClassNameMap = resourceToClassNameMap;
117 		}
118 		
119 		private Builder() {
120 		}
121 		
122 		public static Builder create() {
123 			return new Builder();
124 		}
125 		
126 		public static Builder create(RestServiceDefinition restServiceDefinition) {
127 			Builder builder = create();
128 			builder.copyServiceDefinitionProperties(restServiceDefinition);
129 			builder.setResourceClass(restServiceDefinition.getResourceClass());
130 			if (restServiceDefinition.getResourceToClassNameMap() != null) {
131 				builder.setResourceToClassNameMap(restServiceDefinition.getResourceToClassNameMap());
132 			}
133 			return builder;
134 		}
135 
136 		@Override
137 		public RestServiceConfiguration build() {
138 			return new RestServiceConfiguration(this);
139 		}
140 		
141 	}
142 	
143 	/**
144      * Defines some internal constants used on this class.
145      */
146     static class Constants {
147     	final static String ROOT_ELEMENT_NAME = "restServiceConfiguration";
148         final static String TYPE_NAME = "RestServiceConfigurationType";
149     }
150 
151     /**
152      * A private class which exposes constants which define the XML element names to use
153      * when this object is marshalled to XML.
154      */
155      static class Elements {
156     	protected final static String RESOURCE_CLASS = "resourceClass";
157     	protected final static String RESOURCE_TO_CLASS_NAME_MAP = "resourceToClassNameMap";
158     }
159 
160 	
161 }