1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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.RegistryObject;
27 import org.apache.juddi.datatype.binding.BindingTemplate;
28 import org.apache.juddi.datatype.publisher.Publisher;
29 import org.apache.juddi.datatype.request.AuthInfo;
30 import org.apache.juddi.datatype.request.SaveBinding;
31 import org.apache.juddi.datatype.response.BindingDetail;
32 import org.apache.juddi.datatype.tmodel.TModel;
33 import org.apache.juddi.error.InvalidKeyPassedException;
34 import org.apache.juddi.error.RegistryException;
35 import org.apache.juddi.error.UserMismatchException;
36 import org.apache.juddi.registry.RegistryEngine;
37 import org.apache.juddi.util.Config;
38 import org.apache.juddi.uuidgen.UUIDGen;
39 import org.apache.juddi.uuidgen.UUIDGenFactory;
40
41 /***
42 * @author Steve Viens (sviens@apache.org)
43 */
44 public class SaveBindingFunction extends AbstractFunction
45 {
46
47 private static Log log = LogFactory.getLog(SaveBindingFunction.class);
48
49 /***
50 *
51 */
52 public SaveBindingFunction(RegistryEngine registry)
53 {
54 super(registry);
55 }
56
57 /***
58 *
59 */
60 public RegistryObject execute(RegistryObject regObject)
61 throws RegistryException
62 {
63 SaveBinding request = (SaveBinding)regObject;
64 String generic = request.getGeneric();
65 AuthInfo authInfo = request.getAuthInfo();
66 Vector bindingVector = request.getBindingTemplateVector();
67 UUIDGen uuidgen = UUIDGenFactory.getUUIDGen();
68
69
70 DataStore dataStore = DataStoreFactory.getDataStore();
71
72 try
73 {
74 dataStore.beginTrans();
75
76
77 Publisher publisher = getPublisher(authInfo,dataStore);
78 String publisherID = publisher.getPublisherID();
79
80
81 for (int i=0; i<bindingVector.size(); i++)
82 {
83
84 BindingTemplate binding = (BindingTemplate)bindingVector.elementAt(i);
85 String serviceKey = binding.getServiceKey();
86 String bindingKey = binding.getBindingKey();
87
88
89
90 if ((serviceKey == null) || (serviceKey.length() == 0) || (!dataStore.isValidServiceKey(serviceKey)))
91 throw new InvalidKeyPassedException("save_binding: "+
92 "serviceKey="+serviceKey);
93
94
95
96 if (!dataStore.isServicePublisher(serviceKey,publisherID))
97 throw new UserMismatchException("save_binding: "+
98 "publisherID="+publisherID+", "+
99 "serviceKey="+serviceKey);
100
101
102 if ((bindingKey != null) && (bindingKey.length() > 0) && (!dataStore.isValidBindingKey(bindingKey)))
103 throw new InvalidKeyPassedException("save_binding: "+
104 "bindingKey="+bindingKey);
105
106
107
108
109
110
111
112
113
114 CategoryBag categoryBag = binding.getCategoryBag();
115 if (categoryBag != null)
116 {
117 Vector keyedRefVector = categoryBag.getKeyedReferenceVector();
118 if (keyedRefVector != null)
119 {
120 int vectorSize = keyedRefVector.size();
121 if (vectorSize > 0)
122 {
123 for (int j=0; j<vectorSize; j++)
124 {
125 KeyedReference keyedRef = (KeyedReference)keyedRefVector.elementAt(j);
126 String key = keyedRef.getTModelKey();
127
128
129
130
131
132 if ((key == null) || (key.trim().length() == 0))
133 keyedRef.setTModelKey(TModel.GENERAL_KEYWORDS_TMODEL_KEY);
134 }
135 }
136 }
137 }
138 }
139
140 for (int i=0; i<bindingVector.size(); i++)
141 {
142
143 BindingTemplate binding = (BindingTemplate)bindingVector.elementAt(i);
144 String bindingKey = binding.getBindingKey();
145
146
147
148
149 if ((bindingKey != null) && (bindingKey.length() > 0))
150 dataStore.deleteBinding(bindingKey);
151 else
152 binding.setBindingKey(uuidgen.uuidgen());
153
154
155 dataStore.saveBinding(binding);
156 }
157
158 dataStore.commit();
159
160 BindingDetail detail = new BindingDetail();
161 detail.setGeneric(generic);
162 detail.setOperator(Config.getOperator());
163 detail.setTruncated(false);
164 detail.setBindingTemplateVector(bindingVector);
165 return detail;
166 }
167 catch(InvalidKeyPassedException ikpex)
168 {
169 try { dataStore.rollback(); } catch(Exception e) { }
170 log.info(ikpex);
171 throw (RegistryException)ikpex;
172 }
173 catch(UserMismatchException umex)
174 {
175 try { dataStore.rollback(); } catch(Exception e) { }
176 log.info(umex);
177 throw (RegistryException)umex;
178 }
179 catch(RegistryException regex)
180 {
181 try { dataStore.rollback(); } catch(Exception e) { }
182 log.error(regex);
183 throw (RegistryException)regex;
184 }
185 catch(Exception ex)
186 {
187 try { dataStore.rollback(); } catch(Exception e) { }
188 log.error(ex);
189 throw new RegistryException(ex);
190 }
191 finally
192 {
193 if (dataStore != null)
194 dataStore.release();
195 }
196 }
197
198
199 /****************************************************************************/
200 /****************************** TEST DRIVER *********************************/
201 /****************************************************************************/
202
203
204 public static void main(String[] args)
205 {
206
207 RegistryEngine reg = new RegistryEngine();
208 reg.init();
209
210 try
211 {
212 }
213 catch (Exception ex)
214 {
215
216 ex.printStackTrace();
217 }
218 finally
219 {
220
221 reg.dispose();
222 }
223 }
224 }
225