001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.websvc.wsitconf.wsdlmodelext;
043:
044: import org.netbeans.modules.websvc.wsitmodelext.policy.All;
045: import org.netbeans.modules.websvc.wsitmodelext.policy.Policy;
046: import org.netbeans.modules.websvc.wsitmodelext.rm.FlowControl;
047: import org.netbeans.modules.websvc.wsitmodelext.rm.InactivityTimeout;
048: import org.netbeans.modules.websvc.wsitmodelext.rm.RMAssertion;
049: import org.netbeans.modules.websvc.wsitmodelext.rm.RMQName;
050: import org.netbeans.modules.xml.wsdl.model.Binding;
051: import org.netbeans.modules.xml.wsdl.model.Operation;
052: import org.netbeans.modules.xml.wsdl.model.WSDLComponent;
053: import org.netbeans.modules.xml.wsdl.model.WSDLComponentFactory;
054: import org.netbeans.modules.xml.xam.Model;
055:
056: /**
057: *
058: * @author Martin Grebac
059: */
060: public class RMModelHelper {
061:
062: public static final String DEFAULT_TIMEOUT = "600000"; //NOI18N
063: public static final String DEFAULT_MAXRCVBUFFERSIZE = "32"; //NOI18N
064:
065: /**
066: * Creates a new instance of RMModelHelper
067: */
068: public RMModelHelper() {
069: }
070:
071: public static RMAssertion getRMAssertion(Policy p) {
072: return (RMAssertion) PolicyModelHelper.getTopLevelElement(p,
073: RMAssertion.class);
074: }
075:
076: // checks if RM is enabled in the config wsdl on specified binding
077: public static boolean isRMEnabled(WSDLComponent c) {
078: if (c instanceof Operation) {
079: Operation o = (Operation) c;
080: Binding b = (Binding) o.getParent();
081: return isRMEnabledB(b);
082: }
083: if (c instanceof Binding) {
084: return isRMEnabledB((Binding) c);
085: }
086: return false;
087: }
088:
089: // checks if RM is enabled in the config wsdl on specified binding
090: private static boolean isRMEnabledB(Binding b) {
091: Policy p = PolicyModelHelper.getPolicyForElement(b);
092: if (p != null) {
093: RMAssertion rm = getRMAssertion(p);
094: return (rm != null);
095: }
096: return false;
097: }
098:
099: // enables RM in the config wsdl on specified binding
100: public static void enableRM(Binding b) {
101: All a = PolicyModelHelper.createPolicy(b, true);
102: PolicyModelHelper.createElement(a, RMQName.RMASSERTION
103: .getQName(), RMAssertion.class, false);
104: }
105:
106: // disables RM in the config wsdl on specified binding
107: public static void disableRM(Binding b) {
108: Policy p = PolicyModelHelper.getPolicyForElement(b);
109: RMAssertion rm = getRMAssertion(p);
110: if (rm != null) {
111: PolicyModelHelper.removeElement(rm.getParent(),
112: RMAssertion.class, false);
113: PolicyModelHelper.removeElement(rm.getParent(),
114: FlowControl.class, false);
115: }
116: PolicyModelHelper.cleanPolicies(b);
117: }
118:
119: public static String getInactivityTimeout(Binding b) {
120: Policy p = PolicyModelHelper.getPolicyForElement(b);
121: RMAssertion rm = getRMAssertion(p);
122: return getInactivityTimeout(rm);
123: }
124:
125: public static String getInactivityTimeout(RMAssertion rm) {
126: String timeout = null;
127: if (rm != null) {
128: InactivityTimeout time = rm.getInactivityTimeout();
129: if (time != null) {
130: timeout = time.getMilliseconds();
131: }
132: }
133: return timeout;
134: }
135:
136: public static void setInactivityTimeout(Binding b, String value) {
137: Policy p = PolicyModelHelper.getPolicyForElement(b);
138: RMAssertion rm = getRMAssertion(p);
139: setInactivityTimeout(rm, value);
140: }
141:
142: public static void setInactivityTimeout(RMAssertion rm, String value) {
143: if (rm != null) {
144: Model model = rm.getModel();
145: boolean isTransaction = model.isIntransaction();
146: if (!isTransaction) {
147: model.startTransaction();
148: }
149: try {
150: InactivityTimeout inTimeout = rm.getInactivityTimeout();
151: if (inTimeout == null) {
152: if (value != null) { // if is null, then there's no element and we want to remove it -> do nothing
153: WSDLComponentFactory wcf = rm.getModel()
154: .getFactory();
155: InactivityTimeout inT = (InactivityTimeout) wcf
156: .create(rm, RMQName.INACTIVITYTIMEOUT
157: .getQName());
158: inT.setMilliseconds(value);
159: rm.addExtensibilityElement(inT);
160: }
161: } else {
162: if (value == null) {
163: rm.removeInactivityTimeout(inTimeout);
164: } else {
165: inTimeout.setMilliseconds(value);
166: }
167: }
168: } finally {
169: if (!isTransaction) {
170: model.endTransaction();
171: }
172: }
173: }
174: }
175:
176: }
|