KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > share > configbean > customizers > webservice > EditBinding


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 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 /*
20  * EditBinding.java
21  *
22  * Created on April 19, 2006, 5:59 PM
23  */

24
25 package org.netbeans.modules.j2ee.sun.share.configbean.customizers.webservice;
26
27 import java.awt.BorderLayout JavaDoc;
28 import java.awt.Dimension JavaDoc;
29 import java.awt.GridBagConstraints JavaDoc;
30 import java.awt.Insets JavaDoc;
31 import java.beans.PropertyChangeEvent JavaDoc;
32 import java.beans.PropertyChangeListener JavaDoc;
33 import java.util.ArrayList JavaDoc;
34 import java.util.Arrays JavaDoc;
35 import java.util.Collection JavaDoc;
36 import java.util.Comparator JavaDoc;
37 import java.util.ResourceBundle JavaDoc;
38 import javax.swing.JPanel JavaDoc;
39 import javax.swing.event.TableModelEvent JavaDoc;
40 import javax.swing.event.TableModelListener JavaDoc;
41
42 import org.netbeans.modules.j2ee.sun.dd.api.CommonDDBean;
43 import org.netbeans.modules.j2ee.sun.dd.api.common.JavaMethod;
44 import org.netbeans.modules.j2ee.sun.dd.api.common.Message;
45 import org.netbeans.modules.j2ee.sun.dd.api.common.MessageSecurity;
46 import org.netbeans.modules.j2ee.sun.dd.api.common.MessageSecurityBinding;
47
48 import org.netbeans.modules.j2ee.sun.share.Constants;
49 import org.netbeans.modules.j2ee.sun.share.configbean.ASDDVersion;
50 import org.netbeans.modules.j2ee.sun.share.configbean.Base;
51 import org.netbeans.modules.j2ee.sun.share.configbean.StorageBeanFactory;
52 import org.netbeans.modules.j2ee.sun.share.configbean.Utils;
53 import org.netbeans.modules.j2ee.sun.share.configbean.WebServiceDescriptor;
54 import org.netbeans.modules.j2ee.sun.share.configbean.customizers.common.GenericTableModel;
55 import org.netbeans.modules.j2ee.sun.share.configbean.customizers.common.GenericTablePanel;
56 import org.netbeans.modules.j2ee.sun.share.configbean.customizers.common.HelpContext;
57 import org.netbeans.modules.j2ee.sun.share.configbean.customizers.common.InputDialog;
58
59 /**
60  *
61  * @author Peter Williams
62  */

63 public class EditBinding extends JPanel JavaDoc implements TableModelListener JavaDoc {
64     
65     private static final ResourceBundle JavaDoc webserviceBundle = ResourceBundle.getBundle(
66        "org.netbeans.modules.j2ee.sun.share.configbean.customizers.webservice.Bundle"); // NOI18N
67

68     private final Base theBean;
69     private final ASDDVersion asVersion;
70     private final String JavaDoc asCloneVersion;
71     private final MessageSecurityBinding msBinding;
72     private final boolean methodAsOperation;
73     
74     private Dimension JavaDoc initialPreferredSize;
75     
76     // Table for editing MessageSecurity entries
77
private GenericTableModel messageSecurityModel;
78     private GenericTablePanel messageSecurityPanel;
79
80     private MessageSecurity [] bindingData;
81     private MessageSecurity [] newBindingData;
82
83     private String JavaDoc providerId;
84     
85     
86     /** Creates new form EditBinding */
87     public EditBinding(Base bean, MessageSecurityBinding binding, boolean asOperation,
88             ASDDVersion asDDVersion, String JavaDoc stringVersion) {
89         theBean = bean;
90         methodAsOperation = asOperation;
91         asVersion = asDDVersion;
92         asCloneVersion = stringVersion;
93         msBinding = binding;
94         
95         providerId = msBinding.getProviderId();
96         bindingData = expand(binding.getMessageSecurity());
97         
98         initComponents();
99         initUserComponents();
100         
101         initFields();
102     }
103     
104     public String JavaDoc getProviderId() {
105         return providerId;
106     }
107     
108     /** This method is called from within the constructor to
109      * initialize the form.
110      * WARNING: Do NOT modify this code. The content of this method is
111      * always regenerated by the Form Editor.
112      */

113     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
114
private void initComponents() {
115         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
116
117         jLblAuthReq = new javax.swing.JLabel JavaDoc();
118         jLblAuthorizationLayer = new javax.swing.JLabel JavaDoc();
119         jTxtAuthorizationLayer = new javax.swing.JTextField JavaDoc();
120         jLblProvIdReq = new javax.swing.JLabel JavaDoc();
121         jLblProviderId = new javax.swing.JLabel JavaDoc();
122         jTxtProviderId = new javax.swing.JTextField JavaDoc();
123
124         setLayout(new java.awt.GridBagLayout JavaDoc());
125
126         jLblAuthReq.setLabelFor(jTxtAuthorizationLayer);
127         jLblAuthReq.setText("*");
128         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
129         gridBagConstraints.insets = new java.awt.Insets JavaDoc(6, 6, 0, 0);
130         add(jLblAuthReq, gridBagConstraints);
131
132         jLblAuthorizationLayer.setLabelFor(jTxtAuthorizationLayer);
133         jLblAuthorizationLayer.setText(webserviceBundle.getString("LBL_AuthorizationLayer_1"));
134         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
135         gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
136         gridBagConstraints.insets = new java.awt.Insets JavaDoc(6, 6, 0, 0);
137         add(jLblAuthorizationLayer, gridBagConstraints);
138
139         jTxtAuthorizationLayer.setEditable(false);
140         jTxtAuthorizationLayer.setText("SOAP");
141         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
142         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
143         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
144         gridBagConstraints.weightx = 1.0;
145         gridBagConstraints.insets = new java.awt.Insets JavaDoc(6, 6, 0, 5);
146         add(jTxtAuthorizationLayer, gridBagConstraints);
147
148         jLblProvIdReq.setLabelFor(jTxtProviderId);
149         jLblProvIdReq.setText("*");
150         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
151         gridBagConstraints.insets = new java.awt.Insets JavaDoc(6, 6, 0, 0);
152         add(jLblProvIdReq, gridBagConstraints);
153
154         jLblProviderId.setLabelFor(jTxtProviderId);
155         jLblProviderId.setText(webserviceBundle.getString("LBL_Provider_Id_1"));
156         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
157         gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
158         gridBagConstraints.insets = new java.awt.Insets JavaDoc(6, 6, 0, 0);
159         add(jLblProviderId, gridBagConstraints);
160
161         jTxtProviderId.addKeyListener(new java.awt.event.KeyAdapter JavaDoc() {
162             public void keyReleased(java.awt.event.KeyEvent JavaDoc evt) {
163                 jTxtProviderIdKeyReleased(evt);
164             }
165         });
166
167         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
168         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
169         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
170         gridBagConstraints.weightx = 1.0;
171         gridBagConstraints.insets = new java.awt.Insets JavaDoc(6, 6, 0, 5);
172         add(jTxtProviderId, gridBagConstraints);
173
174     }// </editor-fold>//GEN-END:initComponents
175

176     private void jTxtProviderIdKeyReleased(java.awt.event.KeyEvent JavaDoc evt) {//GEN-FIRST:event_jTxtProviderIdKeyReleased
177
providerId = jTxtProviderId.getText();
178         firePropertyChange(Constants.USER_DATA_CHANGED, null, null);
179     }//GEN-LAST:event_jTxtProviderIdKeyReleased
180

181     
182     // Variables declaration - do not modify//GEN-BEGIN:variables
183
private javax.swing.JLabel JavaDoc jLblAuthReq;
184     private javax.swing.JLabel JavaDoc jLblAuthorizationLayer;
185     private javax.swing.JLabel JavaDoc jLblProvIdReq;
186     private javax.swing.JLabel JavaDoc jLblProviderId;
187     private javax.swing.JTextField JavaDoc jTxtAuthorizationLayer;
188     private javax.swing.JTextField JavaDoc jTxtProviderId;
189     // End of variables declaration//GEN-END:variables
190

191     private void initUserComponents() {
192         /* Save preferred size before adding table. We have our own width and
193          * will add a constant of our own choosing for the height in init(), below.
194          */

195         initialPreferredSize = getPreferredSize();
196
197 // /** Add table after preferred size is saved.
198
// */
199
// messageSecurityTable = new JTable();
200
// messageSecurityTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
201
//// messageSecurityTable.getSelectionModel().addListSelectionListener(this);
202
//
203
// JScrollPane scrollPane = new JScrollPane();
204
// scrollPane.setViewportView(messageSecurityTable);
205
// GridBagConstraints gridBagConstraints = new GridBagConstraints();
206
// gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
207
// gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
208
// gridBagConstraints.weightx = 1.0;
209
// gridBagConstraints.weighty = 1.0;
210
// gridBagConstraints.insets = new Insets(0, 6, 5, 5);
211
// add(scrollPane, gridBagConstraints);
212
//
213
// jLblMessageBinding.setLabelFor(messageSecurityTable);
214
/* Add locale-charset mapping table panel :
215          * TableEntry list has four properties: locale, agent, charset, description
216          */

217         ArrayList JavaDoc tableColumns = new ArrayList JavaDoc(5);
218         tableColumns.add(new MessageEntry(methodAsOperation));
219         tableColumns.add(new AuthorizationEntry(MessageSecurity.REQUEST_PROTECTION,
220                 "AuthSource", "ReqAuthSource")); // NOI18N - property name
221
tableColumns.add(new AuthorizationEntry(MessageSecurity.REQUEST_PROTECTION,
222                 "AuthRecipient", "ReqAuthRecipient")); // NOI18N - property name
223
tableColumns.add(new AuthorizationEntry(MessageSecurity.RESPONSE_PROTECTION,
224                 "AuthSource", "RespAuthSource")); // NOI18N - property name
225
tableColumns.add(new AuthorizationEntry(MessageSecurity.RESPONSE_PROTECTION,
226                 "AuthRecipient", "RespAuthRecipient")); // NOI18N - property name
227

228         messageSecurityModel = new GenericTableModel(messageSecurityFactory, tableColumns);
229         messageSecurityModel.addTableModelListener(this);
230         messageSecurityPanel = new GenericTablePanel(messageSecurityModel,
231             webserviceBundle, "MessageSecurity", // NOI18N - property name
232
MessageSecurityEntryPanel.class,
233             HelpContext.HELP_SERVICE_ENDPOINT_SECURITY_POPUP, Boolean.valueOf(this.methodAsOperation));
234
235         GridBagConstraints JavaDoc gridBagConstraints = new GridBagConstraints JavaDoc();
236         gridBagConstraints.gridwidth = GridBagConstraints.REMAINDER;
237         gridBagConstraints.fill = GridBagConstraints.BOTH;
238         gridBagConstraints.weightx = 1.0;
239         gridBagConstraints.weighty = 1.0;
240         gridBagConstraints.insets = new Insets JavaDoc(0, 6, 0, 5);
241         add(messageSecurityPanel, gridBagConstraints);
242         
243         getAccessibleContext().setAccessibleName(webserviceBundle.getString("ACSN_EditBindings")); // NOI18N
244
getAccessibleContext().setAccessibleDescription(webserviceBundle.getString("ACSD_EditBindings")); // NOI18N
245
}
246     
247     private void initFields() {
248         // Initialize table data model
249
messageSecurityPanel.setModel(bindingData, asVersion);
250         
251         // Initialize text fields.
252
updateTextFields();
253         
254         // Set preferred size (just height really) to be something reasonable (because
255
// the default is unnecessarily tall).
256
setPreferredSize(new Dimension JavaDoc(initialPreferredSize.width, initialPreferredSize.height + 148));
257     }
258     
259     private void updateTextFields() {
260         jTxtProviderId.setText(providerId);
261     }
262     
263     Collection JavaDoc getErrors() {
264         // Validate what the user typed in as a valid group name
265
ArrayList JavaDoc errors = new ArrayList JavaDoc();
266         String JavaDoc newProviderId = getProviderId();
267
268         /** Providier ID must not be blank...
269          */

270         if(!Utils.notEmpty(newProviderId)) {
271             errors.add(webserviceBundle.getString("ERR_BlankProviderId")); // NOI18N
272
}
273         
274         /** Should be at least one binding...
275          */

276         if(newBindingData == null || newBindingData.length == 0) {
277             errors.add(webserviceBundle.getString("ERR_NoSecurityBindings")); // NOI18N
278
}
279         
280         /* Should only be one binding if any are named "*"
281          */

282         if(newBindingData != null && newBindingData.length > 1 && hasStarBinding()) {
283             errors.add(webserviceBundle.getString("ERR_StarBindingConflict")); // NOI18N
284
}
285
286         return errors;
287     }
288     
289     private boolean hasStarBinding() {
290         boolean result = false;
291         
292         for(int i = 0; i < newBindingData.length; i++) {
293             String JavaDoc name = null;
294             if(newBindingData[i] != null) {
295                 Message [] msgs = newBindingData[i].getMessage();
296                 if(msgs != null && msgs.length > 0 && msgs[0] != null) {
297                     name = getMethodName(msgs[0]);
298                 }
299             }
300             if("*".equals(name)) {
301                 result = true;
302                 break;
303             }
304         }
305         
306         return result;
307     }
308     
309     /** -----------------------------------------------------------------------
310      * Implementation of TableModelListener interface
311      */

312     public void tableChanged(TableModelEvent JavaDoc e) {
313         newBindingData = (MessageSecurity[]) messageSecurityModel.getData().toArray(new MessageSecurity [0]);
314         firePropertyChange(Constants.USER_DATA_CHANGED, null, null);
315     }
316     
317     private void commit() {
318         MessageSecurityBinding oldBinding = (MessageSecurityBinding) msBinding.clone();
319
320         // Set authorization layer
321
msBinding.setAuthLayer("SOAP"); // NOI18N
322

323         // Set provider id to binding.
324
String JavaDoc newProviderId = getProviderId();
325         msBinding.setProviderId(newProviderId);
326
327         // Set message security entries.
328
newBindingData = compress(newBindingData);
329         msBinding.setMessageSecurity(newBindingData);
330         
331         // Mark bean as dirty. Note that property name here notes endpoint, but
332
// message security binding elements are also found on service-ref's.
333
theBean.firePropertyChange(WebServiceDescriptor.ENDPOINT_SECURITY_BINDING, oldBinding, msBinding);
334     }
335
336     private MessageSecurity [] expand(MessageSecurity [] bindingData) {
337         MessageSecurity [] result = new MessageSecurity[0];
338         
339         if(bindingData != null && bindingData.length > 0) {
340             ArrayList JavaDoc bindings = new ArrayList JavaDoc(bindingData.length * 10);
341             
342             for(int i = 0; i < bindingData.length; i++) {
343                 MessageSecurity ms = bindingData[i];
344                 Message [] messages = ms.getMessage();
345                 if(messages != null && messages.length > 0) {
346                     if(messages.length == 1) {
347                         bindings.add(ms);
348                     } else {
349                         for(int j = 0; j < messages.length; j++) {
350                             MessageSecurity newMS = (MessageSecurity) ms.cloneVersion(asCloneVersion);
351                             Message newMessage = newMS.getMessage(j);
352                             newMS.setMessage(new Message [] { newMessage } );
353                             bindings.add(newMS);
354                         }
355                     }
356                 }
357             }
358             
359             result = (MessageSecurity []) bindings.toArray(result);
360         }
361         
362         return result;
363     }
364     
365     private MessageSecurity [] compress(MessageSecurity [] bindingData) {
366         MessageSecurity [] result = null;
367         
368         if(bindingData != null && bindingData.length > 0) {
369             ArrayList JavaDoc securityList = new ArrayList JavaDoc(bindingData.length);
370             Arrays.sort(bindingData, new MessageSecurityComparator(true));
371             
372             MessageSecurityComparator protectionComparator = new MessageSecurityComparator(false);
373             for(int i = 0; i < bindingData.length; ) {
374                 int j = i + 1;
375                 while(j < bindingData.length && protectionComparator.compare(bindingData[i], bindingData[j]) == 0) {
376                     j++;
377                 }
378                 int elementsToMerge = j - i;
379                 assert elementsToMerge > 0;
380                         
381                 if(elementsToMerge == 1) {
382                     securityList.add(bindingData[i].clone());
383                 } else {
384                     ArrayList JavaDoc messageList = new ArrayList JavaDoc(elementsToMerge);
385                     for(int m = i; m < j; m++) {
386                         Message [] message = bindingData[m].getMessage();
387                         if(message != null && message.length > 0) {
388                             // Shouldn't be more than one message element by this point.
389
messageList.add(message[0].clone());
390                         }
391                     }
392                     Message [] messages = (Message []) messageList.toArray(new Message [0]);
393                     bindingData[i].setMessage(messages);
394                     securityList.add(bindingData[i].clone());
395                 }
396                 i += elementsToMerge;
397             }
398             result = (MessageSecurity []) securityList.toArray(new MessageSecurity[0]);
399         }
400         
401         return result;
402     }
403     
404     private static class MessageSecurityComparator implements Comparator JavaDoc {
405         
406         private boolean sortNames;
407         
408         public MessageSecurityComparator(boolean sn) {
409             sortNames = sn;
410         }
411         
412         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
413             // Order by response/request settings, then alphabetically by operation/method name
414
MessageSecurity ms1 = (MessageSecurity) o1;
415             MessageSecurity ms2 = (MessageSecurity) o2;
416
417             int result = Utils.strCompareTo(ms1.getRequestProtectionAuthSource(), ms2.getRequestProtectionAuthSource());
418             if(result != 0) {
419                 return result;
420             }
421             result = Utils.strCompareTo(ms1.getRequestProtectionAuthRecipient(), ms2.getRequestProtectionAuthRecipient());
422             if(result != 0) {
423                 return result;
424             }
425             result = Utils.strCompareTo(ms1.getResponseProtectionAuthSource(), ms2.getResponseProtectionAuthSource());
426             if(result != 0) {
427                 return result;
428             }
429             result = Utils.strCompareTo(ms1.getResponseProtectionAuthRecipient(), ms2.getResponseProtectionAuthRecipient());
430             if(result != 0) {
431                 return result;
432             }
433             
434             if(sortNames) {
435                 Message [] m1 = ms1.getMessage();
436                 Message [] m2 = ms2.getMessage();
437
438                 assert m1.length == 1;
439                 assert m2.length == 1;
440
441                 result = Utils.strCompareTo(getMethodName(m1[0]), getMethodName(m2[0]));
442             }
443             
444             return result;
445         }
446
447         public boolean equals(Object JavaDoc obj) {
448             return this == obj;
449         }
450     }
451     
452     private static String JavaDoc getMethodName(Message m) {
453         String JavaDoc name = m.getOperationName();
454         if(name == null) {
455             JavaMethod method = m.getJavaMethod();
456             if(method != null) {
457                 name = method.getMethodName();
458             }
459         }
460         return name;
461     }
462     
463     /** Displays panel for editing the message-security-bindings of the selected endpoint.
464      *
465      * @param parent JPanel that is the parent of this popup - used for centering and sizing.
466      * @param binding Reference to current binding data.
467      * @param aSDDVersion For versioning the UI and creating the correct implementation
468      * of any needed MessageSecurity objects.
469      */

470     static void editMessageSecurityBinding(JPanel JavaDoc parent, Base theBean, boolean editMethodAsOperation,
471             MessageSecurityBinding binding, ASDDVersion asDDVersion, String JavaDoc stringVersion) {
472         EditBinding bindingPanel = new EditBinding(theBean, binding, editMethodAsOperation, asDDVersion, stringVersion);
473         bindingPanel.displayDialog(parent, webserviceBundle.getString("TITLE_EditBindings"), // NOI18N
474
HelpContext.HELP_SERVICE_ENDPOINT_SECURITY); // NOI18N
475
}
476     
477     private void displayDialog(JPanel JavaDoc parent, String JavaDoc title, String JavaDoc helpId) {
478         BetterInputDialog dialog = new BetterInputDialog(parent, title, helpId, this);
479
480         do {
481             int dialogChoice = dialog.display();
482
483             if(dialogChoice == dialog.CANCEL_OPTION) {
484                 break;
485             }
486
487             if(dialogChoice == dialog.OK_OPTION) {
488                 Collection JavaDoc errors = getErrors();
489
490                 if(dialog.hasErrors()) {
491                     // !PW is this even necessary w/ new validation model?
492
dialog.showErrors();
493                 } else {
494                     commit();
495                 }
496             }
497         } while(dialog.hasErrors());
498     }
499
500     private static class BetterInputDialog extends InputDialog {
501         private final EditBinding dialogPanel;
502         private final String JavaDoc panelHelpId;
503
504         public BetterInputDialog(JPanel JavaDoc parent, String JavaDoc title, String JavaDoc helpId, EditBinding childPanel) {
505             super(parent, title);
506
507             dialogPanel = childPanel;
508             panelHelpId = helpId;
509
510             dialogPanel.setPreferredSize(new Dimension JavaDoc(parent.getWidth()*3/4,
511                 dialogPanel.getPreferredSize().height));
512
513             this.getAccessibleContext().setAccessibleName(dialogPanel.getAccessibleContext().getAccessibleName());
514             this.getAccessibleContext().setAccessibleDescription(dialogPanel.getAccessibleContext().getAccessibleDescription());
515
516             getContentPane().add(childPanel, BorderLayout.CENTER);
517             addListeners();
518             pack();
519             setLocationInside(parent);
520             handleErrorDisplay();
521         }
522
523         private void addListeners() {
524             dialogPanel.addPropertyChangeListener(Constants.USER_DATA_CHANGED, new PropertyChangeListener JavaDoc() {
525                 public void propertyChange(PropertyChangeEvent JavaDoc evt) {
526                     handleErrorDisplay();
527                 }
528             });
529         }
530
531         private void handleErrorDisplay() {
532             ArrayList JavaDoc errors = new ArrayList JavaDoc();
533             errors.addAll(dialogPanel.getErrors());
534             setErrors(errors);
535         }
536
537         protected String JavaDoc getHelpId() {
538             return panelHelpId;
539         }
540     }
541  
542     // New for migration to sun DD API model. Factory instance to pass to generic table model
543
// to allow it to create messageDestination beans.
544
static GenericTableModel.ParentPropertyFactory messageSecurityFactory =
545         new GenericTableModel.ParentPropertyFactory() {
546             public CommonDDBean newParentProperty(ASDDVersion asVersion) {
547                 return StorageBeanFactory.getStorageBeanFactory(asVersion).createMessageSecurity();
548             }
549         };
550 }
Popular Tags