View Javadoc
1   /*
2    * Copyright 2013 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  package org.apache.juddi.v3.migration.tool;
17  
18  import java.io.File;
19  import java.io.FileInputStream;
20  import java.util.HashSet;
21  import java.util.Iterator;
22  import java.util.Properties;
23  import java.util.Set;
24  import javax.xml.bind.JAXB;
25  import org.apache.juddi.api_v3.GetPublisherDetail;
26  import org.apache.juddi.api_v3.PublisherDetail;
27  import org.apache.juddi.api_v3.SavePublisher;
28  import org.apache.juddi.v3.client.config.UDDIClerk;
29  import org.apache.juddi.v3.client.config.UDDIClient;
30  import org.apache.juddi.v3.client.config.UDDIClientContainer;
31  import org.apache.juddi.v3.client.cryptor.XmlUtils;
32  import org.apache.juddi.v3.client.transport.Transport;
33  import org.apache.juddi.v3_service.JUDDIApiPortType;
34  import org.uddi.api_v3.BusinessDetail;
35  import org.uddi.api_v3.GetAuthToken;
36  import org.uddi.api_v3.GetBusinessDetail;
37  import org.uddi.api_v3.GetTModelDetail;
38  import org.uddi.api_v3.SaveBusiness;
39  import org.uddi.api_v3.SaveTModel;
40  import org.uddi.api_v3.TModelDetail;
41  import org.uddi.v3_service.UDDIInquiryPortType;
42  import org.uddi.v3_service.UDDIPublicationPortType;
43  import org.uddi.v3_service.UDDISecurityPortType;
44  
45  /**
46   * Provides UDDI import functions
47   *
48   * @author Alex O'Ree
49   * @since 3.2
50   */
51  public class Import {
52  
53          private static UDDISecurityPortType security = null;
54          private static JUDDIApiPortType juddiApi = null;
55          private static UDDIPublicationPortType publish = null;
56          private static UDDIInquiryPortType inquiry;
57          private JUDDIApiPortType juddi;
58          String token = null;
59          String username = null;
60          String pass=null;
61          String tmodelfile = null;
62          String businessfile = null;
63          String publishersfile = null;
64          String mappingsfile = null;
65          boolean safemode = false;
66          boolean stripSig = false;
67          boolean preserveOwnership = false;
68          Set<String> usernames = new HashSet<String>();
69          /**
70           * item to user
71           */
72          Properties mapping = new Properties();
73          /**
74           * user to password
75           */
76          Properties userpwd = new Properties();
77          String credFile;
78  
79          public void Execute(String config, String name, String username, String pass,
80                  String tmodelIn, String businessIn,
81                  boolean isJuddi, boolean safe, String publishersFile,
82                  boolean preserveOwnership,
83                  String mappingsfile, String credFile, boolean stripSig) throws Exception {
84  
85                  this.safemode = safe;
86                  this.credFile = credFile;
87                  this.stripSig = stripSig;
88                  this.publishersfile = publishersFile;
89                  this.tmodelfile = tmodelIn;
90                  this.businessfile = businessIn;
91                  this.preserveOwnership = preserveOwnership;
92                  this.mappingsfile = mappingsfile;
93  
94                  // create a manager and read the config in the archive; 
95                  // you can use your config file name
96                  UDDIClient clerkManager = new UDDIClient(config);
97                  clerkManager.start();
98                  UDDIClerk clerk = clerkManager.getClerk(name);
99                  // a ClerkManager can be a client to multiple UDDI nodes, so 
100                 // supply the nodeName (defined in your uddi.xml.
101                 // The transport can be WS, inVM, etc which is defined in the uddi.xml
102                 Transport transport = clerkManager.getTransport(name);
103                 // Now you create a reference to the UDDI API
104                 security = transport.getUDDISecurityService();
105                 publish = transport.getUDDIPublishService();
106                 inquiry = transport.getUDDIInquiryService();
107                 juddi = transport.getJUDDIApiService();
108                 token = null;
109                 if (username == null || pass == null) {
110                         this.username = clerk.getPublisher();
111                         this.pass = clerk.getPassword();
112                 }
113                 else
114                 {
115                         this.username=username;
116                         this.pass=pass;
117                 }
118                 token = Common.GetAuthToken(username, pass, security);
119                 
120 
121                 //load mapping files
122                 //prompt for credentials
123                 LoadProperties();
124 
125                 if (isJuddi) {
126                         ImportPublishers();
127                 }
128 
129                 if (preserveOwnership) {
130                         //we'll need credentials for all the users
131                         EnsureCredentials();
132                 }
133 
134 
135 
136                 ImportTmodels();
137                 ImportBusiness();
138 
139 
140 
141         }
142 
143         public String GetOwner(String key) {
144                 return mapping.getProperty(key);
145         }
146 
147         public String GetPwd(String username) {
148                 return userpwd.getProperty(username);
149         }
150 
151         private void ImportTmodels() throws Exception {
152                 
153                 SaveTModel stm = (SaveTModel) XmlUtils.unmarshal(new File(tmodelfile), SaveTModel.class);
154                 System.out.println();
155                 System.out.println("Attempting to save " + stm.getTModel().size() + " tModels");
156                 if (stripSig) {
157                         int x=0;
158                         for (int i = 0; i < stm.getTModel().size(); i++) {
159                                 x+=stm.getTModel().get(i).getSignature().size();
160                                 stm.getTModel().get(i).getSignature().clear();
161                         }
162                         System.out.println(x + " signatures stripped");
163                 }
164 
165                 if (safemode) {
166                         for (int i = 0; i < stm.getTModel().size(); i++) {
167                                 if (!TModelExists(stm.getTModel().get(i).getTModelKey(), token)) {
168                                         SaveTModel stm2 = new SaveTModel();
169                                         if (preserveOwnership) {
170                                                 stm2.setAuthInfo(Common.GetAuthToken(
171                                                         GetOwner(stm.getTModel().get(i).getTModelKey()),
172                                                         GetPwd(GetOwner(stm.getTModel().get(i).getTModelKey())), security));
173                                         } else {
174                                                 stm2.setAuthInfo(token);
175                                         }
176                                         stm2.getTModel().add(stm.getTModel().get(i));
177                                         try {
178                                                 publish.saveTModel(stm2);
179                                                 System.out.println(stm.getTModel().get(i).getTModelKey() + " saved");
180                                         } catch (Exception ex) {
181                                                 System.out.println("Error saving " + stm.getTModel().get(i).getTModelKey() + " " + ex.getMessage());
182                                         }
183                                 } else {
184                                         System.out.println(stm.getTModel().get(i).getTModelKey() + " skipped, it exists already");
185                                 }
186                         }
187                 } else {
188                         stm.setAuthInfo(token);
189 
190                         publish.saveTModel(stm);
191                         System.out.println("All " + stm.getTModel().size() + " saved!");
192                 }
193 
194         }
195 
196         private void ImportBusiness() throws Exception {
197                 SaveBusiness stm = (SaveBusiness) XmlUtils.unmarshal(new File(businessfile), SaveBusiness.class);
198                 if (stripSig) {
199                         int x=0;
200                         for (int i = 0; i < stm.getBusinessEntity().size(); i++) {
201                                 x +=  stm.getBusinessEntity().get(i).getSignature().size();
202                                 stm.getBusinessEntity().get(i).getSignature().clear();
203                                 if (stm.getBusinessEntity().get(i).getBusinessServices() != null) {
204                                         for (int i2 = 0; i2 < stm.getBusinessEntity().get(i).getBusinessServices().getBusinessService().size(); i2++) {
205                                                 x +=  stm.getBusinessEntity().get(i).getBusinessServices().getBusinessService().get(i2).getSignature().size();
206                                                 stm.getBusinessEntity().get(i).getBusinessServices().getBusinessService().get(i2).getSignature().clear();
207                                                 if (stm.getBusinessEntity().get(i).getBusinessServices().getBusinessService().get(i2).getBindingTemplates() != null) {
208                                                         for (int i3 = 0; i3 < stm.getBusinessEntity().get(i).getBusinessServices().getBusinessService().get(i2).getBindingTemplates().getBindingTemplate().size(); i3++) {
209                                                                 x += stm.getBusinessEntity().get(i).getBusinessServices().getBusinessService().get(i2).getBindingTemplates().getBindingTemplate().get(i3).getSignature().size();
210                                                                 stm.getBusinessEntity().get(i).getBusinessServices().getBusinessService().get(i2).getBindingTemplates().getBindingTemplate().get(i3).getSignature().clear();
211                                                         }
212                                                 }
213                                         }
214                                 }
215                         }
216                         System.out.println(x + " signatures stripped");
217                 }
218 
219                 if (safemode) {
220                         for (int i = 0; i < stm.getBusinessEntity().size(); i++) {
221                                 if (!BusinessExists(stm.getBusinessEntity().get(i).getBusinessKey(), token)) {
222                                         SaveBusiness stm2 = new SaveBusiness();
223                                         if (preserveOwnership) {
224                                                 stm2.setAuthInfo(Common.GetAuthToken(GetOwner(stm.getBusinessEntity().get(i).getBusinessKey()), GetPwd(GetOwner(stm.getBusinessEntity().get(i).getBusinessKey())), security));
225                                         } else {
226                                                 stm2.setAuthInfo(token);
227                                         }
228                                         stm2.getBusinessEntity().add(stm.getBusinessEntity().get(i));
229                                         try {
230                                                 publish.saveBusiness(stm2);
231                                                 System.out.println(stm.getBusinessEntity().get(i).getBusinessKey() + " saved");
232                                         } catch (Exception ex) {
233                                                 System.out.println("Error saving " + stm.getBusinessEntity().get(i).getBusinessKey() + " " + ex.getMessage());
234                                         }
235                                 } else {
236                                         System.out.println(stm.getBusinessEntity().get(i).getBusinessKey() + " skipped, it exists already");
237                                 }
238                         }
239                 } else {
240                         stm.setAuthInfo(token);
241                         publish.saveBusiness(stm);
242                         System.out.println("All " + stm.getBusinessEntity().size() + " businesses aved!");
243                 }
244         }
245 
246         private boolean TModelExists(String tModelKey, String token) {
247                 GetTModelDetail r = new GetTModelDetail();
248                 r.setAuthInfo(token);
249                 r.getTModelKey().add(tModelKey);
250                 try {
251                         TModelDetail tModelDetail = inquiry.getTModelDetail(r);
252                         if (tModelDetail != null && !tModelDetail.getTModel().isEmpty()) {
253                                 return true;
254                         }
255                 } catch (Exception ex) {
256                 }
257                 return false;
258         }
259 
260         private boolean BusinessExists(String businessKey, String token) {
261                 GetBusinessDetail r = new GetBusinessDetail();
262                 r.setAuthInfo(token);
263                 r.getBusinessKey().add(businessKey);
264                 try {
265                         BusinessDetail tModelDetail = inquiry.getBusinessDetail(r);
266                         if (tModelDetail != null && !tModelDetail.getBusinessEntity().isEmpty()) {
267                                 return true;
268                         }
269                 } catch (Exception ex) {
270                 }
271                 return false;
272         }
273 
274         private void ImportPublishers() throws Exception {
275 
276                 SavePublisher stm = (SavePublisher) XmlUtils.unmarshal(new File(publishersfile), SavePublisher.class);
277                 if (safemode) {
278                         for (int i = 0; i < stm.getPublisher().size(); i++) {
279                                 if (!PublisherExists(stm.getPublisher().get(i).getAuthorizedName(), token)) {
280                                         SavePublisher stm2 = new SavePublisher();
281                                         stm2.setAuthInfo(Common.GetAuthToken(username, pass, security));
282                                         stm2.getPublisher().add(stm.getPublisher().get(i));
283                                         try {
284                                                 juddi.savePublisher(stm2);
285                                                 System.out.println(stm.getPublisher().get(i).getAuthorizedName() + " saved");
286                                         } catch (Exception ex) {
287                                                 System.out.println("Error saving " + stm.getPublisher().get(i).getAuthorizedName() + " " + ex.getMessage());
288                                         }
289                                 } else {
290                                         System.out.println(stm.getPublisher().get(i).getAuthorizedName() + " skipped, it exists already");
291                                 }
292                         }
293                 } else {
294                         stm.setAuthInfo(token);
295                         juddi.savePublisher(stm);
296                         System.out.println("All " + stm.getPublisher().size() + " publishers saved!");
297                 }
298         }
299 
300         private boolean PublisherExists(String authorizedName, String token) {
301                 GetPublisherDetail r = new GetPublisherDetail();
302                 r.setAuthInfo(token);
303                 r.getPublisherId().add(authorizedName);
304                 try {
305                         PublisherDetail publisherDetail = juddi.getPublisherDetail(r);
306                         if (publisherDetail != null && !publisherDetail.getPublisher().isEmpty()) {
307                                 return true;
308                         }
309                 } catch (Exception ex) {
310                 }
311                 return false;
312         }
313 
314         private void LoadProperties() {
315 
316 
317                 try {
318                         FileInputStream fos = new FileInputStream(mappingsfile);
319                         mapping.load(fos);
320                         System.out.println(mapping.size() + " entity to user mappings loaded");
321                         fos.close();
322                 } catch (Exception ex) {
323                         ex.printStackTrace();
324                 }
325 
326                 //mapping.getProperty("usernames");
327 
328                 try {
329                         FileInputStream fos = new FileInputStream(credFile);
330                         userpwd.load(fos);
331                         System.out.println(userpwd.size() + " user credentials loaded");
332                         fos.close();
333                 } catch (Exception ex) {
334                         ex.printStackTrace();
335                 }
336         }
337 
338         private void EnsureCredentials() throws Exception {
339                 Iterator<String> it = usernames.iterator();
340                 while (it.hasNext()){
341                         String u = it.next();
342                         if (!userpwd.containsKey(u)){
343                                 throw new Exception("The credential for user '"+u+"' is not in the credential file");
344                         }
345                 }
346         }
347 }