1 16 package org.apache.juddi.function; 17 18 import java.util.Vector ; 19 20 import org.apache.commons.logging.Log; 21 import org.apache.commons.logging.LogFactory; 22 import org.apache.juddi.datastore.DataStore; 23 import org.apache.juddi.datastore.DataStoreFactory; 24 import org.apache.juddi.datatype.CategoryBag; 25 import org.apache.juddi.datatype.KeyedReference; 26 import org.apache.juddi.datatype.Name; 27 import org.apache.juddi.datatype.RegistryObject; 28 import org.apache.juddi.datatype.TModelBag; 29 import org.apache.juddi.datatype.request.FindQualifier; 30 import org.apache.juddi.datatype.request.FindQualifiers; 31 import org.apache.juddi.datatype.request.FindService; 32 import org.apache.juddi.datatype.response.ServiceInfos; 33 import org.apache.juddi.datatype.response.ServiceList; 34 import org.apache.juddi.datatype.tmodel.TModel; 35 import org.apache.juddi.error.NameTooLongException; 36 import org.apache.juddi.error.RegistryException; 37 import org.apache.juddi.error.TooManyOptionsException; 38 import org.apache.juddi.error.UnsupportedException; 39 import org.apache.juddi.registry.RegistryEngine; 40 import org.apache.juddi.util.Config; 41 42 45 public class FindServiceFunction extends AbstractFunction 46 { 47 private static Log log = LogFactory.getLog(FindServiceFunction.class); 49 50 53 public FindServiceFunction(RegistryEngine registry) 54 { 55 super(registry); 56 } 57 58 61 public RegistryObject execute(RegistryObject regObject) 62 throws RegistryException 63 { 64 FindService request = (FindService)regObject; 65 String generic = request.getGeneric(); 66 String businessKey = request.getBusinessKey(); 67 Vector nameVector = request.getNameVector(); 68 CategoryBag categoryBag = request.getCategoryBag(); 69 TModelBag tModelBag = request.getTModelBag(); 70 FindQualifiers qualifiers = request.getFindQualifiers(); 71 int maxRows = request.getMaxRows(); 72 73 if(((businessKey == null) || (businessKey.length() == 0)) && 77 ((nameVector == null) || (nameVector.size() == 0)) && 78 ((categoryBag == null) || (categoryBag.size() == 0)) && 79 ((tModelBag == null) || (tModelBag.size() == 0))) 80 { 81 ServiceList list = new ServiceList(); 82 list.setServiceInfos(new ServiceInfos()); 83 list.setGeneric(generic); 84 list.setOperator(Config.getOperator()); 85 list.setTruncated(false); 86 return list; 87 } 88 89 if (categoryBag != null) 91 { 92 Vector keyedRefVector = categoryBag.getKeyedReferenceVector(); 93 if (keyedRefVector != null) 94 { 95 int vectorSize = keyedRefVector.size(); 96 if (vectorSize > 0) 97 { 98 for (int i=0; i<vectorSize; i++) 99 { 100 KeyedReference keyedRef = (KeyedReference)keyedRefVector.elementAt(i); 101 String key = keyedRef.getTModelKey(); 102 103 if ((key == null) || (key.trim().length() == 0)) 108 keyedRef.setTModelKey(TModel.GENERAL_KEYWORDS_TMODEL_KEY); 109 } 110 } 111 } 112 } 113 114 DataStore dataStore = DataStoreFactory.getDataStore(); 116 117 try 118 { 119 dataStore.beginTrans(); 120 121 if (nameVector != null) 124 { 125 int maxNames = Config.getMaxNameElementsAllowed(); 128 if ((nameVector != null) && (nameVector.size() > maxNames)) 129 throw new TooManyOptionsException("find_service: "+ 130 "names="+nameVector.size()+", "+ 131 "maxNames=" + maxNames); 132 133 int maxNameLength = Config.getMaxNameLengthAllowed(); 137 for (int i=0; i<nameVector.size(); i++) 138 { 139 String name = ((Name)nameVector.elementAt(i)).getValue(); 140 if (name.length() > maxNameLength) 141 throw new NameTooLongException("find_service: "+ 142 "name="+name+", "+ 143 "length="+name.length()+", "+ 144 "maxNameLength="+maxNameLength); 145 } 146 } 147 148 if (qualifiers != null) 151 { 152 Vector qVector = qualifiers.getFindQualifierVector(); 153 if ((qVector!=null) && (qVector.size() > 0)) 154 { 155 for (int i=0; i<qVector.size(); i++) 156 { 157 FindQualifier qualifier = (FindQualifier)qVector.elementAt(i); 158 String qValue = qualifier.getValue(); 159 160 if ((!qValue.equals(FindQualifier.EXACT_NAME_MATCH)) && 161 (!qValue.equals(FindQualifier.CASE_SENSITIVE_MATCH)) && 162 (!qValue.equals(FindQualifier.OR_ALL_KEYS)) && 163 (!qValue.equals(FindQualifier.OR_LIKE_KEYS)) && 164 (!qValue.equals(FindQualifier.AND_ALL_KEYS)) && 165 (!qValue.equals(FindQualifier.SORT_BY_NAME_ASC)) && 166 (!qValue.equals(FindQualifier.SORT_BY_NAME_DESC)) && 167 (!qValue.equals(FindQualifier.SORT_BY_DATE_ASC)) && 168 (!qValue.equals(FindQualifier.SORT_BY_DATE_DESC)) && 169 (!qValue.equals(FindQualifier.SERVICE_SUBSET)) && 170 (!qValue.equals(FindQualifier.COMBINE_CATEGORY_BAGS))) 171 throw new UnsupportedException("find_service: "+ 172 "findQualifier="+qValue); 173 } 174 } 175 } 176 177 Vector infoVector = null; 178 boolean truncatedResults = false; 179 180 Vector keyVector = dataStore.findService(businessKey,nameVector,categoryBag,tModelBag,qualifiers); 182 if ((keyVector != null) && (keyVector.size() > 0)) 183 { 184 int rowCount = keyVector.size(); 188 if ((maxRows > 0) && (maxRows < rowCount)) 189 { 190 rowCount = maxRows; 191 truncatedResults = true; 192 } 193 194 infoVector = new Vector (rowCount); 197 for (int i=0; i<rowCount; i++) 198 infoVector.addElement(dataStore.fetchServiceInfo((String )keyVector.elementAt(i))); 199 } 200 201 dataStore.commit(); 202 203 ServiceInfos infos = new ServiceInfos(); 206 infos.setServiceInfoVector(infoVector); 207 208 ServiceList list = new ServiceList(); 211 list.setGeneric(generic); 212 list.setServiceInfos(infos); 213 list.setOperator(Config.getOperator()); 214 list.setTruncated(truncatedResults); 215 return list; 216 } 217 catch(TooManyOptionsException tmoex) 218 { 219 try { dataStore.rollback(); } catch(Exception e) { } 220 log.info(tmoex.getMessage()); 221 throw (RegistryException)tmoex; 222 } 223 catch(NameTooLongException ntlex) 224 { 225 try { dataStore.rollback(); } catch(Exception e) { } 226 log.info(ntlex.getMessage()); 227 throw (RegistryException)ntlex; 228 } 229 catch(UnsupportedException suppex) 230 { 231 try { dataStore.rollback(); } catch(Exception e) { } 232 log.info(suppex.getMessage()); 233 throw (RegistryException)suppex; 234 } 235 catch(RegistryException regex) 236 { 237 try { dataStore.rollback(); } catch(Exception e) { } 238 log.error(regex); 239 throw (RegistryException)regex; 240 } 241 catch(Exception ex) 242 { 243 try { dataStore.rollback(); } catch(Exception e) { } 244 log.error(ex); 245 throw new RegistryException(ex); 246 } 247 finally 248 { 249 if (dataStore != null) 250 dataStore.release(); 251 } 252 } 253 254 255 256 257 258 259 260 public static void main(String [] args) 261 { 262 RegistryEngine reg = new RegistryEngine(); 264 reg.init(); 265 266 try 267 { 268 TModelBag bag = new TModelBag(); 269 bag.addTModelKey("uuid:4C2A8920-BE0C-11D7-8920-8DA7324351E5"); 270 271 FindService request = new FindService(); 272 request.setGeneric("2.0"); 273 request.setTModelBag(bag); 274 275 ServiceList response = (ServiceList) (new FindServiceFunction(reg).execute(request)); 277 278 System.out.println(response); 279 } 280 catch (Exception ex) 281 { 282 ex.printStackTrace(); 284 } 285 finally 286 { 287 reg.dispose(); 289 } 290 } 291 } | Popular Tags |