summaryrefslogtreecommitdiffstats
path: root/sandbox/axis2-1.4/modules/node-api/src/main/java/org/apache/tuscany/sca/node/util/SCAContributionUtil.java
blob: b005232370dcfd5b2c06db16b03bb6efc5cb3a2a (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
/*
 * 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.node.util;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;


/**
 * Utility methods to find an SCA contribution from a class or a resource.
 * 
 * @version $Rev$ $Date$
 */
public class SCAContributionUtil {
    
    /**
     * Given a class this method finds the contribution that it belongs to.
     * This could be either a local directory of a jar file.
     * 
     * @param clazz
     * @return the contribution URL
     * @throws MalformedURLException
     */  
    public static URL findContributionFromClass(Class<?> clazz) throws MalformedURLException {
        return findContributionFromResource(clazz.getClassLoader(), clazz.getName().replace('.', '/') + ".class");
    }
              
    /**
     * Given the path to a resource this method finds the contribution that it belongs to
     * this could be either a local directory of a jar file.
     * 
     * @param classLoader
     * @param compositeString
     * @return the contribution URL
     * @throws MalformedURLException
     */  
    public static URL findContributionFromResource(ClassLoader classLoader, String compositeString) throws MalformedURLException {
    	   	
        URL contributionURL = classLoader.getResource(compositeString);
        
        if ( contributionURL != null ){ 
            String contributionURLString = contributionURL.toExternalForm();
            String protocol = contributionURL.getProtocol();
            
            if ("file".equals(protocol)) {
                // directory contribution
                if (contributionURLString.endsWith(compositeString)) {
                    String location = contributionURLString.substring(0, contributionURLString.lastIndexOf(compositeString));
                    // workaround from evil URL/URI form Maven
                    contributionURL = toFile(new URL(location)).toURI().toURL();
                }
    
            } else if ("jar".equals(protocol)) {
                // jar contribution
                String location = contributionURLString.substring(4, contributionURLString.lastIndexOf("!/"));
                // workaround for evil URL/URI from Maven
                contributionURL = toFile(new URL(location)).toURI().toURL();
            }
        } 
        
    	return contributionURL;
    } 

    /**
     * Convert from a <code>URL</code> to a <code>File</code>.
     * <p>
     * From version 1.1 this method will decode the URL. Syntax such as
     * <code>file:///my%20docs/file.txt</code> will be correctly decoded to
     * <code>/my docs/file.txt</code>.
     * 
     * @param url the file URL to convert, null returns null
     * @return the equivalent <code>File</code> object, or <code>null</code>
     *         if the URL's protocol is not <code>file</code>
     * @throws IllegalArgumentException if the file is incorrectly encoded
     */
    private static File toFile(URL url) {
        if (url == null || !url.getProtocol().equals("file")) {
            return null;
        } else {
            String filename = url.getFile().replace('/', File.separatorChar);
            int pos = 0;
            while ((pos = filename.indexOf('%', pos)) >= 0) { // NOPMD
                if (pos + 2 < filename.length()) {
                    String hexStr = filename.substring(pos + 1, pos + 3);
                    char ch = (char)Integer.parseInt(hexStr, 16);
                    filename = filename.substring(0, pos) + ch + filename.substring(pos + 3);
                }
            }
            return new File(filename);
        }
    }

}