001/*
002 * (C) Copyright 2006-2016 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     bstefanescu
018 *     jcarsique
019 *     Yannis JULIENNE
020 */
021package org.nuxeo.connect.update.standalone;
022
023import static java.nio.charset.StandardCharsets.UTF_8;
024
025import java.io.File;
026import java.io.IOException;
027import java.nio.file.Files;
028import java.nio.file.Path;
029import java.nio.file.attribute.BasicFileAttributes;
030import java.nio.file.attribute.FileTime;
031import java.util.ArrayList;
032import java.util.HashMap;
033import java.util.List;
034import java.util.Map;
035import java.util.Map.Entry;
036import java.util.Random;
037
038import org.apache.commons.io.FileUtils;
039import org.apache.commons.logging.Log;
040import org.apache.commons.logging.LogFactory;
041import org.nuxeo.common.Environment;
042import org.nuxeo.common.utils.ZipUtils;
043import org.nuxeo.connect.update.AlreadyExistsPackageException;
044import org.nuxeo.connect.update.LocalPackage;
045import org.nuxeo.connect.update.PackageException;
046import org.nuxeo.connect.update.PackageState;
047import org.nuxeo.connect.update.PackageUpdateService;
048
049/**
050 * The file {@code nxserver/data/packages/.packages} stores the state of all local features.
051 * <p>
052 * Each local package have a corresponding directory in {@code nxserver/data/features/store} which is named:
053 * {@code <package_uid>} ("id-version")
054 *
055 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
056 */
057public class PackagePersistence {
058
059    private static final Log log = LogFactory.getLog(PackagePersistence.class);
060
061    protected final File root;
062
063    protected final File store;
064
065    protected final File temp;
066
067    protected final Random random = new Random();
068
069    protected Map<String, PackageState> states;
070
071    private PackageUpdateService service;
072
073    public PackagePersistence(PackageUpdateService pus) throws IOException {
074        Environment env = Environment.getDefault();
075        root = env.getPath(Environment.NUXEO_MP_DIR, Environment.DEFAULT_MP_DIR);
076        if (!root.isAbsolute()) {
077            throw new RuntimeException();
078        }
079        root.mkdirs();
080        store = new File(root, "store");
081        store.mkdirs();
082        temp = new File(root, "tmp");
083        temp.mkdirs();
084        service = pus;
085        states = loadStates();
086    }
087
088    public File getRoot() {
089        return root;
090    }
091
092    /**
093     * @since 7.1
094     */
095    public File getStore() {
096        return store;
097    }
098
099    public synchronized Map<String, PackageState> getStates() {
100        return new HashMap<>(states);
101    }
102
103    protected Map<String, PackageState> loadStates() throws IOException {
104        Map<String, PackageState> result = new HashMap<>();
105        File file = new File(root, ".packages");
106        if (file.isFile()) {
107            List<String> lines = FileUtils.readLines(file, UTF_8);
108            for (String line : lines) {
109                line = line.trim();
110                if (line.length() == 0 || line.startsWith("#")) {
111                    continue;
112                }
113                int i = line.indexOf('=');
114                String pkgId = line.substring(0, i).trim();
115                String value = line.substring(i + 1).trim();
116                PackageState state = PackageState.getByLabel(value);
117                if (state == PackageState.UNKNOWN) {
118                    try {
119                        // Kept for backward compliance with int instead of enum
120                        state = PackageState.getByValue(value);
121                    } catch (NumberFormatException e) {
122                        // Set as REMOTE if undefined/unreadable
123                        state = PackageState.REMOTE;
124                    }
125                }
126                result.put(pkgId, state);
127            }
128        }
129        return result;
130    }
131
132    protected void writeStates() throws IOException {
133        StringBuilder buf = new StringBuilder();
134        for (Entry<String, PackageState> entry : states.entrySet()) {
135            buf.append(entry.getKey()).append('=').append(entry.getValue()).append("\n");
136        }
137        File file = new File(root, ".packages");
138        FileUtils.writeStringToFile(file, buf.toString(), UTF_8);
139    }
140
141    public LocalPackage getPackage(String id) throws PackageException {
142        File file = new File(store, id);
143        if (file.isDirectory()) {
144            return new LocalPackageImpl(file, getState(id), service);
145        }
146        return null;
147    }
148
149    public synchronized LocalPackage addPackage(File file) throws PackageException {
150        if (file.isDirectory()) {
151            return addPackageFromDir(file);
152        } else if (file.isFile()) {
153            File tmp = newTempDir(file.getName());
154            try {
155                ZipUtils.unzip(file, tmp);
156                return addPackageFromDir(tmp);
157            } catch (IOException e) {
158                throw new PackageException("Failed to unzip package: " + file.getName());
159            } finally {
160                // cleanup tmp if exists
161                org.apache.commons.io.FileUtils.deleteQuietly(tmp);
162            }
163        } else {
164            throw new PackageException("Not found: " + file);
165        }
166    }
167
168    /**
169     * Add unzipped packaged to local cache. It replaces SNAPSHOT packages if not installed
170     *
171     * @throws PackageException
172     * @throws AlreadyExistsPackageException If not replacing a SNAPSHOT or if the existing package is installed
173     */
174    protected LocalPackage addPackageFromDir(File file) throws PackageException {
175        LocalPackageImpl pkg = new LocalPackageImpl(file, PackageState.DOWNLOADED, service);
176        File dir = null;
177        try {
178            dir = new File(store, pkg.getId());
179            if (dir.exists()) {
180                LocalPackage oldpkg = getPackage(pkg.getId());
181                if (!pkg.getVersion().isSnapshot()) {
182                    throw new AlreadyExistsPackageException("Package " + pkg.getId() + " already exists");
183                }
184                if (oldpkg.getPackageState().isInstalled()) {
185                    throw new AlreadyExistsPackageException("Package " + pkg.getId() + " is already installed");
186                }
187                log.info(String.format("Replacement of %s in local cache...", oldpkg));
188                org.apache.commons.io.FileUtils.deleteQuietly(dir);
189            }
190            org.apache.commons.io.FileUtils.copyDirectory(file, dir);
191            pkg.getData().setRoot(dir);
192            updateState(pkg.getId(), pkg.state);
193            return pkg;
194        } catch (IOException e) {
195            throw new PackageException(String.format("Failed to move %s to %s", file, dir), e);
196        }
197    }
198
199    public synchronized PackageState getState(String packageId) {
200        PackageState state = states.get(packageId);
201        if (state == null) {
202            return PackageState.REMOTE;
203        }
204        return state;
205    }
206
207    /**
208     * Get the local package having the given name and which is in either one of the following states:
209     * <ul>
210     * <li>{@link PackageState#INSTALLING}
211     * <li>{@link PackageState#INSTALLED}
212     * <li>{@link PackageState#STARTED}
213     * </ul>
214     */
215    public LocalPackage getActivePackage(String name) throws PackageException {
216        String pkgId = getActivePackageId(name);
217        if (pkgId == null) {
218            return null;
219        }
220        return getPackage(pkgId);
221    }
222
223    public synchronized String getActivePackageId(String name) throws PackageException {
224        for (Entry<String, PackageState> entry : states.entrySet()) {
225            String pkgId = entry.getKey();
226            if (pkgId.startsWith(name) && entry.getValue().isInstalled() && getPackage(pkgId).getName().equals(name)) {
227                return pkgId;
228            }
229        }
230        return null;
231    }
232
233    public synchronized List<LocalPackage> getPackages() throws PackageException {
234        File[] list = store.listFiles();
235        if (list != null) {
236            List<LocalPackage> pkgs = new ArrayList<>(list.length);
237            for (File file : list) {
238                if (!file.isDirectory()) {
239                    log.warn("Ignoring file '" + file.getName() + "' in package store");
240                    continue;
241                }
242                pkgs.add(new LocalPackageImpl(file, getState(file.getName()), service));
243            }
244            return pkgs;
245        }
246        return new ArrayList<>();
247    }
248
249    public synchronized void removePackage(String id) throws PackageException {
250        states.remove(id);
251        try {
252            writeStates();
253        } catch (IOException e) {
254            throw new PackageException("Failed to write package states", e);
255        }
256        File file = new File(store, id);
257        org.apache.commons.io.FileUtils.deleteQuietly(file);
258    }
259
260    /**
261     * @deprecated Since 5.7. Use {@link #updateState(String, PackageState)} instead.
262     */
263    @Deprecated
264    public synchronized void updateState(String id, int state) throws PackageException {
265        states.put(id, PackageState.getByValue(state));
266        try {
267            writeStates();
268        } catch (IOException e) {
269            throw new PackageException("Failed to write package states", e);
270        }
271    }
272
273    /**
274     * @since 5.7
275     */
276    public synchronized void updateState(String id, PackageState state) throws PackageException {
277        states.put(id, state);
278        try {
279            writeStates();
280        } catch (IOException e) {
281            throw new PackageException("Failed to write package states", e);
282        }
283    }
284
285    public synchronized void reset() throws PackageException {
286        String[] keys = states.keySet().toArray(new String[states.size()]);
287        for (String key : keys) {
288            states.put(key, PackageState.DOWNLOADED);
289        }
290        try {
291            writeStates();
292        } catch (IOException e) {
293            throw new PackageException("Failed to write package states", e);
294        }
295    }
296
297    protected File newTempDir(String id) {
298        File tmp;
299        synchronized (temp) {
300            do {
301                tmp = new File(temp, id + "-" + random.nextInt());
302            } while (tmp.exists());
303            tmp.mkdirs();
304        }
305        return tmp;
306    }
307
308    /**
309     * @since 5.8
310     */
311    public FileTime getInstallDate(String id) {
312        File file = new File(store, id);
313        if (file.isDirectory()) {
314            Path path = file.toPath();
315            try {
316                FileTime lastModifiedTime = Files.readAttributes(path, BasicFileAttributes.class).lastModifiedTime();
317                return lastModifiedTime;
318            } catch (IOException e) {
319                log.error(e);
320            }
321        }
322        return null;
323    }
324}