This project has retired. For details please refer to its Attic page.
UddiRelatedBusinesses xref
View Javadoc
1   /*
2    * Copyright 2001-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   */
17  package org.apache.juddi.samples;
18  
19  import java.util.ArrayList;
20  import java.util.GregorianCalendar;
21  import java.util.List;
22  import javax.xml.datatype.DatatypeFactory;
23  import javax.xml.datatype.XMLGregorianCalendar;
24  import javax.xml.ws.Holder;
25  import org.apache.juddi.v3.client.config.UDDIClient;
26  import org.apache.juddi.v3.client.transport.Transport;
27  import org.uddi.api_v3.*;
28  import org.uddi.v3_service.UDDIPublicationPortType;
29  import org.uddi.v3_service.UDDISecurityPortType;
30  
31  /**
32   * This will create two businesses under different users, then setup a
33   * relationship between the two
34   *
35   * @author <a href="mailto:alexoree@apache.org">Alex O'Ree</a>
36   */
37  public class UddiRelatedBusinesses {
38  
39          private UDDISecurityPortType security = null;
40          private UDDIPublicationPortType publish = null;
41  
42          public UddiRelatedBusinesses() {
43                  try {
44                          // create a manager and read the config in the archive; 
45                          // you can use your config file name
46                          UDDIClient clerkManager = new UDDIClient("META-INF/simple-publish-uddi.xml");
47                          Transport transport = clerkManager.getTransport();
48                          // Now you create a reference to the UDDI API
49                          security = transport.getUDDISecurityService();
50                          publish = transport.getUDDIPublishService();
51                  } catch (Exception e) {
52                          e.printStackTrace();
53                  }
54          }
55  
56          public void fire(String businessKey, String authInfo, String businessKey1, String authInfo1, String relationship) throws Exception {
57                  try {
58  
59                          
60                          GregorianCalendar gcal = new GregorianCalendar();
61                          gcal.setTimeInMillis(System.currentTimeMillis());
62                          
63  
64                          //ROOT creates half of the relationship
65                          //create a business relationship (publisher assertion)
66                          Holder<List<PublisherAssertion>> x = new Holder<List<PublisherAssertion>>();
67                          PublisherAssertion pa = new PublisherAssertion();
68                          pa.setFromKey(businessKey);
69                          pa.setToKey(businessKey1);
70                          pa.setKeyedReference(new KeyedReference());
71                          pa.getKeyedReference().setKeyName("Subsidiary");
72                          pa.getKeyedReference().setKeyValue(relationship);
73  
74                          pa.getKeyedReference().setTModelKey("uddi:uddi.org:relationships");
75                          x.value = new ArrayList<PublisherAssertion>();
76                          x.value.add(pa);
77                          publish.setPublisherAssertions(authInfo, x);
78  
79                          //now "UDDI" the user, creates the other half. It has to be mirrored exactly
80                          x = new Holder<List<PublisherAssertion>>();
81                          pa = new PublisherAssertion();
82                          pa.setFromKey(businessKey);
83                          pa.setToKey(businessKey1);
84                          pa.setKeyedReference(new KeyedReference());
85                          pa.getKeyedReference().setKeyName("Subsidiary");
86                          pa.getKeyedReference().setKeyValue(relationship);
87  
88                          pa.getKeyedReference().setTModelKey("uddi:uddi.org:relationships");
89                          x.value = new ArrayList<PublisherAssertion>();
90                          x.value.add(pa);
91                          publish.setPublisherAssertions(authInfo1, x);
92  
93                          System.out.println("Success!");
94                          /*
95                           * Here's some notes:
96                           * You can use
97                           * List<AssertionStatusItem> assertionStatusReport = publish.getAssertionStatusReport(UDDIAuthToken.getAuthInfo(), CompletionStatus.STATUS_FROM_KEY_INCOMPLETE);
98                           * to determine if there's any assertions/relationships requests that are pending
99                           * this should have one item it in, the relationship that's incomplete
100                          * 
101                          * There's also publish.deletePublisherAssertions();
102                          */
103                 } catch (Exception e) {
104                         e.printStackTrace();
105                 }
106         }
107 
108         public static void main(String args[]) throws Exception {
109                 UddiRelatedBusinesses sp = new UddiRelatedBusinesses();
110 
111                 GetAuthToken getAuthTokenRoot = new GetAuthToken();
112                 getAuthTokenRoot.setUserID("root");
113                 getAuthTokenRoot.setCred("root");
114 
115                 // Making API call that retrieves the authentication token for the 'root' user.
116                 AuthToken rootAuthToken = sp.security.getAuthToken(getAuthTokenRoot);
117                 System.out.println("root AUTHTOKEN = " + "don't log auth tokens!");
118                 BusinessEntity rootbiz = sp.createBusiness("root");
119 
120                 getAuthTokenRoot = new GetAuthToken();
121                 getAuthTokenRoot.setUserID("uddi");
122                 getAuthTokenRoot.setCred("uddi");
123 
124                 // Making API call that retrieves the authentication token for the 'root' user.
125                 AuthToken uddiAuthToken = sp.security.getAuthToken(getAuthTokenRoot);
126                 System.out.println("uddi AUTHTOKEN = " + "don't log auth tokens!");
127                 BusinessEntity uddibiz = sp.createBusiness("uddi");
128 
129                 //save user uddi's business
130                 SaveBusiness sb = new SaveBusiness();
131                 sb.setAuthInfo(uddiAuthToken.getAuthInfo());
132                 sb.getBusinessEntity().add(uddibiz);
133                 BusinessDetail uddibize = sp.publish.saveBusiness(sb);
134 
135                 sb = new SaveBusiness();
136                 sb.setAuthInfo(rootAuthToken.getAuthInfo());
137                 sb.getBusinessEntity().add(rootbiz);
138                 BusinessDetail rootbize = sp.publish.saveBusiness(sb);
139 
140                 sp.fire(rootbize.getBusinessEntity().get(0).getBusinessKey(), rootAuthToken.getAuthInfo(),
141                         uddibize.getBusinessEntity().get(0).getBusinessKey(), uddiAuthToken.getAuthInfo(),
142                         "parent-child");
143         }
144 
145         private BusinessEntity createBusiness(String user) {
146                 BusinessEntity be = new BusinessEntity();
147                 be.getName().add(new Name(user + "'s business", null));
148                 return be;
149         }
150 
151 }