View Javadoc
1   /*
2    * Copyright 2001-2010 The Apache Software Foundation.
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   */
17  package org.apache.juddi.v3.annotations;
18  
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.Properties;
22  
23  import javax.jws.WebService;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.juddi.api_v3.AccessPointType;
28  import org.apache.juddi.v3.client.ClassUtil;
29  import org.apache.juddi.v3.client.config.TokenResolver;
30  import org.uddi.api_v3.AccessPoint;
31  import org.uddi.api_v3.BindingTemplate;
32  import org.uddi.api_v3.BindingTemplates;
33  import org.uddi.api_v3.BusinessService;
34  import org.uddi.api_v3.CategoryBag;
35  import org.uddi.api_v3.Description;
36  import org.uddi.api_v3.KeyedReference;
37  import org.uddi.api_v3.Name;
38  import org.uddi.api_v3.TModelInstanceDetails;
39  import org.uddi.api_v3.TModelInstanceInfo;
40  
41  public class AnnotationProcessor {
42  	
43  	private static final String KEYED_REFERENCE="keyedReference=";
44  	private static final String KEY_NAME="keyName=";
45  	private static final String KEY_VALUE="keyValue=";
46  	private static final String TMODEL_KEY="tModelKey=";
47  	
48  	private Log log = LogFactory.getLog(AnnotationProcessor.class);
49  	
50  	public Collection<BusinessService> readServiceAnnotations(String[] classesWithAnnotations, Properties properties) {
51  		Collection<BusinessService> services = new ArrayList<BusinessService>();
52  		for (String className : classesWithAnnotations) {
53  			try {		
54  				BusinessService service = readServiceAnnotations(className, properties);
55  				services.add(service);
56  			} catch (Exception e) {
57  				e.printStackTrace();
58  			}
59  		}
60  		return services;
61  	}
62  	
63  	public BusinessService readServiceAnnotations(String classWithAnnotations, Properties properties) throws ClassNotFoundException {
64  		
65  		BusinessService service = new BusinessService();
66  		Class<?> clazz = ClassUtil.forName(classWithAnnotations, this.getClass());
67  		UDDIService uddiService= (UDDIService) clazz.getAnnotation(UDDIService.class);
68  		WebService webServiceAnnotation = (WebService) clazz.getAnnotation(WebService.class);
69  		
70  		if (uddiService!=null) {
71  			//service
72  			String lang = "en"; //default to english
73  			if (uddiService.lang()!=null) {
74  				lang = uddiService.lang();
75  			}
76  			Name name = new Name();
77  			name.setLang(lang); 
78  			service.setBusinessKey(TokenResolver.replaceTokens(uddiService.businessKey(),properties));
79  			service.setServiceKey(TokenResolver.replaceTokens(uddiService.serviceKey(),properties));
80  			if (!"".equals(uddiService.serviceName())) {
81  				name.setValue(TokenResolver.replaceTokens(uddiService.serviceName(),properties));
82  			} else if (webServiceAnnotation!=null && !"".equals(webServiceAnnotation.serviceName())) {
83  				name.setValue(webServiceAnnotation.serviceName());
84  			} else {
85  				name.setValue(clazz.getSimpleName());
86  			}
87  			service.getName().add(name);
88  			Description description = new Description();
89  			description.setLang(lang);
90  			description.setValue(TokenResolver.replaceTokens(uddiService.description(),properties));
91  			service.getDescription().add(description);
92  			
93  			//categoryBag on the service
94  			if (!"".equals(uddiService.categoryBag())) {
95  				CategoryBag categoryBag = parseCategoryBag(uddiService.categoryBag());
96  		        service.setCategoryBag(categoryBag);
97  			}
98  			
99  			//bindingTemplate on service
100 			BindingTemplate bindingTemplate = parseServiceBinding(clazz, lang, webServiceAnnotation, properties);
101 			if (bindingTemplate!=null) {
102 				bindingTemplate.setServiceKey(service.getServiceKey());
103 				if (service.getBindingTemplates()==null) {
104 					service.setBindingTemplates(new BindingTemplates());
105 				}
106 				service.getBindingTemplates().getBindingTemplate().add(bindingTemplate);
107 			}
108 			
109 			
110 		} else {
111 			log.error("Missing UDDIService annotation in class " + classWithAnnotations);
112 		}
113 			
114 		return service;
115 	}
116 	
117 	protected BindingTemplate parseServiceBinding(Class<?> classWithAnnotations, String lang, 
118 			WebService webServiceAnnotation, Properties properties) {
119 		
120 		BindingTemplate bindingTemplate = null;
121 		UDDIServiceBinding uddiServiceBinding= (UDDIServiceBinding) classWithAnnotations.getAnnotation(UDDIServiceBinding.class);
122 		//binding
123 		if (uddiServiceBinding!=null) {
124 			bindingTemplate = new BindingTemplate();
125 			
126 			bindingTemplate.setBindingKey(TokenResolver.replaceTokens(uddiServiceBinding.bindingKey(), properties));
127 			
128 			String bindingLang = String.valueOf(lang);
129 			if (uddiServiceBinding.lang()!=null) {
130 				bindingLang = TokenResolver.replaceTokens(uddiServiceBinding.lang(),properties);
131 			}
132 			Description bindingDescription = new Description();
133 			bindingDescription.setLang(bindingLang);
134 			bindingDescription.setValue(TokenResolver.replaceTokens(uddiServiceBinding.description(),properties));
135 			bindingTemplate.getDescription().add(bindingDescription);
136 			
137 			AccessPoint accessPoint = new AccessPoint();
138 			accessPoint.setUseType(AccessPointType.WSDL_DEPLOYMENT.toString());
139 			if (!"".equals(uddiServiceBinding.accessPointType())) {
140 				accessPoint.setUseType(uddiServiceBinding.accessPointType());
141 			}
142 			if (!"".equals(uddiServiceBinding.accessPoint())) {
143 				String endPoint = uddiServiceBinding.accessPoint();
144 				endPoint = TokenResolver.replaceTokens(endPoint, properties);
145                 log.debug("AccessPoint EndPoint=" + endPoint);
146 				accessPoint.setValue(endPoint);
147 			} else if (webServiceAnnotation!=null && webServiceAnnotation.wsdlLocation()!=null) {
148 				accessPoint.setValue(webServiceAnnotation.wsdlLocation());
149 			}
150 			bindingTemplate.setAccessPoint(accessPoint);
151 			
152 			//tModelKeys on the binding
153 			if (!"".equals(uddiServiceBinding.tModelKeys())) {
154 				String[] tModelKeys= uddiServiceBinding.tModelKeys().split(",");
155 				for (String tModelKey : tModelKeys) {
156 					TModelInstanceInfo instanceInfo = new TModelInstanceInfo();
157 					instanceInfo.setTModelKey(tModelKey);
158 					if (bindingTemplate.getTModelInstanceDetails()==null) {
159 						bindingTemplate.setTModelInstanceDetails(new TModelInstanceDetails());
160 					}
161 					bindingTemplate.getTModelInstanceDetails().getTModelInstanceInfo().add(instanceInfo);
162 				}
163 			}
164 			//categoryBag on the binding
165 			if (!"".equals(uddiServiceBinding.categoryBag())) {
166 				CategoryBag categoryBag = parseCategoryBag(uddiServiceBinding.categoryBag());
167 		        bindingTemplate.setCategoryBag(categoryBag);
168 			}
169 		} else {
170 			log.error("Missing UDDIServiceBinding annotation in class " + classWithAnnotations);
171 		}
172 		return bindingTemplate;
173 	}
174 	/**
175 	 * parse something like: [keyName=uddi-org:types:wsdl,keyValue=wsdlDeployment,tModelKey=uddi:uddi.org:categorization:types]
176 							
177 	 * @param categoryBagStr
178 	 * @return a UDDI CategoryBag
179          * @see CategoryBag
180 	 */
181 	protected CategoryBag parseCategoryBag(String categoryBagStr) {
182 		
183 		CategoryBag categoryBag = new CategoryBag();
184 		log.debug("CategoryBag Annotation=" + categoryBagStr);
185 		if (!"".equals(categoryBagStr)) {
186 			String[] sections = categoryBagStr.split(",");
187 	        for (String section : sections) {
188 	        	if (section.startsWith(KEYED_REFERENCE)) {
189 		            String keyedReferenceStr = section.substring(KEYED_REFERENCE.length(),section.length());
190 		            log.debug("Found KeyedReference=" + keyedReferenceStr);
191 		            String[] keyedReferences = keyedReferenceStr.split(";");
192 		            KeyedReference keyedReference = new KeyedReference();
193 		            for (String key : keyedReferences) {
194 						if (key.startsWith(KEY_NAME)) keyedReference.setKeyName(key.substring(KEY_NAME.length(),key.length()));
195 						if (key.startsWith(KEY_VALUE)) keyedReference.setKeyValue(key.substring(KEY_VALUE.length(),key.length()));
196 						if (key.startsWith(TMODEL_KEY)) keyedReference.setTModelKey(key.substring(TMODEL_KEY.length(),key.length()));
197 					}
198 		            log.debug("KeyedReference = " + KEY_NAME + keyedReference.getKeyName() + " " 
199 		            		                      + KEY_VALUE + keyedReference.getKeyValue() + " "
200 		            		                      + TMODEL_KEY + keyedReference.getTModelKey());
201 		            categoryBag.getKeyedReference().add(keyedReference);
202 	        	} else {
203 	        		log.warn("Ignoring " + section);
204 	                 //TODO add support for KeyedReferenceGroups?
205 	        	}
206 	        }
207         }
208         return categoryBag;
209 	}
210 }