| /* |
| * 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 |
| * |
| * Unless required by applicable law or agreed to in writing, |
| * software distributed under the License is distributed on an |
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| * KIND, either express or implied. See the License for the |
| * specific language governing permissions and limitations |
| * under the License. |
| */ |
| package org.apache.sling.distribution.packaging.impl; |
| |
| import java.io.BufferedInputStream; |
| import java.io.File; |
| import java.io.IOException; |
| import java.io.InputStream; |
| import java.io.OutputStream; |
| import java.util.Map; |
| |
| import org.apache.commons.io.FileUtils; |
| import org.apache.sling.distribution.DistributionRequestType; |
| import org.apache.sling.distribution.packaging.DistributionPackage; |
| import org.apache.sling.distribution.packaging.DistributionPackageInfo; |
| import org.jetbrains.annotations.NotNull; |
| import org.jetbrains.annotations.Nullable; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| /** |
| * A {@link DistributionPackage} based on a {@link File}. |
| */ |
| public class FileDistributionPackage extends AbstractDistributionPackage { |
| private final Logger log = LoggerFactory.getLogger(getClass()); |
| |
| private final File file; |
| |
| public FileDistributionPackage(@NotNull File file, |
| @NotNull String type, |
| @Nullable String digestAlgorithm, |
| @Nullable String digestMessage, |
| @Nullable Map<String, Object> baseInfoMap) { |
| super(file.getName(), type, digestAlgorithm, digestMessage); |
| this.file = file; |
| |
| if (null == baseInfoMap) { |
| try (InputStream metaInfoIS = FileUtils.openInputStream(getMetaInfoFile())) { |
| DistributionPackageUtils.readInfo(metaInfoIS, this.getInfo()); |
| } catch (IOException e) { |
| log.error("cannot read meta-info for the package at {}", file.getAbsoluteFile(), e); |
| } |
| } |
| this.getInfo().put(DistributionPackageInfo.PROPERTY_REQUEST_TYPE, DistributionRequestType.ADD); |
| if (null != baseInfoMap) { |
| this.getInfo().putAll(baseInfoMap); |
| try (OutputStream metaInfoOS = FileUtils.openOutputStream(getMetaInfoFile(), false)) { |
| DistributionPackageUtils.writeInfo(metaInfoOS, baseInfoMap); |
| } catch (IOException e) { |
| log.error("cannot create meta-info for the package at {}", file.getAbsoluteFile(), e); |
| } |
| } |
| } |
| |
| @NotNull |
| public InputStream createInputStream() throws IOException { |
| return new PackageInputStream(file); |
| } |
| |
| @Override |
| public long getSize() { |
| return file.length(); |
| } |
| |
| public void close() { |
| // do nothing |
| } |
| |
| public void delete() { |
| FileUtils.deleteQuietly(file); |
| FileUtils.deleteQuietly(getStatusFile()); |
| FileUtils.deleteQuietly(getMetaInfoFile()); |
| } |
| |
| public File getFile() { |
| return file; |
| } |
| |
| @Override |
| public void acquire(@NotNull String... holderNames) { |
| try { |
| DistributionPackageUtils.acquire(getStatusFile(), holderNames); |
| } catch (IOException e) { |
| log.error("cannot release package", e); |
| } |
| } |
| |
| @Override |
| public void release(@NotNull String... holderNames) { |
| try { |
| boolean doDelete = DistributionPackageUtils.release(getStatusFile(), holderNames); |
| |
| if (doDelete) { |
| delete(); |
| } |
| } catch (IOException e) { |
| log.error("cannot release package", e); |
| } |
| } |
| |
| |
| private File getStatusFile() { |
| String statusFilePath = file.getAbsolutePath() + ".status"; |
| return new File(statusFilePath); |
| } |
| |
| private File getMetaInfoFile() { |
| String metaInfoFilePath = file.getAbsolutePath() + ".metainfo"; |
| return new File(metaInfoFilePath); |
| } |
| |
| |
| public class PackageInputStream extends BufferedInputStream { |
| private final File file; |
| |
| public PackageInputStream(File file) throws IOException { |
| super(FileUtils.openInputStream(file)); |
| |
| this.file = file; |
| } |
| |
| |
| public File getFile() { |
| return file; |
| } |
| } |
| |
| } |