| 1 | /* |
| 2 | * $Id: DispatchJavaCommon.java,v 1.3 2006/03/01 19:21:07 hastings Exp $ |
| 3 | * |
| 4 | * (c) Copyright, Moebius Solutions, Inc., 2004 |
| 5 | * |
| 6 | * All Rights Reserved |
| 7 | * |
| 8 | * This material may be reproduced by or for the U. S. Government |
| 9 | * pursuant to the copyright license under the clause at |
| 10 | * DFARS 252.227-7014 (OCT 2001). |
| 11 | */ |
| 12 | package com.moesol.bindings; |
| 13 | |
| 14 | import java.lang.reflect.Field; |
| 15 | import java.util.logging.Level; |
| 16 | import java.util.logging.Logger; |
| 17 | |
| 18 | import com.moesol.bindings.platform_sdk.component_services.GUID; |
| 19 | |
| 20 | /** |
| 21 | * Help the JNI code recover the LIBID, major version, minor version, and lcid |
| 22 | * for a class. |
| 23 | * |
| 24 | * @author Hastings |
| 25 | * |
| 26 | */ |
| 27 | public class DispatchJavaCommon { |
| 28 | private static final Logger s_logger = Logger.getLogger(DispatchJavaCommon.class.getName()); |
| 29 | |
| 30 | private GUID m_libid; |
| 31 | private int m_major; |
| 32 | private int m_minor; |
| 33 | private int m_lcid; |
| 34 | |
| 35 | protected GUID m_uuid; |
| 36 | |
| 37 | protected DispatchJavaCommon() { |
| 38 | } |
| 39 | |
| 40 | protected void fillLibraryInfo(Class c) throws ClassNotFoundException, SecurityException, IllegalArgumentException, NoSuchFieldException, IllegalAccessException { |
| 41 | Class tc = getTypelibClass(c); |
| 42 | if (tc == null) { |
| 43 | tc = getOldStyleTypelibClass(c); |
| 44 | if (tc == null) { |
| 45 | return; |
| 46 | } |
| 47 | } |
| 48 | copyInfoFromTypelibClass(tc); |
| 49 | } |
| 50 | |
| 51 | private Class getOldStyleTypelibClass(Class c) { |
| 52 | Package p = c.getPackage(); |
| 53 | String typelib = p.getName() + ".TYPELIB"; |
| 54 | Class tc = null; |
| 55 | try { |
| 56 | tc = Class.forName(typelib); |
| 57 | } catch (ClassNotFoundException e) { |
| 58 | s_logger.log(Level.INFO, "No TYPELIB for {0}", c.getName()); |
| 59 | } |
| 60 | return tc; |
| 61 | } |
| 62 | |
| 63 | private Class getTypelibClass(Class c) { |
| 64 | Class tc = null; |
| 65 | try { |
| 66 | tc = (Class)getObjectField(c, "TYPELIB"); |
| 67 | } catch (SecurityException e) { |
| 68 | reportTypelibException(e); |
| 69 | } catch (IllegalArgumentException e) { |
| 70 | reportTypelibException(e); |
| 71 | } catch (NoSuchFieldException e) { |
| 72 | reportTypelibException(e); |
| 73 | } catch (IllegalAccessException e) { |
| 74 | reportTypelibException(e); |
| 75 | } |
| 76 | return tc; |
| 77 | } |
| 78 | |
| 79 | private void reportTypelibException(Exception e) { |
| 80 | s_logger.log(Level.INFO, "Exception trying to find TYPELIB field, will try old style TYPELIB class", e); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * @param tc |
| 85 | * @throws NoSuchFieldException |
| 86 | * @throws IllegalAccessException |
| 87 | */ |
| 88 | private void copyInfoFromTypelibClass(Class tc) throws NoSuchFieldException, IllegalAccessException { |
| 89 | setLibid((GUID)getObjectField(tc, "UUID")); |
| 90 | setMajorVersion(getIntField(tc, "MAJOR_VERSION")); |
| 91 | setMinorVersion(getIntField(tc, "MINOR_VERSION")); |
| 92 | setLcid(getIntField(tc, "LCID")); |
| 93 | } |
| 94 | |
| 95 | private int getIntField(Class tc, String string) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException { |
| 96 | Field f = tc.getField(string); |
| 97 | return f.getInt(tc); |
| 98 | } |
| 99 | |
| 100 | protected Object getObjectField(Class tc, String string) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException { |
| 101 | Field f = tc.getField(string); |
| 102 | return f.get(tc); |
| 103 | } |
| 104 | |
| 105 | public GUID getLibid() { |
| 106 | return m_libid; |
| 107 | } |
| 108 | |
| 109 | public void setLibid(GUID v) { |
| 110 | m_libid = v; |
| 111 | } |
| 112 | |
| 113 | public int getMajorVersion() { |
| 114 | return m_major; |
| 115 | } |
| 116 | |
| 117 | public void setMajorVersion(int v) { |
| 118 | m_major = v; |
| 119 | } |
| 120 | |
| 121 | public int getMinorVersion() { |
| 122 | return m_minor; |
| 123 | } |
| 124 | |
| 125 | public void setMinorVersion(int v) { |
| 126 | m_minor = v; |
| 127 | } |
| 128 | |
| 129 | public int getLcid() { |
| 130 | return m_lcid; |
| 131 | } |
| 132 | |
| 133 | public void setLcid(int v) { |
| 134 | m_lcid = v; |
| 135 | } |
| 136 | |
| 137 | public GUID getUuid() { |
| 138 | return m_uuid; |
| 139 | } |
| 140 | |
| 141 | public void setUuid(GUID v) { |
| 142 | m_uuid = v; |
| 143 | } |
| 144 | |
| 145 | } |