1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: localschemasupplier.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_configmgr.hxx"
34 #include "localschemasupplier.hxx"
35 #include "localfilehelper.hxx"
36 #include "oslstream.hxx"
38 #ifndef CONFIGMGR_API_FACTORY_HXX_
39 #include "confapifactory.hxx"
40 #endif // CONFIGMGR_API_FACTORY_HXX_
41 #include "serviceinfohelper.hxx"
42 #include "bootstrap.hxx"
43 #include "filehelper.hxx"
44 #include <rtl/ustrbuf.hxx>
45 #include <com/sun/star/uno/XComponentContext.hpp>
46 #include <com/sun/star/configuration/backend/InsufficientAccessRightsException.hpp>
47 #include <osl/file.hxx>
48 #include <osl/process.h>
51 namespace configmgr
{ namespace localbe
{
53 //==============================================================================
55 //------------------------------------------------------------------------------
57 LocalSchemaSupplier::LocalSchemaSupplier(
58 const uno::Reference
<uno::XComponentContext
>& xContext
)
59 : cppu::WeakComponentImplHelper3
<backend::XVersionedSchemaSupplier
, lang::XInitialization
, lang::XServiceInfo
>(mMutex
), mFactory(xContext
->getServiceManager(),uno::UNO_QUERY
) {
61 //------------------------------------------------------------------------------
63 LocalSchemaSupplier::~LocalSchemaSupplier(void) {}
64 //------------------------------------------------------------------------------
65 static const rtl::OUString
kSchemaDataUrl(
66 RTL_CONSTASCII_USTRINGPARAM(CONTEXT_ITEM_PREFIX_
"SchemaDataUrl")) ;
68 static const rtl::OUString
kSchemaVersion(
69 RTL_CONSTASCII_USTRINGPARAM(CONTEXT_ITEM_PREFIX_
"SchemaVersion")) ;
71 void SAL_CALL
LocalSchemaSupplier::initialize(
72 const uno::Sequence
<uno::Any
>& aParameters
)
73 throw (uno::RuntimeException
, uno::Exception
,
74 css::configuration::InvalidBootstrapFileException
,
75 backend::CannotConnectException
,
76 backend::BackendSetupException
)
78 if (aParameters
.getLength() == 0) {
79 throw lang::IllegalArgumentException(
80 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
81 "No parameters provided to LocalSchemaSupplier")),
84 uno::Reference
<uno::XComponentContext
> context
;
86 for (sal_Int32 i
= 0 ; i
< aParameters
.getLength() ; ++ i
) {
87 if (aParameters
[i
] >>= context
) { break ; }
90 // Setting: schema version
91 // TODO: Add support for repository-specific versions
92 uno::Any
const aSchemaVersionSetting
= context
->getValueByName(kSchemaVersion
);
93 aSchemaVersionSetting
>>= mSchemaVersion
;
95 // Setting: schema diretory(ies)
96 uno::Any
const aSchemaDataSetting
= context
->getValueByName(kSchemaDataUrl
);
97 uno::Sequence
< rtl::OUString
> aSchemas
;
98 rtl::OUString schemas
;
100 if (aSchemaDataSetting
>>= schemas
)
102 fillFromBlankSeparated(schemas
, aSchemas
) ;
106 aSchemaDataSetting
>>= aSchemas
;
108 //validate SchemaDataUrls
109 mSchemaDataUrls
.realloc(aSchemas
.getLength());
111 sal_Int32 nSchemaLocations
=0;
112 sal_Int32 nExistingSchemaLocations
= 0;
113 for (sal_Int32 j
= 0; j
< aSchemas
.getLength(); ++j
)
115 bool bOptional
= checkOptionalArg(aSchemas
[j
]);
118 validateFileURL(aSchemas
[j
],*this);
119 else if (!isValidFileURL(aSchemas
[j
]))
122 OSL_ASSERT(isValidFileURL(aSchemas
[j
]));
125 implEnsureAbsoluteURL(aSchemas
[j
]);
126 if(!normalizeURL(aSchemas
[j
],*this,bOptional
))
129 //now we have a correct file URL, which we will use
130 mSchemaDataUrls
[nSchemaLocations
++]= aSchemas
[j
];
132 checkFileExists(aSchemas
[j
],*this);
134 else if(!FileHelper::fileExists(aSchemas
[j
]))
135 continue; // skip the directory check
137 checkIfDirectory(aSchemas
[j
],*this);
138 ++nExistingSchemaLocations
;
140 if (0 == nExistingSchemaLocations
)
142 rtl::OUString sMsg
= rtl::OUString::createFromAscii("LocalBackend: No schema directories found");
143 throw backend::BackendSetupException(sMsg
,*this, uno::Any()) ;
145 mSchemaDataUrls
.realloc(nSchemaLocations
);
147 //------------------------------------------------------------------------------
149 rtl::OUString SAL_CALL
150 LocalSchemaSupplier::getSchemaVersion(const rtl::OUString
& /*aComponent*/)
151 throw (backend::BackendAccessException
, lang::IllegalArgumentException
,
152 uno::RuntimeException
)
154 // TODO: Add support for repository-specific versions
155 return mSchemaVersion
;
157 //------------------------------------------------------------------------------
158 static const rtl::OUString
kSchemaSuffix(RTL_CONSTASCII_USTRINGPARAM(".xcs")) ;
159 static const rtl::OUString
kXMLSchemaParser(RTL_CONSTASCII_USTRINGPARAM(
160 "com.sun.star.configuration.backend.xml.SchemaParser")) ;
162 uno::Reference
<backend::XSchema
> SAL_CALL
163 LocalSchemaSupplier::getComponentSchema(const rtl::OUString
& aComponent
)
164 throw (backend::BackendAccessException
, lang::IllegalArgumentException
,
165 uno::RuntimeException
)
167 rtl::OUString subPath
= componentToPath(aComponent
) ;
169 osl::File
* schemaFile
= NULL
;
170 rtl::OUString errorMessage
;
171 bool bInsufficientAccess
= false;
172 for (sal_Int32 ix
= 0; ix
< mSchemaDataUrls
.getLength(); ++ix
)
174 rtl::OUStringBuffer
schemaUrl(mSchemaDataUrls
[ix
]) ;
176 schemaUrl
.append(subPath
).append(kSchemaSuffix
) ;
178 rtl::OUString
const aFileUrl
= schemaUrl
.makeStringAndClear();
180 std::auto_ptr
<osl::File
> checkFile( new osl::File(aFileUrl
) );
181 osl::File::RC rc
= checkFile
->open(OpenFlag_Read
) ;
183 if (rc
== osl::File::E_None
)
185 schemaFile
= checkFile
.release();
188 else if (rc
!= osl::File::E_NOENT
)
190 if (rc
== osl::File::E_ACCES
)
191 bInsufficientAccess
=true;
193 // accumulate error messages
194 rtl::OUStringBuffer
sMsg(errorMessage
);
195 if (errorMessage
.getLength())
196 sMsg
.appendAscii("LocalFile SchemaSupplier - Error accessing schema: ");
198 sMsg
.appendAscii("\n- Cannot open input file \"");
199 sMsg
.append(aFileUrl
);
200 sMsg
.appendAscii("\" : ");
201 sMsg
.append(FileHelper::createOSLErrorString(rc
));
203 errorMessage
= sMsg
.makeStringAndClear();
207 if (NULL
== schemaFile
)
209 if (errorMessage
.getLength() != 0)
211 // a real error occured
212 io::IOException
ioe(errorMessage
,*this);
214 rtl::OUStringBuffer sMsg
;
215 sMsg
.appendAscii("LocalFileLayer - Cannot readData: ").append(errorMessage
);
217 if (bInsufficientAccess
)
218 throw backend::InsufficientAccessRightsException(sMsg
.makeStringAndClear(),*this,uno::makeAny(ioe
));
220 throw backend::BackendAccessException(sMsg
.makeStringAndClear(),*this,uno::makeAny(ioe
));
226 uno::Sequence
<uno::Any
> arguments(1) ;
227 uno::Reference
<io::XInputStream
> stream( new OSLInputStreamWrapper(schemaFile
, true) );
229 arguments
[0] <<= stream
;
230 uno::Reference
<backend::XSchema
> schema(
231 mFactory
->createInstanceWithArguments(kXMLSchemaParser
, arguments
),
236 throw uno::RuntimeException(
237 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
238 "Cannot instantiate Schema Parser for ")) + aComponent
,
243 //------------------------------------------------------------------------------
246 //------------------------------------------------------------------------------
248 static const sal_Char
* const kImplementation
=
249 "com.sun.star.comp.configuration.backend.LocalSchemaSupplier" ;
250 static const sal_Char
* const kSchemaService
=
251 "com.sun.star.configuration.backend.SchemaSupplier" ;
252 static const sal_Char
* const kLocalService
=
253 "com.sun.star.configuration.backend.LocalSchemaSupplier" ;
255 static sal_Char
const * kServiceNames
[] = {kLocalService
, 0, kSchemaService
, 0 } ;
256 static const ServiceImplementationInfo kServiceInfo
= { kImplementation
, kServiceNames
,kServiceNames
+2 } ;
258 const ServiceRegistrationInfo
*getLocalSchemaSupplierServiceInfo()
259 { return getRegistrationInfo(&kServiceInfo
) ; }
261 uno::Reference
<uno::XInterface
> SAL_CALL
262 instantiateLocalSchemaSupplier(const uno::Reference
< uno::XComponentContext
>& xContext
) {
263 return *new LocalSchemaSupplier(xContext
) ;
265 //------------------------------------------------------------------------------
267 rtl::OUString SAL_CALL
LocalSchemaSupplier::getImplementationName(void)
268 throw (uno::RuntimeException
)
270 return ServiceInfoHelper(&kServiceInfo
).getImplementationName() ;
272 //------------------------------------------------------------------------------
274 sal_Bool SAL_CALL
LocalSchemaSupplier::supportsService(
275 const rtl::OUString
& aServiceName
)
276 throw (uno::RuntimeException
)
278 return ServiceInfoHelper(&kServiceInfo
).supportsService(aServiceName
);
280 //------------------------------------------------------------------------------
282 uno::Sequence
<rtl::OUString
>
283 SAL_CALL
LocalSchemaSupplier::getSupportedServiceNames(void)
284 throw (uno::RuntimeException
)
286 return ServiceInfoHelper(&kServiceInfo
).getSupportedServiceNames() ;
289 // ---------------------------------------------------------------------------------------
291 } } // configmgr.localbe