summaryrefslogtreecommitdiffstats
path: root/sandbox/mobile-android/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDeclaration.java
blob: ff7d215d2ed1f1b9be3ff2894840e6cdb813db4b (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
/*
 * 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.extensibility;

import java.lang.ref.WeakReference;
import java.net.URL;
import java.util.Map;

/**
 * Service declaration using J2SE Jar service provider spec Classes specified
 * inside this declaration are loaded using the ClassLoader used to read the
 * configuration file corresponding to this declaration.
 */
public class ServiceDeclaration {

    private WeakReference<ClassLoader> classLoader;

    private String className;

    private Map<String, String> attributes;

    /**
     * Service declaration constructor
     * 
     * @param className Service implementation class name
     * @param classLoader ClassLoader corresponding to this service
     *                implementation
     * @param attributes Optional attributes for this service declaration
     */
    public ServiceDeclaration(String className, ClassLoader classLoader, Map<String, String> attributes) {

        this.className = className;
        this.classLoader = new WeakReference<ClassLoader>(classLoader);
        this.attributes = attributes;
    }

    /**
     * Load this service implementation class
     * 
     * @return Class
     * @throws ClassNotFoundException
     */
    @SuppressWarnings("unchecked")
    public Class<?> loadClass() throws ClassNotFoundException {

        return Class.forName(className, true, classLoader.get());
    }

    /**
     * Load another class using the ClassLoader of this service implementation
     * 
     * @param anotherClassName
     * @return Class
     * @throws ClassNotFoundException
     */
    public Class<?> loadClass(String anotherClassName) throws ClassNotFoundException {

        return Class.forName(anotherClassName, true, classLoader.get());
    }

    /**
     * Return the resource corresponding to this service implementation class
     * 
     * @return resource URL
     */
    public URL getResource() {
        return classLoader.get().getResource(className);
    }

    /**
     * ClassLoader associated with this service declaration
     * 
     * @return ClassLoader
     */
    public ClassLoader getClassLoader() {
        return classLoader.get();
    }

    /**
     * Service implementation class corresponding to this declaration
     * 
     * @return The Service implementation class corresponding to this declaration 
     */
    public String getClassName() {
        return className;
    }

    /**
     * Attributes specified for this declaration
     * 
     * @return attributes
     */
    public Map<String, String> getAttributes() {
        return attributes;
    }

    /**
     * Equals method used to ensure that each service declaration is stored only
     * once in a set of declarations.
     */
    @Override
    public boolean equals(Object o) {
        if (!(o instanceof ServiceDeclaration))
            return false;
        ServiceDeclaration s = (ServiceDeclaration)o;
        if (!className.equals(s.className))
            return false;
        else if (!classLoader.equals(s.classLoader))
            return false;
        else if (attributes == null)
            return s.attributes == null;
        else
            return attributes.equals(s.attributes);

    }

}