| 1 | package com.moesol.bindings; |
| 2 | |
| 3 | import java.util.logging.Level; |
| 4 | import java.util.logging.Logger; |
| 5 | |
| 6 | import com.moesol.bindings.platform_sdk.component_services.GUID; |
| 7 | |
| 8 | /** |
| 9 | * Internal helper, carry the computed information for |
| 10 | * finding an IRecordInfo for a VT_RECORD. |
| 11 | * |
| 12 | * @author Hastings |
| 13 | */ |
| 14 | public class DispatchJavaStructure extends DispatchJavaCommon { |
| 15 | static final Logger s_logger = Logger.getLogger(DispatchJavaStructure.class.getName()); |
| 16 | |
| 17 | private static long s_records_alloc = 0; |
| 18 | |
| 19 | /** JNI code calls this to track allocated records. */ |
| 20 | synchronized static void incRecordsAlloc() { |
| 21 | s_records_alloc++; |
| 22 | } |
| 23 | /** JNI code calls this to track allocated records. */ |
| 24 | synchronized static void decRecordsAlloc() { |
| 25 | s_records_alloc--; |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * @return The number of records that have been allocated but not freed. |
| 30 | */ |
| 31 | public synchronized static long getRecordsAlloc() { |
| 32 | return s_records_alloc; |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Normally called from JNI code to learn enough information |
| 37 | * to convert the Java structure format into the native format. |
| 38 | * |
| 39 | * @param c |
| 40 | * @return DispatchJavaStructure with information about {@code c}. |
| 41 | */ |
| 42 | public static DispatchJavaStructure forClass(Class c) { |
| 43 | DispatchJavaStructure r = new DispatchJavaStructure(); |
| 44 | try { |
| 45 | r.fillLibraryInfo(c); |
| 46 | r.fillStructureInfo(c); |
| 47 | } catch (SecurityException e) { |
| 48 | throw new RuntimeException(e); |
| 49 | } catch (IllegalArgumentException e) { |
| 50 | throw new RuntimeException(e); |
| 51 | } catch (ClassNotFoundException e) { |
| 52 | throw new RuntimeException(e); |
| 53 | } catch (NoSuchFieldException e) { |
| 54 | throw new RuntimeException(e); |
| 55 | } catch (IllegalAccessException e) { |
| 56 | throw new RuntimeException(e); |
| 57 | } |
| 58 | |
| 59 | if (!r.isSearchable()) { |
| 60 | return null; |
| 61 | } |
| 62 | return r; |
| 63 | } |
| 64 | |
| 65 | private void fillStructureInfo(Class c) throws SecurityException, IllegalArgumentException, IllegalAccessException { |
| 66 | try { |
| 67 | setUuid((GUID)getObjectField(c, "UUID")); |
| 68 | } catch (NoSuchFieldException e) { |
| 69 | // Some structures don't have UUID, use the name instead |
| 70 | s_logger.log(Level.INFO, "No UUID for structure {0}", c.getName()); |
| 71 | } |
| 72 | // if (getUuid() == null || GUID.NULL.equals(getUuid())) { |
| 73 | // setStructureName(getSimpleName(c)); |
| 74 | // } |
| 75 | } |
| 76 | |
| 77 | // private String getSimpleName(Class c) { |
| 78 | // String long_name = c.getName(); |
| 79 | // int r = long_name.lastIndexOf('.'); |
| 80 | // return long_name.substring(r + 1); |
| 81 | // } |
| 82 | |
| 83 | // I did not have success looking |
| 84 | // an IRecordInfo for a structure with no UUID |
| 85 | // therefore I dropped support for trying to |
| 86 | // find IRecordInfo if the structure has not UUID |
| 87 | // public String getStructureName() { |
| 88 | // return m_name; |
| 89 | // } |
| 90 | // public void setStructureName(String v) { |
| 91 | // m_name = v; |
| 92 | // } |
| 93 | // |
| 94 | public boolean isSearchable() { |
| 95 | if (getLibid() == null) { |
| 96 | return false; |
| 97 | } |
| 98 | if (m_uuid == null || GUID.NULL.equals(m_uuid) /* && m_name == null */) { |
| 99 | return false; |
| 100 | } |
| 101 | return true; |
| 102 | } |
| 103 | } |