KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > iiop > rmi > InterfaceAnalysis


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.iiop.rmi;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.HashMap JavaDoc;
27
28
29 /**
30  * Interface analysis.
31  *
32  * Routines here are conforming to the "Java(TM) Language to IDL Mapping
33  * Specification", version 1.1 (01-06-07).
34  *
35  * @author <a HREF="mailto:osh@sparre.dk">Ole Husgaard</a>
36  * @version $Revision: 37459 $
37  */

38 public class InterfaceAnalysis
39    extends ContainerAnalysis
40 {
41    // Constants -----------------------------------------------------
42

43    // Attributes ----------------------------------------------------
44

45    /**
46     * Map of IDL operation names to operation analyses.
47     */

48    Map JavaDoc operationAnalysisMap;
49
50    // Static --------------------------------------------------------
51

52    private static final org.jboss.logging.Logger logger =
53                org.jboss.logging.Logger.getLogger(InterfaceAnalysis.class);
54
55    private static WorkCacheManager cache
56                                = new WorkCacheManager(InterfaceAnalysis.class);
57
58    public static InterfaceAnalysis getInterfaceAnalysis(Class JavaDoc cls)
59       throws RMIIIOPViolationException
60    {
61       return (InterfaceAnalysis)cache.getAnalysis(cls);
62    }
63
64    // Constructors --------------------------------------------------
65

66    protected InterfaceAnalysis(Class JavaDoc cls)
67    {
68       super(cls);
69       logger.debug("new InterfaceAnalysis: " + cls.getName());
70    }
71
72    protected void doAnalyze()
73       throws RMIIIOPViolationException
74    {
75       super.doAnalyze();
76
77       calculateOperationAnalysisMap();
78       fixupCaseNames();
79    }
80
81    // Public --------------------------------------------------------
82

83    public boolean isAbstractInterface()
84    {
85       return abstractInterface;
86    }
87
88    public boolean isRmiIdlRemoteInterface()
89    {
90       return (!abstractInterface);
91    }
92
93    public String JavaDoc[] getAllTypeIds()
94    {
95       if (allTypeIds == null)
96          logger.debug(cls + " null allTypeIds");
97       return (String JavaDoc[])allTypeIds.clone();
98    }
99
100    // Protected -----------------------------------------------------
101

102    /**
103     * Return a list of all the entries contained here.
104     *
105     * @param entries The list of entries contained here. Entries in this list
106     * are subclasses of <code>AbstractAnalysis</code>.
107     */

108    protected ArrayList JavaDoc getContainedEntries()
109    {
110       ArrayList JavaDoc ret = new ArrayList JavaDoc(constants.length +
111                                     attributes.length +
112                                     operations.length);
113
114       for (int i = 0; i < constants.length; ++i)
115          ret.add(constants[i]);
116       for (int i = 0; i < attributes.length; ++i)
117          ret.add(attributes[i]);
118       for (int i = 0; i < operations.length; ++i)
119          ret.add(operations[i]);
120
121       return ret;
122    }
123
124    /**
125     * Analyse operations.
126     * This will fill in the <code>operations</code> array.
127     */

128    protected void analyzeOperations()
129       throws RMIIIOPViolationException
130    {
131       logger.debug(cls + " analyzeOperations");
132
133       if (!cls.isInterface())
134          throw new IllegalArgumentException JavaDoc("Class \"" + cls.getName() +
135                                             "\" is not an interface.");
136
137       abstractInterface = RmiIdlUtil.isAbstractInterface(cls);
138       calculateAllTypeIds();
139
140       int operationCount = 0;
141       for (int i = 0; i < methods.length; ++i)
142          if ((m_flags[i] & (M_READ|M_WRITE|M_READONLY)) == 0)
143             ++operationCount;
144       operations = new OperationAnalysis[operationCount];
145       operationCount = 0;
146       for (int i = 0; i < methods.length; ++i) {
147          if ((m_flags[i] & (M_READ|M_WRITE|M_READONLY)) == 0) {
148             operations[operationCount] = new OperationAnalysis(methods[i]);
149             ++operationCount;
150          }
151       }
152
153       logger.debug(cls + " analyzeOperations operations=" + operations.length);
154    }
155
156    /**
157     * Calculate the map that maps IDL operation names to operation analyses.
158     * Besides mapped operations, this map also contains the attribute
159     * accessor and mutator operations.
160     */

161    protected void calculateOperationAnalysisMap()
162    {
163       operationAnalysisMap = new HashMap JavaDoc();
164       OperationAnalysis oa;
165
166       // Map the operations
167
for (int i = 0; i < operations.length; ++i) {
168          oa = operations[i];
169          operationAnalysisMap.put(oa.getIDLName(), oa);
170       }
171
172       // Map the attributes
173
for (int i = 0; i < attributes.length; ++i) {
174          AttributeAnalysis attr = attributes[i];
175
176          oa = attr.getAccessorAnalysis();
177
178          // Not having an accessor analysis means that
179
// the attribute is not in a remote interface
180
if (oa != null) {
181             operationAnalysisMap.put(oa.getIDLName(), oa);
182
183             oa = attr.getMutatorAnalysis();
184             if (oa != null)
185                operationAnalysisMap.put(oa.getIDLName(), oa);
186          }
187       }
188    }
189
190    /**
191     * Calculate the array containing all type ids of this interface,
192     * in the format that org.omg.CORBA.portable.Servant._all_interfaces()
193     * is expected to return.
194     */

195    protected void calculateAllTypeIds()
196    {
197       if (!isRmiIdlRemoteInterface()) {
198          allTypeIds = new String JavaDoc[0];
199       }
200       else {
201          ArrayList JavaDoc a = new ArrayList JavaDoc();
202          InterfaceAnalysis[] intfs = getInterfaces();
203          for (int i = 0; i < intfs.length; ++i) {
204             String JavaDoc[] ss = intfs[i].getAllTypeIds();
205
206             for (int j = 0; j < ss.length; ++j)
207                if (!a.contains(ss[j]))
208                   a.add(ss[j]);
209          }
210          allTypeIds = new String JavaDoc[a.size() + 1];
211          allTypeIds[0] = getRepositoryId();
212          for (int i = 1; i <= a.size(); ++i)
213             allTypeIds[i] = (String JavaDoc)a.get(a.size()-i);
214       }
215    }
216
217    // Private -------------------------------------------------------
218

219    private boolean abstractInterface;
220
221    private String JavaDoc[] allTypeIds;
222 }
223
224
Popular Tags