Skip to main content
summaryrefslogtreecommitdiffstats
blob: fef492d93345c0e60c87c43f5a3f48dd6e656622 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*******************************************************************************
 * Copyright (c) 2007, 2010 WSO2 Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * WSO2 Inc. - initial API and implementation
 * yyyymmdd bug      Email and other contact information
 * -------- -------- -----------------------------------------------------------
 * 20070130   168762 sandakith@wso2.com - Lahiru Sandakith, Initial code to introduse the Axis2 
 * 										  runtime to the framework for 168762
 * 20070426   183046 sandakith@wso2.com - Lahiru Sandakith
 * 20070501   180284 sandakith@wso2.com - Lahiru Sandakith
 * 20070824   200515 sandakith@wso2.com - Lahiru Sandakith, NON-NLS move to seperate file
 * 20080625   210817 samindaw@wso2.com - Saminda Wijeratne, Setting the proxyBean and proxyEndPoint values - Refactoring
 * 20080924   247929 samindaw@wso2.com - Saminda Wijeratne, source folder not correctly set
 * 20090307   196954 samindaw@wso2.com - Saminda Wijeratne, Support XMLBeans data binding
 *******************************************************************************/
package org.eclipse.jst.ws.axis2.core.utils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.internal.core.JavaProject;
import org.eclipse.jst.ws.axis2.core.constant.Axis2Constants;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

public class Axis2CoreUtils {
	
	private static boolean alreadyComputedTempAxis2Directory = false;
	private static String tempAxis2Dir = null;
	
	public static void addResourcesFolderAsClassPath(IProject project){
		try {
			IJavaProject javaProject = (IJavaProject)project.getNature(JavaCore.NATURE_ID);
			IClasspathEntry[] rawClasspath = javaProject.getRawClasspath();
			List list = new LinkedList(java.util.Arrays.asList(rawClasspath));
				String resourceName = "resources";
				IFolder resourcesFolder = project.getFolder(resourceName);
				boolean isAlreadyAdded=false;
				for(IClasspathEntry cpe:rawClasspath){
					isAlreadyAdded=cpe.getPath().toOSString().equals(resourcesFolder.getFullPath().toOSString());
					if (isAlreadyAdded) break;
				}
				if (!isAlreadyAdded){
					IClasspathEntry jarEntry = JavaCore.newLibraryEntry(resourcesFolder.getFullPath(),null,null);
					list.add(jarEntry);
				}
			IClasspathEntry[] newClasspath = (IClasspathEntry[])list.toArray(new IClasspathEntry[0]);
			javaProject.setRawClasspath(newClasspath,null);
		} catch (CoreException e) {
			e.printStackTrace();
		}

	}

	public static String tempAxis2Directory() {
		if (!alreadyComputedTempAxis2Directory){
			String[] nodes = {Axis2Constants.DIR_DOT_METADATA,
					Axis2Constants.DIR_DOT_PLUGINS,
					Axis2Constants.TEMP_AXIS2_FACET_DIR};
			tempAxis2Dir =FileUtils.addNodesToPath(
					ResourcesPlugin.getWorkspace().getRoot().getLocation().toOSString(), nodes); 
			alreadyComputedTempAxis2Directory= true;
		}
		return tempAxis2Dir;
	}
	
	public static String tempAxis2WebappFileLocation() {
		return
		addAnotherNodeToPath(tempAxis2Directory(),
				Axis2Constants.WEBAPP_EXPLODED_SERVER_LOCATION_FILE);
	}
	
	
	public static String tempRuntimeStatusFileLocation() {
		return
		addAnotherNodeToPath(tempAxis2Directory(),
				Axis2Constants.SERVER_STATUS_LOCATION_FILE);
	}
	
	public static String tempWarStatusFileLocation() {
		return
		addAnotherNodeToPath(tempAxis2Directory(),
				Axis2Constants.WAR_STATUS_LOCATION_FILE);
	}
	
	public static String addAnotherNodeToPath(String currentPath, String newNode) {
		return currentPath + File.separator + newNode;
	}
	
	public static void  writePropertyToFile(File file,String key, String value) throws IOException {
		Writer out = new BufferedWriter(new OutputStreamWriter(
						new FileOutputStream(file), "8859_1"));
	       out.write(key+"="+value+"\n");
	       out.close();
	}

	public static String getServiceEndPointFromWSDL(String fileName, String serviceName){
		String proxyEndPoint=null;
		try{
			Document doc;
			doc = Axis2CoreUtils.getDocumentFromLocation(fileName);
			Element documentElement = doc.getDocumentElement();
			HashMap<String, String> portElements=new HashMap<String, String>();

			//Iterate the root element children to find service nodes
			for (int i = 0; i < documentElement.getChildNodes().getLength(); i++) {
				Node serviceElement = documentElement.getChildNodes().item(i);
				if (serviceElement.getNodeName().equals("wsdl:service")){
					if (serviceName.equalsIgnoreCase(serviceElement.getAttributes().getNamedItem("name").getNodeValue())){

						//iterate the service node children to find wsdl:port nodes
						for (int j = 0; j < serviceElement.getChildNodes().getLength(); j++) {
							Node portElement = serviceElement.getChildNodes().item(j);
							if (portElement.getNodeName().equals("wsdl:port")){
								String portBinding=portElement.getAttributes().getNamedItem("binding").getNodeValue().toUpperCase();
	
								//iterate the port node children to find the soap address node
								for (int k = 0; k < portElement.getChildNodes().getLength(); k++) {
									Node soapElement = portElement.getChildNodes().item(k);
									if (!soapElement.getNodeName().equals("#text")){
										String soapLocation=soapElement.getAttributes().getNamedItem("location").getNodeValue();
										while (portElements.containsKey(portBinding))
											portBinding="K"+portBinding;
										portElements.put(portBinding, soapLocation);
									}
								}
							}
						}
					}
				}
			}
			String portBindType="";
			String soap11B="SOAP11Binding".toUpperCase();
			String soap12B="SOAP12Binding".toUpperCase();
			String httpB="HttpBinding".toUpperCase();
			String https="https";

			//iterating through all found end points to determine the required endpoint in the order soap11, soap12, http
			for (String string : portElements.keySet()) {
				if (proxyEndPoint==null){
					proxyEndPoint=portElements.get(string);
					portBindType=string;
				}
				if (string.endsWith(soap11B)){
					if (proxyEndPoint.startsWith(https)||(!portBindType.endsWith(soap11B))){
						proxyEndPoint=portElements.get(string);
						portBindType=string;
					}
				}
				if ((!portBindType.endsWith(soap11B))&&(string.endsWith(soap12B))){
					if (proxyEndPoint.startsWith(https)||(!portBindType.endsWith(soap12B))){
						proxyEndPoint=portElements.get(string);
						portBindType=string;
					}
				}
				if (!((portBindType.endsWith(soap11B))||(portBindType.endsWith(soap12B)))&&(string.endsWith(httpB))){
					if (proxyEndPoint.startsWith(https)||(!portBindType.endsWith(httpB))){
						proxyEndPoint=portElements.get(string);
						portBindType=string;
					}
				}
			}
		}catch(Exception e){}
		return proxyEndPoint;
	}

	public static String getServiceNameFromWSDL(String fileName){
		String serviceName="";
		try{
			Document doc;
			doc = Axis2CoreUtils.getDocumentFromLocation(fileName);
			Element documentElement = doc.getDocumentElement();

			//Iterate the root element children to find services
			for (int i = 0; i < documentElement.getChildNodes().getLength(); i++) {
				Node serviceElement = documentElement.getChildNodes().item(i);
				if (serviceElement.getNodeName().equals("wsdl:service")){
					serviceName=serviceElement.getAttributes().getNamedItem("name").getNodeValue();
				}
			}
		}catch(Exception e){}
		return serviceName;
	} 

	private static Document getDocumentFromLocation(String location){
		Document doc=null;
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		dbf.setIgnoringElementContentWhitespace(true);
		dbf.setNamespaceAware(false);
		DocumentBuilder db;
		try {
			db = dbf.newDocumentBuilder();
			if (location.toUpperCase().startsWith("HTTP:")){
				URL url = new URL(location);
				doc=db.parse(url.openStream());
			}else{
				if (location.toUpperCase().startsWith("FILE:")) location=location.substring(5,location.length());
				doc=db.parse(new File(location));
			}		
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			//e.printStackTrace();
		}
		return doc;
	}
	
	
	public static String getSourceFolder(String projectName){
		String src=Axis2Constants.DIR_SRC;
		try {
			String[] javaProjectSourceDirectories = getJavaProjectSourceDirectories(projectName);
			src=javaProjectSourceDirectories[0];
		} catch (JavaModelException e) {
		} catch (CoreException e) {
		}
		return src;
	}
	
	public static String[] getJavaProjectSourceDirectories (String projectName) throws CoreException, JavaModelException{
	    ArrayList paths = new ArrayList();
	    
	    IProject project =ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
	    if (project.isOpen() && JavaProject.hasJavaNature(project)) {
	      IJavaProject javaProject = JavaCore.create(project);
	    
	      IClasspathEntry[] classpathEntries = null;
	      classpathEntries = javaProject.getResolvedClasspath(true);
	      
	      for (int i = 0; i<classpathEntries.length;i++){         
	      	IClasspathEntry entry = classpathEntries[i];
	        if (entry.getContentKind() == IPackageFragmentRoot.K_SOURCE)
	        {
	          IPath path = entry.getPath();
	          String srcPath = path.segments()[path.segmentCount()-1];
	          paths.add(srcPath);
	        }
	      }
	    }
	    return (String[])paths.toArray(new String[0]);
	  } 
}

Back to the top