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.Description; 26 import org.apache.juddi.datatype.KeyedReference; 27 import org.apache.juddi.datatype.Name; 28 import org.apache.juddi.datatype.RegistryObject; 29 import org.apache.juddi.datatype.business.BusinessEntity; 30 import org.apache.juddi.datatype.publisher.Publisher; 31 import org.apache.juddi.datatype.request.AuthInfo; 32 import org.apache.juddi.datatype.request.GetAuthToken; 33 import org.apache.juddi.datatype.request.SaveBusiness; 34 import org.apache.juddi.datatype.request.SaveService; 35 import org.apache.juddi.datatype.response.AuthToken; 36 import org.apache.juddi.datatype.response.BusinessDetail; 37 import org.apache.juddi.datatype.response.ServiceDetail; 38 import org.apache.juddi.datatype.service.BusinessService; 39 import org.apache.juddi.datatype.service.BusinessServices; 40 import org.apache.juddi.datatype.tmodel.TModel; 41 import org.apache.juddi.error.InvalidKeyPassedException; 42 import org.apache.juddi.error.RegistryException; 43 import org.apache.juddi.error.UserMismatchException; 44 import org.apache.juddi.registry.RegistryEngine; 45 import org.apache.juddi.util.Config; 46 import org.apache.juddi.uuidgen.UUIDGen; 47 import org.apache.juddi.uuidgen.UUIDGenFactory; 48 49 52 public class SaveServiceFunction extends AbstractFunction 53 { 54 private static Log log = LogFactory.getLog(SaveServiceFunction.class); 56 57 60 public SaveServiceFunction(RegistryEngine registry) 61 { 62 super(registry); 63 } 64 65 68 public RegistryObject execute(RegistryObject regObject) 69 throws RegistryException 70 { 71 SaveService request = (SaveService)regObject; 72 String generic = request.getGeneric(); 73 AuthInfo authInfo = request.getAuthInfo(); 74 Vector serviceVector = request.getBusinessServiceVector(); 75 UUIDGen uuidgen = UUIDGenFactory.getUUIDGen(); 76 77 DataStore dataStore = DataStoreFactory.getDataStore(); 79 80 try 81 { 82 dataStore.beginTrans(); 83 84 Publisher publisher = getPublisher(authInfo,dataStore); 86 String publisherID = publisher.getPublisherID(); 87 88 for (int i=0; i<serviceVector.size(); i++) 90 { 91 BusinessService service = (BusinessService)serviceVector.elementAt(i); 93 String businessKey = service.getBusinessKey(); 94 String serviceKey = service.getServiceKey(); 95 96 if ((businessKey == null) || (businessKey.length() == 0) || (!dataStore.isValidBusinessKey(businessKey))) 99 throw new InvalidKeyPassedException("save_service: "+ 100 "businessKey="+businessKey); 101 102 if (!dataStore.isBusinessPublisher(businessKey,publisherID)) 105 throw new UserMismatchException("save_service: "+ 106 "userID="+publisherID+", "+ 107 "businessKey="+serviceKey); 108 109 if (((serviceKey != null) && (serviceKey.length() > 0)) && (!dataStore.isValidServiceKey(serviceKey))) 111 throw new InvalidKeyPassedException("save_service: "+ 112 "serviceKey="+serviceKey); 113 114 CategoryBag categoryBag = service.getCategoryBag(); 123 if (categoryBag != null) 124 { 125 Vector keyedRefVector = categoryBag.getKeyedReferenceVector(); 126 if (keyedRefVector != null) 127 { 128 int vectorSize = keyedRefVector.size(); 129 if (vectorSize > 0) 130 { 131 for (int j=0; j<vectorSize; j++) 132 { 133 KeyedReference keyedRef = (KeyedReference)keyedRefVector.elementAt(j); 134 String key = keyedRef.getTModelKey(); 135 136 if ((key == null) || (key.trim().length() == 0)) 141 keyedRef.setTModelKey(TModel.GENERAL_KEYWORDS_TMODEL_KEY); 142 } 143 } 144 } 145 } 146 } 147 148 for (int i=0; i<serviceVector.size(); i++) 149 { 150 BusinessService service = (BusinessService)serviceVector.elementAt(i); 152 String serviceKey = service.getServiceKey(); 153 154 if ((serviceKey != null) && (serviceKey.length() > 0)) 158 dataStore.deleteService(serviceKey); 159 else 160 service.setServiceKey(uuidgen.uuidgen()); 161 162 dataStore.saveService(service); 164 } 165 166 dataStore.commit(); 167 168 ServiceDetail detail = new ServiceDetail(); 171 detail.setGeneric(generic); 172 detail.setOperator(Config.getOperator()); 173 detail.setTruncated(false); 174 detail.setBusinessServiceVector(serviceVector); 175 return detail; 176 } 177 catch(InvalidKeyPassedException ikpex) 178 { 179 try { dataStore.rollback(); } catch(Exception e) { } 180 log.info(ikpex); 181 throw (RegistryException)ikpex; 182 } 183 catch(UserMismatchException umex) 184 { 185 try { dataStore.rollback(); } catch(Exception e) { } 186 log.info(umex); 187 throw (RegistryException)umex; 188 } 189 catch(RegistryException regex) 190 { 191 try { dataStore.rollback(); } catch(Exception e) { } 192 log.error(regex); 193 throw (RegistryException)regex; 194 } 195 catch(Exception ex) 196 { 197 try { dataStore.rollback(); } catch(Exception e) { } 198 log.error(ex); 199 throw new RegistryException(ex); 200 } 201 finally 202 { 203 if (dataStore != null) 204 dataStore.release(); 205 } 206 } 207 208 209 210 211 212 213 214 public static void main(String [] args) 215 { 216 RegistryEngine reg = new RegistryEngine(); 218 reg.init(); 219 220 try 221 { 222 GetAuthToken authTokenRequest = new GetAuthToken("sviens","password"); 224 AuthToken authToken = (AuthToken)reg.execute(authTokenRequest); 225 AuthInfo authInfo = authToken.getAuthInfo(); 226 227 Vector nameVector = new Vector (); 229 nameVector.add(new Name("Dow Chemical")); 230 231 BusinessService service = new BusinessService(); 233 service.addName(new Name("Reaction Finder")); 234 service.addDescription(new Description("Finds side effects when combining chemicals")); 235 236 BusinessServices services = new BusinessServices(); 238 services.addBusinessService(service); 239 240 BusinessEntity businessEntity = new BusinessEntity(); 242 businessEntity.setBusinessKey(null); 243 businessEntity.setNameVector(nameVector); 244 businessEntity.setBusinessServices(services); 245 246 Vector businessEntityVector = new Vector (); 248 businessEntityVector.add(businessEntity); 249 250 SaveBusiness request = new SaveBusiness(); 252 request.setAuthInfo(authInfo); 253 request.setBusinessEntityVector(businessEntityVector); 254 BusinessDetail detail = (BusinessDetail)reg.execute(request); 255 } 256 catch (Exception ex) 257 { 258 ex.printStackTrace(); 260 } 261 finally 262 { 263 reg.dispose(); 265 } 266 } 267 } | Popular Tags |