KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.netbeans.modules.websvc.wsitmodelext.policy.All;
23 import org.netbeans.modules.websvc.wsitmodelext.policy.Policy;
24 import org.netbeans.modules.websvc.wsitmodelext.rm.FlowControl;
25 import org.netbeans.modules.websvc.wsitmodelext.rm.InactivityTimeout;
26 import org.netbeans.modules.websvc.wsitmodelext.rm.RMAssertion;
27 import org.netbeans.modules.websvc.wsitmodelext.rm.RMQName;
28 import org.netbeans.modules.xml.wsdl.model.Binding;
29 import org.netbeans.modules.xml.wsdl.model.Operation;
30 import org.netbeans.modules.xml.wsdl.model.WSDLComponent;
31 import org.netbeans.modules.xml.wsdl.model.WSDLComponentFactory;
32 import org.netbeans.modules.xml.wsdl.model.WSDLModel;
33 import org.netbeans.modules.xml.xam.Model;
34
35 /**
36  *
37  * @author Martin Grebac
38  */

39 public class RMModelHelper {
40     
41     public static final String JavaDoc DEFAULT_TIMEOUT = "600000"; //NOI18N
42
public static final String JavaDoc DEFAULT_MAXRCVBUFFERSIZE = "32"; //NOI18N
43

44     /**
45      * Creates a new instance of RMModelHelper
46      */

47     public RMModelHelper() {
48     }
49     
50     public static RMAssertion getRMAssertion(Policy p) {
51         return (RMAssertion) PolicyModelHelper.getTopLevelElement(p, RMAssertion.class);
52     }
53     
54     // checks if RM is enabled in the config wsdl on specified binding
55
public static boolean isRMEnabled(WSDLComponent c) {
56         if (c instanceof Operation) {
57             Operation o = (Operation)c;
58             Binding b = (Binding)o.getParent();
59             return isRMEnabledB(b);
60         }
61         if (c instanceof Binding) {
62             return isRMEnabledB((Binding)c);
63         }
64         return false;
65     }
66     
67     // checks if RM is enabled in the config wsdl on specified binding
68
private static boolean isRMEnabledB(Binding b) {
69         WSDLModel model = b.getModel();
70         Policy p = PolicyModelHelper.getPolicyForElement(b);
71         if (p != null) {
72             RMAssertion rm = getRMAssertion(p);
73             return (rm != null);
74         }
75         return false;
76     }
77     
78     // enables RM in the config wsdl on specified binding
79
public static void enableRM(Binding b) {
80         All a = PolicyModelHelper.createPolicy(b);
81         PolicyModelHelper.createElement(a, RMQName.RMASSERTION.getQName(), RMAssertion.class, false);
82     }
83
84     // disables RM in the config wsdl on specified binding
85
public static void disableRM(Binding b) {
86         WSDLModel model = b.getModel();
87         Policy p = PolicyModelHelper.getPolicyForElement(b);
88         RMAssertion rm = getRMAssertion(p);
89         if (rm != null) {
90             PolicyModelHelper.removeElement(rm.getParent(), RMAssertion.class, false);
91             PolicyModelHelper.removeElement(rm.getParent(), FlowControl.class, false);
92         }
93         PolicyModelHelper.cleanPolicies(b);
94     }
95     
96     public static String JavaDoc getInactivityTimeout(Binding b) {
97         Policy p = PolicyModelHelper.getPolicyForElement(b);
98         RMAssertion rm = getRMAssertion(p);
99         return getInactivityTimeout(rm);
100     }
101     
102     public static String JavaDoc getInactivityTimeout(RMAssertion rm) {
103         String JavaDoc timeout = null;
104         if (rm != null) {
105             InactivityTimeout time = rm.getInactivityTimeout();
106             if (time != null) {
107                 timeout = time.getMilliseconds();
108             }
109         }
110         return timeout;
111     }
112
113     public static void setInactivityTimeout(Binding b, String JavaDoc value) {
114         Policy p = PolicyModelHelper.getPolicyForElement(b);
115         RMAssertion rm = getRMAssertion(p);
116         setInactivityTimeout(rm, value);
117     }
118     
119     public static void setInactivityTimeout(RMAssertion rm, String JavaDoc value) {
120         if (rm != null) {
121             Model model = rm.getModel();
122             boolean isTransaction = model.isIntransaction();
123             if (!isTransaction) {
124                 model.startTransaction();
125             }
126             try {
127                 InactivityTimeout inTimeout = rm.getInactivityTimeout();
128                 if (inTimeout == null) {
129                     if (value != null) { // if is null, then there's no element and we want to remove it -> do nothing
130
WSDLComponentFactory wcf = rm.getModel().getFactory();
131                         InactivityTimeout inT = (InactivityTimeout)wcf.create(rm,
132                                 RMQName.INACTIVITYTIMEOUT.getQName()
133                                );
134                         inT.setMilliseconds(value);
135                         rm.addExtensibilityElement(inT);
136                     }
137                 } else {
138                     if (value == null) {
139                         rm.removeInactivityTimeout(inTimeout);
140                     } else {
141                         inTimeout.setMilliseconds(value);
142                     }
143                 }
144             } finally {
145                 if (!isTransaction) {
146                     model.endTransaction();
147                 }
148             }
149         }
150     }
151
152 }
153
Popular Tags