KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > iiop > IiopSkeleton


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.iiop;
31
32 import com.caucho.log.Log;
33
34 import org.omg.CORBA.NO_IMPLEMENT JavaDoc;
35
36 import java.lang.reflect.Method JavaDoc;
37 import java.util.HashMap JavaDoc;
38 import java.util.logging.Level JavaDoc;
39 import java.util.logging.Logger JavaDoc;
40
41 public class IiopSkeleton extends DummyObjectImpl {
42   private static final Logger JavaDoc log = Log.open(IiopSkeleton.class);
43   
44   private static HashMap JavaDoc<String JavaDoc,String JavaDoc> _knownClasses;
45
46   private ClassLoader JavaDoc _loader;
47   private Class JavaDoc _remoteClass;
48
49   private Object JavaDoc _obj;
50
51   IiopSkeleton(Object JavaDoc obj, Class JavaDoc apiClass, ClassLoader JavaDoc loader,
52            String JavaDoc host, int port, String JavaDoc oid)
53   {
54     super(new IOR(apiClass, host, port, oid));
55     
56     if (obj == null)
57       throw new NullPointerException JavaDoc();
58     
59     _obj = obj;
60     _remoteClass = apiClass;
61     _loader = loader;
62   }
63
64   Object JavaDoc getObject()
65   {
66     return _obj;
67   }
68
69   Class JavaDoc getRemoteClass()
70   {
71     return _remoteClass;
72   }
73   
74   void service(Object JavaDoc obj, IiopReader reader, IiopWriter writer)
75     throws Throwable JavaDoc
76   {
77     String JavaDoc op = reader.getOperation().toString();
78
79     Method JavaDoc method = null;
80     SkeletonMethod skelMethod = null;
81
82     if (log.isLoggable(Level.FINE))
83       log.fine("IIOP-call: " + _remoteClass.getName() + "." + op);
84
85     if ((method = getMethod(op)) != null) {
86       boolean isJava = !_remoteClass.getName().startsWith("com.caucho.iiop");
87       
88       skelMethod = new SkeletonMethod(this, method, isJava);
89       Thread JavaDoc thread = Thread.currentThread();
90       ClassLoader JavaDoc oldLoader = thread.getContextClassLoader();
91       try {
92     thread.setContextClassLoader(_loader);
93     
94     skelMethod.service(obj, reader, writer);
95       } finally {
96     thread.setContextClassLoader(oldLoader);
97       }
98     }
99     else if (serviceSystemMethod(obj, op, reader, writer)) {
100     }
101     else {
102       throw new NO_IMPLEMENT JavaDoc("no such method: " + op + " for " + _remoteClass.getName());
103     }
104   }
105
106   Method JavaDoc getMethod(String JavaDoc name)
107   {
108     Method JavaDoc []methods = _remoteClass.getMethods();
109     for (int i = 0; i < methods.length; i++)
110       if (methods[i].getName().equals(name))
111         return methods[i];
112
113     return null;
114   }
115
116   boolean serviceSystemMethod(Object JavaDoc obj, String JavaDoc op, IiopReader reader, IiopWriter writer)
117     throws Exception JavaDoc
118   {
119     if (op.equals("_is_a")) {
120       String JavaDoc name = reader.read_string();
121
122       String JavaDoc className = _knownClasses.get(name);
123
124       if (className != null) {
125       }
126       else if (name.startsWith("RMI:")) {
127         className = name.substring(4);
128         int p = className.indexOf(':');
129         if (p > 0)
130           className = className.substring(0, p);
131       }
132       else
133         className = name;
134
135       Class JavaDoc cl = obj.getClass();
136
137       boolean value = isA(cl, className);
138
139       if (log.isLoggable(Level.FINE))
140     log.fine("IIOP _is_a: " + obj.getClass() + " " + className + " " + value);
141
142       writer.startReplyOk(reader.getRequestId());
143       writer.write_boolean(value);
144       
145       return true;
146     }
147     else
148       return false;
149   }
150
151   private boolean isA(Class JavaDoc cl, String JavaDoc className)
152   {
153     for (; cl != null; cl = cl.getSuperclass()) {
154       if (cl.getName().equals(className))
155     return true;
156
157       Class JavaDoc []ifs = cl.getInterfaces();
158       for (int i = 0; i < ifs.length; i++) {
159     if (isA(ifs[i], className))
160       return true;
161       }
162     }
163
164     return false;
165   }
166
167   public String JavaDoc toString()
168   {
169     return "IiopSkeleton[" + _remoteClass.getName() + "]";
170   }
171
172   static {
173     _knownClasses = new HashMap JavaDoc<String JavaDoc,String JavaDoc>();
174     _knownClasses.put("IDL:omg.org/CosNaming/NamingContext:1.0",
175                      "org.omg.CosNaming.NamingContext");
176   }
177 }
178
Popular Tags