⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 identitymanager.java

📁 用于Java 组件和通过Windows COM 对象或Windows DLL 来公开的组件之间的互操作
💻 JAVA
字号:
//******************************************************************
// Released under the DevelopMentor OpenSource Software License.
// Please consult the LICENSE file in the project root directory,
// or at http://www.develop.com for details before using this
// software.
//******************************************************************

package com.develop.jawin;

import com.develop.jawin.*;
import com.develop.jawin.marshal.*;
import com.develop.io.*;
import java.io.*;
import java.util.*;

public class IdentityManager {

  public static final boolean traceRefs = 
          (null != System.getProperty("com.develop.jawin.traceRefs"));
  static private HashMap idents = new HashMap();
  static private Object lock = new int[0];
  static private HashMap guidToClass = new HashMap();

  static public COMPtr queryInterface(Class newItf, COMPtr src) { 
    COMPtr result = newCOMPtr(newItf);
    int unknown = src.getUnknown();
    if (unknown != 0) {
      result.setUnknown(Bootstrap.queryInterface(unknown, result.getGuidToken()));
    } else {
      result.setUnknown(Bootstrap.unmarshalFromGIT(src.getPeer(), result.getGuidToken()));
    }
    if (IdentityManager.traceRefs) {
      System.out.println(src + " QI --> " + result);
    }
    return result;
  }

  static private COMPtr newCOMPtr(Class template) {
    try {
      return (COMPtr)template.newInstance();
    } catch (Exception e) {
      throw new COMError("Unable to create instance of " + template.getClass());
    }
  }
  public static COMPtr createGITRef(COMPtr src) {
    COMPtr result = newCOMPtr(src.getClass());
    int peer = 0;
    synchronized (src) {
      peer = src.getPeer();
      if (peer == 0) {
	peer = Bootstrap.marshalToGIT(src.getUnknown(), src.getGuidToken());
      }      
    }
    result.setPeer(peer);
    return result;
  }

  public static COMPtr createDirectRef(COMPtr src) {
    COMPtr result = newCOMPtr(src.getClass());
    int unknown = 0;
    synchronized (src) {
      unknown = src.getUnknown();
      if (unknown == 0) {
	unknown = Bootstrap.unmarshalFromGIT(src.getPeer(), src.getGuidToken());
      }      
    }
    result.setUnknown(unknown);
    if (IdentityManager.traceRefs && (0 == src.getUnknown())) {
       System.out.println(src + " unmarshal --> " + result);
    }
    return result;
  }
  /** 
     * increments the cached ref count for the proxy.  If identity
     * does not exist yet, create it.
     */
  static Identity IncRef(int peer) {
    Integer key = new Integer(peer);
    synchronized (lock) {
      Identity i = (Identity) idents.get(key);
      if (i == null) {
	i = new Identity(peer); 
	idents.put(key, i);
      } else {
	i.IncRef();
      }
      return i;
    }
  }
    
  /**
     * decrements the cached ref count for the proxy.  If count 
     * reaches zero, revoke the GIT pointer
     */
  static public void DecRef(int peer) {
    int refCount = 0;
    Integer key = new Integer(peer);
    synchronized (lock) {
      Identity i = (Identity) idents.get(key);
      if (i == null) {
	throw new COMError("Peer " + peer + " is not a COM identity");
      }
      refCount = i.DecRef();
      if (refCount == 0) {
	idents.remove(key);
      }
    }
    if (refCount == 0) {
      Bootstrap.revokeGIT(peer);
    }
  }
  public static COMPtr getCOMPtr(int peer, int unk, GUID iid) {
    Class proxyClass = (Class) guidToClass.get(iid);
    if (proxyClass == null) 
    {
      throw new COMError("No Java class for ITF" + iid);
    }
    COMPtr ret= null;
    try {
      ret = (COMPtr) proxyClass.newInstance();
    } catch (Exception e) {
      throw new COMError("Could not create COMPtr " + e.getMessage());
    }
    ret.setUnknown(unk);
    ret.setPeer(peer);
    return ret;
  }
  public static COMPtr getCOMPtr(LittleEndianInputStream leis, GUID iid) 
    throws IOException, COMException    
  {
    int unk = leis.readInt();
    int peer = leis.readInt();
    return getCOMPtr(peer, unk, iid);
  }
  public static COMPtr getCOMPtr(byte[] stream, int offset, GUID iid) 
                throws COMException    
  {
    int unk = StructConverter.bytesIntoInt(stream, offset);
    int peer = StructConverter.bytesIntoInt(stream, offset + 4);
    return getCOMPtr(peer, unk, iid);
  }
    static private void registerProxyPrototype(GUID iid, Class proxy) {
        guidToClass.put(iid, proxy);
    }

    /**
     * Each COM iid must have a proxy class registered with the system.
     */
    static public int registerProxy(GUID iid, Class proxy) 
    {
        IdentityManager.registerProxyPrototype(iid, proxy);
        NakedByteStream nbs = new NakedByteStream();
        LittleEndianOutputStream leos = new LittleEndianOutputStream(nbs);
        try {
            iid.marshal(leos, null);
            return Bootstrap.registerGUID(nbs.getInternalBuffer());
        }
        catch (Exception e) {
            e.printStackTrace();
            throw new Error("could not initialize proxy");  
        }
    }

  }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -