Back to index

im-sdk  12.3.91
AuxProxy.java
Go to the documentation of this file.
00001 /*
00002 Copyright 1990-2001 Sun Microsystems, Inc. All Rights Reserved.
00003 
00004 Permission is hereby granted, free of charge, to any person obtaining a
00005 copy of this software and associated documentation files (the
00006 "Software"), to deal in the Software without restriction, including
00007 without limitation the rights to use, copy, modify, merge, publish,
00008 distribute, sublicense, and/or sell copies of the Software, and to
00009 permit persons to whom the Software is furnished to do so, subject to
00010 the following conditions: The above copyright notice and this
00011 permission notice shall be included in all copies or substantial
00012 portions of the Software.
00013 
00014 
00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00016 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00017 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00018 IN NO EVENT SHALL THE OPEN GROUP OR SUN MICROSYSTEMS, INC. BE LIABLE
00019 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
00020 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
00021 THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE EVEN IF
00022 ADVISED IN ADVANCE OF THE POSSIBILITY OF SUCH DAMAGES.
00023 
00024 
00025 Except as contained in this notice, the names of The Open Group and/or
00026 Sun Microsystems, Inc. shall not be used in advertising or otherwise to
00027 promote the sale, use or other dealings in this Software without prior
00028 written authorization from The Open Group and/or Sun Microsystems,
00029 Inc., as applicable.
00030 
00031 
00032 X Window System is a trademark of The Open Group
00033 
00034 OSF/1, OSF/Motif and Motif are registered trademarks, and OSF, the OSF
00035 logo, LBX, X Window System, and Xinerama are trademarks of the Open
00036 Group. All other trademarks and registered trademarks mentioned herein
00037 are the property of their respective owners. No right, title or
00038 interest in or to any trademark, service mark, logo or trade name of
00039 Sun Microsystems, Inc. or its licensors is granted.
00040 
00041 */
00042 
00043 
00044 package sun.awt.im.iiimp;
00045 
00046 import java.util.*;
00047 import java.util.jar.*;
00048 import java.net.*;
00049 import java.io.*;
00050 
00051 import com.sun.iiim.*;
00052 
00053 public class AuxProxy extends Thread implements IIIMProtocol {
00054 
00055     public static void main(String[] args) {
00056        SecurityManager sm = new SecurityManager();
00057        System.setSecurityManager(sm);
00058        AuxProxy t = new AuxProxy();
00059        t.start();
00060     }
00061 
00062     private IIIMPInputStream in;
00063     private IIIMPOutputStream out;
00064     private String hostName;
00065     private int imID;
00066     private int icID;
00067     private HashSet doSet;
00068     private HashMap auxMap;
00069     private static ODClassLoader odLoader;
00070     private IIIMPURLConnection uc;
00071 
00072     public void run() {
00073        // connection setup
00074        try {
00075            hostName = InetAddress.getLocalHost().getHostName();
00076 
00077            Manager.initProps();
00078            String urlString = Manager.getProperty("iiimp.server");
00079            if (urlString == null) {
00080               urlString = "iiimp://" + hostName + ":9010";
00081            }
00082            URL url = new URL(urlString);
00083            uc = (IIIMPURLConnection)url.openConnection();
00084 
00085            if (!uc.isConnected()) {
00086               uc.connect();
00087 
00088               if (!uc.isConnected()) {
00089                   throw new IOException();
00090               }
00091            }
00092            in = new IIIMPInputStream(uc.getInputStream());
00093            out = new IIIMPOutputStream(uc.getOutputStream());
00094            doSet = new HashSet();
00095            auxMap = new HashMap();
00096            odLoader = new ODClassLoader();
00097 
00098            imConnect();
00099            
00100            while(true) {
00101               Protocol protocol = new Protocol();
00102               protocol.read(in);
00103               try {
00104                   driveProtocol(protocol);
00105               } catch(Exception e) {
00106                   if (Manager.DEBUG) {
00107                      e.printStackTrace();
00108                   }
00109                   System.exit(1);
00110               }
00111            }
00112 
00113        } catch(Exception e) {
00114            if (Manager.DEBUG) {
00115               e.printStackTrace();
00116            }
00117            System.exit(1);
00118        }
00119     }
00120 
00121     static final String CLIENT_TYPE = "JavaAuxProxy";
00122 
00123     static public ODClassLoader getLoader() {
00124        return odLoader;
00125     }
00126 
00127     private void imConnect() throws IOException {
00128        String userName = System.getProperty("user.name", null);
00129        userName += "@" + hostName;
00130        ProtocolData data = new ProtocolData();
00131        data.write(BIGENDIAN);
00132        data.write(VERSION);
00133        data.writeString(userName);
00134        data.write2(0);
00135        send(new Protocol(IM_CONNECT, data));
00136        getReply(IM_CONNECT_REPLY);
00137     }
00138 
00143     void setClientType() {
00144        try {
00145            ProtocolData data = new ProtocolData();
00146            data.write2(imID);
00147            data.write2(0); // pad
00148 
00149            IIIMPClientDescripter desc = new IIIMPClientDescripter();
00150            ProtocolData d = desc.getData();
00151            data.write4(d.count);
00152            data.writeBytes(d.buf, d.count);
00153 
00154            send(new Protocol(IM_SETIMVALUES, data));
00155            getReply(IM_SETIMVALUES_REPLY);
00156        } catch(Exception e) {
00157            if (Manager.DEBUG) {
00158               e.printStackTrace();
00159            }
00160        }
00161     }
00162 
00163     void downloadObject() {
00164        if (doSet.size() == 0) {
00165            return;
00166        }
00167        try {
00168            ProtocolData data = new ProtocolData();
00169            data.write2(imID);
00170            data.write2(0); // pad
00171            data.write4(doSet.size() * 2);
00172 
00173            Iterator it = doSet.iterator();
00174            while(it.hasNext()) { 
00175               IIIMPObjectDescripter desc =
00176                   (IIIMPObjectDescripter)it.next();
00177               data.write2(desc.getDAttribID());
00178               debug(desc.toString());
00179            }
00180               
00181            data.pad();
00182            send(new Protocol(IM_GETIMVALUES, data));
00183            getReply(IM_GETIMVALUES_REPLY);
00184        } catch(Exception e) {
00185            if (Manager.DEBUG) {
00186               e.printStackTrace();
00187            }
00188        }
00189     }
00190 
00191     public void dispatchAuxEvent(IIIMAuxEvent e) {
00192        int[] idata;
00193        String[] sdata; 
00194        try {
00195            ProtocolData data = new ProtocolData();
00196 
00197            data.write2(imID);
00198            data.write2(icID);
00199            data.write4(e.getIndex());
00200            data.writeString(e.getName());
00201            if ((idata = e.getIntValues()) == null) {
00202               data.write4(0);
00203            } else {
00204               data.write4(idata.length * 4);
00205               for (int i = 0; i < idata.length; i++) {
00206                   data.write4(idata[i]);
00207               }
00208            }
00209            if ((sdata = e.getStringValues())== null) {
00210               data.write4(0);
00211            } else {
00212               ProtocolData d = new ProtocolData();
00213               for (int i = 0; i < sdata.length; i++) {
00214                   d.writeString(sdata[i]);
00215               }
00216               data.write4(d.count);
00217               data.writeBytes(d.buf, d.count);
00218            }
00219            send(new Protocol(IM_AUX_SETVALUES, data));
00220        } catch(Exception ex) {
00221            if (Manager.DEBUG) {
00222               ex.printStackTrace();
00223            }
00224        }
00225     }
00226 
00227     void createPseudoIC() {
00228        try {
00229            send(new Protocol(IM_CREATEIC, ProtocolDriver.getCreateICData(imID)));
00230            getReply(IM_CREATEIC_REPLY);
00231        } catch(Exception e) {
00232            if (Manager.DEBUG) {
00233               e.printStackTrace();
00234            }
00235        }
00236     }
00237 
00238     void getReply(int protocolID) throws IOException {
00239        try {
00240            Protocol protocol = new Protocol();
00241            protocol.read(in);
00242            driveProtocol(protocol);
00243        } catch(Exception e) {
00244            if (Manager.DEBUG) {
00245               e.printStackTrace();
00246            }
00247        }
00248     }
00249     
00250     private void driveProtocol(Protocol protocol) throws IOException {
00251        int opcode = protocol.getOpCode();
00252        ProtocolData data = protocol.getData();
00253        ProtocolData replyData = new ProtocolData();
00254 
00255        imID = data.read2();
00256        switch(opcode) {
00257          case IM_SETIMVALUES:
00258            data.read2(); // pad
00259            int length = data.read4();
00260 
00261            if (length > 0) {
00262               while(data.available() > 0) {
00263                   int type = data.read2();
00264                   data.read2(); // pad
00265                   int size = data.read4();
00266                   byte[] buf = new byte[size];
00267                   data.read(buf, 0, size);
00268                   ProtocolData adata = new ProtocolData(buf);
00269                   switch(type) {
00270                     case INPUTMETHOD_LIST:
00271                      debug(" im list");
00272                      break;
00273                     case OBJECT_DESCRIPTER_LIST:
00274                      debug(" obj desc list");
00275                      while(adata.available() > 0) {
00276                          int objCategory = adata.read2();
00277                          adata.read2(); // pad
00278                          int objSize = adata.read4();
00279                          int attribID = adata.read2();
00280                          int dattribID = adata.read2();
00281                          String objName = adata.readString();
00282                          String objHRN = adata.readString();
00283                          String objSignature = adata.readString();
00284                          String objUser = adata.readString();
00285                          IIIMPObjectDescripter objDesc =
00286                             new IIIMPObjectDescripter(objCategory,
00287                                                    objSize,
00288                                                    attribID,
00289                                                    dattribID,
00290                                                    objName,
00291                                                    objHRN,
00292                                                    objSignature,
00293                                                    objUser);
00294                          doSet.add(objDesc);
00295                      }
00296                      break;
00297                     case CCDEF:
00298                      debug(" ccdef");
00299                      break;
00300                     case GUI_OBJECT:
00301                      debug(" gui obj");
00302                      break;
00303                     case LWE_OBJECT:
00304                      debug(" lwe obj");
00305                      break;
00306                     default:
00307                      debug(" unknown");
00308                      break;
00309                   }
00310               }
00311            }
00312            replyData.write2(imID);
00313            replyData.write2(0); // pad
00314            send(new Protocol(IM_SETIMVALUES_REPLY, replyData));
00315            break;
00316          case IM_SETIMVALUES_REPLY:
00317            break;
00318          case IM_GETIMVALUES_REPLY:
00319            data.read2(); // pad
00320            data.read4(); // size, not used
00321 
00322            while(data.available() > 7) {
00323               debug(" available = " + data.available());
00324               int attrID = data.read2();
00325               debug(" attrID = " + attrID);
00326               data.read2(); // pad
00327               int size = data.read4();
00328               debug(" attrsize = " + size);
00329               if (size > 0) {
00330                   int classNameListSize = data.read4();
00331                   debug(" cnls = " + classNameListSize);
00332                   ProtocolData classNameData =
00333                      new ProtocolData(data.buf, data.pos, classNameListSize);
00334                   data.skipBytes(classNameListSize);
00335                   while(classNameData.available() > 0) {
00336                      String className = classNameData.readString();
00337                      debug(" class = " + className);
00338                   }
00339                   int jarSize = data.read4();
00340                   debug(" jar size = " + jarSize);
00341                   byte[] attrval = new byte[jarSize];
00342                   data.read(attrval, 0, jarSize);
00343 
00344                   JarInputStream jis =
00345                      new JarInputStream(new ByteArrayInputStream(attrval));
00346                   
00347                   odLoader.addJar(jis);
00348                   
00349                   int pad = ProtocolData.paddings(size);
00350                   debug(" paddings = " + pad);
00351                   for (int i = 0; i < pad; i++) {
00352                      data.read();
00353                   }
00354               }
00355            }
00356            break;
00357          case IM_REGISTER_TRIGGER_KEYS:
00358            break;
00359          case IM_CONNECT_REPLY:
00360            int langLen = data.read2();
00361            debug(" langLen = " + langLen);
00362            while(data.available() > 0) {
00363               debug(" lang = " + data.readString());
00364            }
00365            setClientType();
00366            downloadObject();
00367            createPseudoIC();
00368            break;
00369          case IM_CREATEIC_REPLY:
00370            icID = data.read2();
00371            debug(" CREATEIC_REPLY icID = " + icID);
00372            ProtocolData d = new ProtocolData();
00373            d.write2(imID);
00374            d.write2(icID);
00375            send(new Protocol(IM_SETICFOCUS, d));
00376            getReply(IM_SETICFOCUS_REPLY);
00377            break;
00378          case IM_AUX_START: {
00379              icID = data.read2();
00380              
00381              int index = data.read4();
00382              String name = data.readString();
00383              debug(" AUX START by PROXY - " + name);
00384              
00385              try {
00386                 IIIMAuxListener aux =
00387                     (IIIMAuxListener)odLoader.loadClass(name).newInstance();
00388                 auxMap.put(name, aux);
00389                 ((IIIMComponent)aux).setProxy(this);
00390                 aux.auxStart(new IIIMAuxEvent(IIIMAuxEvent.START,
00391                                           index, name));
00392              } catch(Exception e) {
00393                 if (Manager.DEBUG) {
00394                     e.printStackTrace();
00395                 }
00396              }
00397 
00398              replyData.write2(imID);
00399              replyData.write2(icID);
00400              replyData.write4(index);
00401              replyData.writeString(name);
00402              send(new Protocol(IM_AUX_START_REPLY, replyData));
00403              break;
00404          }
00405          case IM_AUX_DRAW: {
00406              icID = data.read2(); // ic
00407              int index = data.read4(); // aux index
00408              String name = data.readString();
00409              int intSize = data.read4();
00410              int[] intA = new int[intSize / 4];
00411              for (int i = 0; i < intA.length; i++) {
00412                 intA[i] = data.read4();
00413              }
00414              int strSize = data.read4();
00415              Vector v = new Vector();
00416              while(data.available() > 0) {
00417                 v.add(data.readString());
00418              }
00419              String[] strA = new String[v.size()];
00420              for (int i = 0; i < v.size(); i++) {
00421                 strA[i] = (String)v.elementAt(i);
00422              }
00423              
00424              IIIMAuxListener aux = (IIIMAuxListener)auxMap.get(name); 
00425              if (aux == null) {
00426                 debug(" Unknown aux : " + name);
00427                 break;
00428              }
00429              
00430              debug(" aux draw.");
00431 
00432              aux.auxDraw(new IIIMAuxEvent(IIIMAuxEvent.DRAW,
00433                                       index, name, intA, strA));
00434              replyData.write2(imID);
00435              replyData.write2(icID);
00436              replyData.write4(index);
00437              replyData.writeString(name);
00438              send(new Protocol(IM_AUX_DRAW_REPLY, replyData));
00439              break;
00440          }
00441          case IM_AUX_DONE:
00442            icID = data.read2();
00443            int index = data.read4();
00444            String name = data.readString();
00445            IIIMAuxListener aux = (IIIMAuxListener)auxMap.get(name);
00446            if (aux == null) {
00447               debug(" Unknown aux : " + name);
00448               break;
00449            }
00450            aux.auxDone(new IIIMAuxEvent(IIIMAuxEvent.DONE, index, name));
00451            replyData.write2(imID);
00452            replyData.write2(icID);
00453            replyData.write4(index);
00454            replyData.writeString(name);
00455            send(new Protocol(IM_AUX_DONE_REPLY, replyData));
00456            break;
00457          case IM_AUX_SETVALUES_REPLY:
00458            break;
00459          case IM_SETICFOCUS_REPLY:
00460            break;
00461          default:
00462            debug("AuxProxy does not intend to receive " + protocol);
00463        }
00464     }
00465 
00466     private synchronized void send(Protocol protocol) throws IOException {
00467        protocol.write(out);
00468        out.flush();
00469     }
00470 
00471     private void debug(String str) {
00472        if (Manager.DEBUG) {
00473            System.err.println(str);
00474        }
00475     }
00476 
00477     private static final String IIIMP_PKG_PREFIX = "sun.awt.im";
00478     private static final String JAVA_PROTOCOL_HANDLER_PKGS =
00479        "java.protocol.handler.pkgs";
00480 
00481     static {
00482        // Add sun.awt.im to the java.protocol.handler.pkgs property
00483        // if it is not defined yet.
00484        Properties defaults = System.getProperties();
00485        synchronized(defaults) {
00486            String pkgs = defaults.getProperty(JAVA_PROTOCOL_HANDLER_PKGS);
00487            if (pkgs == null) {
00488               pkgs = IIIMP_PKG_PREFIX;
00489            } else {
00490               StringTokenizer st = new StringTokenizer(pkgs, "|");
00491               boolean hasIIIMP = false;
00492               while(st.hasMoreTokens()) {
00493                   if (st.nextToken().equals(IIIMP_PKG_PREFIX)) {
00494                      hasIIIMP = true;
00495                   }
00496               }
00497               if (!hasIIIMP) {
00498                   pkgs += "|" + IIIMP_PKG_PREFIX;
00499               }
00500            }
00501            defaults.put(JAVA_PROTOCOL_HANDLER_PKGS, pkgs);
00502        }
00503     }
00504 }