This project has retired. For details please refer to its Attic page.
Export xref
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.FileNotFoundException;
20  import java.io.FileOutputStream;
21  import java.util.HashSet;
22  import java.util.Iterator;
23  import java.util.Properties;
24  import java.util.Set;
25  import javax.xml.bind.JAXB;
26  import org.apache.juddi.api_v3.GetAllPublisherDetail;
27  import org.apache.juddi.api_v3.PublisherDetail;
28  import org.apache.juddi.api_v3.SavePublisher;
29  import org.apache.juddi.v3.client.UDDIConstants;
30  import org.apache.juddi.v3.client.config.UDDIClerk;
31  import org.apache.juddi.v3.client.config.UDDIClient;
32  import org.apache.juddi.v3.client.transport.Transport;
33  import org.apache.juddi.v3_service.JUDDIApiPortType;
34  import org.uddi.api_v3.BusinessEntity;
35  import org.uddi.api_v3.BusinessList;
36  import org.uddi.api_v3.FindBusiness;
37  import org.uddi.api_v3.FindQualifiers;
38  import org.uddi.api_v3.FindTModel;
39  import org.uddi.api_v3.GetAuthToken;
40  import org.uddi.api_v3.GetBusinessDetail;
41  import org.uddi.api_v3.GetTModelDetail;
42  import org.uddi.api_v3.Name;
43  import org.uddi.api_v3.SaveBusiness;
44  import org.uddi.api_v3.SaveTModel;
45  import org.uddi.api_v3.TModel;
46  import org.uddi.api_v3.TModelInfo;
47  import org.uddi.api_v3.TModelList;
48  import org.uddi.v3_service.UDDIInquiryPortType;
49  import org.uddi.v3_service.UDDIPublicationPortType;
50  import org.uddi.v3_service.UDDISecurityPortType;
51  
52  /**
53   * Provides UDDI export functions
54   *
55   * @author Alex O'Ree
56   * @since 3.2
57   */
58  public class Export {
59  
60          private static UDDISecurityPortType security = null;
61          private static JUDDIApiPortType juddiApi = null;
62          private static UDDIPublicationPortType publish = null;
63          private static UDDIInquiryPortType inquiry;
64          private JUDDIApiPortType juddi;
65          String token = null;
66          String username = null;
67          String tmodelfile = null;
68          String businessfile = null;
69          String publishersfile = null;
70          String mappingsfile = null;
71          boolean safemode = false;
72          boolean myitemsonly = false;
73          boolean preserveOwnership;
74          String credFile;
75          Set<String> usernames = new HashSet<String>();
76          Properties mapping = new Properties();
77          boolean stripSig = false;
78  
79          public void Execute(String config, String name, String user, String pass,
80                  String tmodelout, String businessOut,
81                  boolean isJuddi, boolean safe, String publishersFile,
82                  boolean myItemsOnly,
83                  String mappingsfile,
84                  boolean preserveOwnership, String credFile, boolean stripSig) throws Exception {
85                  // create a manager and read the config in the archive; 
86                  // you can use your config file name
87                  this.stripSig = stripSig;
88                  this.publishersfile = publishersFile;
89                  this.tmodelfile = tmodelout;
90                  this.businessfile = businessOut;
91                  this.myitemsonly = myItemsOnly;
92                  this.mappingsfile = mappingsfile;
93                  this.credFile = credFile;
94                  this.preserveOwnership = preserveOwnership;
95  
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                 this.username = user;
109                 if (username == null || pass == null) {
110                         username = clerk.getPublisher();
111                         pass = clerk.getPassword();
112                 }
113                 if (username == null || pass==null) {
114                    System.out.println("No credentials are available. This will probably fail spectacularly");
115                 } else {
116                         GetAuthToken getAuthTokenRoot = new GetAuthToken();
117                         getAuthTokenRoot.setUserID(username);
118                         getAuthTokenRoot.setCred(pass);
119                         token = security.getAuthToken(getAuthTokenRoot).getAuthInfo();
120                 }
121 
122 
123                 ExportTmodels();
124                 ExportBusiness();
125 
126                 if (isJuddi) {
127                         //optional juddi
128                         ExportNodes();
129                         ExportClerks();
130                         ExportPublishers();
131                 }
132                 if (preserveOwnership) {
133                         System.out.println("Saving user mappings file to " + mappingsfile);
134                         SaveProperties();
135                         System.out.println("Saving user credentials template to " + credFile);
136                         SaveCredFileTemplate();
137                 }
138                 clerkManager.stop();
139         }
140 
141         private void ExportBusiness() throws Exception {
142                 if (new File(businessfile).exists())
143                 {
144                         System.err.println(businessfile + " already exists!");
145                         System.exit(1);
146                 }
147                 FileOutputStream fos = new FileOutputStream(businessfile);
148                 FindBusiness req = new FindBusiness();
149                 req.setAuthInfo(token);
150                 req.getName().add(new Name(UDDIConstants.WILDCARD, null));
151                 req.setFindQualifiers(new FindQualifiers());
152                 req.getFindQualifiers().getFindQualifier().add(UDDIConstants.APPROXIMATE_MATCH);
153                 int offset = 0;
154                 int maxrows = 200;
155 
156                 req.setMaxRows(maxrows);
157                 req.setListHead(offset);
158                 BusinessList findTModel = null;
159                 SaveBusiness sb = new SaveBusiness();
160                 do {
161                         findTModel = inquiry.findBusiness(req);
162                         if (findTModel.getBusinessInfos() != null) {
163                                 for (int i = 0; i < findTModel.getBusinessInfos().getBusinessInfo().size(); i++) {
164                                         boolean go = true;
165                                         String owner = Common.GetOwner(findTModel.getBusinessInfos().getBusinessInfo().get(i).getBusinessKey(), token, inquiry);
166                                         if (owner!=null && !usernames.contains(owner)) {
167                                                 usernames.add(owner);
168                                         }
169                                         if (myitemsonly) {
170                                                 if (owner == null || !owner.equalsIgnoreCase(username)) {
171                                                         go = false;
172                                                         System.out.println("skipping " + findTModel.getBusinessInfos().getBusinessInfo().get(i).getBusinessKey() + " owned by " + owner);
173                                                 }
174                                         }
175                                         if (go) {
176                                            if (owner!=null)
177                                                 mapping.setProperty(findTModel.getBusinessInfos().getBusinessInfo().get(i).getBusinessKey(), owner);
178                                                 System.out.println("Exporting " + findTModel.getBusinessInfos().getBusinessInfo().get(i).getBusinessKey() + " owner " + owner);
179                                                 sb.getBusinessEntity().add(GetBusiness(findTModel.getBusinessInfos().getBusinessInfo().get(i).getBusinessKey(), token));
180                                         }
181                                 }
182                         }
183                         //do stuff
184                         offset = offset + maxrows;
185                         req.setListHead(offset);
186 
187                 } while (false);//findTModel.getListDescription().getIncludeCount() > 0 );
188 
189                 if (stripSig) {
190                         int x=0;
191                         
192                         for (int i = 0; i < sb.getBusinessEntity().size(); i++) {
193                                 x+=sb.getBusinessEntity().get(i).getSignature().size();
194                                 sb.getBusinessEntity().get(i).getSignature().clear();
195                                 if (sb.getBusinessEntity().get(i).getBusinessServices() != null) {
196                                         for (int i2 = 0; i2 < sb.getBusinessEntity().get(i).getBusinessServices().getBusinessService().size(); i2++) {
197                                                 x+=sb.getBusinessEntity().get(i).getBusinessServices().getBusinessService().get(i2).getSignature().size();
198                                                 sb.getBusinessEntity().get(i).getBusinessServices().getBusinessService().get(i2).getSignature().clear();
199                                                 
200                                                 if (sb.getBusinessEntity().get(i).getBusinessServices().getBusinessService().get(i2).getBindingTemplates() != null) {
201                                                         for (int i3 = 0; i3 < sb.getBusinessEntity().get(i).getBusinessServices().getBusinessService().get(i2).getBindingTemplates().getBindingTemplate().size(); i3++) {
202                                                                 x+=sb.getBusinessEntity().get(i).getBusinessServices().getBusinessService().get(i2).getBindingTemplates().getBindingTemplate().get(i3).getSignature().size();
203                                                                 sb.getBusinessEntity().get(i).getBusinessServices().getBusinessService().get(i2).getBindingTemplates().getBindingTemplate().get(i3).getSignature().clear();
204                                                         }
205                                                 }
206                                         }
207                                 }
208                         }
209                         System.out.println(x + " signatures stripped");
210                 }
211 
212                 System.out.println("Saving to disk");
213                 JAXB.marshal(sb, fos);
214                 fos.close();
215                 System.out.println("Done with businesses. Export count: " + sb.getBusinessEntity().size());
216         }
217 
218         private void ExportTmodels() throws Exception {
219                  if (new File(tmodelfile).exists())
220                 {
221                         System.err.println(tmodelfile + " already exists!");
222                         System.exit(1);
223                 }
224                 FileOutputStream fos = new FileOutputStream(tmodelfile);
225                 FindTModel req = new FindTModel();
226                 req.setName(new Name(UDDIConstants.WILDCARD, null));
227                 req.setAuthInfo(token);
228                 req.setFindQualifiers(new FindQualifiers());
229                 req.getFindQualifiers().getFindQualifier().add(UDDIConstants.APPROXIMATE_MATCH);
230                 int offset = 0;
231                 int maxrows = 200;
232 
233                 req.setMaxRows(maxrows);
234                 req.setListHead(offset);
235                 TModelList findTModel = null;
236                 SaveTModel stm = new SaveTModel();
237                 do {
238                         findTModel = inquiry.findTModel(req);
239                         if (findTModel.getTModelInfos() != null) {
240                                 for (int i = 0; i < findTModel.getTModelInfos().getTModelInfo().size(); i++) {
241                                         boolean go = true;
242                                         String owner = Common.GetOwner(findTModel.getTModelInfos().getTModelInfo().get(i).getTModelKey(), token, inquiry);
243                                         if (owner!=null && !usernames.contains(owner)) {
244                                                 usernames.add(owner);
245                                         }
246                                         if (myitemsonly) {
247                                                 if (owner == null || !owner.equalsIgnoreCase(username)) {
248                                                         go = false;
249                                                 }
250                                         }
251                                         if (go) {
252                                                 if (owner!=null)
253                                                       mapping.setProperty(findTModel.getTModelInfos().getTModelInfo().get(i).getTModelKey(), owner);
254                                                 System.out.println("Exporting " + findTModel.getTModelInfos().getTModelInfo().get(i).getTModelKey() + " owner " + owner);
255                                                 stm.getTModel().add(GetTmodel(findTModel.getTModelInfos().getTModelInfo().get(i), token));
256                                         }
257                                 }
258                         }
259                         offset = offset + maxrows;
260                         req.setListHead(offset);
261                 } while (false);//findTModel.getListDescription().getIncludeCount() > 0);
262                 
263                 if (stripSig) {
264                         int x=0;
265                         for (int i = 0; i < stm.getTModel().size(); i++) {
266                                 x+=stm.getTModel().get(i).getSignature().size();
267                                 stm.getTModel().get(i).getSignature().clear();
268                         }
269                         System.out.println(x + " signatures stripped");
270                 }
271                 
272                 System.out.println("Storing to disk ");
273                 JAXB.marshal(stm, fos);
274                 fos.close();
275                 System.out.println("Done with tModels. Export count: " + stm.getTModel().size());
276         }
277 
278         private TModel GetTmodel(TModelInfo get, String token) throws Exception {
279                 GetTModelDetail r = new GetTModelDetail();
280                 r.setAuthInfo(token);
281                 r.getTModelKey().add(get.getTModelKey());
282                 return inquiry.getTModelDetail(r).getTModel().get(0);
283         }
284 
285         private BusinessEntity GetBusiness(String businessKey, String token) throws Exception {
286                 GetBusinessDetail r = new GetBusinessDetail();
287                 r.getBusinessKey().add(businessKey);
288                 r.setAuthInfo(token);
289                 return inquiry.getBusinessDetail(r).getBusinessEntity().get(0);
290         }
291 
292         private void ExportNodes() {
293                 //TODO wait for JUDDI-706
294         }
295 
296         private void ExportClerks() {
297                 //TODO wait for JUDDI-706
298         }
299 
300         private void ExportPublishers() throws Exception {
301                  if (new File(publishersfile).exists())
302                 {
303                         System.err.println(publishersfile + " already exists!");
304                         System.exit(1);
305                 }
306                 FileOutputStream fos = new FileOutputStream(publishersfile);
307 
308                 GetAllPublisherDetail r = new GetAllPublisherDetail();
309                 r.setAuthInfo(token);
310                 PublisherDetail allPublisherDetail = juddi.getAllPublisherDetail(r);
311                 if (stripSig) {
312                         for (int i = 0; i < allPublisherDetail.getPublisher().size(); i++) {
313                                 allPublisherDetail.getPublisher().get(i).getSignature().clear();
314                         }
315                 }
316                 SavePublisher saver = new SavePublisher();
317                 saver.getPublisher().addAll(allPublisherDetail.getPublisher());
318                 System.out.println("Storing to disk ");
319                 JAXB.marshal(saver, fos);
320                 fos.close();
321                 System.out.println("Done with Publishers");
322         }
323 
324         private void SaveProperties() throws FileNotFoundException {
325                 StringBuilder sb = new StringBuilder();
326                 Iterator<String> it = usernames.iterator();
327                 while (it.hasNext()) {
328                         sb.append(it.next());
329                         if (it.hasNext()) {
330                                 sb.append(",");
331                         }
332                 }
333                 mapping.put("usernames", sb.toString());
334                 if (new File(mappingsfile).exists())
335                 {
336                         System.err.println(mappingsfile + " already exists!");
337                         System.exit(1);
338                 }
339                 try {
340                         FileOutputStream fos = new FileOutputStream(mappingsfile);
341                         mapping.store(fos, "no comments");
342                         fos.close();
343                 } catch (Exception ex) {
344                         ex.printStackTrace();
345                 }
346         }
347 
348         private void SaveCredFileTemplate() {
349                 Iterator<String> it = usernames.iterator();
350                 Properties p = new Properties();
351                 while (it.hasNext()) {
352                         String s = it.next();
353                         p.setProperty(s, s);
354                 }
355                 if (new File(credFile).exists())
356                 {
357                         System.err.println(credFile + " already exists!");
358                         System.exit(1);
359                 }
360                 try {
361                         FileOutputStream fos = new FileOutputStream(credFile);
362                         p.store(fos, "no comments");
363                         fos.close();
364                 } catch (Exception ex) {
365                         ex.printStackTrace();
366                 }
367         }
368 }