View Javadoc
1   /*
2    * #%L
3    * wcm.io
4    * %%
5    * Copyright (C) 2017 wcm.io
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * 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, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package io.wcm.tooling.commons.packmgr.install;
21  
22  import java.io.File;
23  import java.io.IOException;
24  import java.util.Collection;
25  
26  import org.apache.http.client.protocol.HttpClientContext;
27  import org.apache.http.impl.client.CloseableHttpClient;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  
31  import io.wcm.tooling.commons.packmgr.PackageManagerException;
32  import io.wcm.tooling.commons.packmgr.PackageManagerHelper;
33  import io.wcm.tooling.commons.packmgr.PackageManagerProperties;
34  
35  /**
36   * Installs a list of AEM content packages via Package Manager.
37   */
38  public final class PackageInstaller {
39  
40    private final PackageManagerProperties props;
41    private final PackageManagerHelper pkgmgr;
42  
43    private boolean replicate;
44  
45    private static final Logger log = LoggerFactory.getLogger(PackageInstaller.class);
46  
47    /**
48     * @param props Package manager configuration properties.
49     */
50    public PackageInstaller(PackageManagerProperties props) {
51      this.props = props;
52      this.pkgmgr = new PackageManagerHelper(props);
53    }
54  
55    /**
56     * @param replicate Whether to replicate the package after upload.
57     */
58    public void setReplicate(boolean replicate) {
59      this.replicate = replicate;
60    }
61  
62    /**
63     * Deploy files via package manager.
64     * @param packageFiles AEM content packages
65     */
66    public void installFiles(Collection<PackageFile> packageFiles) {
67      try (CloseableHttpClient httpClient = pkgmgr.getHttpClient()) {
68        HttpClientContext packageManagerHttpClientContext = pkgmgr.getPackageManagerHttpClientContext();
69        HttpClientContext consoleHttpClientContext = pkgmgr.getConsoleHttpClientContext();
70  
71        for (PackageFile packageFile : packageFiles) {
72          installFile(packageFile, httpClient, packageManagerHttpClientContext, consoleHttpClientContext);
73        }
74      }
75      catch (IOException ex) {
76        throw new PackageManagerException("Install operation failed.", ex);
77      }
78    }
79  
80    /**
81     * Deploy file via package manager.
82     * @param packageFile AEM content package
83     */
84    public void installFile(PackageFile packageFile) {
85      try (CloseableHttpClient httpClient = pkgmgr.getHttpClient()) {
86        HttpClientContext packageManagerHttpClientContext = pkgmgr.getPackageManagerHttpClientContext();
87        HttpClientContext consoleHttpClientContext = pkgmgr.getConsoleHttpClientContext();
88  
89        installFile(packageFile, httpClient, packageManagerHttpClientContext, consoleHttpClientContext);
90      }
91      catch (IOException ex) {
92        throw new PackageManagerException("Install operation failed.", ex);
93      }
94    }
95  
96    private void installFile(PackageFile packageFile, CloseableHttpClient httpClient,
97        HttpClientContext packageManagerHttpClientContext, HttpClientContext consoleHttpClientContext) throws IOException {
98      File file = packageFile.getFile();
99      if (!file.exists()) {
100       throw new PackageManagerException("File does not exist: " + file.getAbsolutePath());
101     }
102 
103     // before install: if bundles are still stopping/starting, wait for completion
104     pkgmgr.waitForBundlesActivation(httpClient, consoleHttpClientContext);
105     // before install: if packages are still installing, wait for completion
106     pkgmgr.waitForPackageManagerInstallStatusFinished(httpClient, packageManagerHttpClientContext);
107 
108     if (packageFile.isInstall()) {
109       log.info("Upload and install {}{} to {}", packageFile.isForce() ? "(force) " : "", file.getName(), props.getPackageManagerUrl());
110     }
111     else {
112       log.info("Upload {} to {}", file.getName(), props.getPackageManagerUrl());
113     }
114 
115     VendorPackageInstaller installer = VendorInstallerFactory.getPackageInstaller(props.getPackageManagerUrl());
116     if (installer != null) {
117       installer.installPackage(packageFile, replicate,
118           pkgmgr, httpClient, packageManagerHttpClientContext, consoleHttpClientContext, props);
119     }
120   }
121 
122 }