View Javadoc

1   /****************************************************************************
2    * Copyright (c) 2005, 2006, 2007, 2008, 2009 Imola Informatica.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the LGPL License v2.1
5    * which accompanies this distribution, and is available at
6    * http://www.gnu.org/licenses/lgpl.html
7    ****************************************************************************/
8   package it.imolinfo.jbi4corba.jbi.wsdl;
9   
10  import it.imolinfo.jbi4corba.Logger;
11  import it.imolinfo.jbi4corba.LoggerFactory;
12  
13  import java.io.Serializable;
14  import java.util.Collections;
15  import java.util.HashMap;
16  import java.util.Map;
17  import java.util.Properties;
18  import java.util.Vector;
19  import java.util.regex.Matcher;
20  import java.util.regex.Pattern;
21  
22  import javax.wsdl.Definition;
23  import javax.wsdl.WSDLException;
24  import javax.wsdl.extensions.ExtensibilityElement;
25  import javax.wsdl.extensions.ExtensionDeserializer;
26  import javax.wsdl.extensions.ExtensionRegistry;
27  import javax.xml.namespace.QName;
28  
29  import org.w3c.dom.Element;
30  
31  import com.ibm.wsdl.util.xml.DOMUtils;
32  import com.ibm.wsdl.util.xml.QNameUtils;
33  import com.sun.jbi.internationalization.Messages;
34  
35  /**
36   * Deserializer for the Jbi4Corba WSDL Extension (addess element), according
37   * with JWSDL specs. See JSR 110.
38   * 
39   * @author <a href="mailto:mpiraccini@imolinfo.it">Marco Piraccini</a>
40   */
41  @SuppressWarnings("unchecked")
42  public class Jbi4CorbaAddressDeserializer implements ExtensionDeserializer,
43          Serializable {
44  
45      /** serialVersionUID. */
46      private static final long serialVersionUID = -8967121375407535073L;
47      private static final Logger LOG = LoggerFactory.getLogger(Jbi4CorbaAddressDeserializer.class);
48      // Pattern for finding application variable tokens
49      private static final String ENV_VAR_REGEX = "\\$\\{([a-zA-Z0-9\\.\\-\\_^\\{\\}]+)\\}";
50      private static final Pattern mPattern = Pattern.compile(ENV_VAR_REGEX);
51      protected Map<String, String[]> mEnvVariableMap = new HashMap<String, String[]>();
52      private static final Messages mMessages = Messages.getMessages(Jbi4CorbaAddressDeserializer.class);
53  
54      /**
55       * Default constructor.
56       */
57      public Jbi4CorbaAddressDeserializer() {
58      }
59  
60      public Jbi4CorbaAddressDeserializer(Map<String, String[]> envVariableMap) {
61          this();
62          mEnvVariableMap.putAll(envVariableMap);
63      }
64  
65      /*
66       * (non-Javadoc)
67       * 
68       * @see
69       * javax.wsdl.extensions.ExtensionDeserializer#unmarshall(java.lang.Class,
70       * javax.xml.namespace.QName, org.w3c.dom.Element, javax.wsdl.Definition,
71       * javax.wsdl.extensions.ExtensionRegistry)
72       */
73      /**
74       * @param parentType
75       *            The parent type
76       * @param elementType
77       *            The element type
78       * @param el
79       *            The elemnt
80       * @param def
81       *            The definition
82       * @param extReg
83       *            The extension registry
84       * @return The return
85       * @throws WSDLException
86       *             The WSDL exception
87       */
88      public ExtensibilityElement unmarshall(Class parentType, QName elementType,
89              Element el, Definition def, ExtensionRegistry extReg)
90              throws WSDLException {
91  
92          Jbi4CorbaAddress jbi4CorbaAddress = (Jbi4CorbaAddress) extReg.createExtension(parentType, elementType);
93  
94          jbi4CorbaAddress.setName(getAttrAndResolveEnvVar(el,
95                  Jbi4CorbaExtension.NAME_ATTRIBUTE));
96  
97          jbi4CorbaAddress.setLocalizationType(getAttrAndResolveEnvVar(el,
98                  Jbi4CorbaExtension.LOCALIZATION_TYPE_ATTRIBUTE));
99  
100         jbi4CorbaAddress.setPeristent(new Boolean(getAttrAndResolveEnvVar(el,
101                 Jbi4CorbaExtension.PERSISTENT_ATTRIBUTE)));
102 
103         // Reads the ORB element
104         Element tempEl = DOMUtils.getFirstChildElement(el);
105         while (tempEl != null) {
106             // ORB element
107             if (QNameUtils.matches(Jbi4CorbaExtension.Q_ELEM_JBI4CORBA_ORB,
108                     tempEl)) {
109 
110                 // loads the ORB properties
111                 Properties orbProperties = new Properties();
112 
113                 Element propertyElement = DOMUtils.getFirstChildElement(tempEl);
114 
115                 while (propertyElement != null) {
116                     String propertyName = DOMUtils.getAttribute(
117                             propertyElement, Jbi4CorbaExtension.NAME_ATTRIBUTE);
118                     String propertyValue = getAttrAndResolveEnvVar(
119                             propertyElement, Jbi4CorbaExtension.VALUE_ATTRIBUTE);
120                     orbProperties.put(propertyName, propertyValue);
121                     LOG.debug("Read ORB properties: [" + propertyName + "] " + " [" + propertyValue + "]");
122                     propertyElement = DOMUtils.getNextSiblingElement(propertyElement);
123                 }
124 
125                 LOG.debug("Read orb properties: " + orbProperties);
126                 jbi4CorbaAddress.setOrbProperties(orbProperties);
127             }
128             tempEl = DOMUtils.getNextSiblingElement(tempEl);
129         }
130 
131         LOG.debug("Loaded from address extension the CORBA name: " + jbi4CorbaAddress.getName());
132         return jbi4CorbaAddress;
133     }
134 
135     public Map<String, String[]> getEnvVariableMap() {
136         return Collections.unmodifiableMap(mEnvVariableMap);
137     }
138 
139     // protected Object[] getEnvVariableNames(String attrName, String attrVal)
140     protected Object[] getEnvVariableNames(String attrName, String attrVal)
141             throws Exception {
142         String tokenName = null;
143         Matcher m = mPattern.matcher(attrVal);
144         Vector refs = new Vector();
145         while (m.find()) {
146             tokenName = m.group(1);
147             if (tokenName == null || tokenName.trim().length() == 0) {
148                 throw new Exception(mMessages.getString(
149                         "CRB000304_Invalid_Token_Name", tokenName));
150             }
151             refs.add(tokenName);
152         }
153 
154         if (attrVal.indexOf("${}") >= 0) {
155             throw new Exception(mMessages.getString(
156                     "CRB000305_Invalid_Empty_Token_Name", new Object[]{
157                         attrVal, attrName}));
158         }
159 
160         return refs.toArray();
161     }
162 
163     protected String getAttrAndResolveEnvVar(Element el, String attrName)
164             throws WSDLException {
165         String attrVal = DOMUtils.getAttribute(el, attrName);
166         if (attrVal != null) {
167             try {
168                 if (hasMigrationEnvVarRef(attrVal)) {
169                     // attribute contains env var reference(s)
170 
171                     Object[] vars = getEnvVariableNames(attrName, attrVal);
172                     if (vars != null) {
173                         for (int i = 0; i < vars.length; i++) {
174                             String[] varDesc = (String[]) mEnvVariableMap.get(vars[i]);
175                             if (varDesc == null || varDesc.length != 2) {
176                                 throw new WSDLException(
177                                         "INVALID_WSDL",
178                                         mMessages.getString(
179                                         "CRB000306_Invalid_Env_Var_Ref_No_Def",
180                                         new Object[]{vars[i],
181                                             attrVal,
182                                             attrName}));
183                             } else {
184                                 // check if the de-referenced value has ${ in it
185                                 String varVal = varDesc[0];
186                                 if (varVal == null) {
187                                     throw new WSDLException(
188                                             "INVALID_WSDL",
189                                             mMessages.getString(
190                                             "CRB000307_Invalid_Env_Var_Value_Null",
191                                             new Object[]{
192                                                 vars[i],
193                                                 attrName}));
194                                 }
195                                 if (varVal.indexOf("${") >= 0) {
196                                     throw new WSDLException(
197                                             "INVALID_WSDL",
198                                             mMessages.getString(
199                                             "CRB000308_Invalid_Var_Value_Contains_Var_Ref",
200                                             new Object[]{
201                                                 attrName,
202                                                 attrVal,
203                                                 vars[i],
204                                                 varVal}));
205                                 }
206                                 attrVal = attrVal.replace("${" + vars[i] + "}",
207                                         varVal);
208                             }
209                         }
210                     }
211                 }
212             } catch (WSDLException e) {
213                 throw e;
214             } catch (Exception e) {
215                 throw new WSDLException("INVALID_WSDL", e.getMessage());
216             }
217         }
218         return attrVal;
219     }
220 
221     protected boolean hasMigrationEnvVarRef(String attrVal) throws Exception {
222         return mPattern.matcher(attrVal).find();
223     }
224 }