summaryrefslogtreecommitdiffstats
path: root/java/sca/modules/binding-ws-axis2/src/main/java/org/apache/tuscany/sca/binding/ws/axis2/TuscanyAxisConfigurator.java
blob: c8050912d6145055e5f9d78026957e03129a23b4 (plain)
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.    
 */
package org.apache.tuscany.sca.binding.ws.axis2;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.apache.axis2.AxisFault;
import org.apache.axis2.Constants;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.context.ConfigurationContextFactory;
import org.apache.axis2.deployment.DeploymentConstants;
import org.apache.axis2.deployment.DeploymentErrorMsgs;
import org.apache.axis2.deployment.DeploymentException;
import org.apache.axis2.deployment.ModuleBuilder;
import org.apache.axis2.deployment.URLBasedAxisConfigurator;
import org.apache.axis2.description.AxisModule;
import org.apache.axis2.description.Parameter;
import org.apache.axis2.engine.AxisConfiguration;
import org.apache.axis2.engine.AxisConfigurator;
import org.apache.axis2.i18n.Messages;
import org.apache.tuscany.sca.extensibility.ServiceDiscovery;
import org.oasisopen.sca.ServiceRuntimeException;

/**
 * Helps configure Axis2 from a resource in binding.ws.axis2 instead of Axis2.xml 
 * <p/> TODO: Review: should there be a single global Axis ConfigurationContext
 *
 * @version $Rev$ $Date$
 */
public class TuscanyAxisConfigurator extends URLBasedAxisConfigurator implements AxisConfigurator {
    
    /* these two fields are part of a temporary fix to solve problems that Maven has with including
     * rampart-1.4.mar into the classpath and also at the time of Release 1.0 rampart-1.4.mar seems
     * to pull in a SNAPSHOT version of rampart-project pom.  Hence rampart.mar has been excluded
     * as a Maven dependency and has been packed with this module 
     */
    /************start of fix *********************************************************************/
    private URL axis2_xml = 
        getResource("/org/apache/tuscany/sca/binding/ws/axis2/engine/config/axis2.xml");
    private URL axis2_repository = null;
    private URL rampart_mar_url =
        getResource("/org/apache/tuscany/sca/binding/ws/axis2/engine/config/modules/rampart-1.4.mar");
    /************** end of fix *************************************************************/
    
    private boolean isRampartRequired;
    
    public TuscanyAxisConfigurator(boolean isRampartRequired) throws AxisFault {
        //super(TuscanyAxisConfigurator.class.getResource("/org/apache/tuscany/sca/binding/ws/axis2/engine/config/axis2.xml"), 
        //      TuscanyAxisConfigurator.class.getResource("/org/apache/tuscany/sca/binding/ws/axis2/engine/config/modules/rampart.mar"));
        super(getResource("/org/apache/tuscany/sca/binding/ws/axis2/engine/config/axis2.xml"), 
                    null);
        this.isRampartRequired = isRampartRequired;
    }
    
    private static URL getResource(final String name) {
        return AccessController.doPrivileged(new PrivilegedAction<URL>() {
            public URL run() {
                return TuscanyAxisConfigurator.class.getResource(name);
            }
        });
    }

    public ConfigurationContext getConfigurationContext() throws AxisFault {
        if (configContext == null) {
            configContext = ConfigurationContextFactory.createConfigurationContext(this);
        }
        return configContext;
    }
    
    private InputStream getResourceAsStream(final String resource) {
        return AccessController.doPrivileged(new PrivilegedAction<InputStream>() {
            public InputStream run() {
                ClassLoader cl = Thread.currentThread().getContextClassLoader();
                return cl.getResourceAsStream(resource);
            }
        });
    }
    
    /* these three methods are part of a temporary fix to solve problems that Maven has with including
     * rampart-1.3.mar into the classpath and also at the time of Release 1.0 rampart-1.3.mar seems
     * to pull in a SNAPSHOT version of rampart-project pom.  Hence rampart.mar has been excluded
     * as a Maven dependency and has been packed with this module 
     */
    /************start of fix *********************************************************************/
    @Override
    public AxisConfiguration getAxisConfiguration() throws AxisFault {
        InputStream axis2xmlStream;
        try {
            if (axis2_xml == null) {
                axis2xmlStream =
                        getResourceAsStream(DeploymentConstants.AXIS2_CONFIGURATION_RESOURCE);
            } else {
                axis2xmlStream = axis2_xml.openStream();
            }
            axisConfig = populateAxisConfiguration(axis2xmlStream);
            if (isRampartRequired) {
                axisConfig.addGlobalModuleRef("rampart");
            }   
            if (axis2_repository == null) {
                Parameter axis2repoPara = axisConfig.getParameter(DeploymentConstants.AXIS2_REPO);
                if (axis2repoPara != null) {
                    String repoValue = (String) axis2repoPara.getValue();
                    if (repoValue != null && !"".equals(repoValue.trim())) {
                        if (repoValue.startsWith("file:/")) {
                            // we treat this case specially , by assuming file is
                            // located in the local machine
                            loadRepository(repoValue);
                        } else {
                            loadRepositoryFromURL(new URL(repoValue));
                        }
                    }
                } else {
                    //log.info("No repository found , module will be loaded from classpath");
                    try {
                        loadFromClassPath(); 
                    } catch ( Exception e ) {
                        if (isRampartRequired) {
                            loadRampartModule();
                        }
                    }
                }
                
            } else {
                loadRepositoryFromURL(axis2_repository);
            }

        } catch (IOException e) {
            throw new AxisFault(e.getMessage());
        }
        axisConfig.setConfigurator(this);
        return axisConfig;
    }
    
    public void loadRampartModule() throws DeploymentException {
        try {
            ClassLoader deploymentClassLoader =
                    org.apache.axis2.deployment.util.Utils.createClassLoader(
                            new URL[]{rampart_mar_url},
                            axisConfig.getModuleClassLoader(),
                            true,
                            (File) axisConfig.getParameterValue(Constants.Configuration.ARTIFACTS_TEMP_DIR));
            final AxisModule module = new AxisModule();
            module.setModuleClassLoader(deploymentClassLoader);
            module.setParent(axisConfig);
            //String moduleFile = fileUrl.substring(0, fileUrl.indexOf(".mar"));
            if (module.getName() == null) {
                module.setName("rampart");
                module.setVersion("1.4");
            }
            populateModule(module, rampart_mar_url);
            module.setFileName(rampart_mar_url);
            // Allow privileged access to read properties. Requires PropertiesPermission read in
            // security policy.
            try {
                AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
                    public Object run() throws IOException {
                        addNewModule(module, axisConfig);
                        return null;
                    }
                });
            } catch (PrivilegedActionException e) {
                throw (AxisFault)e.getException();
            }            
           
            calculateDefaultModuleVersion(axisConfig.getModules(), axisConfig);
            axisConfig.validateSystemPredefinedPhases();
        } catch (IOException e) {
            throw new DeploymentException(e);
        }
    }
    /**
     * Get the name of the module , where archive name is combination of module name + its version
     * The format of the name is as follows:
     * moduleName-00.0000
     * Example: "addressing-01.0001.mar" would return "addressing"
     *
     * @param moduleName the name of the module archive
     * @return the module name parsed out of the file name
     */
    public static String getModuleName(String moduleName) {
        if (moduleName.endsWith("-SNAPSHOT")) {
            return moduleName.substring(0, moduleName.indexOf("-SNAPSHOT"));
        }
        char delimiter = '-';
        int version_index = moduleName.lastIndexOf(delimiter);
        if (version_index > 0) {
            String versionString = getModuleVersion(moduleName);
            if (versionString == null) {
                return moduleName;
            } else {
                return moduleName.substring(0, version_index);
            }
        } else {
            return moduleName;
        }
    }

    public static String getModuleVersion(String moduleName) {
        if (moduleName.endsWith("-SNAPSHOT")) {
            return "SNAPSHOT";
        }
        char version_seperator = '-';
        int version_index = moduleName.lastIndexOf(version_seperator);
        if (version_index > 0) {
            String versionString = moduleName.substring(version_index + 1, moduleName.length());
            try {
                Float.parseFloat(versionString);
                return versionString;
            } catch (NumberFormatException e) {
                return null;
            }
        } else {
            return null;
        }
    }

    public static String getModuleName(String moduleName, String moduleVersion) {
        if (moduleVersion != null && moduleVersion.length() != 0) {
            moduleName = moduleName + "-" + moduleVersion;
        } 
        return moduleName;
    }

    public static boolean isLatest(String moduleVersion, String currentDefaultVersion) {
        if (AxisModule.VERSION_SNAPSHOT.equals(moduleVersion)) {
            return true;
        } else {
            float m_version = Float.parseFloat(moduleVersion);
            float m_c_vresion = Float.parseFloat(currentDefaultVersion);
            return m_version > m_c_vresion;
        }
    }

    public static void calculateDefaultModuleVersion(HashMap modules,
                                                     AxisConfiguration axisConfig) {
        Iterator allModules = modules.values().iterator();
        HashMap defaultModules = new HashMap();
        while (allModules.hasNext()) {
            AxisModule axisModule = (AxisModule) allModules.next();
            String moduleName = axisModule.getName();
            String moduleNameString;
            String moduleVersionString;
            if (AxisModule.VERSION_SNAPSHOT.equals(axisModule.getVersion())) {
                moduleNameString = axisModule.getName();
                moduleVersionString = axisModule.getVersion();
            } else {
                if (axisModule.getVersion() == null) {
                    moduleNameString = getModuleName(moduleName);
                    moduleVersionString = getModuleVersion(moduleName);
                    if (moduleVersionString != null) {
                        try {
                            Float.valueOf(moduleVersionString);
                            axisModule.setVersion(moduleVersionString);
                            axisModule.setName(moduleName);
                        } catch (NumberFormatException e) {
                            moduleVersionString = null;
                        }
                    }
                } else {
                    moduleNameString = axisModule.getName();
                    moduleVersionString = axisModule.getVersion();
                }
            }
            String currentDefaultVerison = (String) defaultModules.get(moduleNameString);
            if (currentDefaultVerison != null) {
                // if the module version is null then , that will be ignore in this case
                if (!AxisModule.VERSION_SNAPSHOT.equals(currentDefaultVerison)) {
                    if (moduleVersionString != null &&
                        isLatest(moduleVersionString, currentDefaultVerison)) {
                        defaultModules.put(moduleNameString, moduleVersionString);
                    }
                }
            } else {
                defaultModules.put(moduleNameString, moduleVersionString);
            }

        }
        Iterator def_mod_itr = defaultModules.keySet().iterator();
        while (def_mod_itr.hasNext()) {
            String moduleName = (String) def_mod_itr.next();
            axisConfig.addDefaultModuleVersion(moduleName, (String) defaultModules.get(moduleName));
        }
    }

    
    private void populateModule(AxisModule module, URL moduleUrl) throws DeploymentException {
        try {
            ClassLoader classLoader = module.getModuleClassLoader();
            InputStream moduleStream = classLoader.getResourceAsStream("META-INF/module.xml");
            if (moduleStream == null) {
                moduleStream = classLoader.getResourceAsStream("meta-inf/module.xml");
            }
            if (moduleStream == null) {
                throw new DeploymentException(
                        Messages.getMessage(
                                DeploymentErrorMsgs.MODULE_XML_MISSING, moduleUrl.toString()));
            }
            ModuleBuilder moduleBuilder = new ModuleBuilder(moduleStream, module, axisConfig);
            moduleBuilder.populateModule();
        } catch (IOException e) {
            throw new DeploymentException(e);
        }
    }

}