KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > wsitconf > wsdlmodelext > TxModelHelper


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
20 package org.netbeans.modules.websvc.wsitconf.wsdlmodelext;
21
22 import java.util.List JavaDoc;
23 import org.netbeans.modules.websvc.wsitconf.ui.ComboConstants;
24 import org.netbeans.modules.websvc.wsitmodelext.tx.ATAlwaysCapability;
25 import org.netbeans.modules.websvc.wsitmodelext.tx.ATAssertion;
26 import org.netbeans.modules.websvc.wsitmodelext.tx.TxQName;
27 import org.netbeans.modules.websvc.wsitmodelext.policy.All;
28 import org.netbeans.modules.websvc.wsitmodelext.policy.Policy;
29 import org.netbeans.modules.xml.wsdl.model.BindingOperation;
30 import org.netbeans.modules.xml.wsdl.model.WSDLComponentFactory;
31 import org.netbeans.modules.xml.wsdl.model.WSDLModel;
32 import org.openide.nodes.Node;
33
34 /**
35  *
36  * @author Martin Grebac
37  */

38 public class TxModelHelper {
39     
40     /**
41      * Creates a new instance of TxModelHelper
42      */

43     public TxModelHelper() {
44     }
45     
46     private static ATAssertion getATAssertion(Policy p) {
47         return (ATAssertion) PolicyModelHelper.getTopLevelElement(p, ATAssertion.class);
48     }
49     
50     private static ATAlwaysCapability getATAlwaysAssertion(Policy p) {
51         return (ATAlwaysCapability) PolicyModelHelper.getTopLevelElement(p, ATAlwaysCapability.class);
52     }
53
54     /** Tx Value should be one of ComboConstants.TX_*
55      */

56     public static void setTx(BindingOperation bop, Node node, String JavaDoc txValue) {
57         
58 // String txAnnot = getTxFromAnnotation(bop, node);
59
// String txConfig = getTxFromConfig(bop);
60

61 // if (WSITModelSupport.isServiceFromWsdl(node) || // do not care about annotation if WS from WSDL
62
// (ComboConstants.TX_NOTSUPPORTED.equals(txConfig) && (txAnnot == null)) || ( // Nothing is set
63
// !ComboConstants.TX_NOTSUPPORTED.equals(txConfig))) { // Something is set in wsit config
64
setTxInConfig(bop, node, txValue);
65 // } else {
66
// setTxInAnnotation(bop, node, txValue);
67
// }
68
}
69
70     private static void setTxInAnnotation(BindingOperation bop, Node node, String JavaDoc txValue) {
71 // Method m = JMIUtils.getMethod(node, bop.getName());
72
//
73
// VariableAccess va = getTxAnnotationVariable(m);
74
// if (va != null) {
75
// va.setName(txValue);
76
// }
77
//
78
}
79     
80     private static void setTxInConfig(BindingOperation bop, Node node, String JavaDoc txValue) {
81         WSDLModel model = bop.getModel();
82         Policy p = PolicyModelHelper.getPolicyForElement(bop);
83         boolean isTransaction = model.isIntransaction();
84         if (!isTransaction) {
85             model.startTransaction();
86         }
87         try {
88
89             ATAssertion tx = getATAssertion(p);
90             ATAlwaysCapability txAlways = getATAlwaysAssertion(p);
91             
92             // first remove what has been there already
93
if (tx != null) {
94                 tx.getParent().removeExtensibilityElement(tx);
95             }
96             if (txAlways != null) {
97                 txAlways.getParent().removeExtensibilityElement(txAlways);
98             }
99
100             // now add what is required
101
WSDLComponentFactory wcf = model.getFactory();
102             All all = PolicyModelHelper.createPolicy(bop);
103             
104             if ((ComboConstants.TX_NEVER.equals(txValue)) ||
105                 (ComboConstants.TX_NOTSUPPORTED.equals(txValue))) {
106                     PolicyModelHelper.cleanPolicies(bop);
107                     return;
108             }
109             
110             if (ComboConstants.TX_MANDATORY.equals(txValue)) {
111                 tx = (ATAssertion)wcf.create(all, TxQName.ATASSERTION.getQName());
112                 all.addExtensibilityElement(tx);
113             }
114
115             if (ComboConstants.TX_REQUIRED.equals(txValue)) {
116                 tx = (ATAssertion)wcf.create(all, TxQName.ATASSERTION.getQName());
117                 tx.setOptional(true);
118                 all.addExtensibilityElement(tx);
119                 txAlways = (ATAlwaysCapability)wcf.create(all, TxQName.ATALWAYSCAPABILITY.getQName());
120                 all.addExtensibilityElement(txAlways);
121             }
122
123             if (ComboConstants.TX_REQUIRESNEW.equals(txValue)) {
124                 txAlways = (ATAlwaysCapability)wcf.create(all, TxQName.ATALWAYSCAPABILITY.getQName());
125                 all.addExtensibilityElement(txAlways);
126             }
127
128             if (ComboConstants.TX_SUPPORTED.equals(txValue)) {
129                 tx = (ATAssertion)wcf.create(all, TxQName.ATASSERTION.getQName());
130                 tx.setOptional(true);
131                 all.addExtensibilityElement(tx);
132             }
133             
134         } finally {
135             if (!isTransaction) {
136                 model.endTransaction();
137             }
138         }
139     }
140
141     public static String JavaDoc getTx(BindingOperation bop, Node node) {
142         String JavaDoc tx = getTxFromConfig(bop);
143         
144 // if (WSITModelSupport.isServiceFromWsdl(node) || // WS from WSDL doesn't care about annotations
145
// ((tx != null) && (tx != ComboConstants.TX_NOTSUPPORTED) && (tx != ComboConstants.TX_NEVER)) // if there's something in config, do not consider annotations
146
// ) {
147
// return tx;
148
// }
149
// tx = getTxFromAnnotation(bop, node);
150
return tx;
151     }
152
153     private static String JavaDoc getTxFromAnnotation(BindingOperation bop, Node node) {
154 // JavaClass jc = JMIUtils.getJavaClassFromNode(node);
155
// Method m = JMIUtils.getMethod(node, bop.getName());
156
//
157
// // first check if there's annotation on method
158
// String value = getTxFromAnnotation(m);
159
// if (value != null) {
160
// return value;
161
// }
162
//
163
// // then check the class; if set on class - applies to all methods
164
// value = getTxFromAnnotation(jc);
165
// if (value != null) {
166
// return value;
167
// }
168

169         return ComboConstants.TX_NOTSUPPORTED;
170     }
171
172 // private static String getTxFromAnnotation(Feature f) {
173
// VariableAccess va = getTxAnnotationVariable(f);
174
// if (va != null) {
175
// return getTxComboValue(va.getName());
176
// }
177
// return null;
178
// }
179

180 // private static VariableAccess getTxAnnotationVariable(Feature f) {
181
// List<Annotation> annotations = f.getAnnotations();
182
// for (Annotation a : annotations) {
183
// String aName = a.getType().getName();
184
// if ("javax.ejb.TransactionAttribute".equals(aName)) { //NOI18N
185
// List<AttributeValue> attribs = a.getAttributeValues();
186
// for (AttributeValue attr : attribs) {
187
// InitialValue iv = attr.getValue();
188
// if (iv instanceof VariableAccess) {
189
// return (VariableAccess) iv;
190
// }
191
// }
192
// }
193
// }
194
// return null;
195
// }
196

197     private static String JavaDoc getTxComboValue(String JavaDoc annotationAttr) {
198         if (annotationAttr != null) {
199             if ("MANDATORY".equals(annotationAttr)) { //NOI18N
200
return ComboConstants.TX_MANDATORY;
201             }
202             if ("REQUIRED".equals(annotationAttr)) { //NOI18N
203
return ComboConstants.TX_REQUIRED;
204             }
205             if ("REQUIRES_NEW".equals(annotationAttr)) { //NOI18N
206
return ComboConstants.TX_REQUIRESNEW;
207             }
208             if ("SUPPORTED".equals(annotationAttr)) { //NOI18N
209
return ComboConstants.TX_SUPPORTED;
210             }
211             if ("NOT_SUPPORTED".equals(annotationAttr)) { //NOI18N
212
return ComboConstants.TX_NOTSUPPORTED;
213             }
214             if ("NEVER".equals(annotationAttr)) { //NOI18N
215
return ComboConstants.TX_NEVER;
216             }
217         }
218         return null;
219     }
220
221     private static String JavaDoc getAnnotationAttrValue(String JavaDoc comboStr) {
222         if (comboStr != null) {
223             if (ComboConstants.TX_MANDATORY.equals(comboStr)) {
224                 return "MANDATORY"; //NOI18N
225
}
226             if (ComboConstants.TX_REQUIRED.equals(comboStr)) {
227                 return "REQUIRED"; //NOI18N
228
}
229             if (ComboConstants.TX_REQUIRESNEW.equals(comboStr)) {
230                 return "REQUIRES_NEW"; //NOI18N
231
}
232             if (ComboConstants.TX_SUPPORTED.equals(comboStr)) {
233                 return "SUPPORTED"; //NOI18N
234
}
235             if (ComboConstants.TX_NOTSUPPORTED.equals(comboStr)) {
236                 return "NOT_SUPPORTED"; //NOI18N
237
}
238             if (ComboConstants.TX_NEVER.equals(comboStr)) {
239                 return "NEVER"; //NOI18N
240
}
241         }
242         return null;
243     }
244     
245     private static String JavaDoc getTxFromConfig(BindingOperation bop) {
246         WSDLModel model = bop.getModel();
247         Policy p = PolicyModelHelper.getPolicyForElement(bop);
248
249         ATAssertion tx = getATAssertion(p);
250         ATAlwaysCapability txAlways = getATAlwaysAssertion(p);
251         
252         if ((tx != null) && (txAlways == null)) {
253             if (tx.getOptional()) {
254                 return ComboConstants.TX_SUPPORTED;
255             }
256             return ComboConstants.TX_MANDATORY;
257         }
258         if ((tx != null) && (txAlways != null)) {
259             return ComboConstants.TX_REQUIRED;
260         }
261         if ((tx == null) && (txAlways != null)) {
262             return ComboConstants.TX_REQUIRESNEW;
263         }
264         
265         return ComboConstants.TX_NOTSUPPORTED;
266     }
267
268 }
269
Popular Tags