KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > share > configbean > services > MessageSecurityProviderImpl


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.j2ee.sun.share.configbean.services;
20
21 import java.beans.PropertyVetoException JavaDoc;
22 import java.io.File JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import org.netbeans.modules.j2ee.sun.dd.api.VersionNotSupportedException;
26
27 import org.netbeans.modules.j2ee.sun.dd.api.common.MessageSecurityBinding;
28 import org.netbeans.modules.j2ee.sun.dd.api.common.PortInfo;
29 import org.netbeans.modules.j2ee.sun.dd.api.common.WebserviceEndpoint;
30 import org.netbeans.modules.j2ee.sun.dd.api.common.WsdlPort;
31 import org.netbeans.modules.j2ee.sun.dd.api.services.MessageSecurityProvider;
32 import org.netbeans.modules.j2ee.sun.share.configbean.ServiceRef;
33 import org.netbeans.modules.j2ee.sun.share.configbean.SunONEDeploymentConfiguration;
34 import org.netbeans.modules.j2ee.sun.share.configbean.Utils;
35 import org.netbeans.modules.j2ee.sun.share.configbean.WebServiceDescriptor;
36 import org.netbeans.modules.j2ee.sun.share.configbean.WebServices;
37 import org.openide.ErrorManager;
38
39
40 /** Implementation of MessageSecurityProvider interface
41  *
42  * @author Peter Williams
43  */

44 public class MessageSecurityProviderImpl implements MessageSecurityProvider {
45     
46     public MessageSecurityProviderImpl() {
47         ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, "MessageSecurityProvider implementation created.");
48     }
49     
50     /* Retrieve current MessageSecurityBinding data for the specified endpoint.
51      *
52      * FIXME How to differentiate the errors "service not found", "port not found", and
53      * "no binding" from each other?
54      */

55     public MessageSecurityBinding getEndpointBinding(File JavaDoc sunDD, String JavaDoc endpointName, String JavaDoc portName) {
56         MessageSecurityBinding result = null;
57
58         // Validate input parameters
59
validateEndpointParams(endpointName, portName);
60
61         // get configuration (also validates sunDD parameter).
62
SunONEDeploymentConfiguration config = getConfiguration(sunDD);
63         
64         // locate endpoint DConfigBean
65
WebServices wsRoot = config.getWebServicesRoot();
66         if(wsRoot != null) {
67             WebServiceDescriptor wsBean = wsRoot.getWebServiceDescriptor(endpointName);
68             if(wsBean != null) {
69                 // found endpoint, now locate port component reference
70
WebserviceEndpoint endpoint = wsBean.getWebServiceEndpoint(portName);
71                 if(endpoint != null) {
72                     MessageSecurityBinding binding;
73                     try {
74                         binding = endpoint.getMessageSecurityBinding();
75                         if(binding != null) {
76                             // !PW FIXME Find a way to use cloneVersion here. Not required until
77
// there are different versions of this object but there will be.
78
result = (MessageSecurityBinding) binding.clone();
79                         }
80                     } catch (VersionNotSupportedException ex) {
81                         // return null for this case.
82
}
83                 }
84             }
85         }
86         
87         return result;
88     }
89
90     /* Set new MessageSecurityBinding data for the specified endpoint.
91      */

92     public boolean setEndpointBinding(File JavaDoc sunDD, String JavaDoc endpointName, String JavaDoc portName, MessageSecurityBinding binding) {
93         boolean result = false;
94         
95         // Validate input parameters
96
validateEndpointParams(endpointName, portName);
97
98         // get configuration (also validates sunDD parameter).
99
SunONEDeploymentConfiguration config = getConfiguration(sunDD);
100         
101         // locate endpoint DConfigBean
102
WebServices wsRoot = config.getWebServicesRoot();
103         if(wsRoot != null) {
104             WebServiceDescriptor wsBean = wsRoot.getWebServiceDescriptor(endpointName);
105             if(wsBean != null) {
106                 try {
107                     // found web service DConfigBean. Now set the binding on the specified endpoint.
108
wsBean.setMessageSecurityBinding(portName, binding);
109                     result = true;
110                 } catch (VersionNotSupportedException ex) {
111                     // How to notify caller here? Endpoint does not support Message Security Bindings.
112
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
113                 } catch (PropertyVetoException JavaDoc ex) {
114                     // Suppress this. Shouldn't happen anyway.
115
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
116                 }
117             }
118         }
119         
120         return result;
121     }
122
123     /* Retrieve current MessageSecurityBinding data for the specified webservice client.
124      *
125      * NOTE: Temporarily, this API does not allow the user to specify which port they
126      * want the binding for. If the client defines multiple ports, then only the first
127      * MessageSecurityBinding will be returned. Be sure to note corollary in set method.
128      *
129      * FIXME: How to report failures?
130      *
131      * @deprecated
132      */

133     public MessageSecurityBinding getServiceRefBinding(File JavaDoc sunDD, String JavaDoc serviceRefName) {
134         MessageSecurityBinding result = null;
135
136         // Validate input parameters
137
validateServiceRefParams(serviceRefName);
138
139         // get configuration (also validates sunDD parameter).
140
SunONEDeploymentConfiguration config = getConfiguration(sunDD);
141         
142         // locate all service-ref DDBeans
143
ServiceRef serviceRef = Utils.findServiceRef(config, serviceRefName);
144         if(serviceRef != null) {
145             // !PW FIXME for now, just return the first port binding. Need to work out
146
// how to return multiple bindings in a way that the caller knows which bindings
147
// apply to which port.
148
List JavaDoc portInfoList = serviceRef.getPortInfos();
149             if(portInfoList.size() > 0) {
150                 PortInfo portInfo = (PortInfo) portInfoList.get(0);
151                 MessageSecurityBinding binding;
152                 try {
153                     binding = portInfo.getMessageSecurityBinding();
154                     if(binding != null) {
155                         result = binding;
156                     }
157                 } catch (VersionNotSupportedException ex) {
158                     // return null for this case.
159
}
160             }
161         }
162         
163         return result;
164     }
165     
166     /* Retrieve current MessageSecurityBinding data for the specified wsdl-port of the
167      * specified webservice client.
168      */

169     public MessageSecurityBinding getServiceRefBinding(File JavaDoc sunDD, String JavaDoc serviceRefName,
170             String JavaDoc namespaceURI, String JavaDoc localpart) {
171         MessageSecurityBinding result = null;
172
173         // Validate input parameters
174
validateServiceRefParams(serviceRefName, namespaceURI, localpart);
175
176         // get configuration (also validates sunDD parameter).
177
SunONEDeploymentConfiguration config = getConfiguration(sunDD);
178         
179         // locate all service-ref DDBeans
180
ServiceRef serviceRef = Utils.findServiceRef(config, serviceRefName);
181         if(serviceRef != null) {
182             List JavaDoc portInfoList = serviceRef.getPortInfos();
183             Iterator JavaDoc iter = portInfoList.iterator();
184             while(iter.hasNext()) {
185                 PortInfo portInfo = (PortInfo) iter.next();
186                 WsdlPort port = portInfo.getWsdlPort();
187                 if(port != null && namespaceURI.equals(port.getNamespaceURI()) && localpart.equals(port.getLocalpart())) {
188                     MessageSecurityBinding binding;
189                     try {
190                         binding = portInfo.getMessageSecurityBinding();
191                         if(binding != null) {
192                             result = binding;
193                         }
194                     } catch (VersionNotSupportedException ex) {
195                         // return null for this case.
196
}
197                     break;
198                 }
199             }
200         }
201         
202         return result;
203     }
204
205     /* Set the MessageSecurityBinding data for the specified webservice client. The
206      * current implementation applies this binding data to all configured ports on this
207      * client.
208      *
209      * Note that the binding instance passed in is cloned for all ports it is configured to
210      * so subsequent modification of that instance after this call returns will not affect
211      * the data that was configured by this call. A subsequent call to setServiceRefBinding()
212      * would be required to apply new binding data.
213      *
214      * @deprecated
215      */

216     public boolean setServiceRefBinding(File JavaDoc sunDD, String JavaDoc serviceRefName, MessageSecurityBinding binding) {
217         boolean result = false;
218         
219         // Validate input parameters
220
validateServiceRefParams(serviceRefName);
221
222         // get configuration (also validates sunDD parameter).
223
SunONEDeploymentConfiguration config = getConfiguration(sunDD);
224         
225         // locate all service-ref DDBeans
226
ServiceRef serviceRef = Utils.findServiceRef(config, serviceRefName);
227         if(serviceRef != null) {
228             try {
229                 // !PW FIXME For now, set this message security binding data to all ports defined
230
// on this client. We need a definition that allows configuring a specific port.
231
serviceRef.setMessageSecurityBinding(binding);
232                 result = true;
233             } catch (VersionNotSupportedException ex) {
234                 // How to notify caller here? Endpoint does not support Message Security Bindings.
235
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
236             } catch (PropertyVetoException JavaDoc ex) {
237                 // Suppress this. Shouldn't happen anyway.
238
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
239             }
240         }
241         
242         return result;
243     }
244
245     /* Set the MessageSecurityBinding data for the specified wsdl-port of the specified
246      * webservice client.
247      *
248      * Note that the binding instance passed in is cloned for all ports it is configured to
249      * so subsequent modification of that instance after this call returns will not affect
250      * the data that was configured by this call. A subsequent call to setServiceRefBinding()
251      * would be required to apply new binding data.
252      */

253     public boolean setServiceRefBinding(File JavaDoc sunDD, String JavaDoc serviceRefName, String JavaDoc namespaceURI,
254             String JavaDoc localpart, MessageSecurityBinding binding) {
255         boolean result = false;
256         
257         // Validate input parameters
258
validateServiceRefParams(serviceRefName, namespaceURI, localpart);
259
260         // get configuration (also validates sunDD parameter).
261
SunONEDeploymentConfiguration config = getConfiguration(sunDD);
262         
263         // locate all service-ref DDBeans
264
ServiceRef serviceRef = Utils.findServiceRef(config, serviceRefName);
265         if(serviceRef != null) {
266             try {
267                 serviceRef.setMessageSecurityBinding(namespaceURI, localpart, binding);
268                 result = true;
269             } catch (VersionNotSupportedException ex) {
270                 // How to notify caller here? Endpoint does not support Message Security Bindings.
271
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
272             } catch (PropertyVetoException JavaDoc ex) {
273                 // Suppress this. Shouldn't happen anyway.
274
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
275             }
276         }
277         
278         return result;
279     }
280     
281     /** Creates new MessageSecurityBinding instance of the proper version.
282      */

283     public MessageSecurityBinding newMessageSecurityBinding(File JavaDoc sunDD) {
284         SunONEDeploymentConfiguration config = getConfiguration(sunDD);
285         return config.getStorageFactory().createMessageSecurityBinding();
286     }
287     
288 // ------------------- Proof of Concept methods... not implemented yet.-----------------------------
289

290 // /**
291
// * !PW Could we use a DDBean from the DDAPI for the standard descriptor (or the corresponding
292
// * merged node from the proposed annotation-ddbean merged provider) that refers to the
293
// * appropriate webservice endpoint or port-info DD? Then on plugin side, we can translate
294
// * that node to our tree and lookup the bound endpoint or port-info structure, eliminating
295
// * possible name contention (and being able to differentiate between servlet/endpoint and service/port,
296
// * which for string based api requires an extra redundant argument to clarify). For example...
297
// *
298
// * sunddapi only depends on schema2beans and xerces (and now filesystems) Adding j2eeserver
299
// * to get access to DDBean (from JSR-88) is probably a bad idea. Can use BaseBean for now,
300
// * but this is also a bad idea.
301
// *
302
// * Remaining options are add a new module that _can_ depend on both j2eeserver and sunddapi
303
// * or find another type. We could hack this by declaring type Object for api purposes
304
// * since both consumer (some highlevel project module) and implementor (sunddui) can depend
305
// * on j2ee/ddapi to get access to the real DDBean. Once solved, we can resolve ambiguities
306
// * in setMessageSecurityBinding() the same way.
307
// *
308
// * @param sunDD File refering to the sun descriptor file containing the interesting
309
// * information. This must be _the_ descriptor file for the specified j2eemodule or
310
// * an IllegalArgumentException will be thrown.
311
// * @param refBean Instance of object implementing javax.enterprise.deploy.model.DDBean that
312
// * represents the descriptor node from which to get service endpoint/port information and thus
313
// * the corresponding binding information?
314
// *
315
// * We need more info here too. What, exactly?
316
// */
317
// public MessageSecurityBinding getMessageSecurityBinding(File sunDD, Object refBean) {
318
// System.out.println("MSP.getMSB: " + sunDD.getName() + ", " + refBean.toString());
319
// return null;
320
// }
321

322     /** Implementation details
323      */

324     private SunONEDeploymentConfiguration getConfiguration(File JavaDoc sunDD) {
325         SunONEDeploymentConfiguration cachedDC = SunONEDeploymentConfiguration.getConfiguration(sunDD);
326         
327         if(sunDD == null) {
328             throw new IllegalArgumentException JavaDoc("Deployment descriptor file reference cannot be null."); // NOI18N
329
}
330         
331         // handle null configuration here. Do we want to use checked exceptions for this case?
332
if(cachedDC == null) {
333             throw new IllegalStateException JavaDoc("No Sun deployment configuration found for descriptor " + sunDD.getPath()); // NOI18N
334
}
335
336         return cachedDC;
337     }
338     
339     private void validateEndpointParams(String JavaDoc endpointName, String JavaDoc portName) {
340         if(!Utils.notEmpty(endpointName)) {
341             throw new IllegalArgumentException JavaDoc("Web service description name cannot be empty or null."); // NOI18N
342
}
343         if(!Utils.notEmpty(portName)) {
344             throw new IllegalArgumentException JavaDoc("Web service port name cannot be empty or null."); // NOI18N
345
}
346     }
347     
348     private void validateServiceRefParams(String JavaDoc serviceRefName) {
349         if(!Utils.notEmpty(serviceRefName)) {
350             throw new IllegalArgumentException JavaDoc("Web service reference name cannot be empty or null."); // NOI18N
351
}
352     }
353
354     private void validateServiceRefParams(String JavaDoc serviceRefName, String JavaDoc namespaceURI, String JavaDoc localpart) {
355         if(!Utils.notEmpty(serviceRefName)) {
356             throw new IllegalArgumentException JavaDoc("Web service reference name cannot be empty or null."); // NOI18N
357
}
358         if(!Utils.notEmpty(namespaceURI)) {
359             throw new IllegalArgumentException JavaDoc("Wsdl-port namespaceURI for service-ref cannot be empty or null."); // NOI18N
360
}
361         if(!Utils.notEmpty(localpart)) {
362             throw new IllegalArgumentException JavaDoc("Wsdl-port localpart for service-ref cannot be empty or null."); // NOI18N
363
}
364     }
365     
366     private WebserviceEndpoint getEndpoint(SunONEDeploymentConfiguration config, String JavaDoc endpointName, String JavaDoc portName) {
367         WebserviceEndpoint endpoint = null;
368         
369         WebServices wsRoot = config.getWebServicesRoot();
370         if(wsRoot != null) {
371             WebServiceDescriptor wsBean = wsRoot.getWebServiceDescriptor(endpointName);
372             if(wsBean != null) {
373                 // found endpoint, now locate port component reference
374
endpoint = wsBean.getWebServiceEndpoint(portName);
375             }
376         }
377         
378         return endpoint;
379     }
380
381 }
382
Popular Tags