// ********************************************************************** // // Copyright (c) 2003-2016 ZeroC, Inc. All rights reserved. // // This copy of Ice is licensed to you under the terms described in the // ICE_LICENSE file included in this distribution. // // ********************************************************************** #import #import #import #import #import #import #import #import #import #import #import #import #include #include #include namespace { class DefaultServantLocator : public Ice::ServantLocator { public: DefaultServantLocator(const Ice::ObjectPtr& s) : _servant(s) { } virtual Ice::ObjectPtr locate(const Ice::Current&, Ice::LocalObjectPtr&) { return _servant; } virtual void finished(const Ice::Current&, const Ice::ObjectPtr&, const Ice::LocalObjectPtr&) { } virtual void deactivate(const std::string&) { } const Ice::ObjectPtr& servant() const { return _servant; } private: Ice::ObjectPtr _servant; }; typedef IceUtil::Handle DefaultServantLocatorPtr; class Cookie : public Ice::LocalObject { public: Cookie(id cookie) : _cookie(cookie) { [_cookie retain]; } ~Cookie() { [_cookie release]; } id cookie() { return _cookie; } private: id _cookie; }; typedef IceUtil::Handle CookiePtr; class ExceptionWriter : public Ice::UserExceptionWriter { public: ExceptionWriter(const Ice::CommunicatorPtr& communicator, ICEUserException* ex) : Ice::UserExceptionWriter(communicator), _ex(ex) { } ~ExceptionWriter() throw() { [_ex release]; } void write(const Ice::OutputStreamPtr& s) const { ICEOutputStream* os = [ICEOutputStream localObjectWithCxxObjectNoAutoRelease:s.get()]; [_ex write__:os]; [os release]; } bool usesClasses() const { return [_ex usesClasses__]; } std::string ice_name() const { return [[_ex ice_name] UTF8String]; } Ice::UserException* ice_clone() const { return new ExceptionWriter(*this); } void ice_throw() const { throw *this; } private: ICEUserException* _ex; }; class ServantLocatorWrapper : public Ice::ServantLocator { public: ServantLocatorWrapper(id locator) : _locator(locator) { [_locator retain]; } ~ServantLocatorWrapper() { [_locator release]; } virtual Ice::ObjectPtr locate(const Ice::Current& current, Ice::LocalObjectPtr& cookie) { NSException* ex = nil; @autoreleasepool { ICECurrent* cu = [[ICECurrent alloc] initWithCurrent:current]; id co = nil; @try { ICEObject* servant = [_locator locate:cu cookie:&co]; if(co != nil) { cookie = new Cookie(co); } return [servant object__]; } @catch(id e) { ex = [e retain]; } @finally { [cu release]; } } if(ex != nil) { if([ex isKindOfClass:[ICEUserException class]]) { throw ExceptionWriter(current.adapter->getCommunicator(), (ICEUserException*)ex); } rethrowCxxException(ex, true); // True = release the exception. } } virtual void finished(const Ice::Current& current, const Ice::ObjectPtr& servant, const Ice::LocalObjectPtr& cookie) { NSException* ex = nil; @autoreleasepool { ICECurrent* cu = [[ICECurrent alloc] initWithCurrent:current]; id co = cookie ? CookiePtr::dynamicCast(cookie)->cookie() : nil; @try { [_locator finished:cu servant:toObjC(servant) cookie:co]; } @catch(id e) { ex = [e retain]; } @finally { [cu release]; } } if(ex != nil) { if([ex isKindOfClass:[ICEUserException class]]) { throw ExceptionWriter(current.adapter->getCommunicator(), (ICEUserException*)ex); } rethrowCxxException(ex, true); // True = release the exception. } } virtual void deactivate(const std::string& category) { NSException* ex = nil; @autoreleasepool { NSString* cat = toNSString(category); @try { [_locator deactivate:cat]; } @catch(id e) { ex = [e retain]; } @finally { [cat release]; } } if(ex != nil) { rethrowCxxException(ex, true); // True = release the exception. } } id locator() const { return [[_locator retain] autorelease]; } private: id _locator; }; typedef IceUtil::Handle ServantLocatorWrapperPtr; } #define OBJECTADAPTER dynamic_cast(static_cast(cxxObject_)) @implementation ICEObjectAdapter -(Ice::ObjectAdapter*) adapter { return OBJECTADAPTER; } // // @protocol ICEObjectAdapter methods. // -(id) getCommunicator { NSException* nsex = nil; try { return [ICECommunicator localObjectWithCxxObject:OBJECTADAPTER->getCommunicator().get()]; } catch(const std::exception& ex) { nsex = toObjCException(ex); } @throw nsex; return nil; // Keep the compiler happy. } -(NSMutableString*) getName { NSException* nsex = nil; try { return [toNSMutableString(OBJECTADAPTER->getName()) autorelease]; } catch(const std::exception& ex) { nsex = toObjCException(ex); } @throw nsex; return nil; // Keep the compiler happy. } -(void) activate { NSException* nsex = nil; try { OBJECTADAPTER->activate(); } catch(const std::exception& ex) { nsex = toObjCException(ex); } if(nsex != nil) { @throw nsex; } } -(void) hold { NSException* nsex = nil; try { OBJECTADAPTER->hold(); } catch(const std::exception& ex) { nsex = toObjCException(ex); } if(nsex != nil) { @throw nsex; } } -(void) waitForHold { NSException* nsex = nil; try { OBJECTADAPTER->waitForHold(); } catch(const std::exception& ex) { nsex = toObjCException(ex); } if(nsex != nil) { @throw nsex; } } -(void) deactivate { NSException* nsex = nil; try { OBJECTADAPTER->deactivate(); } catch(const std::exception& ex) { nsex = toObjCException(ex); } if(nsex != nil) { @throw nsex; } } -(void) waitForDeactivate { NSException* nsex = nil; try { OBJECTADAPTER->waitForDeactivate(); } catch(const std::exception& ex) { nsex = toObjCException(ex); } if(nsex != nil) { @throw nsex; } } -(BOOL) isDeactivated { NSException* nsex = nil; try { return OBJECTADAPTER->isDeactivated(); } catch(const std::exception& ex) { nsex = toObjCException(ex); } @throw nsex; return NO; // Keep the compiler happy. } -(void) destroy { NSException* nsex = nil; try { OBJECTADAPTER->destroy(); } catch(const std::exception& ex) { nsex = toObjCException(ex); } if(nsex != nil) { @throw nsex; } } -(id) add:(ICEObject*)servant identity:(ICEIdentity*)ident { NSException* nsex = nil; try { return [ICEObjectPrx objectPrxWithObjectPrx__:OBJECTADAPTER->add([servant object__], [ident identity])]; } catch(const std::exception& ex) { nsex = toObjCException(ex); } @throw nsex; return nil; // Keep the compiler happy. } -(id) addFacet:(ICEObject*)servant identity:(ICEIdentity*)ident facet:(NSString*)facet { NSException* nsex = nil; try { return [ICEObjectPrx objectPrxWithObjectPrx__:OBJECTADAPTER->addFacet([servant object__], [ident identity], fromNSString(facet))]; } catch(const std::exception& ex) { nsex = toObjCException(ex); } @throw nsex; return nil; // Keep the compiler happy. } -(id) addWithUUID:(ICEObject*)servant { NSException* nsex = nil; try { return [ICEObjectPrx objectPrxWithObjectPrx__:OBJECTADAPTER->addWithUUID([servant object__])]; } catch(const std::exception& ex) { nsex = toObjCException(ex); } @throw nsex; return nil; // Keep the compiler happy. } -(id) addFacetWithUUID:(ICEObject*)servant facet:(NSString*)facet { NSException* nsex = nil; try { return [ICEObjectPrx objectPrxWithObjectPrx__:OBJECTADAPTER->addFacetWithUUID([servant object__], fromNSString(facet))]; } catch(const std::exception& ex) { nsex = toObjCException(ex); } @throw nsex; return nil; // Keep the compiler happy. } -(void) addDefaultServant:(ICEObject*)servant category:(NSString*)category { NSException* nsex = nil; try { Ice::ServantLocatorPtr servantLocator = new DefaultServantLocator([servant object__]); OBJECTADAPTER->addServantLocator(servantLocator, fromNSString(category)); } catch(const std::exception& ex) { nsex = toObjCException(ex); } if(nsex != nil) { @throw nsex; } } -(ICEObject*) remove:(ICEIdentity*)ident { NSException* nsex = nil; try { return toObjC(OBJECTADAPTER->remove([ident identity])); } catch(const std::exception& ex) { nsex = toObjCException(ex); } @throw nsex; return nil; // Keep the compiler happy. } -(ICEObject*) removeFacet:(ICEIdentity*)ident facet:(NSString*)facet { NSException* nsex = nil; try { return toObjC(OBJECTADAPTER->removeFacet([ident identity], fromNSString(facet))); } catch(const std::exception& ex) { nsex = toObjCException(ex); } @throw nsex; return nil; // Keep the compiler happy. } -(ICEMutableFacetMap*) removeAllFacets:(ICEIdentity*)ident { NSException* nsex = nil; try { return toNSDictionary(OBJECTADAPTER->removeAllFacets([ident identity])); } catch(const std::exception& ex) { nsex = toObjCException(ex); } @throw nsex; return nil; // Keep the compiler happy. } -(ICEObject*) removeDefaultServant:(NSString*)category { NSException* nsex = nil; try { Ice::ServantLocatorPtr locator = OBJECTADAPTER->removeServantLocator(fromNSString(category)); if(!locator) { return nil; } DefaultServantLocatorPtr defaultServantLocator = DefaultServantLocatorPtr::dynamicCast(locator); if(!defaultServantLocator) { return nil; } return toObjC(defaultServantLocator->servant()); } catch(const std::exception& ex) { nsex = toObjCException(ex); } @throw nsex; return nil; // Keep the compiler happy. } -(ICEObject*) find:(ICEIdentity*)ident { NSException* nsex = nil; try { return toObjC(OBJECTADAPTER->find([ident identity])); } catch(const std::exception& ex) { nsex = toObjCException(ex); } @throw nsex; return nil; // Keep the compiler happy. } -(ICEObject*) findFacet:(ICEIdentity*)ident facet:(NSString*)facet { NSException* nsex = nil; try { return toObjC(OBJECTADAPTER->findFacet([ident identity], fromNSString(facet))); } catch(const std::exception& ex) { nsex = toObjCException(ex); } @throw nsex; return nil; // Keep the compiler happy. } -(NSMutableDictionary*) findAllFacets:(ICEIdentity*)ident { NSException* nsex = nil; try { return toNSDictionary(OBJECTADAPTER->findAllFacets([ident identity])); } catch(const std::exception& ex) { nsex = toObjCException(ex); } @throw nsex; return nil; // Keep the compiler happy. } -(ICEObject*) findByProxy:(id)proxy { NSException* nsex = nil; try { return toObjC(OBJECTADAPTER->findByProxy([(ICEObjectPrx*)proxy objectPrx__])); } catch(const std::exception& ex) { nsex = toObjCException(ex); } @throw nsex; return nil; // Keep the compiler happy. } -(void) addServantLocator:(id)locator category:(NSString*)category { NSException* nsex = nil; try { OBJECTADAPTER->addServantLocator(new ServantLocatorWrapper(locator), fromNSString(category)); return; } catch(const std::exception& ex) { nsex = toObjCException(ex); } @throw nsex; } -(id) removeServantLocator:(NSString*)category { NSException* nsex = nil; try { Ice::ServantLocatorPtr locator = OBJECTADAPTER->removeServantLocator(fromNSString(category)); return ServantLocatorWrapperPtr::dynamicCast(locator)->locator(); } catch(const std::exception& ex) { nsex = toObjCException(ex); } @throw nsex; } -(id) findServantLocator:(NSString*)category { NSException* nsex = nil; try { Ice::ServantLocatorPtr locator = OBJECTADAPTER->removeServantLocator(fromNSString(category)); return locator ? ServantLocatorWrapperPtr::dynamicCast(locator)->locator() : nil; } catch(const std::exception& ex) { nsex = toObjCException(ex); } @throw nsex; } -(ICEObject*) findDefaultServant:(NSString*)category { NSException* nsex = nil; try { Ice::ServantLocatorPtr servantLocator = OBJECTADAPTER->findServantLocator(fromNSString(category)); if(servantLocator == 0) { return nil; } DefaultServantLocatorPtr defaultServantLocator = DefaultServantLocatorPtr::dynamicCast(servantLocator); if(defaultServantLocator == 0) { return nil; // should never happen! } else { return toObjC(defaultServantLocator->servant()); } } catch(const std::exception& ex) { nsex = toObjCException(ex); } @throw nsex; return nil; // Keep the compiler happy. } -(id) createProxy:(ICEIdentity*)ident { NSException* nsex = nil; try { return [ICEObjectPrx objectPrxWithObjectPrx__:OBJECTADAPTER->createProxy([ident identity])]; } catch(const std::exception& ex) { nsex = toObjCException(ex); } @throw nsex; return nil; // Keep the compiler happy. } -(id) createDirectProxy:(ICEIdentity*)ident { NSException* nsex = nil; try { return [ICEObjectPrx objectPrxWithObjectPrx__:OBJECTADAPTER->createDirectProxy([ident identity])]; } catch(const std::exception& ex) { nsex = toObjCException(ex); } @throw nsex; return nil; // Keep the compiler happy. } -(id) createIndirectProxy:(ICEIdentity*)ident { NSException* nsex = nil; try { return [ICEObjectPrx objectPrxWithObjectPrx__:OBJECTADAPTER->createIndirectProxy([ident identity])]; } catch(const std::exception& ex) { nsex = toObjCException(ex); } @throw nsex; return nil; // Keep the compiler happy. } -(void) setLocator:(id)loc { NSException* nsex = nil; try { OBJECTADAPTER->setLocator(Ice::LocatorPrx::uncheckedCast(Ice::ObjectPrx([(ICEObjectPrx*)loc objectPrx__]))); } catch(const std::exception& ex) { nsex = toObjCException(ex); } if(nsex != nil) { @throw nsex; } } -(id) getLocator { NSException* nsex = nil; try { return (id)[ICELocatorPrx objectPrxWithObjectPrx__:OBJECTADAPTER->getLocator()]; } catch(const std::exception& ex) { nsex = toObjCException(ex); } if(nsex != nil) { @throw nsex; } return nil; // Keep the compiler happy. } -(void) refreshPublishedEndpoints { NSException* nsex = nil; try { OBJECTADAPTER->refreshPublishedEndpoints(); } catch(const std::exception& ex) { nsex = toObjCException(ex); } if(nsex != nil) { @throw nsex; } } -(ICEEndpointSeq*) getEndpoints { NSException* nsex = nil; try { return [toNSArray(OBJECTADAPTER->getEndpoints()) autorelease]; } catch(const std::exception& ex) { nsex = toObjCException(ex); } if(nsex != nil) { @throw nsex; } return nil; } -(ICEEndpointSeq*) getPublishedEndpoints { NSException* nsex = nil; try { return [toNSArray(OBJECTADAPTER->getPublishedEndpoints()) autorelease]; } catch(const std::exception& ex) { nsex = toObjCException(ex); } if(nsex != nil) { @throw nsex; } return nil; } @end