summaryrefslogtreecommitdiffstats
path: root/sca-java-1.x/branches/sca-java-0.91/modules/binding-ejb/src/main/java/org/apache/tuscany/sca/binding/ejb/java2idl/InterfaceType.java
blob: 6943eb7a6fb4c990b0388d161659a230e57d0877 (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
/*
 * 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.ejb.java2idl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * IDL Interface Routines here are conforming to the "Java(TM) Language to IDL
 * Mapping Specification", version 1.1 (01-06-07).
 */
public class InterfaceType extends ContainerType {

    private boolean abstractInterface;
    private String[] typeIDs;

    /**
     * Map of IDL operation names to operation parses.
     */
    private Map operationTypeMap;

    private static WorkCache cache = new WorkCache(InterfaceType.class);

    public static InterfaceType getInterfaceType(Class cls) {
        return (InterfaceType)cache.getType(cls);
    }

    protected InterfaceType(Class cls) {
        super(cls);
    }

    protected void parse() {
        super.parse();
        if (!javaClass.isInterface())
            throw new IllegalArgumentException("Class [" + javaClass.getName() + "] is not an interface.");
        abstractInterface = Java2IDLUtil.isAbstractInterface(javaClass);
        calculateOperationTypeMap();
        calculateAllTypeIds();
        fixupCaseNames();
    }

    public boolean isAbstractInterface() {
        return abstractInterface;
    }

    private boolean isRemoteInterface() {
        return (!abstractInterface);
    }

    public String[] getTypeIDs() {
        return (String[])typeIDs.clone();
    }

    /**
     * Return a list of all the entries contained here.
     * 
     * @param entries The list of entries contained here. Entries in this list
     *            are subclasses of <code>AbstractType</code>.
     */
    protected ArrayList getContainedEntries() {
        ArrayList ret = new ArrayList(constants.length + attributes.length + operations.length);
        for (int i = 0; i < constants.length; ++i)
            ret.add(constants[i]);
        for (int i = 0; i < attributes.length; ++i)
            ret.add(attributes[i]);
        for (int i = 0; i < operations.length; ++i)
            ret.add(operations[i]);
        return ret;
    }

    /**
     * Analyse operations. This will fill in the <code>operations</code>
     * array.
     */
    protected void parseOperations() {
        int operationCount = 0;
        for (int i = 0; i < methods.length; ++i)
            if ((m_flags[i] & (M_READ | M_WRITE | M_READONLY)) == 0)
                ++operationCount;
        operations = new OperationType[operationCount];
        operationCount = 0;
        for (int i = 0; i < methods.length; ++i) {
            if ((m_flags[i] & (M_READ | M_WRITE | M_READONLY)) == 0) {
                operations[operationCount] = new OperationType(methods[i]);
                ++operationCount;
            }
        }
    }

    /**
     * Calculate the map that maps IDL operation names to operation parses.
     * Besides mapped operations, this map also contains the attribute accessor
     * and mutator operations.
     */
    protected void calculateOperationTypeMap() {
        operationTypeMap = new HashMap();
        OperationType oa;
        // Map the operations
        for (int i = 0; i < operations.length; ++i) {
            oa = operations[i];
            operationTypeMap.put(oa.getIDLName(), oa);
        }
        // Map the attributes
        for (int i = 0; i < attributes.length; ++i) {
            AttributeType attr = attributes[i];
            oa = attr.getReadOperationType();
            // Not having an accessor analysis means that
            // the attribute is not in a remote interface
            if (oa != null) {
                operationTypeMap.put(oa.getIDLName(), oa);
                oa = attr.getWriteOperationType();
                if (oa != null)
                    operationTypeMap.put(oa.getIDLName(), oa);
            }
        }
    }

    /**
     * Calculate the array containing all type ids of this interface, in the
     * format that org.omg.CORBA.portable.Servant._all_interfaces() is expected
     * to return.
     */
    protected void calculateAllTypeIds() {
        if (!isRemoteInterface()) {
            typeIDs = new String[0];
        } else {
            ArrayList a = new ArrayList();
            InterfaceType[] intfs = getInterfaces();
            for (int i = 0; i < intfs.length; ++i) {
                String[] ss = intfs[i].getTypeIDs();
                for (int j = 0; j < ss.length; ++j)
                    if (!a.contains(ss[j]))
                        a.add(ss[j]);
            }
            typeIDs = new String[a.size() + 1];
            typeIDs[0] = getRepositoryId();
            for (int i = 1; i <= a.size(); ++i)
                typeIDs[i] = (String)a.get(a.size() - i);
        }
    }
}