001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.wsrp.producer.config;
023:
024: import org.jboss.portal.common.util.ParameterValidation;
025: import org.jboss.portal.wsrp.WSRPConstants;
026: import org.jboss.portal.wsrp.WSRPUtils;
027: import org.jboss.portal.wsrp.producer.config.impl.ProducerConfigurationImpl;
028: import org.jboss.portal.wsrp.producer.config.impl.ProducerRegistrationRequirementsImpl;
029: import org.jboss.portal.wsrp.registration.LocalizedString;
030: import org.jboss.portal.wsrp.registration.RegistrationPropertyDescription;
031: import org.jboss.util.StringPropertyReplacer;
032: import org.jboss.xb.binding.ObjectModelFactory;
033: import org.jboss.xb.binding.UnmarshallingContext;
034: import org.xml.sax.Attributes;
035:
036: import javax.xml.namespace.QName;
037: import java.util.Locale;
038:
039: /**
040: * @author <a href="mailto:chris.laprun@jboss.com">Chris Laprun</a>
041: * @version $Revision: 9471 $
042: * @since 2.6
043: */
044: public class ProducerConfigurationFactory implements ObjectModelFactory {
045: private static final String REGISTRATION_CONFIG = "registration-configuration";
046: private static final boolean DEBUG = false;
047: private boolean registrationDone = false;
048:
049: // kept to support old-style type
050: private static final String LEGACY_XSD_STRING = "xsd:string";
051:
052: public Object newRoot(Object root, UnmarshallingContext nav,
053: String nsURI, String localName, Attributes attrs) {
054: if (DEBUG) {
055: System.out.println("newRoot " + localName);
056: }
057:
058: if ("producer-configuration".equals(localName)) {
059: return new ProducerConfigurationImpl();
060: }
061: throw new IllegalArgumentException(
062: "The processed file doesn't seem to have the proper format, "
063: + "was expecting producer-configuration, instead got "
064: + localName);
065: }
066:
067: public Object newChild(
068: ProducerConfigurationImpl producerConfiguration,
069: UnmarshallingContext nav, String nsURI, String localName,
070: Attributes attrs) {
071: if (DEBUG) {
072: System.out.println("newChild prodConf " + localName);
073: }
074:
075: if (REGISTRATION_CONFIG.equals(localName)) {
076: // check that we don't have several registration-configuration elements.
077: if (registrationDone) {
078: throw new IllegalArgumentException(
079: "Only one registration-configuration element can be defined!");
080: }
081:
082: ProducerRegistrationRequirements registrationRequirements = new ProducerRegistrationRequirementsImpl();
083: registrationRequirements.setRegistrationRequired(true);
084: String descRequiresRegistration = attrs
085: .getValue("fullServiceDescriptionRequiresRegistration");
086: if ("true".equals(descRequiresRegistration)) {
087: registrationRequirements
088: .setRegistrationRequiredForFullDescription(true);
089: } else if (descRequiresRegistration == null
090: || "false".equals(descRequiresRegistration)) {
091: registrationRequirements
092: .setRegistrationRequiredForFullDescription(false);
093: } else {
094: throw new IllegalArgumentException(
095: "Invalid value for fullServiceDescriptionRequiresRegistration attribute. Acceptable values are: true, false.");
096: }
097: registrationDone = true;
098: return registrationRequirements;
099: }
100: return null;
101: }
102:
103: public Object completeRoot(Object root, UnmarshallingContext nav,
104: String nsURI, String localName) {
105: return root;
106: }
107:
108: public Object newChild(ProducerRegistrationRequirementsImpl regReq,
109: UnmarshallingContext nav, String nsURI, String localName,
110: Attributes attrs) {
111: if (DEBUG) {
112: System.out.println("newChild regReq " + localName);
113: }
114:
115: if ("registration-property-description".equals(localName)) {
116: return new RegistrationPropertyDescription();
117: } else {
118: return null;
119: }
120: }
121:
122: public void setValue(ProducerRegistrationRequirementsImpl regReq,
123: UnmarshallingContext nav, String nsURI, String localName,
124: String value) {
125: if ("registration-policy".equals(localName)) {
126: value = StringPropertyReplacer.replaceProperties(value);
127: regReq.setPolicyClassName(value);
128: } else if ("registration-property-validator".equals(localName)) {
129: value = StringPropertyReplacer.replaceProperties(value);
130: regReq.setValidatorClassName(value);
131: }
132: }
133:
134: public void setValue(RegistrationPropertyDescription desc,
135: UnmarshallingContext nav, String nsURI, String localName,
136: String value) {
137: if (DEBUG) {
138: System.out.println("setvalue desc " + localName);
139: }
140:
141: if ("name".equals(localName)) {
142: value = StringPropertyReplacer.replaceProperties(value);
143: desc.setName(new QName(value));
144: } else if ("type".equals(localName)) {
145: value = StringPropertyReplacer.replaceProperties(value);
146:
147: // first check that we still support type as "xsd:string"...
148: if (!LEGACY_XSD_STRING.equals(value)) {
149: QName type = nav.resolveQName(value);
150: if (!WSRPConstants.XSD_STRING.equals(type)) {
151: throw new IllegalArgumentException(
152: "'"
153: + value
154: + "' is not a supported type. Currently, only 'xsd:string' is supported.");
155: }
156: }
157:
158: desc.setType(WSRPConstants.XSD_STRING);
159: }
160: }
161:
162: public Object newChild(RegistrationPropertyDescription desc,
163: UnmarshallingContext nav, String nsURI, String localName,
164: Attributes attrs) {
165: if (DEBUG) {
166: System.out.println("newchild desc " + localName);
167: }
168:
169: if ("hint".equals(localName) || "label".equals(localName)) {
170: String lang = attrs.getValue("xml:lang");
171: ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(
172: lang, "xml:lang", localName);
173: Locale locale = WSRPUtils.getLocale(lang);
174:
175: String resourceName = attrs.getValue("resourceName");
176:
177: LocalizedString string = new LocalizedString();
178: string.setLocale(locale);
179:
180: if (resourceName != null && resourceName.length() > 0) {
181: string.setResourceName(resourceName);
182: }
183:
184: return string;
185: }
186:
187: return null;
188: }
189:
190: public void setValue(LocalizedString string,
191: UnmarshallingContext nav, String nsURI, String localName,
192: String value) {
193: if (DEBUG) {
194: System.out.println("setvalue string " + localName);
195: }
196:
197: value = StringPropertyReplacer.replaceProperties(value);
198: string.setValue(value);
199: }
200:
201: public void addChild(ProducerConfigurationImpl conf,
202: ProducerRegistrationRequirementsImpl regReq,
203: UnmarshallingContext nav, String nsURI, String localName) {
204: if (DEBUG) {
205: System.out.println("addchild conf regReq " + localName);
206: }
207:
208: if (!ProducerRegistrationRequirementsImpl.DEFAULT_POLICY_CLASS_NAME
209: .equals(regReq.getPolicyClassName())
210: && regReq.getValidatorClassName() != null) {
211: throw new IllegalStateException(
212: "Doesn't make sense to define a property validator without using DefaultRegistrationPolicy!");
213: }
214:
215: conf.setRegistrationRequirements(regReq);
216: }
217:
218: public void addChild(ProducerRegistrationRequirementsImpl regReq,
219: RegistrationPropertyDescription desc,
220: UnmarshallingContext nav, String nsURI, String localName) {
221: if (DEBUG) {
222: System.out.println("addchild regreq desc " + localName);
223: }
224:
225: regReq.addRegistrationProperty(desc);
226: }
227:
228: public void addChild(RegistrationPropertyDescription desc,
229: LocalizedString string, UnmarshallingContext nav,
230: String nsURI, String localName) {
231: if (DEBUG) {
232: System.out.println("addchild desc string " + localName);
233: }
234:
235: if ("hint".equals(localName)) {
236: desc.setHint(string);
237: } else if ("label".equals(localName)) {
238: desc.setLabel(string);
239: }
240: }
241: }
|