| 1 |
/* |
| 2 |
* This file is part of the LibreOffice project. |
| 3 |
* |
| 4 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 5 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 6 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 7 |
* |
| 8 |
* This file incorporates work covered by the following license notice: |
| 9 |
* |
| 10 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
| 11 |
* contributor license agreements. See the NOTICE file distributed |
| 12 |
* with this work for additional information regarding copyright |
| 13 |
* ownership. The ASF licenses this file to you under the Apache |
| 14 |
* License, Version 2.0 (the "License"); you may not use this file |
| 15 |
* except in compliance with the License. You may obtain a copy of |
| 16 |
* the License at http://www.apache.org/licenses/LICENSE-2.0 . |
| 17 |
*/ |
| 18 |
package com.sun.star.script.framework.container; |
| 19 |
|
| 20 |
import com.sun.star.deployment.XPackage; |
| 21 |
|
| 22 |
import com.sun.star.io.XOutputStream; |
| 23 |
import com.sun.star.io.XTruncate; |
| 24 |
|
| 25 |
import com.sun.star.script.framework.io.XInputStreamWrapper; |
| 26 |
import com.sun.star.script.framework.io.XOutputStreamWrapper; |
| 27 |
import com.sun.star.script.framework.log.LogUtils; |
| 28 |
import com.sun.star.script.framework.provider.PathUtils; |
| 29 |
|
| 30 |
import com.sun.star.uno.UnoRuntime; |
| 31 |
import com.sun.star.uno.XComponentContext; |
| 32 |
|
| 33 |
import java.io.InputStream; |
| 34 |
import java.io.OutputStream; |
| 35 |
|
| 36 |
import java.util.HashMap; |
| 37 |
import java.util.Map; |
| 38 |
|
| 39 |
public class UnoPkgContainer extends ParcelContainer { |
| 40 |
|
| 41 |
private final Map<String, ParcelContainer> registeredPackages = new HashMap<String, ParcelContainer>(); |
| 42 |
private final String extensionDb; |
| 43 |
private final String extensionRepository; |
| 44 |
|
| 45 |
public UnoPkgContainer(XComponentContext xCtx, String locationURL, |
| 46 |
String _extensionDb, String _extensionRepository, |
| 47 |
String language) throws |
| 48 |
com.sun.star.lang.IllegalArgumentException, |
| 49 |
com.sun.star.lang.WrappedTargetException { |
| 50 |
|
| 51 |
super(xCtx, locationURL, language, false); |
| 52 |
extensionDb = _extensionDb; |
| 53 |
extensionRepository = _extensionRepository; |
| 54 |
init(); |
| 55 |
} |
| 56 |
|
| 57 |
// gets the ParcelContainer for persisted uno packages |
| 58 |
public ParcelContainer getRegisteredUnoPkgContainer(String url) { |
| 59 |
|
| 60 |
if (!url.endsWith("/")) { |
| 61 |
url += "/"; |
| 62 |
} |
| 63 |
|
| 64 |
LogUtils.DEBUG("** getRegisterPackage ctx = " + containerUrl); |
| 65 |
LogUtils.DEBUG("** getRegisterPackage for uri " + url); |
| 66 |
LogUtils.DEBUG("** getRegisterPackage for language " + language); |
| 67 |
|
| 68 |
ParcelContainer result = registeredPackages.get(url); |
| 69 |
LogUtils.DEBUG("getRegisterPackage result is " + result); |
| 70 |
return result; |
| 71 |
} |
| 72 |
|
| 73 |
public boolean hasRegisteredUnoPkgContainer(String url) { |
| 74 |
return getRegisteredUnoPkgContainer(url) != null; |
| 75 |
} |
| 76 |
|
| 77 |
private void registerPackageContainer(String url, ParcelContainer c) { |
| 78 |
|
| 79 |
if (!url.endsWith("/")) { |
| 80 |
url += "/"; |
| 81 |
} |
| 82 |
|
| 83 |
LogUtils.DEBUG("RegisterPackage ctx = " + containerUrl); |
| 84 |
LogUtils.DEBUG("RegisterPackage language = " + language); |
| 85 |
LogUtils.DEBUG("RegisterPackage " + c + " for url " + url); |
| 86 |
registeredPackages.put(url, c); |
| 87 |
} |
| 88 |
|
| 89 |
public void deRegisterPackageContainer(String url) { |
| 90 |
|
| 91 |
if (!url.endsWith("/")) { |
| 92 |
url += "/"; |
| 93 |
} |
| 94 |
|
| 95 |
LogUtils.DEBUG("In deRegisterPackageContainer for " + url); |
| 96 |
|
| 97 |
if (hasRegisteredUnoPkgContainer(url)) { |
| 98 |
try { |
| 99 |
DeployedUnoPackagesDB db = getUnoPackagesDB(); |
| 100 |
|
| 101 |
if (db != null && db.removePackage(language, url)) { |
| 102 |
writeUnoPackageDB(db); |
| 103 |
ParcelContainer container = registeredPackages.get(url); |
| 104 |
|
| 105 |
if (!container.hasElements()) { |
| 106 |
// When all libraries within a package bundle |
| 107 |
// ( for this language ) are removed also |
| 108 |
// remove the container from its parent |
| 109 |
// Otherwise, a container ( with no containers ) |
| 110 |
// representing the uno package bundle will |
| 111 |
// still exist and so will get displayed |
| 112 |
if (container.parent() != null) { |
| 113 |
container.parent().removeChildContainer(container); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
registeredPackages.remove(url); |
| 118 |
} |
| 119 |
} catch (Exception e) { |
| 120 |
//TODO revisit exception handling and exception here |
| 121 |
//means something very wrong |
| 122 |
LogUtils.DEBUG("***** deRegisterPackageContainer() got exception " + e); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
LogUtils.DEBUG("Leaving deRegisterPackageContainer for " + url); |
| 127 |
} |
| 128 |
|
| 129 |
private void init() throws com.sun.star.lang.IllegalArgumentException { |
| 130 |
LogUtils.DEBUG("getting container for " + containerUrl); |
| 131 |
|
| 132 |
try { |
| 133 |
DeployedUnoPackagesDB db = getUnoPackagesDB(); |
| 134 |
|
| 135 |
if (db != null) { |
| 136 |
String[] packages = db.getDeployedPackages(language); |
| 137 |
|
| 138 |
for (String thepackage : packages) { |
| 139 |
try { |
| 140 |
processUnoPackage(thepackage, language); |
| 141 |
} catch (com.sun.star.lang.IllegalArgumentException ila) { |
| 142 |
LogUtils.DEBUG("Failed to process " + thepackage |
| 143 |
+ " for " + language); |
| 144 |
LogUtils.DEBUG(" Reason: " + ila); |
| 145 |
} catch (Exception e) { |
| 146 |
// TODO proper exception or do we wish |
| 147 |
// to ignore errors here |
| 148 |
LogUtils.DEBUG("Something very wrong!!!!!"); |
| 149 |
LogUtils.DEBUG("Failed to process " + thepackage |
| 150 |
+ " for " + language); |
| 151 |
LogUtils.DEBUG(" Reason: " + e); |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
} catch (com.sun.star.lang.WrappedTargetException e) { |
| 156 |
// no deployed packages |
| 157 |
LogUtils.DEBUG("No deployed uno-packages for " + containerUrl); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
@Override |
| 162 |
public ScriptMetaData findScript(ParsedScriptUri psu) throws |
| 163 |
com.sun.star.container.NoSuchElementException, |
| 164 |
com.sun.star.lang.WrappedTargetException { |
| 165 |
|
| 166 |
String functionName = psu.function; |
| 167 |
String parcelName = psu.parcel; |
| 168 |
String location = psu.location; |
| 169 |
|
| 170 |
LogUtils.DEBUG("*** UnoPkgContainer.findScript() ***" + |
| 171 |
"\ncontainerUrl = " + containerUrl + |
| 172 |
"\nfunction = " + functionName + |
| 173 |
"\nlocation = " + location + |
| 174 |
"\nparcel = " + parcelName); |
| 175 |
|
| 176 |
ParcelContainer pc = getChildContainer(location); |
| 177 |
|
| 178 |
if (pc == null) { |
| 179 |
throw new com.sun.star.lang.WrappedTargetException( |
| 180 |
"Failed to resolve script " , null, |
| 181 |
new com.sun.star.lang.IllegalArgumentException( |
| 182 |
"Cannot resolve script location for script = " + functionName)); |
| 183 |
} |
| 184 |
|
| 185 |
return pc.findScript(psu); |
| 186 |
} |
| 187 |
|
| 188 |
private DeployedUnoPackagesDB getUnoPackagesDB() throws |
| 189 |
com.sun.star.lang.WrappedTargetException { |
| 190 |
|
| 191 |
InputStream is = null; |
| 192 |
DeployedUnoPackagesDB dp = null; |
| 193 |
|
| 194 |
try { |
| 195 |
|
| 196 |
String packagesUrl = |
| 197 |
PathUtils.make_url(extensionDb, |
| 198 |
"/Scripts/" + extensionRepository + "-extension-desc.xml"); |
| 199 |
|
| 200 |
LogUtils.DEBUG("getUnoPackagesDB() looking for existing db in " |
| 201 |
+ packagesUrl); |
| 202 |
|
| 203 |
if (m_xSFA.exists(packagesUrl)) { |
| 204 |
if (packagesUrl.startsWith("vnd.sun.star.tdoc")) { |
| 205 |
// handles using XStorage directly |
| 206 |
throw new com.sun.star.lang.WrappedTargetException( |
| 207 |
"Can't handle documents yet"); |
| 208 |
} |
| 209 |
|
| 210 |
is = new XInputStreamWrapper(m_xSFA.openFileRead(packagesUrl)); |
| 211 |
dp = new DeployedUnoPackagesDB(is); |
| 212 |
|
| 213 |
try { |
| 214 |
is.close(); |
| 215 |
is = null; |
| 216 |
} catch (Exception ignore) { |
| 217 |
} |
| 218 |
} else { |
| 219 |
LogUtils.DEBUG("getUnoPackagesDB() " + packagesUrl |
| 220 |
+ " does not exist"); |
| 221 |
} |
| 222 |
} catch (Exception e) { |
| 223 |
LogUtils.DEBUG("getUnoPackagesDB() caught Exception: " + e); |
| 224 |
LogUtils.DEBUG(LogUtils.getTrace(e)); |
| 225 |
throw new com.sun.star.lang.WrappedTargetException(e); |
| 226 |
} finally { |
| 227 |
if (is != null) { |
| 228 |
try { |
| 229 |
is.close(); |
| 230 |
is = null; |
| 231 |
} catch (Exception ignore) { |
| 232 |
} |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
return dp; |
| 237 |
} |
| 238 |
|
| 239 |
private void writeUnoPackageDB(DeployedUnoPackagesDB dp) throws |
| 240 |
com.sun.star.lang.IllegalArgumentException, |
| 241 |
com.sun.star.lang.WrappedTargetException { |
| 242 |
|
| 243 |
LogUtils.DEBUG("In writeUnoPackageDB() "); |
| 244 |
|
| 245 |
XOutputStream xos = null; |
| 246 |
OutputStream os = null; |
| 247 |
|
| 248 |
try { |
| 249 |
|
| 250 |
String packagesUrl = |
| 251 |
PathUtils.make_url(extensionDb, "/Scripts/" + extensionRepository |
| 252 |
+ "-extension-desc.xml"); |
| 253 |
|
| 254 |
xos = m_xSFA.openFileWrite(packagesUrl); |
| 255 |
XTruncate xTrc = UnoRuntime.queryInterface(XTruncate.class, xos); |
| 256 |
|
| 257 |
if (xTrc != null) { |
| 258 |
LogUtils.DEBUG("In writeUnoPackageDB() Truncating..."); |
| 259 |
xTrc.truncate(); |
| 260 |
} else { |
| 261 |
LogUtils.DEBUG("In writeUnoPackageDB() CAN'T Truncate..."); |
| 262 |
} |
| 263 |
|
| 264 |
os = new XOutputStreamWrapper(xos); |
| 265 |
dp.write(os); |
| 266 |
|
| 267 |
try { |
| 268 |
os.close(); // will close xos |
| 269 |
os = null; |
| 270 |
} catch (Exception ignore) { |
| 271 |
} |
| 272 |
} catch (Exception e) { |
| 273 |
LogUtils.DEBUG("In writeUnoPackageDB() Exception: " + e); |
| 274 |
throw new com.sun.star.lang.WrappedTargetException(e); |
| 275 |
} finally { |
| 276 |
if (os != null) { |
| 277 |
try { |
| 278 |
os.close(); // will close xos |
| 279 |
os = null; |
| 280 |
} catch (Exception ignore) { |
| 281 |
} |
| 282 |
} |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
public void processUnoPackage(XPackage dPackage, |
| 287 |
String language) throws |
| 288 |
com.sun.star.lang.IllegalArgumentException, |
| 289 |
com.sun.star.lang.WrappedTargetException, |
| 290 |
com.sun.star.container.ElementExistException { |
| 291 |
|
| 292 |
LogUtils.DEBUG("** in processUnoPackage "); |
| 293 |
|
| 294 |
String uri = dPackage.getURL(); |
| 295 |
|
| 296 |
if (!uri.endsWith("/")) { |
| 297 |
uri += "/"; |
| 298 |
} |
| 299 |
|
| 300 |
LogUtils.DEBUG("** processUnoPackage getURL() -> " + uri); |
| 301 |
LogUtils.DEBUG("** processUnoPackage getName() -> " + dPackage.getName()); |
| 302 |
LogUtils.DEBUG("** processUnoPackage getMediaType() -> " |
| 303 |
+ dPackage.getPackageType().getMediaType()); |
| 304 |
|
| 305 |
try { |
| 306 |
LogUtils.DEBUG("** processUnoPackage getDisplayName() -> " |
| 307 |
+ dPackage.getDisplayName()); |
| 308 |
} catch (com.sun.star.deployment.ExtensionRemovedException e) { |
| 309 |
throw new com.sun.star.lang.WrappedTargetException(e.getMessage(), this, e); |
| 310 |
} |
| 311 |
|
| 312 |
processUnoPackage(uri, language); |
| 313 |
|
| 314 |
DeployedUnoPackagesDB db = getUnoPackagesDB(); |
| 315 |
|
| 316 |
if (db == null) { |
| 317 |
try { |
| 318 |
db = new DeployedUnoPackagesDB(); |
| 319 |
} catch (java.io.IOException ioe) { |
| 320 |
throw new com.sun.star.lang.WrappedTargetException(ioe); |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
db.addPackage(language, uri); |
| 325 |
writeUnoPackageDB(db); |
| 326 |
} |
| 327 |
|
| 328 |
private void processUnoPackage(String uri, |
| 329 |
String language) throws |
| 330 |
com.sun.star.lang.IllegalArgumentException, |
| 331 |
com.sun.star.lang.WrappedTargetException, |
| 332 |
com.sun.star.container.ElementExistException { |
| 333 |
|
| 334 |
if (hasRegisteredUnoPkgContainer(uri)) { |
| 335 |
throw new com.sun.star.container.ElementExistException( |
| 336 |
"Already a registered uno package " + uri + " for language " |
| 337 |
+ language); |
| 338 |
} |
| 339 |
|
| 340 |
LogUtils.DEBUG("processUnoPackage - URL = " + uri); |
| 341 |
LogUtils.DEBUG("processUnoPackage - script library package"); |
| 342 |
String parentUrl = uri; |
| 343 |
|
| 344 |
if (uri.contains("%2Funo_packages%2F") || |
| 345 |
uri.contains("/uno_packages/") || |
| 346 |
uri.contains("$UNO_USER_PACKAGES_CACHE/") || |
| 347 |
uri.contains("$UNO_SHARED_PACKAGES_CACHE/") || |
| 348 |
uri.contains("$BUNDLED_EXTENSIONS/")) { |
| 349 |
|
| 350 |
//its in a bundle need to determine the uno-package file its in |
| 351 |
LogUtils.DEBUG("processUnoPackage - is part of a UNO bundle"); |
| 352 |
|
| 353 |
int index = uri.lastIndexOf('/'); |
| 354 |
|
| 355 |
if (uri.endsWith("/")) { |
| 356 |
uri = uri.substring(0, index); |
| 357 |
index = uri.lastIndexOf('/'); |
| 358 |
} |
| 359 |
|
| 360 |
if (index > -1) { |
| 361 |
parentUrl = uri.substring(0, index); |
| 362 |
LogUtils.DEBUG("processUnoPackage - composition is contained in " |
| 363 |
+ parentUrl); |
| 364 |
} |
| 365 |
|
| 366 |
ParcelContainer pkgContainer = getChildContainerForURL(parentUrl); |
| 367 |
|
| 368 |
if (pkgContainer == null) { |
| 369 |
pkgContainer = |
| 370 |
new ParcelContainer(this, m_xCtx, parentUrl, language, false); |
| 371 |
|
| 372 |
if (pkgContainer.loadParcel(uri) == null) { |
| 373 |
throw new com.sun.star.lang.IllegalArgumentException( |
| 374 |
"Couldn't load script library from composition package " |
| 375 |
+ uri + " for language " + language); |
| 376 |
} |
| 377 |
|
| 378 |
addChildContainer(pkgContainer); |
| 379 |
} else { |
| 380 |
if (pkgContainer.loadParcel(uri) == null) { |
| 381 |
throw new com.sun.star.lang.IllegalArgumentException( |
| 382 |
"Couldn't load script library from composition package " |
| 383 |
+ uri + " for language " + language); |
| 384 |
} |
| 385 |
|
| 386 |
} |
| 387 |
|
| 388 |
registerPackageContainer(uri, pkgContainer); |
| 389 |
} else { |
| 390 |
// stand-alone library package, e.g. not contained in |
| 391 |
// a uno package |
| 392 |
if (loadParcel(uri) == null) { |
| 393 |
throw new com.sun.star.lang.IllegalArgumentException( |
| 394 |
"Couldn't load script library package " + uri |
| 395 |
+ " for language " + language); |
| 396 |
} |
| 397 |
|
| 398 |
registerPackageContainer(uri, this); |
| 399 |
} |
| 400 |
} |
| 401 |
} |