1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 // MARKER(update_precomp.py): autogen include statement, do not remove 25 #include "precompiled_bridges.hxx" 26 27 #include "com/sun/star/bridge/XBridge.hpp" 28 #include "com/sun/star/bridge/XBridgeFactory.hpp" 29 #include "com/sun/star/connection/Connector.hpp" 30 #include "com/sun/star/connection/XConnection.hpp" 31 #include "com/sun/star/connection/XConnector.hpp" 32 #include "com/sun/star/lang/XMultiComponentFactory.hpp" 33 #include "com/sun/star/lang/XServiceInfo.hpp" 34 #include "com/sun/star/lang/XSingleComponentFactory.hpp" 35 #include "com/sun/star/registry/InvalidRegistryException.hpp" 36 #include "com/sun/star/registry/XRegistryKey.hpp" 37 #include "com/sun/star/uno/Exception.hpp" 38 #include "com/sun/star/uno/Reference.hxx" 39 #include "com/sun/star/uno/RuntimeException.hpp" 40 #include "com/sun/star/uno/Sequence.hxx" 41 #include "com/sun/star/uno/XComponentContext.hpp" 42 #include "com/sun/star/uno/XInterface.hpp" 43 #include "cppuhelper/factory.hxx" 44 #include "cppuhelper/implbase2.hxx" 45 #include "cppuhelper/weak.hxx" 46 #include "rtl/string.h" 47 #include "rtl/ustring.hxx" 48 #include "sal/types.h" 49 #include "test/java_uno/equals/XBase.hpp" 50 #include "test/java_uno/equals/XDerived.hpp" 51 #include "test/java_uno/equals/XTestInterface.hpp" 52 #include "uno/environment.h" 53 #include "uno/lbnames.h" 54 55 namespace css = com::sun::star; 56 57 namespace { 58 59 class Service: public cppu::WeakImplHelper2< 60 css::lang::XServiceInfo, test::java_uno::equals::XTestInterface > 61 { 62 public: 63 virtual inline rtl::OUString SAL_CALL getImplementationName() 64 { return rtl::OUString::createFromAscii(getImplementationName_static()); } 65 66 virtual sal_Bool SAL_CALL supportsService( 67 rtl::OUString const & rServiceName); 68 69 virtual inline css::uno::Sequence< rtl::OUString > SAL_CALL 70 getSupportedServiceNames() 71 { return getSupportedServiceNames_static(); } 72 73 virtual void SAL_CALL connect(rtl::OUString const & rConnection, 74 rtl::OUString const & rProtocol); 75 76 virtual css::uno::Reference< css::uno::XInterface > SAL_CALL get( 77 rtl::OUString const & rName); 78 79 static inline sal_Char const * getImplementationName_static() 80 { return "com.sun.star.test.bridges.testequals.impl"; } 81 82 static css::uno::Sequence< rtl::OUString > 83 getSupportedServiceNames_static(); 84 85 static css::uno::Reference< css::uno::XInterface > SAL_CALL createInstance( 86 css::uno::Reference< css::uno::XComponentContext > const & rContext); 87 88 private: 89 explicit inline Service( 90 css::uno::Reference< css::uno::XComponentContext > const & rContext): 91 m_xContext(rContext) {} 92 93 css::uno::Reference< css::uno::XComponentContext > m_xContext; 94 css::uno::Reference< css::bridge::XBridge > m_xBridge; 95 }; 96 97 } 98 99 sal_Bool Service::supportsService(rtl::OUString const & rServiceName) 100 { 101 css::uno::Sequence< rtl::OUString > aNames( 102 getSupportedServiceNames_static()); 103 for (sal_Int32 i = 0; i< aNames.getLength(); ++i) 104 if (aNames[i] == rServiceName) 105 return true; 106 return false; 107 } 108 109 void Service::connect(rtl::OUString const & rConnection, 110 rtl::OUString const & rProtocol) 111 { 112 css::uno::Reference< css::connection::XConnection > xConnection( 113 css::connection::Connector::create(m_xContext)->connect(rConnection)); 114 css::uno::Reference< css::bridge::XBridgeFactory > xBridgeFactory( 115 m_xContext->getServiceManager()->createInstanceWithContext( 116 rtl::OUString::createFromAscii("com.sun.star.bridge.BridgeFactory"), 117 m_xContext), 118 css::uno::UNO_QUERY); 119 m_xBridge = xBridgeFactory->createBridge(rtl::OUString(), rProtocol, 120 xConnection, 0); 121 } 122 123 css::uno::Reference< css::uno::XInterface > 124 Service::get(rtl::OUString const & rName) 125 { 126 return m_xBridge->getInstance(rName); 127 } 128 129 css::uno::Sequence< rtl::OUString > Service::getSupportedServiceNames_static() 130 { 131 css::uno::Sequence< rtl::OUString > aNames(1); 132 aNames[0] = rtl::OUString::createFromAscii( 133 "com.sun.star.test.bridges.testequals"); 134 return aNames; 135 } 136 137 css::uno::Reference< css::uno::XInterface > Service::createInstance( 138 css::uno::Reference< css::uno::XComponentContext > const & rContext) 139 { 140 // Make types known: 141 getCppuType( 142 static_cast< 143 css::uno::Reference< test::java_uno::equals::XBase > const * >(0)); 144 getCppuType( 145 static_cast< 146 css::uno::Reference< test::java_uno::equals::XDerived > const * >(0)); 147 getCppuType( 148 static_cast< 149 css::uno::Reference< test::java_uno::equals::XTestInterface > const * >( 150 0)); 151 152 return static_cast< cppu::OWeakObject * >(new Service(rContext)); 153 } 154 155 extern "C" void SAL_CALL component_getImplementationEnvironment( 156 sal_Char const ** pEnvTypeName, uno_Environment **) 157 { 158 *pEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME; 159 } 160 161 extern "C" void * SAL_CALL component_getFactory(sal_Char const * pImplName, 162 void * pServiceManager, void *) 163 { 164 void * pFactory = 0; 165 if (pServiceManager) 166 if (rtl_str_compare(pImplName, Service::getImplementationName_static()) 167 == 0) 168 { 169 css::uno::Reference< css::lang::XSingleComponentFactory > 170 xFactory(cppu::createSingleComponentFactory( 171 &Service::createInstance, 172 rtl::OUString::createFromAscii( 173 Service::getImplementationName_static()), 174 Service::getSupportedServiceNames_static())); 175 if (xFactory.is()) 176 { 177 xFactory->acquire(); 178 pFactory = xFactory.get(); 179 } 180 } 181 return pFactory; 182 } 183 184 namespace { 185 186 bool writeInfo(void * pRegistryKey, sal_Char const * pImplementationName, 187 css::uno::Sequence< rtl::OUString > const & rServiceNames) 188 { 189 rtl::OUString aKeyName(rtl::OUString::createFromAscii("/")); 190 aKeyName += rtl::OUString::createFromAscii(pImplementationName); 191 aKeyName += rtl::OUString::createFromAscii("/UNO/SERVICES"); 192 css::uno::Reference< css::registry::XRegistryKey > xKey; 193 try 194 { 195 xKey = static_cast< css::registry::XRegistryKey * >(pRegistryKey)-> 196 createKey(aKeyName); 197 } 198 catch (css::registry::InvalidRegistryException &) {} 199 if (!xKey.is()) 200 return false; 201 bool bSuccess = true; 202 for (sal_Int32 i = 0; i < rServiceNames.getLength(); ++i) 203 try 204 { 205 xKey->createKey(rServiceNames[i]); 206 } 207 catch (css::registry::InvalidRegistryException &) 208 { 209 bSuccess = false; 210 break; 211 } 212 return bSuccess; 213 } 214 215 } 216 217 extern "C" sal_Bool SAL_CALL component_writeInfo(void *, void * pRegistryKey) 218 { 219 return pRegistryKey 220 && writeInfo(pRegistryKey, Service::getImplementationName_static(), 221 Service::getSupportedServiceNames_static()); 222 } 223