/* * This file is part of the LibreOffice project. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * * This file incorporates work covered by the following license notice: * * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed * with this work for additional information regarding copyright * ownership. The ASF licenses this file to you under the Apache * License, Version 2.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.apache.org/licenses/LICENSE-2.0 . */ package com.sun.star.script.framework.container; import com.sun.star.script.framework.log.LogUtils; import com.sun.star.script.framework.provider.PathUtils; import com.sun.star.script.framework.io.XOutputStreamWrapper; import com.sun.star.script.framework.io.XInputStreamWrapper; import java.util.Map; import java.util.HashMap; import java.io.OutputStream; import java.io.InputStream; import com.sun.star.uno.XComponentContext; import com.sun.star.uno.UnoRuntime; import com.sun.star.io.XOutputStream; import com.sun.star.io.XTruncate; import com.sun.star.deployment.XPackage; public class UnoPkgContainer extends ParcelContainer { private Map registeredPackages = new HashMap(); private String extensionDb; private String extensionRepository; public UnoPkgContainer( XComponentContext xCtx, String locationURL, String _extensionDb, String _extensionRepository, String language ) throws com.sun.star.lang.IllegalArgumentException, com.sun.star.lang.WrappedTargetException { super( xCtx, locationURL, language, false ); extensionDb = _extensionDb; extensionRepository = _extensionRepository; init(); } // gets the ParcelContainer for persisted uno packages public ParcelContainer getRegisteredUnoPkgContainer( String url ) { if (!url.endsWith("/")) { url += "/"; } LogUtils.DEBUG("** getRegisterPackage ctx = " + containerUrl ); LogUtils.DEBUG("** getRegisterPackage for uri " + url ); LogUtils.DEBUG("** getRegisterPackage for langugage " + language ); ParcelContainer result = registeredPackages.get( url ); LogUtils.DEBUG("getRegisterPackage result is " + result ); return result; } public boolean hasRegisteredUnoPkgContainer( String url ) { boolean result = false; if ( getRegisteredUnoPkgContainer( url ) != null ) { result = true; } return result; } private void registerPackageContainer( String url, ParcelContainer c ) { if (!url.endsWith("/")) { url += "/"; } LogUtils.DEBUG("RegisterPackage ctx = " + containerUrl ); LogUtils.DEBUG("RegisterPackage language = " + language ); LogUtils.DEBUG("RegisterPackage " + c + " for url " + url ); registeredPackages.put( url, c ); } public void deRegisterPackageContainer( String url ) { if (!url.endsWith("/")) { url += "/"; } LogUtils.DEBUG("In deRegisterPackageContainer for " + url ); if ( hasRegisteredUnoPkgContainer( url ) ) { try { DeployedUnoPackagesDB db = getUnoPackagesDB(); if ( db != null ) { if ( db.removePackage( language, url ) ) { writeUnoPackageDB( db ); ParcelContainer container = registeredPackages.get( url ); if ( !container.hasElements() ) { // When all libraries within a package bundle // ( for this language ) are removed also // remove the container from its parent // Otherwise, a container ( with no containees ) // representing the uno package bundle will // still exist and so will get displayed if ( container.parent() != null ) { container.parent().removeChildContainer( container ); } } registeredPackages.remove( url ); } } } catch (Exception e) { //TODO revisit exception handling and exception here //means something very wrong LogUtils.DEBUG("***** deRegisterPackageContainer() got exception " + e ); } } LogUtils.DEBUG("Leaving deRegisterPackageContainer for " + url ); } private void init() throws com.sun.star.lang.IllegalArgumentException { LogUtils.DEBUG("getting container for " + containerUrl ); DeployedUnoPackagesDB db = null; try { db = getUnoPackagesDB(); if ( db != null ) { String[] packages = db.getDeployedPackages( language ); for ( int i=0; i " + uri ); LogUtils.DEBUG("** processUnoPackage getName() -> " + dPackage.getName() ); LogUtils.DEBUG("** processUnoPackage getMediaType() -> " + dPackage.getPackageType().getMediaType() ); try { LogUtils.DEBUG("** processUnoPackage getDisplayName() -> " + dPackage.getDisplayName() ); } catch (com.sun.star.deployment.ExtensionRemovedException e) { throw new com.sun.star.lang.WrappedTargetException(e.toString(), this, e); } processUnoPackage( uri, language ); db = getUnoPackagesDB(); if ( db == null ) { try { db = new DeployedUnoPackagesDB(); } catch ( java.io.IOException ioe ) { throw new com.sun.star.lang.WrappedTargetException( ioe.toString()); } } db.addPackage( language, uri ); writeUnoPackageDB( db ); } private void processUnoPackage( String uri, String language ) throws com.sun.star.lang.IllegalArgumentException, com.sun.star.lang.WrappedTargetException, com.sun.star.container.ElementExistException { if ( hasRegisteredUnoPkgContainer( uri ) ) { throw new com.sun.star.container.ElementExistException( "Already a registered uno package " + uri + " for language " + language ); } LogUtils.DEBUG("processUnoPackage - URL = " + uri ); LogUtils.DEBUG("processUnoPackage - script library package"); String parentUrl = uri; if ( uri.indexOf( "%2Funo_packages%2F" ) > -1 || uri.indexOf( "/uno_packages/" ) > -1 || uri.indexOf("$UNO_USER_PACKAGES_CACHE/") > -1 || uri.indexOf("$UNO_SHARED_PACKAGES_CACHE/") > -1 || uri.indexOf("$BUNDLED_EXTENSIONS/") > -1 ) { //its in a bundle need to determine the uno-package file its in LogUtils.DEBUG("processUnoPackage - is part of a uno bundle"); int index = uri.lastIndexOf("/"); if ( uri.endsWith("/") ) { uri = uri.substring( 0, index ); index = uri.lastIndexOf("/"); } if ( index > -1 ) { parentUrl = uri.substring( 0, index ); LogUtils.DEBUG("processUnoPackage - composition is contained in " + parentUrl); } ParcelContainer pkgContainer = getChildContainerForURL( parentUrl ); if ( pkgContainer == null ) { pkgContainer = new ParcelContainer( this, m_xCtx, parentUrl, language, false ); if ( pkgContainer.loadParcel( uri ) == null ) { throw new com.sun.star.lang.IllegalArgumentException( "Couldn't load script library from composition package " + uri + " for language " + language ); } addChildContainer( pkgContainer ); } else { if ( pkgContainer.loadParcel( uri ) == null ) { throw new com.sun.star.lang.IllegalArgumentException( "Couldn't load script library from composition package " + uri + " for language " + language ); } } registerPackageContainer( uri, pkgContainer ); } else { // stand-alone library package, e.g. not contained in // an uno package if ( loadParcel( uri ) == null ) { throw new com.sun.star.lang.IllegalArgumentException( "Couldn't load script library package " + uri + " for language " + language ); } registerPackageContainer( uri, this ); } } }