View Javadoc

1   /*
2    *  soapui, copyright (C) 2005 Ole Matzura / eviware.com 
3    *
4    *  SoapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of the GNU Lesser General Public License as published by the Free Software Foundation; 
6    *  either version 2.1 of the License, or (at your option) any later version.
7    *
8    *  SoapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
10   *  See the GNU Lesser General Public License for more details at gnu.org.
11   */
12  
13  package com.eviware.soapui.impl.wsdl.support;
14  
15  import javax.wsdl.Definition;
16  import javax.wsdl.factory.WSDLFactory;
17  import javax.wsdl.xml.WSDLReader;
18  import javax.xml.namespace.QName;
19  
20  import org.apache.log4j.Logger;
21  import org.apache.xmlbeans.SchemaType;
22  import org.apache.xmlbeans.SchemaTypeLoader;
23  
24  import com.eviware.soapui.SoapUI;
25  import com.eviware.soapui.support.ProgressDialog;
26  import com.eviware.soapui.support.SwingWorker;
27  
28  /***
29   * Holder for WSDL4J Definitions and related SchemaTypeLoader types
30   * 
31   * @author Ole.Matzura
32   */
33  
34  public class WsdlContext
35  {
36     private String url;
37     private Definition definition;
38     private SchemaTypeLoader schemaTypes;
39     private boolean loaded;
40     private final static Logger log = Logger.getLogger( WsdlContext.class );
41  
42     public WsdlContext( String url )
43     {
44        this.url = url;
45     }
46     
47     public Definition getDefinition() throws Exception
48     {
49        if( !loaded )
50           load();
51        
52        return definition;
53     }
54     
55     public void setDefinition( String url ) 
56     {
57     	this.url = url;
58     	loaded = false;
59     }
60     
61     public void load() throws Exception
62     {
63        ProgressDialog progressDialog = SoapUI.getInstance() == null ? null : 
64        	new ProgressDialog( "Progress", "Loading WSDL", 1, "Loading definition..", null );
65  
66        Loader loader = new Loader( progressDialog );
67        loader.start();
68        if( progressDialog != null )
69        {
70        	progressDialog.setVisible( true );
71        }
72        
73        if( loader.get() != null )
74        {
75           throw (Exception)loader.get();
76        }
77        else loaded = true;
78     }
79  
80     public SchemaTypeLoader getSchemaTypes() throws Exception
81     {
82        if( !loaded )
83           load();
84        
85        return schemaTypes;
86     }
87  
88     private class Loader extends SwingWorker
89     {
90        private ProgressDialog progressDialog;
91  
92        public Loader( ProgressDialog progressDialog )
93        {
94           this.progressDialog = progressDialog;
95        }
96        
97        public Object construct()
98        {
99           try
100          {
101             WSDLFactory factory = WSDLFactory.newInstance();
102             WSDLReader wsdlReader = factory.newWSDLReader();
103             wsdlReader.setFeature("javax.wsdl.verbose", true);
104             wsdlReader.setFeature("javax.wsdl.importDocuments", true);
105             
106             if( progressDialog != null )
107             	progressDialog.setProgress( 1, "Loading wsdl.." );
108             definition = wsdlReader.readWSDL(url);
109             if( progressDialog != null )
110             	progressDialog.setProgress( 1, "Loading schema types" );
111             
112             schemaTypes = SchemaUtils.loadSchemaTypes(url);
113             return null;
114          }
115          catch (Exception e)
116          {
117             return e;
118          }
119       }
120 
121       public void finished()
122       {
123       	if( progressDialog != null )
124       		progressDialog.setVisible( false );
125       }
126    }
127 
128    public boolean hasSchemaTypes() throws Exception
129    {
130       if( !loaded )
131          load();
132       
133       return schemaTypes != null;
134    }
135 
136    public SchemaType findType(QName typeName) throws Exception
137    {
138       if( !loaded )
139          load();
140       
141       return schemaTypes == null ? null : schemaTypes.findType( typeName );
142    }
143 
144    public String getUrl()
145    {
146       return url;
147    }
148 }