| 1 | /* |
| 2 | * $Id: ComClassFactory.java,v 1.7 2006/02/01 18:54:35 adamp 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.platform_sdk.component_services; |
| 13 | |
| 14 | import java.nio.ByteBuffer; |
| 15 | import java.util.logging.Level; |
| 16 | import java.util.logging.Logger; |
| 17 | |
| 18 | public class ComClassFactory extends ComObject implements IClassFactory { |
| 19 | private static final Logger s_logger = Logger.getLogger(ComClassFactory.class.getName()); |
| 20 | private final Class m_factory_for_class; |
| 21 | |
| 22 | public ComClassFactory(Class factory_for_class) { |
| 23 | m_factory_for_class = factory_for_class; |
| 24 | } |
| 25 | |
| 26 | public void CreateInstance(IUnknown pUnkOuter, Object[] out_iface) { |
| 27 | checkAggregation(pUnkOuter, out_iface); |
| 28 | |
| 29 | if (!isAggregation(pUnkOuter)) { |
| 30 | create().QueryInterface(out_iface); |
| 31 | } else { |
| 32 | createInner(pUnkOuter).QueryInterface(out_iface); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | public void LockServer(boolean fLock) { |
| 37 | // TODO Auto-generated method stub |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * @param pUnkOuter |
| 42 | * @return |
| 43 | */ |
| 44 | private boolean isAggregation(IUnknown pUnkOuter) { |
| 45 | return pUnkOuter != null; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * @param pUnkOuter |
| 50 | * @param out_iface |
| 51 | */ |
| 52 | private void checkAggregation(IUnknown pUnkOuter, Object[] out_iface) { |
| 53 | if (isAggregation(pUnkOuter) && !(out_iface instanceof IUnknown[])) { |
| 54 | // can't ask for anything other than IUnknown when aggregating |
| 55 | s_logger.log(Level.FINE, "asked for non IUnknown interface while creating an aggregated object"); |
| 56 | throw new COMException(HRESULT.CLASS_E_NOAGGREGATION); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | private ComObject createInner(IUnknown unkOuter) { |
| 61 | ComObject inner = new ComObject(); |
| 62 | inner.m_iunknown_support.aggregate(create()); |
| 63 | |
| 64 | ComIUnknownOuter outer = new ComIUnknownOuter(unkOuter); |
| 65 | outer.aggregate(inner); |
| 66 | return inner; |
| 67 | } |
| 68 | |
| 69 | private ComObject create() { |
| 70 | try { |
| 71 | return (ComObject)m_factory_for_class.newInstance(); |
| 72 | } catch (ClassCastException e) { |
| 73 | s_logger.log(Level.INFO, "caught", e); |
| 74 | throw new COMException(COM.mapExceptionToHRESULT(e)); |
| 75 | } catch (InstantiationException e) { |
| 76 | s_logger.log(Level.INFO, "caught", e); |
| 77 | throw new COMException(COM.mapExceptionToHRESULT(e)); |
| 78 | } catch (IllegalAccessException e) { |
| 79 | s_logger.log(Level.INFO, "caught", e); |
| 80 | throw new COMException(COM.mapExceptionToHRESULT(e)); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | static IUnknown thunkedCreateInstance(Object oFactory, IUnknown pUnkOuter, ByteBuffer bb_iid) { |
| 85 | ComClassFactory factory = (ComClassFactory)oFactory; |
| 86 | GUID iid = new GUID(bb_iid); |
| 87 | return factory.create().m_iunknown_support.queryInterface(iid); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | |